sqlleaf 0.0.3__tar.gz → 0.0.5__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.3/sqlleaf.egg-info → sqlleaf-0.0.5}/PKG-INFO +1 -1
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/pyproject.toml +1 -1
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/mappings.py +245 -64
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/context.py +6 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/node/base.py +27 -11
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/node/column.py +27 -23
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/node/dynamodb.py +11 -4
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/node/function.py +4 -3
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/node/interval.py +4 -4
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/node/json.py +13 -7
- sqlleaf-0.0.5/sqlleaf/models/node/literal.py +27 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/node/pivot.py +4 -4
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/node/program.py +10 -3
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/node/sequence.py +2 -3
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/node/star.py +3 -0
- sqlleaf-0.0.5/sqlleaf/models/node/stream.py +13 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/node/user_defined_function.py +12 -8
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/node/window.py +4 -4
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/__init__.py +2 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/base.py +76 -58
- sqlleaf-0.0.5/sqlleaf/models/query/case.py +35 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/copy.py +4 -3
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/ctas.py +2 -8
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/insert.py +0 -3
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/open.py +1 -1
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/prepare.py +7 -2
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/table.py +0 -7
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/type.py +3 -3
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/unload.py +3 -2
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/collector/collector.py +63 -48
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/generator/dialects/athena.py +2 -4
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/generator/dialects/base.py +36 -51
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/generator/dialects/postgres.py +2 -4
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/generator/dialects/redshift.py +4 -12
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/generator/dialects/snowflake.py +6 -12
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/generator/generator.py +69 -1
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/substitutor/substitute.py +41 -18
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/__init__.py +4 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/base.py +33 -45
- sqlleaf-0.0.5/sqlleaf/processors/transformer/case.py +10 -0
- sqlleaf-0.0.5/sqlleaf/processors/transformer/copy.py +72 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/ctas.py +2 -3
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/delete.py +2 -1
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/expressions/functional_notation.py +1 -2
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/expressions/row.py +1 -2
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/insert.py +3 -4
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/merge.py +2 -1
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/open.py +2 -2
- sqlleaf-0.0.5/sqlleaf/processors/transformer/prepare.py +20 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/transformer.py +28 -33
- sqlleaf-0.0.5/sqlleaf/processors/transformer/unload.py +16 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/update.py +4 -5
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/typing.py +6 -14
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/util/expression.py +3 -3
- {sqlleaf-0.0.3 → sqlleaf-0.0.5/sqlleaf.egg-info}/PKG-INFO +1 -1
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf.egg-info/SOURCES.txt +3 -0
- sqlleaf-0.0.3/sqlleaf/models/node/literal.py +0 -17
- sqlleaf-0.0.3/sqlleaf/models/node/stream.py +0 -14
- sqlleaf-0.0.3/sqlleaf/processors/transformer/copy.py +0 -65
- sqlleaf-0.0.3/sqlleaf/processors/transformer/unload.py +0 -32
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/LICENSE +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/README.md +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/setup.cfg +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/__init__.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/dialects/__init__.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/dialects/plpgsql/__init__.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/dialects/plpgsql/p_classes.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/dialects/plpgsql/p_dialect.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/dialects/plpgsql/p_generator.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/dialects/plpgsql/p_parser.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/dialects/plpgsql/p_tokenizer.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/exception.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/lineage.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/__init__.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/hook.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/node/__init__.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/node/null.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/node/var.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/node/variable.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/block.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/call.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/database.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/declare_item.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/delete.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/do.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/exception_.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/execute.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/execute_dynamic.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/fetch.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/for_in.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/holder.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/if_.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/loop.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/merge.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/multitable_insert.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/perform.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/procedure.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/put.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/replace.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/schema.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/select.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/sequence.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/set.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/stage.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/trigger.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/update.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/user_defined_function.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/values.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/view.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/models/query/while_.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/path.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/__init__.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/collector/__init__.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/collector/classify.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/generator/__init__.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/generator/dialects/__init__.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/generator/dialects/mysql.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/substitutor/__init__.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/assignment.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/call.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/execute.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/execute_dynamic.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/expressions/__init__.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/expressions/format.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/expressions/values.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/for_in.py +1 -1
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/multitable_insert.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/replace.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/processors/transformer/values.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/settings/__init__.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/settings/athena.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/settings/base.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/settings/postgres.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/util/__init__.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/util/graph.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/util/helpers.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf/util/iterators.py +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf.egg-info/dependency_links.txt +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf.egg-info/requires.txt +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/sqlleaf.egg-info/top_level.txt +0 -0
- {sqlleaf-0.0.3 → sqlleaf-0.0.5}/tests/test_hooks.py +0 -0
|
@@ -2,16 +2,20 @@ from __future__ import annotations
|
|
|
2
2
|
|
|
3
3
|
import logging
|
|
4
4
|
import typing as t
|
|
5
|
+
from dataclasses import dataclass, field
|
|
5
6
|
|
|
6
7
|
from sqlglot import MappingSchema, exp
|
|
7
8
|
from sqlglot.dialects.dialect import DialectType
|
|
9
|
+
from sqlglot.optimizer.annotate_types import annotate_types
|
|
8
10
|
from sqlglot.schema import nested_set
|
|
9
11
|
from sqlglot.trie import new_trie
|
|
10
12
|
|
|
11
13
|
from sqlleaf import exception, util
|
|
12
14
|
from sqlleaf.models.query import (
|
|
15
|
+
CallQuery,
|
|
13
16
|
CTASQuery,
|
|
14
17
|
DatabaseQuery,
|
|
18
|
+
FunctionParam,
|
|
15
19
|
PrepareQuery,
|
|
16
20
|
ProcedureQuery,
|
|
17
21
|
Q,
|
|
@@ -26,61 +30,94 @@ from sqlleaf.models.query import (
|
|
|
26
30
|
)
|
|
27
31
|
|
|
28
32
|
ColumnMapping = t.Union[dict, str, list]
|
|
33
|
+
FunctionCallable = ProcedureQuery | UserDefinedFunctionQuery
|
|
29
34
|
|
|
30
35
|
logger = logging.getLogger("sqlleaf")
|
|
31
36
|
|
|
32
37
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
38
|
+
@dataclass
|
|
39
|
+
class ParamBinding:
|
|
40
|
+
"""One parameter's binding for the lifetime of a single CALL."""
|
|
36
41
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
like qualification and simplification).
|
|
40
|
-
"""
|
|
42
|
+
value: exp.Expr
|
|
43
|
+
arg_name: str | None = None
|
|
41
44
|
|
|
42
|
-
def __init__(self, dialect: str):
|
|
43
|
-
super().__init__(
|
|
44
|
-
dialect=dialect, normalize=False
|
|
45
|
-
) # Set `normalize=False` to prevent an unnecessary second parse.
|
|
46
|
-
self.kind_mapping = {}
|
|
47
|
-
self.kind_mapping_trie = {}
|
|
48
|
-
# Session/global variables (e.g., SET var = ...)
|
|
49
|
-
self.session_variables: dict[str, exp.Expr] = {}
|
|
50
|
-
# Block-scoped variables: one dict per active block, innermost scope at the end
|
|
51
|
-
self.variable_scopes: list[dict[str, exp.Expr]] = []
|
|
52
45
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
46
|
+
@dataclass
|
|
47
|
+
class VariableScope:
|
|
48
|
+
"""One CALL boundary: its block stack and parameter bindings."""
|
|
49
|
+
|
|
50
|
+
blocks: list[dict[str, exp.Expr]] = field(default_factory=list)
|
|
51
|
+
bindings: dict[str, ParamBinding] = field(default_factory=dict)
|
|
57
52
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
53
|
+
|
|
54
|
+
class Variables:
|
|
55
|
+
def __init__(self):
|
|
56
|
+
# The base scope runs DO / top-level code and is never popped
|
|
57
|
+
self.scopes: list[VariableScope] = [VariableScope()]
|
|
58
|
+
|
|
59
|
+
@property
|
|
60
|
+
def _active_blocks(self) -> list[dict[str, exp.Expr]]:
|
|
61
|
+
return self.scopes[-1].blocks
|
|
62
|
+
|
|
63
|
+
@property
|
|
64
|
+
def _active_bindings(self) -> dict[str, ParamBinding]:
|
|
65
|
+
return self.scopes[-1].bindings
|
|
66
|
+
|
|
67
|
+
# ---- Block API ----
|
|
68
|
+
def push_block(self) -> None:
|
|
69
|
+
"""Enter a new DECLARE/BEGIN block."""
|
|
70
|
+
self._active_blocks.append({})
|
|
71
|
+
|
|
72
|
+
def pop_block(self) -> None:
|
|
73
|
+
"""Leave the current DECLARE/BEGIN block."""
|
|
74
|
+
if self._active_blocks:
|
|
75
|
+
self._active_blocks.pop()
|
|
76
|
+
|
|
77
|
+
# ---- Scope API ----
|
|
78
|
+
def push_scope(self, bindings: dict[str, ParamBinding]) -> None:
|
|
79
|
+
"""Enter a CALL boundary."""
|
|
80
|
+
self.scopes.append(VariableScope(bindings=bindings))
|
|
81
|
+
|
|
82
|
+
def pop_scope(self, write_back: bool = True) -> None:
|
|
83
|
+
"""Leave the current CALL boundary, optionally writing OUT/INOUT values back to the caller."""
|
|
84
|
+
if len(self.scopes) == 1:
|
|
85
|
+
raise exception.MappingError("Cannot pop the base variable scope")
|
|
86
|
+
scope = self.scopes.pop()
|
|
87
|
+
if write_back:
|
|
88
|
+
for binding in scope.bindings.values():
|
|
89
|
+
if binding.arg_name is not None:
|
|
90
|
+
self.set_variable(binding.arg_name, binding.value)
|
|
62
91
|
|
|
63
92
|
def declare_variable(self, name: str, value: exp.Expr | None = None) -> None:
|
|
64
|
-
"""Declare a variable in the current (innermost)
|
|
93
|
+
"""Declare a variable in the current (innermost) block."""
|
|
65
94
|
val = value if value is not None else exp.Null()
|
|
66
|
-
self.
|
|
67
|
-
logger.debug(f"Declared variable: {name} := {val
|
|
95
|
+
self._active_blocks[-1][name] = val
|
|
96
|
+
logger.debug(f"Declared variable: {name} := {val}")
|
|
68
97
|
|
|
69
98
|
def set_variable(self, name: str, value: exp.Expr) -> None:
|
|
70
|
-
"""Assign a variable in the nearest
|
|
71
|
-
for
|
|
72
|
-
if name in
|
|
73
|
-
|
|
74
|
-
logger.debug(f"Set variable value: {name} := {value
|
|
99
|
+
"""Assign a variable in the nearest block where it was declared."""
|
|
100
|
+
for block in reversed(self._active_blocks):
|
|
101
|
+
if name in block:
|
|
102
|
+
block[name] = value
|
|
103
|
+
logger.debug(f"Set variable value: {name} := {value}")
|
|
75
104
|
return
|
|
76
105
|
|
|
106
|
+
binding = self._active_bindings.get(name)
|
|
107
|
+
if binding is not None:
|
|
108
|
+
binding.value = value
|
|
109
|
+
return
|
|
110
|
+
|
|
77
111
|
raise exception.MappingError(f'"{name}" is not a known variable')
|
|
78
112
|
|
|
79
113
|
def get_variable(self, name: str, raise_on_missing: bool = True) -> exp.Expr | None:
|
|
80
|
-
"""Resolve a variable by searching from innermost
|
|
81
|
-
for
|
|
82
|
-
if name in
|
|
83
|
-
return
|
|
114
|
+
"""Resolve a variable by searching from innermost block outward, then the call's bindings."""
|
|
115
|
+
for block in reversed(self._active_blocks):
|
|
116
|
+
if name in block:
|
|
117
|
+
return block[name]
|
|
118
|
+
binding = self._active_bindings.get(name)
|
|
119
|
+
if binding is not None:
|
|
120
|
+
return binding.value
|
|
84
121
|
if raise_on_missing:
|
|
85
122
|
raise exception.MappingError(f"Could not find value for variable: {name}")
|
|
86
123
|
return None
|
|
@@ -106,8 +143,28 @@ class ObjectMapping(MappingSchema):
|
|
|
106
143
|
return resolved
|
|
107
144
|
|
|
108
145
|
def get_scope(self) -> dict[str, exp.Expr]:
|
|
109
|
-
"""Return the current
|
|
110
|
-
return self.
|
|
146
|
+
"""Return the current block dict (innermost)."""
|
|
147
|
+
return self._active_blocks[-1]
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
class ObjectMapping(MappingSchema):
|
|
151
|
+
"""
|
|
152
|
+
Contains all the objects that get created, such as tables, procedures, functions, and sequences.
|
|
153
|
+
|
|
154
|
+
We extends sqlglot.MappingSchema to provide additional functionality related to tracking these objects,
|
|
155
|
+
while still allowing optimizer inside sqlglot to work (as it needs a mapping of objects to perform functions
|
|
156
|
+
like qualification and simplification).
|
|
157
|
+
"""
|
|
158
|
+
|
|
159
|
+
def __init__(self, dialect: str):
|
|
160
|
+
super().__init__(
|
|
161
|
+
dialect=dialect, normalize=False
|
|
162
|
+
) # Set `normalize=False` to prevent an unnecessary second parse.
|
|
163
|
+
self.kind_mapping = {}
|
|
164
|
+
self.kind_mapping_trie = {}
|
|
165
|
+
# Session/global variables (e.g., SET var = ...)
|
|
166
|
+
self.session_variables: dict[str, exp.Expr] = {}
|
|
167
|
+
self.variables = Variables()
|
|
111
168
|
|
|
112
169
|
def add_database_query(self, query: DatabaseQuery) -> None:
|
|
113
170
|
self._add_query(query=query, dialect=query.dialect)
|
|
@@ -136,7 +193,7 @@ class ObjectMapping(MappingSchema):
|
|
|
136
193
|
self._add_query(query=query, dialect=query.dialect)
|
|
137
194
|
|
|
138
195
|
def add_type_query(self, query: TypeQuery) -> None:
|
|
139
|
-
self._add_query(
|
|
196
|
+
self._add_query(query=query, dialect=query.dialect)
|
|
140
197
|
|
|
141
198
|
def add_udf_query(self, query: UserDefinedFunctionQuery, column_mapping: ColumnMapping | None = None) -> None:
|
|
142
199
|
self._add_query(query=query, column_mapping=column_mapping, dialect=query.dialect)
|
|
@@ -148,7 +205,7 @@ class ObjectMapping(MappingSchema):
|
|
|
148
205
|
dialect: DialectType = None,
|
|
149
206
|
normalize: bool | None = None,
|
|
150
207
|
match_depth: bool = False,
|
|
151
|
-
kind: str = ""
|
|
208
|
+
kind: str = "",
|
|
152
209
|
) -> None:
|
|
153
210
|
"""
|
|
154
211
|
Register or update a table. Updates are only performed if a new column mapping is provided.
|
|
@@ -179,6 +236,11 @@ class ObjectMapping(MappingSchema):
|
|
|
179
236
|
# Store the UDF with other UDFs of the same name
|
|
180
237
|
udfs_with_same_name = self.lookup_udf_query(table, raise_on_missing=False) or []
|
|
181
238
|
query = [query] + udfs_with_same_name
|
|
239
|
+
elif kind == "procedure":
|
|
240
|
+
# Store the procedure with other overloads of the same name, replacing an identical signature
|
|
241
|
+
procedures_with_same_name = self.lookup_procedure_query(table, raise_on_missing=False) or []
|
|
242
|
+
other_overloads = [p for p in procedures_with_same_name if not _has_same_parameter_types(p, query)]
|
|
243
|
+
query = [query] + other_overloads
|
|
182
244
|
|
|
183
245
|
# Store the object
|
|
184
246
|
nested_set(self.kind_mapping[kind], tuple(reversed(parts)), query)
|
|
@@ -230,7 +292,8 @@ class ObjectMapping(MappingSchema):
|
|
|
230
292
|
def lookup_prepare_query(self, table: exp.Table, raise_on_missing: bool = True) -> PrepareQuery | None:
|
|
231
293
|
return self._lookup_query(kind="prepare", object=table, raise_on_missing=raise_on_missing)
|
|
232
294
|
|
|
233
|
-
def lookup_procedure_query(self, table: exp.Table, raise_on_missing: bool = True) -> ProcedureQuery | None:
|
|
295
|
+
def lookup_procedure_query(self, table: exp.Table, raise_on_missing: bool = True) -> list[ProcedureQuery] | None:
|
|
296
|
+
# A procedure can be overloaded with different parameters, thus we return a list
|
|
234
297
|
return self._lookup_query(kind="procedure", object=table, raise_on_missing=raise_on_missing)
|
|
235
298
|
|
|
236
299
|
def lookup_sequence_query(self, table: exp.Table, raise_on_missing: bool = True) -> SequenceQuery | None:
|
|
@@ -281,10 +344,12 @@ class ObjectMapping(MappingSchema):
|
|
|
281
344
|
else:
|
|
282
345
|
knd = "CREATE " + {"udf": "FUNCTION"}.get(kind, kind).upper()
|
|
283
346
|
|
|
284
|
-
raise exception.MappingError(
|
|
347
|
+
raise exception.MappingError(
|
|
348
|
+
f"Could not find '{object.this}' of type '{kind}' in mapping. Did you forget to define a '{knd}' statement?"
|
|
349
|
+
)
|
|
285
350
|
return None
|
|
286
351
|
|
|
287
|
-
parts = self.table_parts(object)[0: len(self.supported_table_args)]
|
|
352
|
+
parts = self.table_parts(object)[0 : len(self.supported_table_args)]
|
|
288
353
|
resolved_parts = self._find_in_trie(parts, self.kind_mapping_trie[kind], raise_on_missing)
|
|
289
354
|
|
|
290
355
|
if resolved_parts is None:
|
|
@@ -344,6 +409,25 @@ class ObjectMapping(MappingSchema):
|
|
|
344
409
|
|
|
345
410
|
return resolve_overloaded_function(node, candidates)
|
|
346
411
|
|
|
412
|
+
def lookup_procedure_call(
|
|
413
|
+
self, node: CallQuery, args: list[exp.Expr] | None = None
|
|
414
|
+
) -> ProcedureQuery | None:
|
|
415
|
+
"""
|
|
416
|
+
Looks up the procedure definition for a CALL statement.
|
|
417
|
+
Returns the matched procedure definition, or None if no procedure with that name exists.
|
|
418
|
+
"""
|
|
419
|
+
procedure_table = exp.table_(table=node.procedure, db=node.schema)
|
|
420
|
+
candidates = self.lookup_procedure_query(table=procedure_table, raise_on_missing=False)
|
|
421
|
+
if not candidates:
|
|
422
|
+
return None
|
|
423
|
+
|
|
424
|
+
# Annotate copies so the call's own expressions are left untouched
|
|
425
|
+
call_args = [arg.copy() for arg in (args if args is not None else node.args)]
|
|
426
|
+
for arg in call_args:
|
|
427
|
+
annotate_types(_get_call_arg_value(arg), dialect=self.dialect, schema=self)
|
|
428
|
+
|
|
429
|
+
return resolve_overloaded_procedure(call_args, candidates)
|
|
430
|
+
|
|
347
431
|
|
|
348
432
|
def resolve_overloaded_function(
|
|
349
433
|
node: exp.Anonymous, candidates: list[UserDefinedFunctionQuery]
|
|
@@ -358,13 +442,7 @@ def resolve_overloaded_function(
|
|
|
358
442
|
|
|
359
443
|
# Function overloading: find the best match based on arguments
|
|
360
444
|
args = node.expressions
|
|
361
|
-
matches =
|
|
362
|
-
for candidate in candidates:
|
|
363
|
-
if match_function_arguments(args, candidate):
|
|
364
|
-
matches.append(candidate)
|
|
365
|
-
|
|
366
|
-
if not matches:
|
|
367
|
-
raise exception.MappingError(f"No matching function signatures found for args: {args}")
|
|
445
|
+
matches = match_overloads(args, candidates, lambda a, c: match_callable_arguments(a, c.parameters), "function")
|
|
368
446
|
|
|
369
447
|
if len(matches) == 1:
|
|
370
448
|
return matches[0]
|
|
@@ -394,25 +472,129 @@ def resolve_overloaded_function(
|
|
|
394
472
|
return matches[0]
|
|
395
473
|
|
|
396
474
|
|
|
397
|
-
def
|
|
475
|
+
def _has_same_parameter_types(left: ProcedureQuery, right: ProcedureQuery) -> bool:
|
|
398
476
|
"""
|
|
399
|
-
|
|
400
|
-
Handles exact types, polymorphic types (anyelement, anyarray), and VARIADIC parameters.
|
|
477
|
+
Compare the parameter types of two procedures.
|
|
401
478
|
"""
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
479
|
+
return [str(p.type) for p in left.parameters] == [str(p.type) for p in right.parameters]
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
def match_overloads(
|
|
483
|
+
args: list[exp.Expr],
|
|
484
|
+
candidates: list[FunctionCallable],
|
|
485
|
+
matcher: t.Callable[[list[exp.Expr], FunctionCallable], bool],
|
|
486
|
+
kind: str,
|
|
487
|
+
) -> list[FunctionCallable]:
|
|
488
|
+
"""
|
|
489
|
+
Returns the candidates whose signature matches the arguments.
|
|
490
|
+
Raises if no candidate matches.
|
|
491
|
+
"""
|
|
492
|
+
matches = [candidate for candidate in candidates if matcher(args, candidate)]
|
|
493
|
+
if not matches:
|
|
494
|
+
raise exception.MappingError(f"No matching {kind} signatures found for args: {args}")
|
|
495
|
+
|
|
496
|
+
return matches
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
def resolve_overloaded_procedure(args: list[exp.Expr], candidates: list[ProcedureQuery]) -> ProcedureQuery:
|
|
500
|
+
"""
|
|
501
|
+
Resolves the procedure overload that the CALL arguments bind to.
|
|
502
|
+
Raises if no overload matches.
|
|
503
|
+
|
|
504
|
+
Example:
|
|
505
|
+
args = [annotate_types(exp.Literal.number(42))] # type INT
|
|
506
|
+
candidates = [p_text_proc, p_int_proc]
|
|
507
|
+
# p_text_proc: ProcedureQuery for process_order(a TEXT)
|
|
508
|
+
# p_int_proc: ProcedureQuery for process_order(a INT)
|
|
509
|
+
resolve_overloaded_procedure(args, candidates)
|
|
510
|
+
-> p_int_proc # process_order(a INT), even though the TEXT candidate is listed first
|
|
511
|
+
"""
|
|
512
|
+
matches = match_overloads(args, candidates, lambda a, c: match_callable_arguments(a, c.parameters), "procedure")
|
|
513
|
+
return matches[0]
|
|
514
|
+
|
|
405
515
|
|
|
406
|
-
|
|
516
|
+
def _get_call_arg_value(arg: exp.Expr) -> exp.Expr:
|
|
517
|
+
"""Returns the value of a CALL argument, unwrapping named notation (`name => value`)."""
|
|
518
|
+
if isinstance(arg, exp.Kwarg):
|
|
519
|
+
return arg.expression
|
|
520
|
+
if isinstance(arg, exp.PropertyEQ):
|
|
521
|
+
return arg.right
|
|
522
|
+
return arg
|
|
407
523
|
|
|
408
|
-
|
|
409
|
-
|
|
524
|
+
|
|
525
|
+
def match_callable_arguments(args: list[exp.Expr], params: list[FunctionParam]) -> bool:
|
|
526
|
+
"""
|
|
527
|
+
Checks if the call arguments bind to the parameters of a UDF or procedure.
|
|
528
|
+
Positional arguments bind in order, named arguments bind by name, and any unbound parameter must have a default.
|
|
529
|
+
A trailing variadic parameter absorbs the remaining positional arguments.
|
|
530
|
+
Bound input parameters are type-checked, while OUT parameters and NULL arguments match any type.
|
|
531
|
+
"""
|
|
532
|
+
positional = [arg for arg in args if not isinstance(arg, (exp.Kwarg, exp.PropertyEQ))]
|
|
533
|
+
named = [arg for arg in args if isinstance(arg, (exp.Kwarg, exp.PropertyEQ))]
|
|
534
|
+
|
|
535
|
+
has_variadic = bool(params) and params[-1].is_variadic
|
|
536
|
+
fixed_count = len(params) - 1 if has_variadic else len(params)
|
|
537
|
+
absorbed = [_get_call_arg_value(arg) for arg in positional[fixed_count:]]
|
|
538
|
+
if absorbed and not has_variadic:
|
|
539
|
+
return False
|
|
540
|
+
|
|
541
|
+
bound = {i: _get_call_arg_value(arg) for i, arg in enumerate(positional[:fixed_count])}
|
|
542
|
+
taken = set(bound)
|
|
543
|
+
if absorbed:
|
|
544
|
+
taken.add(fixed_count)
|
|
545
|
+
|
|
546
|
+
for arg in named:
|
|
547
|
+
arg_key = arg.this if isinstance(arg, exp.Kwarg) else arg.left
|
|
548
|
+
index = next((i for i, p in enumerate(params) if p.name.lower() == arg_key.name.lower()), None)
|
|
549
|
+
if index is None or index in taken:
|
|
410
550
|
return False
|
|
551
|
+
taken.add(index)
|
|
552
|
+
bound[index] = _get_call_arg_value(arg)
|
|
411
553
|
|
|
412
|
-
|
|
413
|
-
|
|
554
|
+
for i, param in enumerate(params):
|
|
555
|
+
if absorbed and i == fixed_count:
|
|
556
|
+
if not _match_variadic_arguments(absorbed, param.type):
|
|
557
|
+
return False
|
|
558
|
+
elif i not in bound:
|
|
559
|
+
if param.default is None and not param.is_variadic:
|
|
560
|
+
return False
|
|
561
|
+
elif param.is_input and not isinstance(bound[i], exp.Null):
|
|
562
|
+
if param.is_output and isinstance(bound[i], exp.Column) and not bound[i].table:
|
|
563
|
+
continue
|
|
564
|
+
if not _match_argument(bound[i], param.type):
|
|
414
565
|
return False
|
|
415
|
-
|
|
566
|
+
|
|
567
|
+
return True
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
def _match_argument(value: exp.Expr, target_type: exp.DataType) -> bool:
|
|
571
|
+
# Mirror match_type's VARCHAR -> TEXT normalization, which is only applied to the argument side
|
|
572
|
+
if target_type.is_type(exp.DataType.Type.VARCHAR):
|
|
573
|
+
target_type = exp.DataType.build("TEXT")
|
|
574
|
+
return match_type(value, target_type)
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
def _match_variadic_arguments(values: list[exp.Expr], array_type: exp.DataType) -> bool:
|
|
578
|
+
"""
|
|
579
|
+
Checks the arguments absorbed by a variadic parameter. A single array argument (`VARIADIC arr`) is matched
|
|
580
|
+
against the whole array type, otherwise each argument is matched against the array's element type.
|
|
581
|
+
|
|
582
|
+
Example:
|
|
583
|
+
# Each INT value matches the element type of INT[]
|
|
584
|
+
values = [annotate_types(exp.Literal.number(1)), annotate_types(exp.Literal.number(2))]
|
|
585
|
+
_match_variadic_arguments(values, exp.DataType.build("INT[]"))
|
|
586
|
+
-> True
|
|
587
|
+
# A string (VARCHAR) does not match INT
|
|
588
|
+
values_fail = [annotate_types(exp.Literal.string("a"))]
|
|
589
|
+
_match_variadic_arguments(values_fail, exp.DataType.build("INT[]"))
|
|
590
|
+
-> False
|
|
591
|
+
"""
|
|
592
|
+
if len(values) == 1 and values[0].type and values[0].type.is_type(exp.DataType.Type.ARRAY):
|
|
593
|
+
return _match_argument(values[0], array_type)
|
|
594
|
+
|
|
595
|
+
element_type = array_type.expressions[0] if array_type.is_type(exp.DataType.Type.ARRAY) else array_type
|
|
596
|
+
return all(isinstance(value, exp.Null) or _match_argument(value, element_type) for value in values)
|
|
597
|
+
|
|
416
598
|
|
|
417
599
|
def match_type(arg: exp.Expr, target_type: exp.DataType) -> bool:
|
|
418
600
|
"""Matches an argument expression to a target data type."""
|
|
@@ -444,4 +626,3 @@ def match_type(arg: exp.Expr, target_type: exp.DataType) -> bool:
|
|
|
444
626
|
return True
|
|
445
627
|
|
|
446
628
|
return exp.DataType.is_type(arg_type, target_type)
|
|
447
|
-
|
|
@@ -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()
|