pytrilogy 0.0.2.26__py3-none-any.whl → 0.0.2.27__py3-none-any.whl
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.
Potentially problematic release.
This version of pytrilogy might be problematic. Click here for more details.
- {pytrilogy-0.0.2.26.dist-info → pytrilogy-0.0.2.27.dist-info}/METADATA +1 -1
- {pytrilogy-0.0.2.26.dist-info → pytrilogy-0.0.2.27.dist-info}/RECORD +20 -20
- {pytrilogy-0.0.2.26.dist-info → pytrilogy-0.0.2.27.dist-info}/WHEEL +1 -1
- trilogy/__init__.py +1 -1
- trilogy/core/graph_models.py +2 -2
- trilogy/core/models.py +111 -85
- trilogy/core/optimizations/inline_datasource.py +4 -4
- trilogy/core/processing/node_generators/select_merge_node.py +7 -1
- trilogy/core/processing/nodes/base_node.py +3 -0
- trilogy/core/processing/nodes/merge_node.py +10 -10
- trilogy/core/processing/nodes/select_node_v2.py +6 -2
- trilogy/core/processing/utility.py +3 -3
- trilogy/core/query_processor.py +21 -17
- trilogy/hooks/query_debugger.py +5 -1
- trilogy/parsing/parse_engine.py +17 -14
- trilogy/parsing/render.py +25 -7
- trilogy/parsing/trilogy.lark +4 -2
- {pytrilogy-0.0.2.26.dist-info → pytrilogy-0.0.2.27.dist-info}/LICENSE.md +0 -0
- {pytrilogy-0.0.2.26.dist-info → pytrilogy-0.0.2.27.dist-info}/entry_points.txt +0 -0
- {pytrilogy-0.0.2.26.dist-info → pytrilogy-0.0.2.27.dist-info}/top_level.txt +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
trilogy/__init__.py,sha256=
|
|
1
|
+
trilogy/__init__.py,sha256=0dmRt4UwzuYJgQIl-qXbs6u3dziHijxpsqFSoJ53myE,291
|
|
2
2
|
trilogy/compiler.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
3
|
trilogy/constants.py,sha256=KiyYnctoZen4Hzv8WG2jeN-IE-dfQbWHdVCUeTZYjBg,1270
|
|
4
4
|
trilogy/engine.py,sha256=R5ubIxYyrxRExz07aZCUfrTsoXCHQ8DKFTDsobXdWdA,1102
|
|
@@ -14,20 +14,20 @@ trilogy/core/environment_helpers.py,sha256=DIsoo-GcXmXVPB1JbNh8Oku25Nyef9mexPIdy
|
|
|
14
14
|
trilogy/core/ergonomics.py,sha256=ASLDd0RqKWrZiG3XcKHo8nyTjaB_8xfE9t4NZ1UvGpc,1639
|
|
15
15
|
trilogy/core/exceptions.py,sha256=NvV_4qLOgKXbpotgRf7c8BANDEvHxlqRPaA53IThQ2o,561
|
|
16
16
|
trilogy/core/functions.py,sha256=IhVpt3n6wEanKHnGu3oA2w6-hKIlxWpEyz7fHN66mpo,10720
|
|
17
|
-
trilogy/core/graph_models.py,sha256=
|
|
17
|
+
trilogy/core/graph_models.py,sha256=mameUTiuCajtihDw_2-W218xyJlvTusOWrEKP1yAWgk,2003
|
|
18
18
|
trilogy/core/internal.py,sha256=jNGFHKENnbMiMCtAgsnLZYVSENDK4b5ALecXFZpTDzQ,1075
|
|
19
|
-
trilogy/core/models.py,sha256=
|
|
19
|
+
trilogy/core/models.py,sha256=TUZtT3oqVMFTuIl2SvfzDO_MgVNzgxK6tP3CaOLBe6s,158789
|
|
20
20
|
trilogy/core/optimization.py,sha256=od_60A9F8J8Nj24MHgrxl4vwRwmBFH13TMdoMQvgVKs,7717
|
|
21
|
-
trilogy/core/query_processor.py,sha256
|
|
21
|
+
trilogy/core/query_processor.py,sha256=mbcZlgjChrRjDHkdmMbKe-T70UpbBkJhS09MyU5a6UY,17785
|
|
22
22
|
trilogy/core/optimizations/__init__.py,sha256=bWQecbeiwiDx9LJnLsa7dkWxdbl2wcnkcTN69JyP8iI,356
|
|
23
23
|
trilogy/core/optimizations/base_optimization.py,sha256=tWWT-xnTbnEU-mNi_isMNbywm8B9WTRsNFwGpeh3rqE,468
|
|
24
24
|
trilogy/core/optimizations/inline_constant.py,sha256=kHNyc2UoaPVdYfVAPAFwnWuk4sJ_IF5faRtVcDOrBtw,1110
|
|
25
|
-
trilogy/core/optimizations/inline_datasource.py,sha256=
|
|
25
|
+
trilogy/core/optimizations/inline_datasource.py,sha256=NqUOVl0pOXF1R_roELVW8I0qN7or2wPtAsRmDD9QJso,3658
|
|
26
26
|
trilogy/core/optimizations/predicate_pushdown.py,sha256=1l9WnFOSv79e341typG3tTdk0XGl1J_ToQih3LYoGIY,8435
|
|
27
27
|
trilogy/core/processing/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
28
28
|
trilogy/core/processing/concept_strategies_v3.py,sha256=7MT_x6QFHrbSDmjz21pYdQB5ux419ES4QS-8lO16eyw,36091
|
|
29
29
|
trilogy/core/processing/graph_utils.py,sha256=aq-kqk4Iado2HywDxWEejWc-7PGO6Oa-ZQLAM6XWPHw,1199
|
|
30
|
-
trilogy/core/processing/utility.py,sha256=
|
|
30
|
+
trilogy/core/processing/utility.py,sha256=KzmlNnJ4or7_0weqtzfFw8WsUqGXtzEO-FeuV7Oz_HQ,17461
|
|
31
31
|
trilogy/core/processing/node_generators/__init__.py,sha256=-mzYkRsaRNa_dfTckYkKVFSR8h8a3ihEiPJDU_tAmDo,672
|
|
32
32
|
trilogy/core/processing/node_generators/basic_node.py,sha256=WQNgJ1MwrMS_BQ-b3XwGGB6eToDykelAVj_fesJuqe0,2069
|
|
33
33
|
trilogy/core/processing/node_generators/common.py,sha256=eslHTTPFTkmwHwKIuUsbFn54jxj-Avtt-QScqtNwzdg,8945
|
|
@@ -37,16 +37,16 @@ trilogy/core/processing/node_generators/group_to_node.py,sha256=R9i_wHipxjXJyfYE
|
|
|
37
37
|
trilogy/core/processing/node_generators/multiselect_node.py,sha256=_KO9lqzHQoy4VAviO0ttQlmK0tjaqrJj4SJPhmoIYm8,6229
|
|
38
38
|
trilogy/core/processing/node_generators/node_merge_node.py,sha256=dIEv5P2MTViAES2MzqJgccYzM3HldjHrQYFwH00cqyc,14003
|
|
39
39
|
trilogy/core/processing/node_generators/rowset_node.py,sha256=KtdN6t2xM8CJxobc4aQX4W8uX98U6IabeuBF_FtBLR4,4583
|
|
40
|
-
trilogy/core/processing/node_generators/select_merge_node.py,sha256=
|
|
40
|
+
trilogy/core/processing/node_generators/select_merge_node.py,sha256=vE7GoPu2_okO3jS96oA5O3jFsrkiSqIvIP5WkyfFil0,11596
|
|
41
41
|
trilogy/core/processing/node_generators/select_node.py,sha256=nwXHQF6C-aQUIelx9dyxN2pK3muL-4-6RIqnqQqNwtw,1808
|
|
42
42
|
trilogy/core/processing/node_generators/unnest_node.py,sha256=cZ26CN338CBnd6asML1OBUtNcDzmNlFpY0Vnade4yrc,2256
|
|
43
43
|
trilogy/core/processing/node_generators/window_node.py,sha256=jy3FF8uN0VA7yyrBeR40B9CAqR_5qBP4PiS6Gr-f-7w,2590
|
|
44
44
|
trilogy/core/processing/nodes/__init__.py,sha256=qS5EJDRwwIrCEfS7ibCA2ESE0RPzsAIii1UWd_wNsHA,4760
|
|
45
|
-
trilogy/core/processing/nodes/base_node.py,sha256=
|
|
45
|
+
trilogy/core/processing/nodes/base_node.py,sha256=8nEG3OPE_LzFXI48-Y6FS8MyO79LY0Sm0EqYz31WJ1Q,15719
|
|
46
46
|
trilogy/core/processing/nodes/filter_node.py,sha256=GfZ9eghpFDI-s7iQP2UqTljCmn25LT_T5TAxDlh7PkQ,2343
|
|
47
47
|
trilogy/core/processing/nodes/group_node.py,sha256=PrBHaGq_f8RmokUw9lXLGJ5YbjdP77P7Ag0pgR6e2cU,7293
|
|
48
|
-
trilogy/core/processing/nodes/merge_node.py,sha256=
|
|
49
|
-
trilogy/core/processing/nodes/select_node_v2.py,sha256=
|
|
48
|
+
trilogy/core/processing/nodes/merge_node.py,sha256=W3eCjmJbs8Wfw7Y5AgIY2pP-ntPCrrMe11UG-QGJvA8,14835
|
|
49
|
+
trilogy/core/processing/nodes/select_node_v2.py,sha256=k5WvqmOkLwnP9SFSF5z33a1SFo4nZ-y9ODLi-P05YkI,8281
|
|
50
50
|
trilogy/core/processing/nodes/unnest_node.py,sha256=mAmFluzm2yeeiQ6NfIB7BU_8atRGh-UJfPf9ROwbhr8,2152
|
|
51
51
|
trilogy/core/processing/nodes/window_node.py,sha256=ro0QfMFi4ZmIn5Q4D0M_vJWfnHH_C0MN7XkVkx8Gygg,1214
|
|
52
52
|
trilogy/dialect/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
@@ -63,21 +63,21 @@ trilogy/dialect/sql_server.py,sha256=owUZbMFrooYIMj1DSLstPWxPO7K7WAUEWNvDKM-DMt0
|
|
|
63
63
|
trilogy/hooks/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
64
64
|
trilogy/hooks/base_hook.py,sha256=Xkb-A2qCHozYjum0A36zOy5PwTVwrP3NLDF0U2GpgHo,1100
|
|
65
65
|
trilogy/hooks/graph_hook.py,sha256=onHvMQPwj_KOS3HOTpRFiy7QLLKAiycq2MzJ_Q0Oh5Y,2467
|
|
66
|
-
trilogy/hooks/query_debugger.py,sha256=
|
|
66
|
+
trilogy/hooks/query_debugger.py,sha256=787umJjdGA057DCC714dqFstzJRUbwmz3MNr66IdpQI,4404
|
|
67
67
|
trilogy/metadata/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
68
68
|
trilogy/parsing/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
69
69
|
trilogy/parsing/common.py,sha256=t7yiL_3f6rz_rouF9et84v5orAgs-EprV4V9ghQ6ql4,10024
|
|
70
70
|
trilogy/parsing/config.py,sha256=Z-DaefdKhPDmSXLgg5V4pebhSB0h590vI0_VtHnlukI,111
|
|
71
71
|
trilogy/parsing/exceptions.py,sha256=92E5i2frv5hj9wxObJZsZqj5T6bglvPzvdvco_vW1Zk,38
|
|
72
72
|
trilogy/parsing/helpers.py,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
|
73
|
-
trilogy/parsing/parse_engine.py,sha256=
|
|
74
|
-
trilogy/parsing/render.py,sha256=
|
|
75
|
-
trilogy/parsing/trilogy.lark,sha256=
|
|
73
|
+
trilogy/parsing/parse_engine.py,sha256=JwG98fotPpvh5VC-CcHknCTFid9-Zj1Wfo8CyPOnJzs,64431
|
|
74
|
+
trilogy/parsing/render.py,sha256=B9J2GrYQcE76kddMQSeAmvAPX-9pv39mpeSHZ10SNj8,14655
|
|
75
|
+
trilogy/parsing/trilogy.lark,sha256=_z5px2N-e8oLUf7SpPMXXNqbAykDkZOvP4_lPgf5-Uk,12245
|
|
76
76
|
trilogy/scripts/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
77
77
|
trilogy/scripts/trilogy.py,sha256=PHxvv6f2ODv0esyyhWxlARgra8dVhqQhYl0lTrSyVNo,3729
|
|
78
|
-
pytrilogy-0.0.2.
|
|
79
|
-
pytrilogy-0.0.2.
|
|
80
|
-
pytrilogy-0.0.2.
|
|
81
|
-
pytrilogy-0.0.2.
|
|
82
|
-
pytrilogy-0.0.2.
|
|
83
|
-
pytrilogy-0.0.2.
|
|
78
|
+
pytrilogy-0.0.2.27.dist-info/LICENSE.md,sha256=5ZRvtTyCCFwz1THxDTjAu3Lidds9WjPvvzgVwPSYNDo,1042
|
|
79
|
+
pytrilogy-0.0.2.27.dist-info/METADATA,sha256=FwOit8tU80ZUP3yP3IpzIyUV30J9z3JlJi5dfawRtTQ,8403
|
|
80
|
+
pytrilogy-0.0.2.27.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
|
|
81
|
+
pytrilogy-0.0.2.27.dist-info/entry_points.txt,sha256=0petKryjvvtEfTlbZC1AuMFumH_WQ9v8A19LvoS6G6c,54
|
|
82
|
+
pytrilogy-0.0.2.27.dist-info/top_level.txt,sha256=cAy__NW_eMAa_yT9UnUNlZLFfxcg6eimUAZ184cdNiE,8
|
|
83
|
+
pytrilogy-0.0.2.27.dist-info/RECORD,,
|
trilogy/__init__.py
CHANGED
trilogy/core/graph_models.py
CHANGED
|
@@ -6,7 +6,7 @@ from trilogy.core.models import Concept, Datasource
|
|
|
6
6
|
def concept_to_node(input: Concept) -> str:
|
|
7
7
|
# if input.purpose == Purpose.METRIC:
|
|
8
8
|
# return f"c~{input.namespace}.{input.name}@{input.grain}"
|
|
9
|
-
return f"c~{input.
|
|
9
|
+
return f"c~{input.address}@{input.grain}"
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
def datasource_to_node(input: Datasource) -> str:
|
|
@@ -14,7 +14,7 @@ def datasource_to_node(input: Datasource) -> str:
|
|
|
14
14
|
# return "ds~join~" + ",".join(
|
|
15
15
|
# [datasource_to_node(sub) for sub in input.datasources]
|
|
16
16
|
# )
|
|
17
|
-
return f"ds~{input.
|
|
17
|
+
return f"ds~{input.identifier}"
|
|
18
18
|
|
|
19
19
|
|
|
20
20
|
class ReferenceGraph(nx.DiGraph):
|
trilogy/core/models.py
CHANGED
|
@@ -1719,7 +1719,7 @@ class SelectStatement(HasUUID, Mergeable, Namespaced, SelectTypeMixin, BaseModel
|
|
|
1719
1719
|
def to_datasource(
|
|
1720
1720
|
self,
|
|
1721
1721
|
namespace: str,
|
|
1722
|
-
|
|
1722
|
+
name: str,
|
|
1723
1723
|
address: Address,
|
|
1724
1724
|
grain: Grain | None = None,
|
|
1725
1725
|
) -> Datasource:
|
|
@@ -1753,7 +1753,7 @@ class SelectStatement(HasUUID, Mergeable, Namespaced, SelectTypeMixin, BaseModel
|
|
|
1753
1753
|
condition = self.having_clause.conditional
|
|
1754
1754
|
|
|
1755
1755
|
new_datasource = Datasource(
|
|
1756
|
-
|
|
1756
|
+
name=name,
|
|
1757
1757
|
address=address,
|
|
1758
1758
|
grain=grain or self.grain,
|
|
1759
1759
|
columns=columns,
|
|
@@ -2059,7 +2059,7 @@ class MergeStatementV2(HasUUID, Namespaced, BaseModel):
|
|
|
2059
2059
|
|
|
2060
2060
|
|
|
2061
2061
|
class Datasource(HasUUID, Namespaced, BaseModel):
|
|
2062
|
-
|
|
2062
|
+
name: str
|
|
2063
2063
|
columns: List[ColumnAssignment]
|
|
2064
2064
|
address: Union[Address, str]
|
|
2065
2065
|
grain: Grain = Field(
|
|
@@ -2094,10 +2094,14 @@ class Datasource(HasUUID, Namespaced, BaseModel):
|
|
|
2094
2094
|
self.add_column(target, original[0].alias, modifiers)
|
|
2095
2095
|
|
|
2096
2096
|
@property
|
|
2097
|
-
def
|
|
2097
|
+
def identifier(self) -> str:
|
|
2098
2098
|
if not self.namespace or self.namespace == DEFAULT_NAMESPACE:
|
|
2099
|
-
return self.
|
|
2100
|
-
return f"{self.namespace}.{self.
|
|
2099
|
+
return self.name
|
|
2100
|
+
return f"{self.namespace}.{self.name}"
|
|
2101
|
+
|
|
2102
|
+
@property
|
|
2103
|
+
def safe_identifier(self) -> str:
|
|
2104
|
+
return self.identifier.replace(".", "_")
|
|
2101
2105
|
|
|
2102
2106
|
@property
|
|
2103
2107
|
def condition(self):
|
|
@@ -2166,13 +2170,13 @@ class Datasource(HasUUID, Namespaced, BaseModel):
|
|
|
2166
2170
|
return self
|
|
2167
2171
|
|
|
2168
2172
|
def __repr__(self):
|
|
2169
|
-
return f"Datasource<{self.
|
|
2173
|
+
return f"Datasource<{self.identifier}@<{self.grain}>"
|
|
2170
2174
|
|
|
2171
2175
|
def __str__(self):
|
|
2172
2176
|
return self.__repr__()
|
|
2173
2177
|
|
|
2174
2178
|
def __hash__(self):
|
|
2175
|
-
return self.
|
|
2179
|
+
return self.identifier.__hash__()
|
|
2176
2180
|
|
|
2177
2181
|
def with_namespace(self, namespace: str):
|
|
2178
2182
|
new_namespace = (
|
|
@@ -2181,7 +2185,7 @@ class Datasource(HasUUID, Namespaced, BaseModel):
|
|
|
2181
2185
|
else namespace
|
|
2182
2186
|
)
|
|
2183
2187
|
return Datasource(
|
|
2184
|
-
|
|
2188
|
+
name=self.name,
|
|
2185
2189
|
namespace=new_namespace,
|
|
2186
2190
|
grain=self.grain.with_namespace(namespace),
|
|
2187
2191
|
address=self.address,
|
|
@@ -2231,19 +2235,6 @@ class Datasource(HasUUID, Namespaced, BaseModel):
|
|
|
2231
2235
|
f" {existing}."
|
|
2232
2236
|
)
|
|
2233
2237
|
|
|
2234
|
-
@property
|
|
2235
|
-
def name(self) -> str:
|
|
2236
|
-
return self.identifier
|
|
2237
|
-
# TODO: namespace all references
|
|
2238
|
-
# return f'{self.namespace}_{self.identifier}'
|
|
2239
|
-
|
|
2240
|
-
@property
|
|
2241
|
-
def full_name(self) -> str:
|
|
2242
|
-
if not self.namespace:
|
|
2243
|
-
return self.identifier
|
|
2244
|
-
namespace = self.namespace.replace(".", "_") if self.namespace else ""
|
|
2245
|
-
return f"{namespace}_{self.identifier}"
|
|
2246
|
-
|
|
2247
2238
|
@property
|
|
2248
2239
|
def safe_location(self) -> str:
|
|
2249
2240
|
if isinstance(self.address, Address):
|
|
@@ -2298,7 +2289,7 @@ class BaseJoin(BaseModel):
|
|
|
2298
2289
|
super().__init__(**data)
|
|
2299
2290
|
if (
|
|
2300
2291
|
self.left_datasource
|
|
2301
|
-
and self.left_datasource.
|
|
2292
|
+
and self.left_datasource.identifier == self.right_datasource.identifier
|
|
2302
2293
|
):
|
|
2303
2294
|
raise SyntaxError(
|
|
2304
2295
|
f"Cannot join a dataself to itself, joining {self.left_datasource} and"
|
|
@@ -2410,6 +2401,10 @@ class QueryDatasource(BaseModel):
|
|
|
2410
2401
|
def __repr__(self):
|
|
2411
2402
|
return f"{self.identifier}@<{self.grain}>"
|
|
2412
2403
|
|
|
2404
|
+
@property
|
|
2405
|
+
def safe_identifier(self):
|
|
2406
|
+
return self.identifier.replace(".", "_")
|
|
2407
|
+
|
|
2413
2408
|
@property
|
|
2414
2409
|
def non_partial_concept_addresses(self) -> List[str]:
|
|
2415
2410
|
return [
|
|
@@ -2474,10 +2469,6 @@ class QueryDatasource(BaseModel):
|
|
|
2474
2469
|
def name(self):
|
|
2475
2470
|
return self.identifier
|
|
2476
2471
|
|
|
2477
|
-
@property
|
|
2478
|
-
def full_name(self):
|
|
2479
|
-
return self.identifier
|
|
2480
|
-
|
|
2481
2472
|
@property
|
|
2482
2473
|
def group_required(self) -> bool:
|
|
2483
2474
|
if self.force_group is True:
|
|
@@ -2524,10 +2515,12 @@ class QueryDatasource(BaseModel):
|
|
|
2524
2515
|
merged_datasources = {}
|
|
2525
2516
|
|
|
2526
2517
|
for ds in [*self.datasources, *other.datasources]:
|
|
2527
|
-
if ds.
|
|
2528
|
-
merged_datasources[ds.
|
|
2518
|
+
if ds.safe_identifier in merged_datasources:
|
|
2519
|
+
merged_datasources[ds.safe_identifier] = (
|
|
2520
|
+
merged_datasources[ds.safe_identifier] + ds
|
|
2521
|
+
)
|
|
2529
2522
|
else:
|
|
2530
|
-
merged_datasources[ds.
|
|
2523
|
+
merged_datasources[ds.safe_identifier] = ds
|
|
2531
2524
|
|
|
2532
2525
|
final_source_map = defaultdict(set)
|
|
2533
2526
|
for key in self.source_map:
|
|
@@ -2538,7 +2531,9 @@ class QueryDatasource(BaseModel):
|
|
|
2538
2531
|
if key not in final_source_map:
|
|
2539
2532
|
final_source_map[key] = other.source_map[key]
|
|
2540
2533
|
for k, v in final_source_map.items():
|
|
2541
|
-
final_source_map[k] = set(
|
|
2534
|
+
final_source_map[k] = set(
|
|
2535
|
+
merged_datasources[x.safe_identifier] for x in list(v)
|
|
2536
|
+
)
|
|
2542
2537
|
self_hidden = self.hidden_concepts or []
|
|
2543
2538
|
other_hidden = other.hidden_concepts or []
|
|
2544
2539
|
hidden = [x for x in self_hidden if x.address in other_hidden]
|
|
@@ -2578,7 +2573,7 @@ class QueryDatasource(BaseModel):
|
|
|
2578
2573
|
)
|
|
2579
2574
|
# partial = "_".join([str(c.address).replace(".", "_") for c in self.partial_concepts])
|
|
2580
2575
|
return (
|
|
2581
|
-
"_join_".join([d.
|
|
2576
|
+
"_join_".join([d.identifier for d in self.datasources])
|
|
2582
2577
|
+ (f"_at_{grain}" if grain else "_at_abstract")
|
|
2583
2578
|
+ (f"_filtered_by_{filters}" if filters else "")
|
|
2584
2579
|
# + (f"_partial_{partial}" if partial else "")
|
|
@@ -2594,8 +2589,9 @@ class QueryDatasource(BaseModel):
|
|
|
2594
2589
|
for x in self.datasources:
|
|
2595
2590
|
# query datasources should be referenced by their alias, always
|
|
2596
2591
|
force_alias = isinstance(x, QueryDatasource)
|
|
2592
|
+
#
|
|
2597
2593
|
use_raw_name = isinstance(x, Datasource) and not force_alias
|
|
2598
|
-
if source and x.
|
|
2594
|
+
if source and x.safe_identifier != source:
|
|
2599
2595
|
continue
|
|
2600
2596
|
try:
|
|
2601
2597
|
return x.get_alias(
|
|
@@ -2649,6 +2645,14 @@ class CTE(BaseModel):
|
|
|
2649
2645
|
base_name_override: Optional[str] = None
|
|
2650
2646
|
base_alias_override: Optional[str] = None
|
|
2651
2647
|
|
|
2648
|
+
@property
|
|
2649
|
+
def identifier(self):
|
|
2650
|
+
return self.name
|
|
2651
|
+
|
|
2652
|
+
@property
|
|
2653
|
+
def safe_identifier(self):
|
|
2654
|
+
return self.name
|
|
2655
|
+
|
|
2652
2656
|
@computed_field # type: ignore
|
|
2653
2657
|
@property
|
|
2654
2658
|
def output_lcl(self) -> LooseConceptList:
|
|
@@ -2746,7 +2750,7 @@ class CTE(BaseModel):
|
|
|
2746
2750
|
return False
|
|
2747
2751
|
if any(
|
|
2748
2752
|
[
|
|
2749
|
-
x.
|
|
2753
|
+
x.safe_identifier == ds_being_inlined.safe_identifier
|
|
2750
2754
|
for x in self.source.datasources
|
|
2751
2755
|
]
|
|
2752
2756
|
):
|
|
@@ -2757,39 +2761,49 @@ class CTE(BaseModel):
|
|
|
2757
2761
|
*[
|
|
2758
2762
|
x
|
|
2759
2763
|
for x in self.source.datasources
|
|
2760
|
-
if x.
|
|
2764
|
+
if x.safe_identifier != qds_being_inlined.safe_identifier
|
|
2761
2765
|
],
|
|
2762
2766
|
]
|
|
2763
2767
|
# need to identify this before updating joins
|
|
2764
2768
|
if self.base_name == parent.name:
|
|
2765
2769
|
self.base_name_override = ds_being_inlined.safe_location
|
|
2766
|
-
self.base_alias_override = ds_being_inlined.
|
|
2770
|
+
self.base_alias_override = ds_being_inlined.safe_identifier
|
|
2767
2771
|
|
|
2768
2772
|
for join in self.joins:
|
|
2769
2773
|
if isinstance(join, InstantiatedUnnestJoin):
|
|
2770
2774
|
continue
|
|
2771
|
-
if
|
|
2775
|
+
if (
|
|
2776
|
+
join.left_cte
|
|
2777
|
+
and join.left_cte.safe_identifier == parent.safe_identifier
|
|
2778
|
+
):
|
|
2772
2779
|
join.inline_cte(parent)
|
|
2773
2780
|
if join.joinkey_pairs:
|
|
2774
2781
|
for pair in join.joinkey_pairs:
|
|
2775
|
-
if pair.cte and pair.cte.
|
|
2782
|
+
if pair.cte and pair.cte.safe_identifier == parent.safe_identifier:
|
|
2776
2783
|
join.inline_cte(parent)
|
|
2777
|
-
if join.right_cte.
|
|
2784
|
+
if join.right_cte.safe_identifier == parent.safe_identifier:
|
|
2778
2785
|
join.inline_cte(parent)
|
|
2779
2786
|
for k, v in self.source_map.items():
|
|
2780
2787
|
if isinstance(v, list):
|
|
2781
2788
|
self.source_map[k] = [
|
|
2782
|
-
|
|
2789
|
+
(
|
|
2790
|
+
ds_being_inlined.safe_identifier
|
|
2791
|
+
if x == parent.safe_identifier
|
|
2792
|
+
else x
|
|
2793
|
+
)
|
|
2794
|
+
for x in v
|
|
2783
2795
|
]
|
|
2784
|
-
elif v == parent.
|
|
2785
|
-
self.source_map[k] = [ds_being_inlined.
|
|
2796
|
+
elif v == parent.safe_identifier:
|
|
2797
|
+
self.source_map[k] = [ds_being_inlined.safe_identifier]
|
|
2786
2798
|
|
|
2787
2799
|
# zip in any required values for lookups
|
|
2788
2800
|
for k in ds_being_inlined.output_lcl.addresses:
|
|
2789
2801
|
if k in self.source_map and self.source_map[k]:
|
|
2790
2802
|
continue
|
|
2791
|
-
self.source_map[k] = [ds_being_inlined.
|
|
2792
|
-
self.parent_ctes = [
|
|
2803
|
+
self.source_map[k] = [ds_being_inlined.safe_identifier]
|
|
2804
|
+
self.parent_ctes = [
|
|
2805
|
+
x for x in self.parent_ctes if x.safe_identifier != parent.safe_identifier
|
|
2806
|
+
]
|
|
2793
2807
|
if force_group:
|
|
2794
2808
|
self.group_to_grain = True
|
|
2795
2809
|
return True
|
|
@@ -3006,28 +3020,22 @@ class Join(BaseModel):
|
|
|
3006
3020
|
def inline_cte(self, cte: CTE):
|
|
3007
3021
|
self.inlined_ctes.add(cte.name)
|
|
3008
3022
|
|
|
3009
|
-
# @property
|
|
3010
|
-
# def left_name(self) -> str:
|
|
3011
|
-
# if self.left_cte.name in self.inlined_ctes:
|
|
3012
|
-
# return self.left_cte.source.datasources[0].identifier
|
|
3013
|
-
# return self.left_cte.name
|
|
3014
|
-
|
|
3015
3023
|
def get_name(self, cte: CTE):
|
|
3016
|
-
if cte.
|
|
3017
|
-
return cte.source.datasources[0].
|
|
3018
|
-
return cte.
|
|
3024
|
+
if cte.identifier in self.inlined_ctes:
|
|
3025
|
+
return cte.source.datasources[0].safe_identifier
|
|
3026
|
+
return cte.safe_identifier
|
|
3019
3027
|
|
|
3020
3028
|
@property
|
|
3021
3029
|
def right_name(self) -> str:
|
|
3022
|
-
if self.right_cte.
|
|
3023
|
-
return self.right_cte.source.datasources[0].
|
|
3024
|
-
return self.right_cte.
|
|
3030
|
+
if self.right_cte.identifier in self.inlined_ctes:
|
|
3031
|
+
return self.right_cte.source.datasources[0].safe_identifier
|
|
3032
|
+
return self.right_cte.safe_identifier
|
|
3025
3033
|
|
|
3026
3034
|
@property
|
|
3027
3035
|
def right_ref(self) -> str:
|
|
3028
|
-
if self.right_cte.
|
|
3029
|
-
return f"{self.right_cte.source.datasources[0].safe_location} as {self.right_cte.source.datasources[0].
|
|
3030
|
-
return self.right_cte.
|
|
3036
|
+
if self.right_cte.identifier in self.inlined_ctes:
|
|
3037
|
+
return f"{self.right_cte.source.datasources[0].safe_location} as {self.right_cte.source.datasources[0].safe_identifier}"
|
|
3038
|
+
return self.right_cte.safe_identifier
|
|
3031
3039
|
|
|
3032
3040
|
@property
|
|
3033
3041
|
def unique_id(self) -> str:
|
|
@@ -3306,7 +3314,9 @@ class Environment(BaseModel):
|
|
|
3306
3314
|
] = Field(default_factory=EnvironmentDatasourceDict)
|
|
3307
3315
|
functions: Dict[str, Function] = Field(default_factory=dict)
|
|
3308
3316
|
data_types: Dict[str, DataType] = Field(default_factory=dict)
|
|
3309
|
-
imports: Dict[str, ImportStatement] = Field(
|
|
3317
|
+
imports: Dict[str, list[ImportStatement]] = Field(
|
|
3318
|
+
default_factory=lambda: defaultdict(list)
|
|
3319
|
+
)
|
|
3310
3320
|
namespace: str = DEFAULT_NAMESPACE
|
|
3311
3321
|
working_path: str | Path = Field(default_factory=lambda: os.getcwd())
|
|
3312
3322
|
environment_config: EnvironmentOptions = Field(default_factory=EnvironmentOptions)
|
|
@@ -3420,14 +3430,28 @@ class Environment(BaseModel):
|
|
|
3420
3430
|
f"Assignment to concept '{lookup}' is a duplicate declaration;"
|
|
3421
3431
|
)
|
|
3422
3432
|
|
|
3423
|
-
def add_import(
|
|
3424
|
-
self
|
|
3425
|
-
|
|
3426
|
-
|
|
3427
|
-
|
|
3428
|
-
|
|
3429
|
-
|
|
3430
|
-
|
|
3433
|
+
def add_import(
|
|
3434
|
+
self, alias: str, source: Environment, imp_stm: ImportStatement | None = None
|
|
3435
|
+
):
|
|
3436
|
+
exists = False
|
|
3437
|
+
existing = self.imports[alias]
|
|
3438
|
+
if imp_stm:
|
|
3439
|
+
if any([x.path == imp_stm.path for x in existing]):
|
|
3440
|
+
exists = True
|
|
3441
|
+
|
|
3442
|
+
else:
|
|
3443
|
+
if any([x.path == source.working_path for x in existing]):
|
|
3444
|
+
exists = True
|
|
3445
|
+
imp_stm = ImportStatement(alias=alias, path=Path(source.working_path))
|
|
3446
|
+
|
|
3447
|
+
if not exists:
|
|
3448
|
+
self.imports[alias].append(imp_stm)
|
|
3449
|
+
|
|
3450
|
+
for _, concept in source.concepts.items():
|
|
3451
|
+
self.add_concept(concept.with_namespace(alias), _ignore_cache=True)
|
|
3452
|
+
|
|
3453
|
+
for _, datasource in source.datasources.items():
|
|
3454
|
+
self.add_datasource(datasource.with_namespace(alias), _ignore_cache=True)
|
|
3431
3455
|
self.gen_concept_list_caches()
|
|
3432
3456
|
return self
|
|
3433
3457
|
|
|
@@ -3438,18 +3462,15 @@ class Environment(BaseModel):
|
|
|
3438
3462
|
apath[-1] = apath[-1] + ".preql"
|
|
3439
3463
|
|
|
3440
3464
|
target: Path = Path(self.working_path, *apath)
|
|
3465
|
+
if alias in self.imports:
|
|
3466
|
+
imports = self.imports[alias]
|
|
3467
|
+
for x in imports:
|
|
3468
|
+
if x.path == target:
|
|
3469
|
+
return imports
|
|
3441
3470
|
if env:
|
|
3442
|
-
self.imports[alias]
|
|
3443
|
-
alias=alias, path=target, environment=env
|
|
3471
|
+
self.imports[alias].append(
|
|
3472
|
+
ImportStatement(alias=alias, path=target, environment=env)
|
|
3444
3473
|
)
|
|
3445
|
-
|
|
3446
|
-
elif alias in self.imports:
|
|
3447
|
-
current = self.imports[alias]
|
|
3448
|
-
env = self.imports[alias].environment
|
|
3449
|
-
if current.path != target:
|
|
3450
|
-
raise ImportError(
|
|
3451
|
-
f"Attempted to import {target} with alias {alias} but {alias} is already imported from {current.path}"
|
|
3452
|
-
)
|
|
3453
3474
|
else:
|
|
3454
3475
|
try:
|
|
3455
3476
|
with open(target, "r", encoding="utf-8") as f:
|
|
@@ -3468,14 +3489,13 @@ class Environment(BaseModel):
|
|
|
3468
3489
|
f"Unable to import file {target.parent}, parsing error: {e}"
|
|
3469
3490
|
)
|
|
3470
3491
|
env = nparser.environment
|
|
3471
|
-
|
|
3472
|
-
|
|
3473
|
-
self.add_concept(concept.with_namespace(alias))
|
|
3492
|
+
for _, concept in env.concepts.items():
|
|
3493
|
+
self.add_concept(concept.with_namespace(alias))
|
|
3474
3494
|
|
|
3475
|
-
|
|
3476
|
-
|
|
3495
|
+
for _, datasource in env.datasources.items():
|
|
3496
|
+
self.add_datasource(datasource.with_namespace(alias))
|
|
3477
3497
|
imps = ImportStatement(alias=alias, path=target, environment=env)
|
|
3478
|
-
self.imports[alias]
|
|
3498
|
+
self.imports[alias].append(imps)
|
|
3479
3499
|
return imps
|
|
3480
3500
|
|
|
3481
3501
|
def parse(
|
|
@@ -3538,8 +3558,14 @@ class Environment(BaseModel):
|
|
|
3538
3558
|
meta: Meta | None = None,
|
|
3539
3559
|
_ignore_cache: bool = False,
|
|
3540
3560
|
):
|
|
3541
|
-
self.datasources[datasource.
|
|
3561
|
+
self.datasources[datasource.identifier] = datasource
|
|
3562
|
+
|
|
3563
|
+
eligible_to_promote_roots = datasource.non_partial_for is None
|
|
3564
|
+
# mark this as canonical source
|
|
3542
3565
|
for current_concept in datasource.output_concepts:
|
|
3566
|
+
if not eligible_to_promote_roots:
|
|
3567
|
+
continue
|
|
3568
|
+
|
|
3543
3569
|
current_derivation = current_concept.derivation
|
|
3544
3570
|
# TODO: refine this section;
|
|
3545
3571
|
# too hacky for maintainability
|
|
@@ -63,14 +63,14 @@ class InlineDatasource(OptimizationRule):
|
|
|
63
63
|
for replaceable in to_inline:
|
|
64
64
|
if replaceable.name not in self.candidates[cte.name]:
|
|
65
65
|
self.candidates[cte.name].add(replaceable.name)
|
|
66
|
-
self.count[replaceable.source.
|
|
66
|
+
self.count[replaceable.source.identifier] += 1
|
|
67
67
|
return True
|
|
68
68
|
if (
|
|
69
|
-
self.count[replaceable.source.
|
|
69
|
+
self.count[replaceable.source.identifier]
|
|
70
70
|
> CONFIG.optimizations.constant_inline_cutoff
|
|
71
71
|
):
|
|
72
72
|
self.log(
|
|
73
|
-
f"Skipping inlining raw datasource {replaceable.source.
|
|
73
|
+
f"Skipping inlining raw datasource {replaceable.source.identifier} ({replaceable.name}) due to multiple references"
|
|
74
74
|
)
|
|
75
75
|
continue
|
|
76
76
|
if not replaceable.source.datasources[0].grain.issubset(replaceable.grain):
|
|
@@ -81,7 +81,7 @@ class InlineDatasource(OptimizationRule):
|
|
|
81
81
|
result = cte.inline_parent_datasource(replaceable, force_group=force_group)
|
|
82
82
|
if result:
|
|
83
83
|
self.log(
|
|
84
|
-
f"Inlined parent {replaceable.name} with {replaceable.source.
|
|
84
|
+
f"Inlined parent {replaceable.name} with {replaceable.source.identifier}"
|
|
85
85
|
)
|
|
86
86
|
optimized = True
|
|
87
87
|
else:
|
|
@@ -193,6 +193,7 @@ def create_select_node(
|
|
|
193
193
|
g,
|
|
194
194
|
environment: Environment,
|
|
195
195
|
depth: int,
|
|
196
|
+
conditions: WhereClause | None = None,
|
|
196
197
|
) -> StrategyNode:
|
|
197
198
|
ds_name = ds_name.split("~")[1]
|
|
198
199
|
all_concepts = [
|
|
@@ -231,6 +232,7 @@ def create_select_node(
|
|
|
231
232
|
c.concept for c in datasource.columns if c.is_nullable and c.concept in all_lcl
|
|
232
233
|
]
|
|
233
234
|
nullable_lcl = LooseConceptList(concepts=nullable_concepts)
|
|
235
|
+
partial_is_full = conditions and (conditions == datasource.non_partial_for)
|
|
234
236
|
|
|
235
237
|
bcandidate: StrategyNode = SelectNode(
|
|
236
238
|
input_concepts=[c.concept for c in datasource.columns],
|
|
@@ -239,12 +241,15 @@ def create_select_node(
|
|
|
239
241
|
g=g,
|
|
240
242
|
parents=[],
|
|
241
243
|
depth=depth,
|
|
242
|
-
partial_concepts=
|
|
244
|
+
partial_concepts=(
|
|
245
|
+
[] if partial_is_full else [c for c in all_concepts if c in partial_lcl]
|
|
246
|
+
),
|
|
243
247
|
nullable_concepts=[c for c in all_concepts if c in nullable_lcl],
|
|
244
248
|
accept_partial=accept_partial,
|
|
245
249
|
datasource=datasource,
|
|
246
250
|
grain=Grain(components=all_concepts),
|
|
247
251
|
conditions=datasource.where.conditional if datasource.where else None,
|
|
252
|
+
render_condition=not partial_is_full,
|
|
248
253
|
)
|
|
249
254
|
|
|
250
255
|
# we need to nest the group node one further
|
|
@@ -312,6 +317,7 @@ def gen_select_merge_node(
|
|
|
312
317
|
accept_partial=accept_partial,
|
|
313
318
|
environment=environment,
|
|
314
319
|
depth=depth,
|
|
320
|
+
conditions=conditions,
|
|
315
321
|
)
|
|
316
322
|
for k, subgraph in sub_nodes.items()
|
|
317
323
|
]
|
|
@@ -165,6 +165,7 @@ class StrategyNode:
|
|
|
165
165
|
hidden_concepts: List[Concept] | None = None,
|
|
166
166
|
existence_concepts: List[Concept] | None = None,
|
|
167
167
|
virtual_output_concepts: List[Concept] | None = None,
|
|
168
|
+
render_condition: bool = True,
|
|
168
169
|
):
|
|
169
170
|
self.input_concepts: List[Concept] = (
|
|
170
171
|
unique(input_concepts, "address") if input_concepts else []
|
|
@@ -208,6 +209,7 @@ class StrategyNode:
|
|
|
208
209
|
)
|
|
209
210
|
self.validate_parents()
|
|
210
211
|
self.log = True
|
|
212
|
+
self.render_condition = render_condition
|
|
211
213
|
|
|
212
214
|
def add_parents(self, parents: list["StrategyNode"]):
|
|
213
215
|
self.parents += parents
|
|
@@ -380,6 +382,7 @@ class StrategyNode:
|
|
|
380
382
|
hidden_concepts=list(self.hidden_concepts),
|
|
381
383
|
existence_concepts=list(self.existence_concepts),
|
|
382
384
|
virtual_output_concepts=list(self.virtual_output_concepts),
|
|
385
|
+
render_condition=self.render_condition,
|
|
383
386
|
)
|
|
384
387
|
|
|
385
388
|
|
|
@@ -89,8 +89,8 @@ def deduplicate_nodes_and_joins(
|
|
|
89
89
|
joins = [
|
|
90
90
|
j
|
|
91
91
|
for j in joins
|
|
92
|
-
if j.left_node.resolve().
|
|
93
|
-
and j.right_node.resolve().
|
|
92
|
+
if j.left_node.resolve().identifier not in removed
|
|
93
|
+
and j.right_node.resolve().identifier not in removed
|
|
94
94
|
]
|
|
95
95
|
return joins, merged
|
|
96
96
|
|
|
@@ -155,8 +155,8 @@ class MergeNode(StrategyNode):
|
|
|
155
155
|
for join in node_joins:
|
|
156
156
|
left = join.left_node.resolve()
|
|
157
157
|
right = join.right_node.resolve()
|
|
158
|
-
if left.
|
|
159
|
-
raise SyntaxError(f"Cannot join node {left.
|
|
158
|
+
if left.identifier == right.identifier:
|
|
159
|
+
raise SyntaxError(f"Cannot join node {left.identifier} to itself")
|
|
160
160
|
joins.append(
|
|
161
161
|
BaseJoin(
|
|
162
162
|
left_datasource=left,
|
|
@@ -168,7 +168,7 @@ class MergeNode(StrategyNode):
|
|
|
168
168
|
)
|
|
169
169
|
return joins
|
|
170
170
|
|
|
171
|
-
def create_full_joins(self, dataset_list: List[QueryDatasource]):
|
|
171
|
+
def create_full_joins(self, dataset_list: List[QueryDatasource | Datasource]):
|
|
172
172
|
joins = []
|
|
173
173
|
seen = set()
|
|
174
174
|
for left_value in dataset_list:
|
|
@@ -198,7 +198,7 @@ class MergeNode(StrategyNode):
|
|
|
198
198
|
environment: Environment,
|
|
199
199
|
) -> List[BaseJoin | UnnestJoin]:
|
|
200
200
|
# only finally, join between them for unique values
|
|
201
|
-
dataset_list: List[QueryDatasource] = sorted(
|
|
201
|
+
dataset_list: List[QueryDatasource | Datasource] = sorted(
|
|
202
202
|
final_datasets, key=lambda x: -len(x.grain.components_copy)
|
|
203
203
|
)
|
|
204
204
|
|
|
@@ -238,13 +238,13 @@ class MergeNode(StrategyNode):
|
|
|
238
238
|
merged: dict[str, QueryDatasource | Datasource] = {}
|
|
239
239
|
final_joins: List[NodeJoin] | None = self.node_joins
|
|
240
240
|
for source in parent_sources:
|
|
241
|
-
if source.
|
|
241
|
+
if source.identifier in merged:
|
|
242
242
|
logger.info(
|
|
243
|
-
f"{self.logging_prefix}{LOGGER_PREFIX} merging parent node with {source.
|
|
243
|
+
f"{self.logging_prefix}{LOGGER_PREFIX} merging parent node with {source.identifier} into existing"
|
|
244
244
|
)
|
|
245
|
-
merged[source.
|
|
245
|
+
merged[source.identifier] = merged[source.identifier] + source
|
|
246
246
|
else:
|
|
247
|
-
merged[source.
|
|
247
|
+
merged[source.identifier] = source
|
|
248
248
|
|
|
249
249
|
# it's possible that we have more sources than we need
|
|
250
250
|
final_joins, merged = deduplicate_nodes_and_joins(
|
|
@@ -49,6 +49,7 @@ class SelectNode(StrategyNode):
|
|
|
49
49
|
conditions: Conditional | Comparison | Parenthetical | None = None,
|
|
50
50
|
preexisting_conditions: Conditional | Comparison | Parenthetical | None = None,
|
|
51
51
|
hidden_concepts: List[Concept] | None = None,
|
|
52
|
+
render_condition: bool = True,
|
|
52
53
|
):
|
|
53
54
|
super().__init__(
|
|
54
55
|
input_concepts=input_concepts,
|
|
@@ -65,6 +66,7 @@ class SelectNode(StrategyNode):
|
|
|
65
66
|
conditions=conditions,
|
|
66
67
|
preexisting_conditions=preexisting_conditions,
|
|
67
68
|
hidden_concepts=hidden_concepts,
|
|
69
|
+
render_condition=render_condition,
|
|
68
70
|
)
|
|
69
71
|
self.accept_partial = accept_partial
|
|
70
72
|
self.datasource = datasource
|
|
@@ -120,7 +122,8 @@ class SelectNode(StrategyNode):
|
|
|
120
122
|
],
|
|
121
123
|
nullable_concepts=[c.concept for c in datasource.columns if c.is_nullable],
|
|
122
124
|
source_type=SourceType.DIRECT_SELECT,
|
|
123
|
-
|
|
125
|
+
# we can skip rendering conditions
|
|
126
|
+
condition=self.conditions if self.render_condition else None,
|
|
124
127
|
# select nodes should never group
|
|
125
128
|
force_group=self.force_group,
|
|
126
129
|
hidden_concepts=self.hidden_concepts,
|
|
@@ -128,7 +131,7 @@ class SelectNode(StrategyNode):
|
|
|
128
131
|
|
|
129
132
|
def resolve_from_constant_datasources(self) -> QueryDatasource:
|
|
130
133
|
datasource = Datasource(
|
|
131
|
-
|
|
134
|
+
name=CONSTANT_DATASET, address=CONSTANT_DATASET, columns=[]
|
|
132
135
|
)
|
|
133
136
|
return QueryDatasource(
|
|
134
137
|
input_concepts=[],
|
|
@@ -205,6 +208,7 @@ class SelectNode(StrategyNode):
|
|
|
205
208
|
conditions=self.conditions,
|
|
206
209
|
preexisting_conditions=self.preexisting_conditions,
|
|
207
210
|
hidden_concepts=self.hidden_concepts,
|
|
211
|
+
render_condition=self.render_condition,
|
|
208
212
|
)
|
|
209
213
|
|
|
210
214
|
|
|
@@ -296,7 +296,7 @@ def add_node_join_concept(
|
|
|
296
296
|
|
|
297
297
|
|
|
298
298
|
def resolve_instantiated_concept(
|
|
299
|
-
concept: Concept, datasource: QueryDatasource
|
|
299
|
+
concept: Concept, datasource: QueryDatasource | Datasource
|
|
300
300
|
) -> Concept:
|
|
301
301
|
if concept.address in datasource.output_concepts:
|
|
302
302
|
return concept
|
|
@@ -309,14 +309,14 @@ def resolve_instantiated_concept(
|
|
|
309
309
|
|
|
310
310
|
|
|
311
311
|
def get_node_joins(
|
|
312
|
-
datasources: List[QueryDatasource],
|
|
312
|
+
datasources: List[QueryDatasource | Datasource],
|
|
313
313
|
environment: Environment,
|
|
314
314
|
# concepts:List[Concept],
|
|
315
315
|
):
|
|
316
316
|
|
|
317
317
|
graph = nx.Graph()
|
|
318
318
|
partials: dict[str, list[str]] = {}
|
|
319
|
-
ds_node_map: dict[str, QueryDatasource] = {}
|
|
319
|
+
ds_node_map: dict[str, QueryDatasource | Datasource] = {}
|
|
320
320
|
concept_map: dict[str, Concept] = {}
|
|
321
321
|
for datasource in datasources:
|
|
322
322
|
ds_node = f"ds~{datasource.identifier}"
|
trilogy/core/query_processor.py
CHANGED
|
@@ -5,7 +5,7 @@ from trilogy.core.graph_models import ReferenceGraph
|
|
|
5
5
|
from trilogy.core.constants import CONSTANT_DATASET
|
|
6
6
|
from trilogy.core.processing.concept_strategies_v3 import source_query_concepts
|
|
7
7
|
from trilogy.core.enums import BooleanOperator
|
|
8
|
-
from trilogy.constants import CONFIG
|
|
8
|
+
from trilogy.constants import CONFIG
|
|
9
9
|
from trilogy.core.processing.nodes import SelectNode, StrategyNode, History
|
|
10
10
|
from trilogy.core.models import (
|
|
11
11
|
Concept,
|
|
@@ -55,12 +55,12 @@ def base_join_to_join(
|
|
|
55
55
|
|
|
56
56
|
def get_datasource_cte(datasource: Datasource | QueryDatasource) -> CTE:
|
|
57
57
|
for cte in ctes:
|
|
58
|
-
if cte.source.
|
|
58
|
+
if cte.source.identifier == datasource.identifier:
|
|
59
59
|
return cte
|
|
60
60
|
for cte in ctes:
|
|
61
|
-
if cte.source.datasources[0].
|
|
61
|
+
if cte.source.datasources[0].identifier == datasource.identifier:
|
|
62
62
|
return cte
|
|
63
|
-
raise ValueError(f"Could not find CTE for datasource {datasource.
|
|
63
|
+
raise ValueError(f"Could not find CTE for datasource {datasource.identifier}")
|
|
64
64
|
|
|
65
65
|
if base_join.left_datasource is not None:
|
|
66
66
|
left_cte = get_datasource_cte(base_join.left_datasource)
|
|
@@ -109,7 +109,7 @@ def generate_source_map(
|
|
|
109
109
|
# now populate anything derived in this level
|
|
110
110
|
for qdk, qdv in query_datasource.source_map.items():
|
|
111
111
|
unnest = [x for x in qdv if isinstance(x, UnnestJoin)]
|
|
112
|
-
for
|
|
112
|
+
for _ in unnest:
|
|
113
113
|
source_map[qdk] = []
|
|
114
114
|
if (
|
|
115
115
|
qdk not in source_map
|
|
@@ -119,16 +119,18 @@ def generate_source_map(
|
|
|
119
119
|
source_map[qdk] = []
|
|
120
120
|
basic = [x for x in qdv if isinstance(x, Datasource)]
|
|
121
121
|
for base in basic:
|
|
122
|
-
source_map[qdk].append(base.
|
|
122
|
+
source_map[qdk].append(base.safe_identifier)
|
|
123
123
|
|
|
124
124
|
ctes = [x for x in qdv if isinstance(x, QueryDatasource)]
|
|
125
125
|
if ctes:
|
|
126
|
-
names = set([x.
|
|
127
|
-
matches = [
|
|
126
|
+
names = set([x.safe_identifier for x in ctes])
|
|
127
|
+
matches = [
|
|
128
|
+
cte for cte in all_new_ctes if cte.source.safe_identifier in names
|
|
129
|
+
]
|
|
128
130
|
|
|
129
131
|
if not matches and names:
|
|
130
132
|
raise SyntaxError(
|
|
131
|
-
f"Missing parent CTEs for source map; expecting {names}, have {[cte.source.
|
|
133
|
+
f"Missing parent CTEs for source map; expecting {names}, have {[cte.source.safe_identifier for cte in all_new_ctes]}"
|
|
132
134
|
)
|
|
133
135
|
for cte in matches:
|
|
134
136
|
output_address = [
|
|
@@ -137,11 +139,11 @@ def generate_source_map(
|
|
|
137
139
|
if x.address not in [z.address for z in cte.partial_concepts]
|
|
138
140
|
]
|
|
139
141
|
if qdk in output_address:
|
|
140
|
-
source_map[qdk].append(cte.
|
|
142
|
+
source_map[qdk].append(cte.safe_identifier)
|
|
141
143
|
# now do a pass that accepts partials
|
|
142
144
|
for cte in matches:
|
|
143
145
|
if qdk not in source_map:
|
|
144
|
-
source_map[qdk] = [cte.
|
|
146
|
+
source_map[qdk] = [cte.safe_identifier]
|
|
145
147
|
if qdk not in source_map:
|
|
146
148
|
if not qdv:
|
|
147
149
|
source_map[qdk] = []
|
|
@@ -154,8 +156,10 @@ def generate_source_map(
|
|
|
154
156
|
# as they cannot be referenced in row resolution
|
|
155
157
|
existence_source_map: Dict[str, list[str]] = defaultdict(list)
|
|
156
158
|
for ek, ev in query_datasource.existence_source_map.items():
|
|
157
|
-
|
|
158
|
-
ematches = [
|
|
159
|
+
ids = set([x.safe_identifier for x in ev])
|
|
160
|
+
ematches = [
|
|
161
|
+
cte.name for cte in all_new_ctes if cte.source.safe_identifier in ids
|
|
162
|
+
]
|
|
159
163
|
existence_source_map[ek] = ematches
|
|
160
164
|
return {
|
|
161
165
|
k: [] if not v else list(set(v)) for k, v in source_map.items()
|
|
@@ -209,7 +213,7 @@ def resolve_cte_base_name_and_alias_v2(
|
|
|
209
213
|
and not source.datasources[0].name == CONSTANT_DATASET
|
|
210
214
|
):
|
|
211
215
|
ds = source.datasources[0]
|
|
212
|
-
return ds.safe_location, ds.
|
|
216
|
+
return ds.safe_location, ds.safe_identifier
|
|
213
217
|
|
|
214
218
|
joins: List[Join] = [join for join in raw_joins if isinstance(join, Join)]
|
|
215
219
|
if joins and len(joins) > 0:
|
|
@@ -268,17 +272,17 @@ def datasource_to_ctes(
|
|
|
268
272
|
# this is required to ensure that constant datasets
|
|
269
273
|
# render properly on initial access; since they have
|
|
270
274
|
# no actual source
|
|
271
|
-
if source.
|
|
275
|
+
if source.name == CONSTANT_DATASET:
|
|
272
276
|
source_map = {k: [] for k in query_datasource.source_map}
|
|
273
277
|
existence_map = source_map
|
|
274
278
|
else:
|
|
275
279
|
source_map = {
|
|
276
|
-
k: [] if not v else [source.
|
|
280
|
+
k: [] if not v else [source.safe_identifier]
|
|
277
281
|
for k, v in query_datasource.source_map.items()
|
|
278
282
|
}
|
|
279
283
|
existence_map = source_map
|
|
280
284
|
|
|
281
|
-
human_id = generate_cte_name(query_datasource.
|
|
285
|
+
human_id = generate_cte_name(query_datasource.identifier, name_map)
|
|
282
286
|
|
|
283
287
|
final_joins = [base_join_to_join(join, parents) for join in query_datasource.joins]
|
|
284
288
|
|
trilogy/hooks/query_debugger.py
CHANGED
trilogy/parsing/parse_engine.py
CHANGED
|
@@ -123,6 +123,13 @@ from trilogy.parsing.common import (
|
|
|
123
123
|
arbitrary_to_concept,
|
|
124
124
|
process_function_args,
|
|
125
125
|
)
|
|
126
|
+
from dataclasses import dataclass
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
@dataclass
|
|
130
|
+
class WholeGrainWrapper:
|
|
131
|
+
where: WhereClause
|
|
132
|
+
|
|
126
133
|
|
|
127
134
|
CONSTANT_TYPES = (int, float, str, bool, list, ListWrapper, MapWrapper)
|
|
128
135
|
|
|
@@ -566,9 +573,11 @@ class ParseToObjects(Transformer):
|
|
|
566
573
|
return args
|
|
567
574
|
|
|
568
575
|
def grain_clause(self, args) -> Grain:
|
|
569
|
-
# namespace=self.environment.namespace,
|
|
570
576
|
return Grain(components=[self.environment.concepts[a] for a in args[0]])
|
|
571
577
|
|
|
578
|
+
def whole_grain_clause(self, args) -> WholeGrainWrapper:
|
|
579
|
+
return WholeGrainWrapper(where=args[0])
|
|
580
|
+
|
|
572
581
|
def MULTILINE_STRING(self, args) -> str:
|
|
573
582
|
return args[3:-3]
|
|
574
583
|
|
|
@@ -582,11 +591,14 @@ class ParseToObjects(Transformer):
|
|
|
582
591
|
grain: Optional[Grain] = None
|
|
583
592
|
address: Optional[Address] = None
|
|
584
593
|
where: Optional[WhereClause] = None
|
|
594
|
+
non_partial_for: Optional[WhereClause] = None
|
|
585
595
|
for val in args[1:]:
|
|
586
596
|
if isinstance(val, Address):
|
|
587
597
|
address = val
|
|
588
598
|
elif isinstance(val, Grain):
|
|
589
599
|
grain = val
|
|
600
|
+
elif isinstance(val, WholeGrainWrapper):
|
|
601
|
+
non_partial_for = val.where
|
|
590
602
|
elif isinstance(val, Query):
|
|
591
603
|
address = Address(location=f"({val.text})", is_query=True)
|
|
592
604
|
elif isinstance(val, WhereClause):
|
|
@@ -596,7 +608,7 @@ class ParseToObjects(Transformer):
|
|
|
596
608
|
"Malformed datasource, missing address or query declaration"
|
|
597
609
|
)
|
|
598
610
|
datasource = Datasource(
|
|
599
|
-
|
|
611
|
+
name=name,
|
|
600
612
|
columns=columns,
|
|
601
613
|
# grain will be set by default from args
|
|
602
614
|
# TODO: move to factory
|
|
@@ -604,6 +616,7 @@ class ParseToObjects(Transformer):
|
|
|
604
616
|
address=address,
|
|
605
617
|
namespace=self.environment.namespace,
|
|
606
618
|
where=where,
|
|
619
|
+
non_partial_for=non_partial_for,
|
|
607
620
|
)
|
|
608
621
|
for column in columns:
|
|
609
622
|
column.concept = column.concept.with_grain(datasource.grain)
|
|
@@ -801,20 +814,10 @@ class ParseToObjects(Transformer):
|
|
|
801
814
|
except Exception as e:
|
|
802
815
|
raise ImportError(f"Unable to import file {target}, parsing error: {e}")
|
|
803
816
|
|
|
804
|
-
for _, concept in nparser.environment.concepts.items():
|
|
805
|
-
self.environment.add_concept(
|
|
806
|
-
concept.with_namespace(alias), _ignore_cache=True
|
|
807
|
-
)
|
|
808
|
-
|
|
809
|
-
for _, datasource in nparser.environment.datasources.items():
|
|
810
|
-
self.environment.add_datasource(
|
|
811
|
-
datasource.with_namespace(alias), _ignore_cache=True
|
|
812
|
-
)
|
|
813
817
|
imps = ImportStatement(
|
|
814
818
|
alias=alias, path=Path(args[0]), environment=nparser.environment
|
|
815
819
|
)
|
|
816
|
-
self.environment.
|
|
817
|
-
self.environment.gen_concept_list_caches()
|
|
820
|
+
self.environment.add_import(alias, nparser.environment, imps)
|
|
818
821
|
return imps
|
|
819
822
|
|
|
820
823
|
@v_args(meta=True)
|
|
@@ -841,7 +844,7 @@ class ParseToObjects(Transformer):
|
|
|
841
844
|
if self.environment.namespace
|
|
842
845
|
else DEFAULT_NAMESPACE
|
|
843
846
|
),
|
|
844
|
-
|
|
847
|
+
name=identifier,
|
|
845
848
|
address=Address(location=address),
|
|
846
849
|
grain=grain,
|
|
847
850
|
)
|
trilogy/parsing/render.py
CHANGED
|
@@ -80,6 +80,9 @@ class Renderer:
|
|
|
80
80
|
metrics = []
|
|
81
81
|
# first, keys
|
|
82
82
|
for concept in arg.concepts.values():
|
|
83
|
+
if "__preql_internal" in concept.address:
|
|
84
|
+
continue
|
|
85
|
+
|
|
83
86
|
# don't render anything that came from an import
|
|
84
87
|
if concept.namespace in arg.imports:
|
|
85
88
|
continue
|
|
@@ -122,10 +125,10 @@ class Renderer:
|
|
|
122
125
|
for datasource in arg.datasources.values()
|
|
123
126
|
if datasource.namespace == DEFAULT_NAMESPACE
|
|
124
127
|
]
|
|
125
|
-
rendered_imports = [
|
|
126
|
-
|
|
127
|
-
for import_statement in
|
|
128
|
-
|
|
128
|
+
rendered_imports = []
|
|
129
|
+
for _, imports in arg.imports.items():
|
|
130
|
+
for import_statement in imports:
|
|
131
|
+
rendered_imports.append(self.to_string(import_statement))
|
|
129
132
|
components = []
|
|
130
133
|
if rendered_imports:
|
|
131
134
|
components.append(rendered_imports)
|
|
@@ -133,19 +136,26 @@ class Renderer:
|
|
|
133
136
|
components.append(rendered_concepts)
|
|
134
137
|
if rendered_datasources:
|
|
135
138
|
components.append(rendered_datasources)
|
|
139
|
+
|
|
136
140
|
final = "\n\n".join("\n".join(x) for x in components)
|
|
137
141
|
return final
|
|
138
142
|
|
|
139
143
|
@to_string.register
|
|
140
144
|
def _(self, arg: Datasource):
|
|
141
145
|
assignments = ",\n ".join([self.to_string(x) for x in arg.columns])
|
|
146
|
+
if arg.non_partial_for:
|
|
147
|
+
non_partial = f"\ncomplete where {self.to_string(arg.non_partial_for)}"
|
|
148
|
+
else:
|
|
149
|
+
non_partial = ""
|
|
142
150
|
base = f"""datasource {arg.name} (
|
|
143
151
|
{assignments}
|
|
144
152
|
)
|
|
145
|
-
{self.to_string(arg.grain)}
|
|
153
|
+
{self.to_string(arg.grain)}{non_partial}
|
|
146
154
|
{self.to_string(arg.address)}"""
|
|
155
|
+
|
|
147
156
|
if arg.where:
|
|
148
157
|
base += f"\nwhere {self.to_string(arg.where)}"
|
|
158
|
+
|
|
149
159
|
base += ";"
|
|
150
160
|
return base
|
|
151
161
|
|
|
@@ -214,9 +224,15 @@ class Renderer:
|
|
|
214
224
|
|
|
215
225
|
@to_string.register
|
|
216
226
|
def _(self, arg: "ColumnAssignment"):
|
|
227
|
+
if arg.modifiers:
|
|
228
|
+
modifiers = "".join(
|
|
229
|
+
[self.to_string(modifier) for modifier in arg.modifiers]
|
|
230
|
+
)
|
|
231
|
+
else:
|
|
232
|
+
modifiers = ""
|
|
217
233
|
if isinstance(arg.alias, str):
|
|
218
|
-
return f"{arg.alias}: {self.to_string(arg.concept)}"
|
|
219
|
-
return f"{self.to_string(arg.alias)}: {self.to_string(arg.concept)}"
|
|
234
|
+
return f"{arg.alias}: {modifiers}{self.to_string(arg.concept)}"
|
|
235
|
+
return f"{self.to_string(arg.alias)}: {modifiers}{self.to_string(arg.concept)}"
|
|
220
236
|
|
|
221
237
|
@to_string.register
|
|
222
238
|
def _(self, arg: "RawColumnExpr"):
|
|
@@ -352,6 +368,8 @@ class Renderer:
|
|
|
352
368
|
|
|
353
369
|
@to_string.register
|
|
354
370
|
def _(self, arg: "ImportStatement"):
|
|
371
|
+
if arg.alias == DEFAULT_NAMESPACE:
|
|
372
|
+
return f"import {arg.path};"
|
|
355
373
|
return f"import {arg.path} as {arg.alias};"
|
|
356
374
|
|
|
357
375
|
@to_string.register
|
trilogy/parsing/trilogy.lark
CHANGED
|
@@ -35,8 +35,10 @@
|
|
|
35
35
|
prop_ident: "<" IDENTIFIER ("," IDENTIFIER )* ","? ">" "." IDENTIFIER
|
|
36
36
|
|
|
37
37
|
// datasource concepts
|
|
38
|
-
datasource: "datasource" IDENTIFIER "(" column_assignment_list ")" grain_clause? (address | query) where?
|
|
39
|
-
|
|
38
|
+
datasource: "datasource" IDENTIFIER "(" column_assignment_list ")" grain_clause? whole_grain_clause? (address | query) where?
|
|
39
|
+
|
|
40
|
+
whole_grain_clause: "complete" where
|
|
41
|
+
|
|
40
42
|
grain_clause: "grain" "(" column_list ")"
|
|
41
43
|
|
|
42
44
|
address: "address" (QUOTED_ADDRESS | ADDRESS)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|