sqlleaf 0.0.2__tar.gz → 0.0.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {sqlleaf-0.0.2/sqlleaf.egg-info → sqlleaf-0.0.4}/PKG-INFO +7 -1
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/README.md +6 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/pyproject.toml +1 -1
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/mappings.py +165 -30
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/context.py +6 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/node/base.py +27 -11
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/node/column.py +27 -23
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/node/dynamodb.py +11 -4
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/node/function.py +4 -3
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/node/interval.py +4 -4
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/node/json.py +13 -7
- sqlleaf-0.0.4/sqlleaf/models/node/literal.py +27 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/node/pivot.py +4 -4
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/node/program.py +10 -3
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/node/sequence.py +2 -3
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/node/star.py +3 -0
- sqlleaf-0.0.4/sqlleaf/models/node/stream.py +13 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/node/user_defined_function.py +12 -8
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/node/window.py +4 -4
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/__init__.py +8 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/base.py +75 -57
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/block.py +4 -0
- sqlleaf-0.0.4/sqlleaf/models/query/case.py +35 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/copy.py +2 -1
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/ctas.py +2 -8
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/declare_item.py +4 -0
- sqlleaf-0.0.4/sqlleaf/models/query/do.py +108 -0
- sqlleaf-0.0.2/sqlleaf/models/query/for_in.py → sqlleaf-0.0.4/sqlleaf/models/query/exception_.py +6 -6
- sqlleaf-0.0.4/sqlleaf/models/query/for_in.py +94 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/insert.py +0 -3
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/prepare.py +7 -2
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/table.py +0 -7
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/type.py +3 -3
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/unload.py +3 -2
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/collector/classify.py +2 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/collector/collector.py +95 -40
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/generator/dialects/athena.py +2 -4
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/generator/dialects/base.py +35 -50
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/generator/dialects/postgres.py +2 -4
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/generator/dialects/redshift.py +4 -12
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/generator/dialects/snowflake.py +6 -12
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/generator/generator.py +69 -1
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/substitutor/substitute.py +3 -6
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/transformer/__init__.py +6 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/transformer/base.py +15 -2
- sqlleaf-0.0.4/sqlleaf/processors/transformer/case.py +10 -0
- sqlleaf-0.0.4/sqlleaf/processors/transformer/copy.py +72 -0
- sqlleaf-0.0.4/sqlleaf/processors/transformer/for_in.py +30 -0
- sqlleaf-0.0.4/sqlleaf/processors/transformer/prepare.py +20 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/transformer/transformer.py +23 -25
- sqlleaf-0.0.4/sqlleaf/processors/transformer/unload.py +16 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/typing.py +4 -1
- {sqlleaf-0.0.2 → sqlleaf-0.0.4/sqlleaf.egg-info}/PKG-INFO +7 -1
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf.egg-info/SOURCES.txt +8 -1
- sqlleaf-0.0.4/tests/test_hooks.py +64 -0
- sqlleaf-0.0.2/sqlleaf/models/node/literal.py +0 -17
- sqlleaf-0.0.2/sqlleaf/models/node/stream.py +0 -14
- sqlleaf-0.0.2/sqlleaf/processors/transformer/copy.py +0 -65
- sqlleaf-0.0.2/sqlleaf/processors/transformer/unload.py +0 -32
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/LICENSE +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/setup.cfg +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/__init__.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/dialects/__init__.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/dialects/plpgsql/__init__.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/dialects/plpgsql/p_classes.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/dialects/plpgsql/p_dialect.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/dialects/plpgsql/p_generator.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/dialects/plpgsql/p_parser.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/dialects/plpgsql/p_tokenizer.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/exception.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/lineage.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/__init__.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/hook.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/node/__init__.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/node/null.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/node/var.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/node/variable.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/call.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/database.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/delete.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/execute.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/execute_dynamic.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/fetch.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/holder.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/if_.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/loop.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/merge.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/multitable_insert.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/open.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/perform.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/procedure.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/put.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/replace.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/schema.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/select.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/sequence.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/set.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/stage.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/trigger.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/update.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/user_defined_function.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/values.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/view.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/models/query/while_.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/path.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/__init__.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/collector/__init__.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/generator/__init__.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/generator/dialects/__init__.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/generator/dialects/mysql.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/substitutor/__init__.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/transformer/assignment.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/transformer/call.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/transformer/ctas.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/transformer/delete.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/transformer/execute.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/transformer/execute_dynamic.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/transformer/expressions/__init__.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/transformer/expressions/format.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/transformer/expressions/functional_notation.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/transformer/expressions/row.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/transformer/expressions/values.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/transformer/insert.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/transformer/merge.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/transformer/multitable_insert.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/transformer/open.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/transformer/replace.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/transformer/update.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/processors/transformer/values.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/settings/__init__.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/settings/athena.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/settings/base.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/settings/postgres.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/util/__init__.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/util/expression.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/util/graph.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/util/helpers.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf/util/iterators.py +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf.egg-info/dependency_links.txt +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf.egg-info/requires.txt +0 -0
- {sqlleaf-0.0.2 → sqlleaf-0.0.4}/sqlleaf.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sqlleaf
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.4
|
|
4
4
|
Summary: Trace your data
|
|
5
5
|
Author-email: zac <zac@sqlleaf.io>
|
|
6
6
|
Project-URL: Homepage, https://github.com/sqlleaf-io/sqlleaf
|
|
@@ -43,6 +43,12 @@ column[logins.name] -> function[LOWER] -> column[users.name]
|
|
|
43
43
|
function[CURRENT_TIMESTAMP] -> column[users.birthday]
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
+
## Installation
|
|
47
|
+
Available on PyPI:
|
|
48
|
+
```
|
|
49
|
+
pip3 install sqlleaf
|
|
50
|
+
```
|
|
51
|
+
|
|
46
52
|
## Applications
|
|
47
53
|
sqlleaf allows you to build a wide range of new applications on top of SQL databases, such as:
|
|
48
54
|
- data tracing and tagging systems, such as personally identifiable information tracking
|
|
@@ -30,6 +30,12 @@ column[logins.name] -> function[LOWER] -> column[users.name]
|
|
|
30
30
|
function[CURRENT_TIMESTAMP] -> column[users.birthday]
|
|
31
31
|
```
|
|
32
32
|
|
|
33
|
+
## Installation
|
|
34
|
+
Available on PyPI:
|
|
35
|
+
```
|
|
36
|
+
pip3 install sqlleaf
|
|
37
|
+
```
|
|
38
|
+
|
|
33
39
|
## Applications
|
|
34
40
|
sqlleaf allows you to build a wide range of new applications on top of SQL databases, such as:
|
|
35
41
|
- data tracing and tagging systems, such as personally identifiable information tracking
|
|
@@ -5,13 +5,16 @@ import typing as t
|
|
|
5
5
|
|
|
6
6
|
from sqlglot import MappingSchema, exp
|
|
7
7
|
from sqlglot.dialects.dialect import DialectType
|
|
8
|
+
from sqlglot.optimizer.annotate_types import annotate_types
|
|
8
9
|
from sqlglot.schema import nested_set
|
|
9
10
|
from sqlglot.trie import new_trie
|
|
10
11
|
|
|
11
12
|
from sqlleaf import exception, util
|
|
12
13
|
from sqlleaf.models.query import (
|
|
14
|
+
CallQuery,
|
|
13
15
|
CTASQuery,
|
|
14
16
|
DatabaseQuery,
|
|
17
|
+
FunctionParam,
|
|
15
18
|
PrepareQuery,
|
|
16
19
|
ProcedureQuery,
|
|
17
20
|
Q,
|
|
@@ -26,6 +29,7 @@ from sqlleaf.models.query import (
|
|
|
26
29
|
)
|
|
27
30
|
|
|
28
31
|
ColumnMapping = t.Union[dict, str, list]
|
|
32
|
+
FunctionCallable = ProcedureQuery | UserDefinedFunctionQuery
|
|
29
33
|
|
|
30
34
|
logger = logging.getLogger("sqlleaf")
|
|
31
35
|
|
|
@@ -60,13 +64,24 @@ class ObjectMapping(MappingSchema):
|
|
|
60
64
|
if self.variable_scopes:
|
|
61
65
|
self.variable_scopes.pop()
|
|
62
66
|
|
|
67
|
+
def declare_variable(self, name: str, value: exp.Expr | None = None) -> None:
|
|
68
|
+
"""Declare a variable in the current (innermost) scope."""
|
|
69
|
+
val = value if value is not None else exp.Null()
|
|
70
|
+
self.variable_scopes[-1][name] = val
|
|
71
|
+
logger.debug(f"Declared variable: {name} := {val.sql(dialect=self.dialect)}")
|
|
72
|
+
|
|
63
73
|
def set_variable(self, name: str, value: exp.Expr) -> None:
|
|
64
|
-
"""Assign
|
|
65
|
-
self.variable_scopes
|
|
66
|
-
|
|
74
|
+
"""Assign a variable in the nearest scope where it was declared."""
|
|
75
|
+
for scope in reversed(self.variable_scopes):
|
|
76
|
+
if name in scope:
|
|
77
|
+
scope[name] = value
|
|
78
|
+
logger.debug(f"Set variable value: {name} := {value.sql(dialect=self.dialect)}")
|
|
79
|
+
return
|
|
80
|
+
|
|
81
|
+
raise exception.MappingError(f'"{name}" is not a known variable')
|
|
67
82
|
|
|
68
83
|
def get_variable(self, name: str, raise_on_missing: bool = True) -> exp.Expr | None:
|
|
69
|
-
"""Resolve a variable by searching from innermost to outermost scopes
|
|
84
|
+
"""Resolve a variable by searching from innermost to outermost scopes."""
|
|
70
85
|
for scope in reversed(self.variable_scopes):
|
|
71
86
|
if name in scope:
|
|
72
87
|
return scope[name]
|
|
@@ -87,7 +102,7 @@ class ObjectMapping(MappingSchema):
|
|
|
87
102
|
resolved = []
|
|
88
103
|
for expr in expressions:
|
|
89
104
|
if isinstance(expr, exp.Column) and not expr.table:
|
|
90
|
-
variable = self.get_variable(expr.name)
|
|
105
|
+
variable = self.get_variable(expr.name, raise_on_missing=False)
|
|
91
106
|
if variable is not None:
|
|
92
107
|
resolved.append(variable.copy())
|
|
93
108
|
continue
|
|
@@ -125,7 +140,7 @@ class ObjectMapping(MappingSchema):
|
|
|
125
140
|
self._add_query(query=query, dialect=query.dialect)
|
|
126
141
|
|
|
127
142
|
def add_type_query(self, query: TypeQuery) -> None:
|
|
128
|
-
self._add_query(
|
|
143
|
+
self._add_query(query=query, dialect=query.dialect)
|
|
129
144
|
|
|
130
145
|
def add_udf_query(self, query: UserDefinedFunctionQuery, column_mapping: ColumnMapping | None = None) -> None:
|
|
131
146
|
self._add_query(query=query, column_mapping=column_mapping, dialect=query.dialect)
|
|
@@ -137,7 +152,7 @@ class ObjectMapping(MappingSchema):
|
|
|
137
152
|
dialect: DialectType = None,
|
|
138
153
|
normalize: bool | None = None,
|
|
139
154
|
match_depth: bool = False,
|
|
140
|
-
kind: str = ""
|
|
155
|
+
kind: str = "",
|
|
141
156
|
) -> None:
|
|
142
157
|
"""
|
|
143
158
|
Register or update a table. Updates are only performed if a new column mapping is provided.
|
|
@@ -168,6 +183,11 @@ class ObjectMapping(MappingSchema):
|
|
|
168
183
|
# Store the UDF with other UDFs of the same name
|
|
169
184
|
udfs_with_same_name = self.lookup_udf_query(table, raise_on_missing=False) or []
|
|
170
185
|
query = [query] + udfs_with_same_name
|
|
186
|
+
elif kind == "procedure":
|
|
187
|
+
# Store the procedure with other overloads of the same name, replacing an identical signature
|
|
188
|
+
procedures_with_same_name = self.lookup_procedure_query(table, raise_on_missing=False) or []
|
|
189
|
+
other_overloads = [p for p in procedures_with_same_name if not _has_same_parameter_types(p, query)]
|
|
190
|
+
query = [query] + other_overloads
|
|
171
191
|
|
|
172
192
|
# Store the object
|
|
173
193
|
nested_set(self.kind_mapping[kind], tuple(reversed(parts)), query)
|
|
@@ -219,7 +239,8 @@ class ObjectMapping(MappingSchema):
|
|
|
219
239
|
def lookup_prepare_query(self, table: exp.Table, raise_on_missing: bool = True) -> PrepareQuery | None:
|
|
220
240
|
return self._lookup_query(kind="prepare", object=table, raise_on_missing=raise_on_missing)
|
|
221
241
|
|
|
222
|
-
def lookup_procedure_query(self, table: exp.Table, raise_on_missing: bool = True) -> ProcedureQuery | None:
|
|
242
|
+
def lookup_procedure_query(self, table: exp.Table, raise_on_missing: bool = True) -> list[ProcedureQuery] | None:
|
|
243
|
+
# A procedure can be overloaded with different parameters, thus we return a list
|
|
223
244
|
return self._lookup_query(kind="procedure", object=table, raise_on_missing=raise_on_missing)
|
|
224
245
|
|
|
225
246
|
def lookup_sequence_query(self, table: exp.Table, raise_on_missing: bool = True) -> SequenceQuery | None:
|
|
@@ -270,10 +291,12 @@ class ObjectMapping(MappingSchema):
|
|
|
270
291
|
else:
|
|
271
292
|
knd = "CREATE " + {"udf": "FUNCTION"}.get(kind, kind).upper()
|
|
272
293
|
|
|
273
|
-
raise exception.MappingError(
|
|
294
|
+
raise exception.MappingError(
|
|
295
|
+
f"Could not find '{object.this}' of type '{kind}' in mapping. Did you forget to define a '{knd}' statement?"
|
|
296
|
+
)
|
|
274
297
|
return None
|
|
275
298
|
|
|
276
|
-
parts = self.table_parts(object)[0: len(self.supported_table_args)]
|
|
299
|
+
parts = self.table_parts(object)[0 : len(self.supported_table_args)]
|
|
277
300
|
resolved_parts = self._find_in_trie(parts, self.kind_mapping_trie[kind], raise_on_missing)
|
|
278
301
|
|
|
279
302
|
if resolved_parts is None:
|
|
@@ -333,6 +356,23 @@ class ObjectMapping(MappingSchema):
|
|
|
333
356
|
|
|
334
357
|
return resolve_overloaded_function(node, candidates)
|
|
335
358
|
|
|
359
|
+
def lookup_procedure_call(self, node: CallQuery) -> ProcedureQuery | None:
|
|
360
|
+
"""
|
|
361
|
+
Looks up the procedure definition for a CALL statement.
|
|
362
|
+
Returns the matched procedure definition, or None if no procedure with that name exists.
|
|
363
|
+
"""
|
|
364
|
+
procedure_table = exp.table_(table=node.procedure, db=node.schema)
|
|
365
|
+
candidates = self.lookup_procedure_query(table=procedure_table, raise_on_missing=False)
|
|
366
|
+
if not candidates:
|
|
367
|
+
return None
|
|
368
|
+
|
|
369
|
+
# Annotate copies so the call's own expressions are left untouched
|
|
370
|
+
args = [arg.copy() for arg in node.args]
|
|
371
|
+
for arg in args:
|
|
372
|
+
annotate_types(_get_call_arg_value(arg), dialect=self.dialect, schema=self)
|
|
373
|
+
|
|
374
|
+
return resolve_overloaded_procedure(args, candidates)
|
|
375
|
+
|
|
336
376
|
|
|
337
377
|
def resolve_overloaded_function(
|
|
338
378
|
node: exp.Anonymous, candidates: list[UserDefinedFunctionQuery]
|
|
@@ -347,13 +387,7 @@ def resolve_overloaded_function(
|
|
|
347
387
|
|
|
348
388
|
# Function overloading: find the best match based on arguments
|
|
349
389
|
args = node.expressions
|
|
350
|
-
matches =
|
|
351
|
-
for candidate in candidates:
|
|
352
|
-
if match_function_arguments(args, candidate):
|
|
353
|
-
matches.append(candidate)
|
|
354
|
-
|
|
355
|
-
if not matches:
|
|
356
|
-
raise exception.MappingError(f"No matching function signatures found for args: {args}")
|
|
390
|
+
matches = match_overloads(args, candidates, lambda a, c: match_callable_arguments(a, c.parameters), "function")
|
|
357
391
|
|
|
358
392
|
if len(matches) == 1:
|
|
359
393
|
return matches[0]
|
|
@@ -383,25 +417,127 @@ def resolve_overloaded_function(
|
|
|
383
417
|
return matches[0]
|
|
384
418
|
|
|
385
419
|
|
|
386
|
-
def
|
|
420
|
+
def _has_same_parameter_types(left: ProcedureQuery, right: ProcedureQuery) -> bool:
|
|
421
|
+
"""
|
|
422
|
+
Compare the parameter types of two procedures.
|
|
423
|
+
"""
|
|
424
|
+
return [str(p.type) for p in left.parameters] == [str(p.type) for p in right.parameters]
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
def match_overloads(
|
|
428
|
+
args: list[exp.Expr],
|
|
429
|
+
candidates: list[FunctionCallable],
|
|
430
|
+
matcher: t.Callable[[list[exp.Expr], FunctionCallable], bool],
|
|
431
|
+
kind: str,
|
|
432
|
+
) -> list[FunctionCallable]:
|
|
433
|
+
"""
|
|
434
|
+
Returns the candidates whose signature matches the arguments.
|
|
435
|
+
Raises if no candidate matches.
|
|
436
|
+
"""
|
|
437
|
+
matches = [candidate for candidate in candidates if matcher(args, candidate)]
|
|
438
|
+
if not matches:
|
|
439
|
+
raise exception.MappingError(f"No matching {kind} signatures found for args: {args}")
|
|
440
|
+
|
|
441
|
+
return matches
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
def resolve_overloaded_procedure(args: list[exp.Expr], candidates: list[ProcedureQuery]) -> ProcedureQuery:
|
|
445
|
+
"""
|
|
446
|
+
Resolves the procedure overload that the CALL arguments bind to.
|
|
447
|
+
Raises if no overload matches.
|
|
448
|
+
|
|
449
|
+
Example:
|
|
450
|
+
args = [annotate_types(exp.Literal.number(42))] # type INT
|
|
451
|
+
candidates = [p_text_proc, p_int_proc]
|
|
452
|
+
# p_text_proc: ProcedureQuery for process_order(a TEXT)
|
|
453
|
+
# p_int_proc: ProcedureQuery for process_order(a INT)
|
|
454
|
+
resolve_overloaded_procedure(args, candidates)
|
|
455
|
+
-> p_int_proc # process_order(a INT), even though the TEXT candidate is listed first
|
|
456
|
+
"""
|
|
457
|
+
matches = match_overloads(args, candidates, lambda a, c: match_callable_arguments(a, c.parameters), "procedure")
|
|
458
|
+
return matches[0]
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
def _get_call_arg_value(arg: exp.Expr) -> exp.Expr:
|
|
462
|
+
"""Returns the value of a CALL argument, unwrapping named notation (`name => value`)."""
|
|
463
|
+
if isinstance(arg, exp.Kwarg):
|
|
464
|
+
return arg.expression
|
|
465
|
+
if isinstance(arg, exp.PropertyEQ):
|
|
466
|
+
return arg.right
|
|
467
|
+
return arg
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
def match_callable_arguments(args: list[exp.Expr], params: list[FunctionParam]) -> bool:
|
|
387
471
|
"""
|
|
388
|
-
Checks if the
|
|
389
|
-
|
|
472
|
+
Checks if the call arguments bind to the parameters of a UDF or procedure.
|
|
473
|
+
Positional arguments bind in order, named arguments bind by name, and any unbound parameter must have a default.
|
|
474
|
+
A trailing variadic parameter absorbs the remaining positional arguments.
|
|
475
|
+
Bound input parameters are type-checked, while OUT parameters and NULL arguments match any type.
|
|
390
476
|
"""
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
477
|
+
positional = [arg for arg in args if not isinstance(arg, (exp.Kwarg, exp.PropertyEQ))]
|
|
478
|
+
named = [arg for arg in args if isinstance(arg, (exp.Kwarg, exp.PropertyEQ))]
|
|
479
|
+
|
|
480
|
+
has_variadic = bool(params) and params[-1].is_variadic
|
|
481
|
+
fixed_count = len(params) - 1 if has_variadic else len(params)
|
|
482
|
+
absorbed = [_get_call_arg_value(arg) for arg in positional[fixed_count:]]
|
|
483
|
+
if absorbed and not has_variadic:
|
|
484
|
+
return False
|
|
394
485
|
|
|
395
|
-
|
|
486
|
+
bound = {i: _get_call_arg_value(arg) for i, arg in enumerate(positional[:fixed_count])}
|
|
487
|
+
taken = set(bound)
|
|
488
|
+
if absorbed:
|
|
489
|
+
taken.add(fixed_count)
|
|
396
490
|
|
|
397
|
-
|
|
398
|
-
if
|
|
491
|
+
for arg in named:
|
|
492
|
+
arg_key = arg.this if isinstance(arg, exp.Kwarg) else arg.left
|
|
493
|
+
index = next((i for i, p in enumerate(params) if p.name.lower() == arg_key.name.lower()), None)
|
|
494
|
+
if index is None or index in taken:
|
|
399
495
|
return False
|
|
496
|
+
taken.add(index)
|
|
497
|
+
bound[index] = _get_call_arg_value(arg)
|
|
400
498
|
|
|
401
|
-
|
|
402
|
-
|
|
499
|
+
for i, param in enumerate(params):
|
|
500
|
+
if absorbed and i == fixed_count:
|
|
501
|
+
if not _match_variadic_arguments(absorbed, param.type):
|
|
403
502
|
return False
|
|
404
|
-
|
|
503
|
+
elif i not in bound:
|
|
504
|
+
if param.default is None and not param.is_variadic:
|
|
505
|
+
return False
|
|
506
|
+
elif param.is_input and not isinstance(bound[i], exp.Null):
|
|
507
|
+
if not _match_argument(bound[i], param.type):
|
|
508
|
+
return False
|
|
509
|
+
|
|
510
|
+
return True
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
def _match_argument(value: exp.Expr, target_type: exp.DataType) -> bool:
|
|
514
|
+
# Mirror match_type's VARCHAR -> TEXT normalization, which is only applied to the argument side
|
|
515
|
+
if target_type.is_type(exp.DataType.Type.VARCHAR):
|
|
516
|
+
target_type = exp.DataType.build("TEXT")
|
|
517
|
+
return match_type(value, target_type)
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
def _match_variadic_arguments(values: list[exp.Expr], array_type: exp.DataType) -> bool:
|
|
521
|
+
"""
|
|
522
|
+
Checks the arguments absorbed by a variadic parameter. A single array argument (`VARIADIC arr`) is matched
|
|
523
|
+
against the whole array type, otherwise each argument is matched against the array's element type.
|
|
524
|
+
|
|
525
|
+
Example:
|
|
526
|
+
# Each INT value matches the element type of INT[]
|
|
527
|
+
values = [annotate_types(exp.Literal.number(1)), annotate_types(exp.Literal.number(2))]
|
|
528
|
+
_match_variadic_arguments(values, exp.DataType.build("INT[]"))
|
|
529
|
+
-> True
|
|
530
|
+
# A string (VARCHAR) does not match INT
|
|
531
|
+
values_fail = [annotate_types(exp.Literal.string("a"))]
|
|
532
|
+
_match_variadic_arguments(values_fail, exp.DataType.build("INT[]"))
|
|
533
|
+
-> False
|
|
534
|
+
"""
|
|
535
|
+
if len(values) == 1 and values[0].type and values[0].type.is_type(exp.DataType.Type.ARRAY):
|
|
536
|
+
return _match_argument(values[0], array_type)
|
|
537
|
+
|
|
538
|
+
element_type = array_type.expressions[0] if array_type.is_type(exp.DataType.Type.ARRAY) else array_type
|
|
539
|
+
return all(isinstance(value, exp.Null) or _match_argument(value, element_type) for value in values)
|
|
540
|
+
|
|
405
541
|
|
|
406
542
|
def match_type(arg: exp.Expr, target_type: exp.DataType) -> bool:
|
|
407
543
|
"""Matches an argument expression to a target data type."""
|
|
@@ -433,4 +569,3 @@ def match_type(arg: exp.Expr, target_type: exp.DataType) -> bool:
|
|
|
433
569
|
return True
|
|
434
570
|
|
|
435
571
|
return exp.DataType.is_type(arg_type, target_type)
|
|
436
|
-
|
|
@@ -60,6 +60,9 @@ class ScopePositions:
|
|
|
60
60
|
self.positions = expr_ids_to_positions
|
|
61
61
|
|
|
62
62
|
|
|
63
|
+
"""
|
|
64
|
+
Holds all the information required to generate lineage for an expression.
|
|
65
|
+
"""
|
|
63
66
|
@dataclass(frozen=True)
|
|
64
67
|
class GeneratorContext[Q, N]:
|
|
65
68
|
graph: nx.MultiDiGraph
|
|
@@ -115,6 +118,9 @@ class GeneratorContext[Q, N]:
|
|
|
115
118
|
return dataclasses.replace(self, **args)
|
|
116
119
|
|
|
117
120
|
|
|
121
|
+
"""
|
|
122
|
+
Holds all the positions that locate an expression.
|
|
123
|
+
"""
|
|
118
124
|
@dataclass(frozen=True)
|
|
119
125
|
class PositionContext:
|
|
120
126
|
# The position of this query inside a list of queries, e.g. SELECT 'a'; SELECT 'b' -> a=0, b=1
|
|
@@ -16,24 +16,38 @@ class NodeAttributes:
|
|
|
16
16
|
|
|
17
17
|
def __init__(
|
|
18
18
|
self,
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
name: str | None = "",
|
|
19
|
+
name: str = "",
|
|
20
|
+
data_type: str = "",
|
|
22
21
|
):
|
|
23
|
-
self.expr: exp.Expr =
|
|
24
|
-
self._data_type: exp.DataType | None =
|
|
25
|
-
self.data_type: str =
|
|
22
|
+
self.expr: exp.Expr | None = None
|
|
23
|
+
self._data_type: exp.DataType | None = None
|
|
24
|
+
self.data_type: str = data_type
|
|
26
25
|
|
|
27
26
|
self.name: str = name
|
|
28
27
|
self.kind: str = self.KIND
|
|
29
|
-
self.
|
|
30
|
-
|
|
28
|
+
self.ctx: PositionContext | None = None
|
|
29
|
+
|
|
30
|
+
def with_context(self, gen_ctx: GeneratorContext) -> t.Self:
|
|
31
|
+
self.expr = gen_ctx.expr
|
|
32
|
+
self._data_type = gen_ctx.data_type
|
|
33
|
+
if not self.data_type:
|
|
34
|
+
self.data_type = str(gen_ctx.data_type)
|
|
35
|
+
return self
|
|
36
|
+
|
|
37
|
+
def with_positions(self, pos_ctx: PositionContext) -> t.Self:
|
|
38
|
+
self.ctx = pos_ctx
|
|
39
|
+
return self
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def _emits_positions(self) -> bool:
|
|
43
|
+
return self.WITH_POSITIONS and self.ctx is not None
|
|
31
44
|
|
|
32
45
|
# Allows the class to be used a networkx node
|
|
33
46
|
def __hash__(self) -> int:
|
|
34
47
|
return hash(self.full_name)
|
|
35
48
|
|
|
36
49
|
def get_data_type(self) -> exp.DataType:
|
|
50
|
+
"""Context-only: requires the node to be bound via `with_context`."""
|
|
37
51
|
assert self._data_type is not None
|
|
38
52
|
return self._data_type
|
|
39
53
|
|
|
@@ -70,7 +84,7 @@ class NodeAttributes:
|
|
|
70
84
|
|
|
71
85
|
@property
|
|
72
86
|
def full_name(self) -> str:
|
|
73
|
-
return self._build_name(self.fields(), with_positions=self.
|
|
87
|
+
return self._build_name(self.fields(), with_positions=self._emits_positions)
|
|
74
88
|
|
|
75
89
|
@property
|
|
76
90
|
def friendly_name(self) -> str:
|
|
@@ -94,14 +108,16 @@ class NodeAttributes:
|
|
|
94
108
|
return name
|
|
95
109
|
|
|
96
110
|
def to_dict(self) -> dict:
|
|
97
|
-
|
|
111
|
+
result = {
|
|
98
112
|
"id": self.id,
|
|
99
113
|
"kind": self.kind,
|
|
100
114
|
"name": self.name,
|
|
101
115
|
"data_type": self.data_type,
|
|
102
116
|
**self.fields(),
|
|
103
|
-
"position": asdict(self.ctx) if self.with_positions else {},
|
|
104
117
|
}
|
|
118
|
+
if self.ctx is not None:
|
|
119
|
+
result["position"] = asdict(self.ctx) if self.WITH_POSITIONS else {}
|
|
120
|
+
return result
|
|
105
121
|
|
|
106
122
|
|
|
107
123
|
class EdgeAttributes:
|
|
@@ -20,31 +20,37 @@ class ColumnNode(NodeAttributes):
|
|
|
20
20
|
|
|
21
21
|
def __init__(
|
|
22
22
|
self,
|
|
23
|
-
catalog: str,
|
|
24
|
-
schema: str,
|
|
25
|
-
table: str,
|
|
26
23
|
column: str,
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
table: str = "",
|
|
25
|
+
schema: str = "",
|
|
26
|
+
catalog: str = "",
|
|
27
|
+
kind: str = "",
|
|
28
|
+
subkind: str = "",
|
|
29
|
+
data_type: str = "",
|
|
29
30
|
source: TableOrScopeType | exp.Select | exp.Subquery | exp.Lateral | None = None,
|
|
30
31
|
):
|
|
31
|
-
super().__init__(
|
|
32
|
+
super().__init__(name=column, data_type=data_type)
|
|
32
33
|
self.catalog = catalog
|
|
33
34
|
self.schema = schema
|
|
34
35
|
self.table = table
|
|
35
36
|
self.member = ""
|
|
36
37
|
|
|
37
|
-
self.parent_kind: str =
|
|
38
|
-
self.parent_subkind: str =
|
|
38
|
+
self.parent_kind: str = kind
|
|
39
|
+
self.parent_subkind: str = subkind
|
|
39
40
|
self.source_scope: TableOrScopeType | None = None
|
|
41
|
+
self._source = source
|
|
40
42
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
43
|
+
def with_context(self, gen_ctx: GeneratorContext) -> t.Self:
|
|
44
|
+
super().with_context(gen_ctx)
|
|
45
|
+
|
|
46
|
+
self.set_table_properties(self.catalog, self.schema, self.table, gen_ctx, source=self._source)
|
|
47
|
+
if self._source is not None and not isinstance(self._source, (exp.Subquery, Scope)):
|
|
48
|
+
self._apply_rename(self._source, gen_ctx.query.dialect)
|
|
44
49
|
|
|
45
50
|
# TODO: new algorithm
|
|
46
51
|
# if table_type == "cte":
|
|
47
52
|
# self.member = gen_ctx.node.recursive_cte_member_kind
|
|
53
|
+
return self
|
|
48
54
|
|
|
49
55
|
def to_dict(self):
|
|
50
56
|
d = super().to_dict()
|
|
@@ -266,7 +272,7 @@ class ColumnNode(NodeAttributes):
|
|
|
266
272
|
if self.parent_kind == TableType.CTE and self.parent_subkind == TableSubtype.RECURSIVE:
|
|
267
273
|
f["member"] = self.member
|
|
268
274
|
|
|
269
|
-
if self.parent_kind in [TableType.CTE, TableType.DERIVED_TABLE]:
|
|
275
|
+
if self.parent_kind in [TableType.CTE, TableType.DERIVED_TABLE] and self.ctx is not None:
|
|
270
276
|
f["statement"] = str(self.ctx.statement_index)
|
|
271
277
|
|
|
272
278
|
return f
|
|
@@ -278,14 +284,13 @@ class FileColumnNode(NodeAttributes):
|
|
|
278
284
|
def __init__(
|
|
279
285
|
self,
|
|
280
286
|
column: str,
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
pos_ctx: PositionContext,
|
|
287
|
+
path: str,
|
|
288
|
+
file_format: str = "",
|
|
289
|
+
data_type: str = "",
|
|
285
290
|
):
|
|
286
|
-
super().__init__(
|
|
291
|
+
super().__init__(name=column, data_type=data_type)
|
|
287
292
|
self.file_format = file_format
|
|
288
|
-
self.file_path =
|
|
293
|
+
self.file_path = path
|
|
289
294
|
|
|
290
295
|
def to_dict(self):
|
|
291
296
|
d = super().to_dict()
|
|
@@ -313,13 +318,12 @@ class StageColumnNode(NodeAttributes):
|
|
|
313
318
|
def __init__(
|
|
314
319
|
self,
|
|
315
320
|
column: str,
|
|
316
|
-
stage:
|
|
317
|
-
gen_ctx: GeneratorContext,
|
|
318
|
-
pos_ctx: PositionContext,
|
|
321
|
+
stage: str,
|
|
319
322
|
path: str = "",
|
|
323
|
+
data_type: str = "",
|
|
320
324
|
):
|
|
321
|
-
super().__init__(
|
|
322
|
-
self.stage = stage
|
|
325
|
+
super().__init__(name=column, data_type=data_type)
|
|
326
|
+
self.stage = stage
|
|
323
327
|
self.path = path
|
|
324
328
|
|
|
325
329
|
def to_dict(self):
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import logging
|
|
4
|
+
import typing as t
|
|
4
5
|
|
|
5
|
-
from sqlleaf.models.context import GeneratorContext
|
|
6
|
+
from sqlleaf.models.context import GeneratorContext
|
|
6
7
|
from sqlleaf.models.node import NodeAttributes
|
|
7
8
|
|
|
8
9
|
logger = logging.getLogger("sqlleaf")
|
|
@@ -13,13 +14,19 @@ class DynamoDbNode(NodeAttributes):
|
|
|
13
14
|
|
|
14
15
|
def __init__(
|
|
15
16
|
self,
|
|
16
|
-
gen_ctx: GeneratorContext,
|
|
17
|
-
pos_ctx: PositionContext,
|
|
18
17
|
column: str,
|
|
18
|
+
table_name: str = "",
|
|
19
|
+
path: str = "",
|
|
20
|
+
data_type: str = "",
|
|
19
21
|
):
|
|
20
|
-
super().__init__(
|
|
22
|
+
super().__init__(name=column, data_type=data_type)
|
|
23
|
+
self.table_name = table_name
|
|
24
|
+
self.path = path
|
|
25
|
+
|
|
26
|
+
def with_context(self, gen_ctx: GeneratorContext) -> t.Self:
|
|
21
27
|
self.path = gen_ctx.expr.name
|
|
22
28
|
self.table_name = self.path.removeprefix("dynamodb://")
|
|
29
|
+
return super().with_context(gen_ctx)
|
|
23
30
|
|
|
24
31
|
def to_dict(self):
|
|
25
32
|
d = super().to_dict()
|
|
@@ -5,7 +5,7 @@ import typing as t
|
|
|
5
5
|
from sqlglot import exp
|
|
6
6
|
|
|
7
7
|
from sqlleaf import util
|
|
8
|
-
from sqlleaf.models.context import GeneratorContext
|
|
8
|
+
from sqlleaf.models.context import GeneratorContext
|
|
9
9
|
from sqlleaf.models.node import NodeAttributes
|
|
10
10
|
|
|
11
11
|
|
|
@@ -13,7 +13,7 @@ class FunctionNode(NodeAttributes):
|
|
|
13
13
|
KIND = "function"
|
|
14
14
|
WITH_POSITIONS = True
|
|
15
15
|
|
|
16
|
-
def
|
|
16
|
+
def with_context(self, gen_ctx: GeneratorContext) -> t.Self:
|
|
17
17
|
expr = t.cast(t.Union[exp.Binary, exp.Func], gen_ctx.expr)
|
|
18
18
|
if isinstance(expr, exp.Binary):
|
|
19
19
|
name = expr.key
|
|
@@ -24,7 +24,8 @@ class FunctionNode(NodeAttributes):
|
|
|
24
24
|
else:
|
|
25
25
|
name = util.calculate_function_name(expr, gen_ctx.query.dialect)
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
self.name = name
|
|
28
|
+
return super().with_context(gen_ctx)
|
|
28
29
|
|
|
29
30
|
def fields(self) -> dict[str, str]:
|
|
30
31
|
return {"type": self.data_type}
|
|
@@ -4,7 +4,7 @@ import typing as t
|
|
|
4
4
|
|
|
5
5
|
from sqlglot import exp
|
|
6
6
|
|
|
7
|
-
from sqlleaf.models.context import GeneratorContext
|
|
7
|
+
from sqlleaf.models.context import GeneratorContext
|
|
8
8
|
from sqlleaf.models.node import NodeAttributes
|
|
9
9
|
|
|
10
10
|
|
|
@@ -12,10 +12,10 @@ class IntervalNode(NodeAttributes):
|
|
|
12
12
|
KIND = "interval"
|
|
13
13
|
WITH_POSITIONS = True
|
|
14
14
|
|
|
15
|
-
def
|
|
15
|
+
def with_context(self, gen_ctx: GeneratorContext) -> t.Self:
|
|
16
16
|
expr = t.cast(exp.Interval, gen_ctx.expr)
|
|
17
|
-
name = f'"{str(expr.this.name)} {str(expr.unit)}"'
|
|
18
|
-
super().
|
|
17
|
+
self.name = f'"{str(expr.this.name)} {str(expr.unit)}"'
|
|
18
|
+
return super().with_context(gen_ctx)
|
|
19
19
|
|
|
20
20
|
def fields(self) -> dict[str, str]:
|
|
21
21
|
return {"type": self.data_type}
|
|
@@ -5,7 +5,7 @@ import typing as t
|
|
|
5
5
|
|
|
6
6
|
from sqlglot import exp
|
|
7
7
|
|
|
8
|
-
from sqlleaf.models.context import GeneratorContext
|
|
8
|
+
from sqlleaf.models.context import GeneratorContext
|
|
9
9
|
from sqlleaf.models.node import NodeAttributes
|
|
10
10
|
|
|
11
11
|
logger = logging.getLogger("sqlleaf")
|
|
@@ -14,16 +14,22 @@ logger = logging.getLogger("sqlleaf")
|
|
|
14
14
|
class JsonPathNode(NodeAttributes):
|
|
15
15
|
KIND = "jsonpath"
|
|
16
16
|
|
|
17
|
-
def __init__(self,
|
|
17
|
+
def __init__(self, selector: str = "", selector_depth: int = 0, data_type: str = ""):
|
|
18
|
+
super().__init__(name=selector, data_type=data_type)
|
|
19
|
+
self.selector = selector
|
|
20
|
+
self.selector_depth = selector_depth
|
|
21
|
+
|
|
22
|
+
def with_context(self, gen_ctx: GeneratorContext) -> t.Self:
|
|
18
23
|
expr = t.cast(exp.JSONExtract, gen_ctx.expr)
|
|
19
24
|
|
|
20
|
-
|
|
21
|
-
self.selector = "".join([str(s) for s in
|
|
22
|
-
self.selector_depth = len(
|
|
25
|
+
selectors = self.json_selectors(expr)
|
|
26
|
+
self.name = self.selector = "".join([str(s) for s in selectors])
|
|
27
|
+
self.selector_depth = len(selectors)
|
|
23
28
|
|
|
24
|
-
super().
|
|
29
|
+
return super().with_context(gen_ctx)
|
|
25
30
|
|
|
26
|
-
|
|
31
|
+
@staticmethod
|
|
32
|
+
def json_selectors(expr: exp.JSONExtract):
|
|
27
33
|
"""
|
|
28
34
|
Collect all the JSON path elements recursively.
|
|
29
35
|
e.g.
|