Skip to content

Commit

Permalink
feat(wren-core-py): implement ManifestExtractor (#959)
Browse files Browse the repository at this point in the history
  • Loading branch information
grieve54706 authored Dec 9, 2024
1 parent b221920 commit a91f5c2
Show file tree
Hide file tree
Showing 23 changed files with 1,231 additions and 148 deletions.
2 changes: 1 addition & 1 deletion ibis-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ vim .env
```
Install the dependencies
```bash
just install
just install && just install-core
```
Run the server
```bash
Expand Down
8 changes: 0 additions & 8 deletions ibis-server/app/mdl/context.py

This file was deleted.

18 changes: 18 additions & 0 deletions ibis-server/app/mdl/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from functools import cache

import wren_core


@cache
def get_session_context(
manifest_str: str | None, function_path: str
) -> wren_core.SessionContext:
return wren_core.SessionContext(manifest_str, function_path)


def get_manifest_extractor(manifest_str: str) -> wren_core.ManifestExtractor:
return wren_core.ManifestExtractor(manifest_str)


def to_json_base64(manifest) -> str:
return wren_core.to_json_base64(manifest)
18 changes: 15 additions & 3 deletions ibis-server/app/mdl/rewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
from loguru import logger

from app.config import get_config
from app.mdl.context import get_session_context
from app.mdl.core import (
get_manifest_extractor,
get_session_context,
to_json_base64,
)
from app.model import InternalServerError, UnprocessableEntityError
from app.model.data_source import DataSource

Expand Down Expand Up @@ -64,14 +68,18 @@ def __init__(self, manifest_str: str):

def rewrite(self, sql: str) -> str:
try:
extractor = get_manifest_extractor(self.manifest_str)
tables = extractor.resolve_used_table_names(sql)
manifest = extractor.extract_by(tables)
manifest_str = to_json_base64(manifest)
r = httpx.request(
method="GET",
url=f"{wren_engine_endpoint}/v2/mdl/dry-plan",
headers={
"Content-Type": "application/json",
"Accept": "application/json",
},
content=orjson.dumps({"manifestStr": self.manifest_str, "sql": sql}),
content=orjson.dumps({"manifestStr": manifest_str, "sql": sql}),
)
return r.raise_for_status().text.replace("\n", " ")
except httpx.ConnectError as e:
Expand All @@ -89,7 +97,11 @@ def __init__(self, manifest_str: str, function_path: str):

def rewrite(self, sql: str) -> str:
try:
session_context = get_session_context(self.manifest_str, self.function_path)
extractor = get_manifest_extractor(self.manifest_str)
tables = extractor.resolve_used_table_names(sql)
manifest = extractor.extract_by(tables)
manifest_str = to_json_base64(manifest)
session_context = get_session_context(manifest_str, self.function_path)
return session_context.transform_sql(sql)
except Exception as e:
raise RewriteError(str(e))
Expand Down
5 changes: 2 additions & 3 deletions ibis-server/app/routers/v3/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from app.config import get_config
from app.dependencies import verify_query_dto
from app.mdl.core import get_session_context
from app.mdl.rewriter import Rewriter
from app.model import (
DryPlanDTO,
Expand Down Expand Up @@ -60,10 +61,8 @@ def validate(data_source: DataSource, rule_name: str, dto: ValidateDTO) -> Respo

@router.get("/{data_source}/functions")
def functions(data_source: DataSource) -> Response:
from wren_core import SessionContext

file_path = get_config().get_remote_function_list_path(data_source)
session_context = SessionContext(None, file_path)
session_context = get_session_context(None, file_path)
func_list = [f.to_dict() for f in session_context.get_available_functions()]

return JSONResponse(func_list)
4 changes: 2 additions & 2 deletions ibis-server/docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ This installs the pre-commit hooks.
## Start the server
To get the application running:
1. Execute `just install` to install the dependencies
2. Create a `.env` file and fill in the required environment variables (see [Environment Variables](#Environment-Variables))
3. If you want to use `wren_core`, you need to install the core by `just install-core`. After you modify the core, you can update it by `just update-core`.
2. Execute `just install-core` to Install the core. If you modify the core, you can update it by `just update-core`.
3. Create a `.env` file and fill in the required environment variables (see [Environment Variables](#Environment-Variables))

To start the server:
- Execute `just run` to start the server
Expand Down
2 changes: 1 addition & 1 deletion ibis-server/tests/mdl/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import orjson

from app.mdl.context import get_session_context
from app.mdl.core import get_session_context
from tests.conftest import file_path


Expand Down
Loading

0 comments on commit a91f5c2

Please sign in to comment.