Skip to content

Add query parameter limit #644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions ibis-server/app/model/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ def __init__(
self.connection = self.data_source.get_connection(connection_info)
self.manifest_str = manifest_str

def query(self, sql) -> pd.DataFrame:
def query(self, sql: str, limit: int) -> pd.DataFrame:
rewritten_sql = rewrite(self.manifest_str, sql)
return self.connection.sql(rewritten_sql, dialect="trino").to_pandas()
return (
self.connection.sql(rewritten_sql, dialect="trino").limit(limit).to_pandas()
)

def dry_run(self, sql) -> None:
def dry_run(self, sql: str) -> None:
try:
rewritten_sql = rewrite(self.manifest_str, sql)
self.connection.sql(rewritten_sql, dialect="trino")
Expand Down
5 changes: 4 additions & 1 deletion ibis-server/app/routers/v2/ibis.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ def query(
data_source: DataSource,
dto: QueryDTO,
dry_run: Annotated[bool, Query(alias="dryRun")] = False,
limit: int | None = None,
) -> 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(to_json(connector.query(dto.sql), dto.column_dtypes))
return JSONResponse(
to_json(connector.query(dto.sql, limit=limit), dto.column_dtypes)
)


@router.post("/{data_source}/validate/{rule_name}")
Expand Down
28 changes: 28 additions & 0 deletions ibis-server/tests/routers/ibis/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,34 @@ def test_query_with_column_dtypes(self, postgres: PostgresContainer):
"timestamptz": "object",
}

def test_query_with_limit(self, postgres: PostgresContainer):
connection_info = self.to_connection_info(postgres)
response = client.post(
url="/v2/ibis/postgres/query",
params={"limit": 1},
json={
"connectionInfo": connection_info,
"manifestStr": self.manifest_str,
"sql": 'SELECT * FROM "Orders"',
},
)
assert response.status_code == 200
result = response.json()
assert len(result["data"]) == 1

response = client.post(
url="/v2/ibis/postgres/query",
params={"limit": 1},
json={
"connectionInfo": connection_info,
"manifestStr": self.manifest_str,
"sql": 'SELECT * FROM "Orders" LIMIT 10',
},
)
assert response.status_code == 200
result = response.json()
assert len(result["data"]) == 1

def test_query_without_manifest(self, postgres: PostgresContainer):
connection_info = self.to_connection_info(postgres)
response = client.post(
Expand Down
Loading