-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dryRun parameter for query API (#593)
* Add dryRun parameter for query API And split ibis.py by data source * Use http status code instead of json response * Rename to Connector
- Loading branch information
1 parent
4e24567
commit 2e08721
Showing
13 changed files
with
247 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from json import loads | ||
|
||
from app.mdl.rewriter import rewrite | ||
from app.model.data_source import DataSource, ConnectionInfo | ||
|
||
|
||
class Connector: | ||
def __init__(self, data_source: DataSource, connection_info: ConnectionInfo, manifest_str: str): | ||
self.data_source = data_source | ||
self.connection = self.data_source.get_connection(connection_info) | ||
self.manifest_str = manifest_str | ||
|
||
def query(self, sql) -> dict: | ||
rewritten_sql = rewrite(self.manifest_str, sql) | ||
return self._to_json(self.connection.sql(rewritten_sql, dialect='trino').to_pandas()) | ||
|
||
def dry_run(self, sql) -> None: | ||
try: | ||
rewritten_sql = rewrite(self.manifest_str, sql) | ||
self.connection.sql(rewritten_sql, dialect='trino') | ||
except Exception as e: | ||
raise QueryDryRunError(f'Exception: {type(e)}, message: {str(e)}') | ||
|
||
@staticmethod | ||
def _to_json(df): | ||
json_obj = loads(df.to_json(orient='split')) | ||
del json_obj['index'] | ||
json_obj['dtypes'] = df.dtypes.apply(lambda x: x.name).to_dict() | ||
return json_obj | ||
|
||
|
||
class QueryDryRunError(Exception): | ||
pass |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from fastapi import APIRouter | ||
|
||
import app.routers.ibis.bigquery as bigquery | ||
import app.routers.ibis.postgres as postgres | ||
import app.routers.ibis.snowflake as snowflake | ||
|
||
prefix = "/v2/ibis" | ||
|
||
router = APIRouter(prefix=prefix) | ||
|
||
router.include_router(bigquery.router) | ||
router.include_router(postgres.router) | ||
router.include_router(snowflake.router) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from typing import Annotated | ||
|
||
from fastapi import APIRouter, Query, Response | ||
from fastapi.responses import JSONResponse | ||
|
||
from app.logger import log_dto | ||
from app.model.connector import Connector | ||
from app.model.data_source import DataSource | ||
from app.model.dto import BigQueryDTO | ||
|
||
router = APIRouter(prefix='/bigquery', tags=['bigquery']) | ||
|
||
data_source = DataSource.bigquery | ||
|
||
|
||
@router.post("/query") | ||
@log_dto | ||
def query(dto: BigQueryDTO, dry_run: Annotated[bool, Query(alias="dryRun")] = False) -> Response: | ||
connector = Connector(data_source, dto.connection_info, dto.manifest_str) | ||
if dry_run: | ||
connector.dry_run(dto.sql) | ||
return Response(status_code=204) | ||
return JSONResponse(connector.query(dto.sql)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from typing import Annotated | ||
|
||
from fastapi import APIRouter, Query, Response | ||
from fastapi.responses import JSONResponse | ||
|
||
from app.logger import log_dto | ||
from app.model.connector import Connector | ||
from app.model.data_source import DataSource | ||
from app.model.dto import PostgresDTO | ||
|
||
router = APIRouter(prefix='/postgres', tags=['postgres']) | ||
|
||
data_source = DataSource.postgres | ||
|
||
|
||
@router.post("/query") | ||
@log_dto | ||
def query(dto: PostgresDTO, dry_run: Annotated[bool, Query(alias="dryRun")] = False) -> Response: | ||
connector = Connector(data_source, dto.connection_info, dto.manifest_str) | ||
if dry_run: | ||
connector.dry_run(dto.sql) | ||
return Response(status_code=204) | ||
return JSONResponse(connector.query(dto.sql)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from typing import Annotated | ||
|
||
from fastapi import APIRouter, Query, Response | ||
from fastapi.responses import JSONResponse | ||
|
||
from app.logger import log_dto | ||
from app.model.connector import Connector | ||
from app.model.data_source import DataSource | ||
from app.model.dto import SnowflakeDTO | ||
|
||
router = APIRouter(prefix='/snowflake', tags=['snowflake']) | ||
|
||
data_source = DataSource.snowflake | ||
|
||
|
||
@router.post("/query") | ||
@log_dto | ||
def query(dto: SnowflakeDTO, dry_run: Annotated[bool, Query(alias="dryRun")] = False) -> Response: | ||
connector = Connector(data_source, dto.connection_info, dto.manifest_str) | ||
if dry_run: | ||
connector.dry_run(dto.sql) | ||
return Response(status_code=204) | ||
return JSONResponse(connector.query(dto.sql)) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.