-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* setup fugue sql * SQL: add basic extensions and tests * update * update * clean up sql files * fix syntax, add save load * add test for load * FugueSQLWorkflow * update version
- Loading branch information
Han Wang
authored
Jun 10, 2020
1 parent
4c61142
commit a55dc33
Showing
11 changed files
with
518 additions
and
332 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.2.2" | ||
__version__ = "0.2.5" |
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,3 @@ | ||
# flake8: noqa | ||
|
||
from fugue.workflow.workflow import FugueWorkflow, WorkflowDataFrame |
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,6 @@ | ||
from typing import Dict, Any | ||
|
||
FUGUE_SQL_DEFAULT_CONF: Dict[str, Any] = { | ||
"fugue.sql.compile.ignore_case": False, | ||
"fugue.sql.compile.simple_assign": True, | ||
} |
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,8 @@ | ||
from jinjasql import JinjaSql | ||
from typing import Dict, Any | ||
|
||
|
||
def fill_sql_template(sql: str, params: Dict[str, Any]): | ||
sql = sql.replace("%", "%%") | ||
query, bind = JinjaSql(param_style="pyformat").prepare_query(sql, params) | ||
return query % bind |
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,55 @@ | ||
import inspect | ||
from typing import Any, Dict | ||
|
||
from fugue.workflow import FugueWorkflow, WorkflowDataFrame | ||
from fugue_sql.constants import FUGUE_SQL_DEFAULT_CONF | ||
from fugue_sql.parse import FugueSQL | ||
from fugue_sql.utils import fill_sql_template | ||
from fugue_sql.visitors import FugueSQLHooks, _Extensions | ||
from triad.collections.dict import ParamDict | ||
from triad.utils.assertion import assert_or_throw | ||
|
||
|
||
class FugueSQLWorkflow(FugueWorkflow): | ||
def __init__(self, *args: Any, **kwargs: Any): | ||
super().__init__(*args, **kwargs) | ||
self._sql_vars: Dict[str, WorkflowDataFrame] = {} | ||
self._sql_conf = ParamDict({**FUGUE_SQL_DEFAULT_CONF, **super().conf}) | ||
|
||
@property | ||
def conf(self) -> ParamDict: | ||
return self._sql_conf | ||
|
||
def __getitem__(self, key: str) -> WorkflowDataFrame: | ||
return self._sql_vars[key] | ||
|
||
def __call__(self, code: str, *args: Any, **kwargs: Any): | ||
cf = inspect.currentframe() | ||
local_vars: Dict[str, Any] = {} | ||
if cf is not None and cf.f_back is not None: | ||
local_vars = cf.f_back.f_locals | ||
variables = self.sql(code, self._sql_vars, local_vars, *args, **kwargs) | ||
if cf is not None: | ||
for k, v in variables.items(): | ||
if isinstance(v, WorkflowDataFrame): | ||
self._sql_vars[k] = v | ||
|
||
def sql(self, code: str, *args: Any, **kwargs: Any) -> Dict[str, WorkflowDataFrame]: | ||
params: Dict[str, Any] = {} | ||
for a in args: | ||
assert_or_throw(isinstance(a, Dict), f"args can only have dict: {a}") | ||
params.update(a) | ||
params.update(kwargs) | ||
code = fill_sql_template(code, params) | ||
sql = FugueSQL( | ||
code, | ||
"fugueLanguage", | ||
ignore_case=self.conf.get_or_throw("fugue.sql.compile.ignore_case", bool), | ||
simple_assign=self.conf.get_or_throw( | ||
"fugue.sql.compile.simple_assign", bool | ||
), | ||
) | ||
dfs = {k: v for k, v in params.items() if isinstance(v, WorkflowDataFrame)} | ||
v = _Extensions(sql, FugueSQLHooks(), self, dfs) | ||
v.visit(sql.tree) | ||
return v.variables |
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,11 @@ | ||
from fugue_sql.utils import fill_sql_template | ||
|
||
|
||
def test_fill_sql_template(): | ||
data = {"a": 1, "b": "x"} | ||
assert "a=select " == fill_sql_template("a=select ", data) | ||
assert "1x1" == fill_sql_template("{{a}}{{b}}{{a}}", data) | ||
assert "" == fill_sql_template("", data) | ||
assert "%s" == fill_sql_template("%s", data) | ||
assert "%%s" == fill_sql_template("%%s", data) | ||
assert "1%%sx1" == fill_sql_template("{{a}}%%s{{b}}{{a}}", data) |
Oops, something went wrong.