Skip to content

Commit 3029c7a

Browse files
authored
Decouple the Rewriter and Connector in Ibis Server (#692)
* decouple the connector and rewriter * enhace the test script * move testing doc to development.md * update doc * Update development.md
1 parent de775d8 commit 3029c7a

File tree

5 files changed

+25
-23
lines changed

5 files changed

+25
-23
lines changed

ibis-server/app/model/connector.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import pandas as pd
22

3-
from app.mdl.rewriter import Rewriter
43
from app.model import ConnectionInfo
54
from app.model.data_source import DataSource
65

@@ -17,19 +16,11 @@ def __init__(
1716
self.manifest_str = manifest_str
1817

1918
def query(self, sql: str, limit: int) -> pd.DataFrame:
20-
rewritten_sql = Rewriter(self.manifest_str, self.data_source).rewrite(sql)
21-
return (
22-
self.connection.sql(
23-
rewritten_sql,
24-
)
25-
.limit(limit)
26-
.to_pandas()
27-
)
19+
return self.connection.sql(sql).limit(limit).to_pandas()
2820

2921
def dry_run(self, sql: str) -> None:
3022
try:
31-
rewritten_sql = Rewriter(self.manifest_str, self.data_source).rewrite(sql)
32-
self.connection.sql(rewritten_sql)
23+
self.connection.sql(sql)
3324
except Exception as e:
3425
raise QueryDryRunError(f"Exception: {type(e)}, message: {e!s}")
3526

ibis-server/app/model/validator.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from __future__ import annotations
22

3+
from app.mdl.rewriter import Rewriter
34
from app.model.connector import Connector
45

56
rules = ["column_is_valid"]
67

78

89
class Validator:
9-
def __init__(self, connector: Connector):
10+
def __init__(self, connector: Connector, rewriter: Rewriter):
1011
self.connector = connector
12+
self.rewriter = rewriter
1113

1214
def validate(self, rule: str, parameters: dict[str, str]):
1315
if rule not in rules:
@@ -28,9 +30,9 @@ def _validate_column_is_valid(self, parameters: dict[str, str]):
2830
raise MissingRequiredParameterError("columnName")
2931

3032
try:
31-
self.connector.dry_run(
32-
f'SELECT "{column_name}" FROM "{model_name}" LIMIT 1'
33-
)
33+
sql = f'SELECT "{column_name}" FROM "{model_name}" LIMIT 1'
34+
rewritten_sql = self.rewriter.rewrite(sql)
35+
self.connector.dry_run(rewritten_sql)
3436
except Exception as e:
3537
raise ValidationError(f"Exception: {type(e)}, message: {e!s}")
3638

ibis-server/app/routers/v2/connector.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,23 @@ def query(
2929
dry_run: Annotated[bool, Query(alias="dryRun")] = False,
3030
limit: int | None = None,
3131
) -> Response:
32+
rewritten_sql = Rewriter(dto.manifest_str, data_source).rewrite(dto.sql)
3233
connector = Connector(data_source, dto.connection_info, dto.manifest_str)
3334
if dry_run:
34-
connector.dry_run(dto.sql)
35+
connector.dry_run(rewritten_sql)
3536
return Response(status_code=204)
3637
return JSONResponse(
37-
to_json(connector.query(dto.sql, limit=limit), dto.column_dtypes)
38+
to_json(connector.query(rewritten_sql, limit=limit), dto.column_dtypes)
3839
)
3940

4041

4142
@router.post("/{data_source}/validate/{rule_name}")
4243
@log_dto
4344
def validate(data_source: DataSource, rule_name: str, dto: ValidateDTO) -> Response:
44-
validator = Validator(Connector(data_source, dto.connection_info, dto.manifest_str))
45+
validator = Validator(
46+
Connector(data_source, dto.connection_info, dto.manifest_str),
47+
Rewriter(dto.manifest_str, data_source),
48+
)
4549
validator.validate(rule_name, dto.parameters)
4650
return Response(status_code=204)
4751

ibis-server/docs/development.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ To start the server:
3737
- Execute `just dev` to start the server in development mode (auto-reloads on code changes)
3838
- The default port is `8000`. You can change it by running `just port=8001 run` or `just port=8001 dev`
3939

40-
To run the tests:
41-
- Execute `just test`
42-
4340
### Environment Variables
4441
- `WREN_ENGINE_ENDPOINT`: The endpoint of the Wren Java engine
4542
- `LOG_LEVEL`: The log level of the server (default is INFO)
@@ -48,6 +45,13 @@ To run the tests:
4845
- Build the image: `just docker-build`
4946
- Run the container: `just docker-run`
5047

48+
### Run the testing
49+
- Preapre the Wren Engine server (see [Wren Engine Example](../example/README.md)
50+
- Run pytest with `WREN_ENGINE_ENDPOINT` env. (The default port of Wren Engine is 8080)
51+
- run specific data source test using [pytest marker](https://docs.pytest.org/en/stable/example/markers.html). There are some markers for different data sources. See the list in [pyproject.toml](../pyproject.toml).
52+
```
53+
WREN_ENGINE_ENDPOINT=http://localhost:8080 just test 'postgres'
54+
```
5155

5256
## How to add new data source
5357
Please see [How to Add a New Data Source](how-to-add-data-source.md) for more information.

ibis-server/justfile

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ run:
3131
dev:
3232
poetry run fastapi dev --port {{port}}
3333

34-
test:
35-
poetry run pytest
34+
# run the pytest tests for the given marker
35+
test MARKER:
36+
poetry run pytest -m {{ MARKER }}
3637

3738
docker-build:
3839
# alias for `docker-build`

0 commit comments

Comments
 (0)