metricflow-sql 0.5.6__tar.gz → 0.5.7__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.
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/PKG-INFO +2 -2
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/pyproject.toml +2 -2
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/compiler.py +28 -2
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/rules/expand_datasets.py +67 -13
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/rules/expand_fill.py +53 -31
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/rules/expand_per.py +12 -7
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/rules/isolate_fragments.py +6 -13
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/rules/nest_grouped_subqueries.py +6 -30
- metricflow_sql-0.5.7/src/metricflow_sql/compiler/rules/nest_joined_sources.py +189 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/rules/nest_windows.py +4 -14
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/rules/passthrough_tables.py +1 -1
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/registry/type_resolver.py +107 -57
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/schemas/bundle.py +13 -3
- metricflow_sql-0.5.7/src/metricflow_sql/schemas/expand.py +259 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/schemas/fragment.schema.json +18 -4
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/schemas/load.py +102 -33
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/schemas/v1/fragment.py +36 -8
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/utils/sql.py +78 -2
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/.gitignore +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/LICENSE +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/PYPI.md +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/__init__.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/__init__.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/catalog/__init__.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/catalog/cluster.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/catalog/information_schema.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/catalog/metadata.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/catalog/passthrough.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/catalog/protocols.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/catalog/semantic.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/guard.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/rules/__init__.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/rules/constellation_permissions.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/rules/expand_constellations.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/rules/expand_metadata.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/table_arguments.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/converters/__init__.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/converters/legacy.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/converters/lookml.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/objects/__init__.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/objects/base.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/objects/constellation.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/objects/dataset.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/objects/entity.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/objects/reference.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/objects/wiki.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/py.typed +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/registry/__init__.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/registry/dao.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/registry/dependency_resolution.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/registry/memory.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/registry/operation.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/registry/reference_resolution.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/registry/registry.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/registry/resolved.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/schemas/__init__.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/schemas/generate.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/schemas/markdown.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/schemas/v1/__init__.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/server/__init__.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/server/app.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/server/compile.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/server/errors.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/server/http.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/server/registry.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/server/serde.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/server/serve.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/utils/__init__.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/utils/composed_set.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/utils/normalize.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/utils/ordered_set.py +0 -0
- {metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/utils/template.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: metricflow-sql
|
|
3
|
-
Version: 0.5.
|
|
3
|
+
Version: 0.5.7
|
|
4
4
|
Summary: MetricFlow's core semantic layer engine
|
|
5
5
|
Project-URL: Documentation, https://docs-internal-git-mfsql-dbt-labs.vercel.app/metricflow
|
|
6
6
|
License-Expression: LicenseRef-Fivetran
|
|
@@ -12,7 +12,7 @@ Classifier: Programming Language :: Python :: 3.14
|
|
|
12
12
|
Requires-Python: >=3.12
|
|
13
13
|
Requires-Dist: jinja2>=3.1
|
|
14
14
|
Requires-Dist: msgspec>=0.19
|
|
15
|
-
Requires-Dist: sqlglot[c]>=30.
|
|
15
|
+
Requires-Dist: sqlglot[c]>=30.34.0
|
|
16
16
|
Requires-Dist: wenmode>=0.15.2
|
|
17
17
|
Provides-Extra: lookml
|
|
18
18
|
Requires-Dist: lkml>=1.3.7; extra == 'lookml'
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "metricflow-sql"
|
|
3
3
|
# A placeholder: a release sets its tag's version, with scripts/set_version.py.
|
|
4
|
-
version = "0.5.
|
|
4
|
+
version = "0.5.7"
|
|
5
5
|
description = "MetricFlow's core semantic layer engine"
|
|
6
6
|
# Its PyPI page: README.md is for this repository.
|
|
7
7
|
readme = "PYPI.md"
|
|
@@ -17,7 +17,7 @@ classifiers = [
|
|
|
17
17
|
dependencies = [
|
|
18
18
|
"jinja2>=3.1",
|
|
19
19
|
"msgspec>=0.19",
|
|
20
|
-
"sqlglot[c]>=30.
|
|
20
|
+
"sqlglot[c]>=30.34.0",
|
|
21
21
|
"wenmode>=0.15.2",
|
|
22
22
|
]
|
|
23
23
|
|
|
@@ -35,6 +35,7 @@ from metricflow_sql.compiler.rules.expand_metadata import ENGINE, ExpandMetadata
|
|
|
35
35
|
from metricflow_sql.compiler.rules.expand_per import expand_per
|
|
36
36
|
from metricflow_sql.compiler.rules.isolate_fragments import isolate_fragments
|
|
37
37
|
from metricflow_sql.compiler.rules.nest_grouped_subqueries import nest_grouped_subqueries
|
|
38
|
+
from metricflow_sql.compiler.rules.nest_joined_sources import nest_joined_sources
|
|
38
39
|
from metricflow_sql.compiler.rules.nest_windows import nest_windows
|
|
39
40
|
from metricflow_sql.compiler.rules.passthrough_tables import name_passthrough_tables
|
|
40
41
|
from metricflow_sql.compiler.table_arguments import bind_table_arguments
|
|
@@ -92,6 +93,7 @@ ANALYZER_RULES: Sequence[OptimizerFn] = (
|
|
|
92
93
|
)
|
|
93
94
|
|
|
94
95
|
PLANNER_RULES: Sequence[OptimizerFn] = (
|
|
96
|
+
nest_joined_sources,
|
|
95
97
|
apply_constellation_permissions,
|
|
96
98
|
isolate_fragments,
|
|
97
99
|
name_passthrough_tables,
|
|
@@ -145,6 +147,7 @@ class Compiler:
|
|
|
145
147
|
db: str | None = None,
|
|
146
148
|
guard: Guard | None = None,
|
|
147
149
|
warehouse: WarehouseDefaults | None = None,
|
|
150
|
+
system_functions: Mapping[type[exp.Func], exp.Expr] | None = None,
|
|
148
151
|
) -> Plan:
|
|
149
152
|
"""Compiles `sql`.
|
|
150
153
|
|
|
@@ -166,6 +169,9 @@ class Compiler:
|
|
|
166
169
|
|
|
167
170
|
With `with_tables`, `Plan.tables` holds the physical tables the compiled SQL reads, e.g.
|
|
168
171
|
for dbt to order a model after the models that build them.
|
|
172
|
+
|
|
173
|
+
`system_functions` replaces each call whose exact sqlglot type is a key with a copy of its
|
|
174
|
+
value. The query's dialect sets the type, e.g. Trino reads `version()` as `CurrentVersion`.
|
|
169
175
|
"""
|
|
170
176
|
input_dialect = self.input_dialect if input_dialect is None else input_dialect
|
|
171
177
|
output_dialect = self.output_dialect if output_dialect is None else output_dialect
|
|
@@ -208,7 +214,13 @@ class Compiler:
|
|
|
208
214
|
for expression in expressions:
|
|
209
215
|
if not expression:
|
|
210
216
|
continue
|
|
211
|
-
self._prepare(
|
|
217
|
+
self._prepare(
|
|
218
|
+
expression,
|
|
219
|
+
input_dialect,
|
|
220
|
+
default_db,
|
|
221
|
+
read_only=read_only,
|
|
222
|
+
system_functions=system_functions or {},
|
|
223
|
+
)
|
|
212
224
|
if guard is not None:
|
|
213
225
|
guard.check_query(expression)
|
|
214
226
|
planned = self._apply_rules(expression, input_dialect, default_db, guard, warehouse)
|
|
@@ -293,14 +305,28 @@ class Compiler:
|
|
|
293
305
|
return self.engine_dialects[engine]
|
|
294
306
|
|
|
295
307
|
def _prepare(
|
|
296
|
-
self,
|
|
308
|
+
self,
|
|
309
|
+
expression: exp.Expr,
|
|
310
|
+
dialect: DialectType,
|
|
311
|
+
db: str | None,
|
|
312
|
+
*,
|
|
313
|
+
read_only: bool,
|
|
314
|
+
system_functions: Mapping[type[exp.Func], exp.Expr],
|
|
297
315
|
) -> None:
|
|
316
|
+
replaced: list[exp.Func] = []
|
|
298
317
|
for node in expression.walk():
|
|
299
318
|
match node:
|
|
300
319
|
case exp.Table(this=exp.Anonymous()) if self._is_dataset(node, dialect, db):
|
|
301
320
|
bind_table_arguments(node)
|
|
302
321
|
case exp.Into() if read_only:
|
|
303
322
|
raise ValueError("Only one query statement is supported")
|
|
323
|
+
case exp.Func() if type(node) in system_functions:
|
|
324
|
+
replaced.append(node)
|
|
325
|
+
# After the walk, not during it: a call's arguments are still to be walked.
|
|
326
|
+
for node in replaced:
|
|
327
|
+
value = system_functions[type(node)].copy()
|
|
328
|
+
value.add_comments(node.comments)
|
|
329
|
+
node.replace(value)
|
|
304
330
|
|
|
305
331
|
def _is_dataset(self, table: exp.Table, dialect: DialectType, db: str | None) -> bool:
|
|
306
332
|
"""Nothing is loaded yet, so a constellation counts too."""
|
{metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/rules/expand_datasets.py
RENAMED
|
@@ -45,6 +45,7 @@ from metricflow_sql.registry.registry import (
|
|
|
45
45
|
)
|
|
46
46
|
from metricflow_sql.utils.normalize import resolve_name
|
|
47
47
|
from metricflow_sql.utils.sql import (
|
|
48
|
+
FIELD_NAME,
|
|
48
49
|
FINAL,
|
|
49
50
|
WAREHOUSE_TABLE,
|
|
50
51
|
conjuncts,
|
|
@@ -486,11 +487,15 @@ class ExpandDatasetsRule:
|
|
|
486
487
|
],
|
|
487
488
|
)
|
|
488
489
|
|
|
490
|
+
# Unless isolate_fragments moved it out, e.g. for an ORDER BY, the WITH is this select's.
|
|
491
|
+
with_ = select.args.get("with_")
|
|
489
492
|
select, havings = self._grouped_select(
|
|
490
493
|
projections, requested_groups, where, user_joins, ctx
|
|
491
494
|
)
|
|
492
495
|
if order is not None:
|
|
493
496
|
select.set("order", order)
|
|
497
|
+
if with_ is not None:
|
|
498
|
+
select.set("with_", with_)
|
|
494
499
|
# The query's PERs, calculations' included, come first: the fan-out dedup is a PER on the
|
|
495
500
|
# scan's aggregates.
|
|
496
501
|
grouped, scan = expand_select(select, ctx.dialect)
|
|
@@ -1190,7 +1195,7 @@ class ExpandDatasetsRule:
|
|
|
1190
1195
|
match node.this:
|
|
1191
1196
|
case exp.Dot() as path:
|
|
1192
1197
|
path_ctx = self._context_for_alias(ctx, path.parts[0].name)
|
|
1193
|
-
aggfunc = self._expand_relationship_path(path, path_ctx)
|
|
1198
|
+
aggfunc = self._expand_relationship_path(path, path_ctx, agg=True)
|
|
1194
1199
|
case exp.Column() as column:
|
|
1195
1200
|
column_ctx = self._context_for_alias(ctx, column.table)
|
|
1196
1201
|
aggfunc = self._expand_col(column.this, column_ctx, agg=True)
|
|
@@ -1240,7 +1245,7 @@ class ExpandDatasetsRule:
|
|
|
1240
1245
|
and isinstance(node, exp.Dot | exp.Column)
|
|
1241
1246
|
and isinstance(ctx.dataset.get_column(path_names(node)[0]), Relationship)
|
|
1242
1247
|
):
|
|
1243
|
-
return self._expand_path(list(path_names(node)), node, ctx, fill=fill)
|
|
1248
|
+
return self._expand_path(list(path_names(node)), node, ctx, fill=fill, agg=agg)
|
|
1244
1249
|
# Only a source's path is: not a field of a struct expression, e.g. `{'n': x}.n`, or of
|
|
1245
1250
|
# an UNNEST's element, e.g. `h.segment`, nor a qualified function, e.g. `udfs.f(x)`.
|
|
1246
1251
|
if (
|
|
@@ -1250,7 +1255,7 @@ class ExpandDatasetsRule:
|
|
|
1250
1255
|
and ((head := node.parts[0].name) == ctx.alias or head in ctx.datasets_by_alias)
|
|
1251
1256
|
):
|
|
1252
1257
|
path_ctx = self._context_for_alias(ctx, head)
|
|
1253
|
-
return self._expand_relationship_path(node, path_ctx, fill)
|
|
1258
|
+
return self._expand_relationship_path(node, path_ctx, fill, agg=agg)
|
|
1254
1259
|
if isinstance(node, exp.Column):
|
|
1255
1260
|
if not node.table or node.args["table"] == source_qualifier:
|
|
1256
1261
|
return self._expand_col(node.this, ctx, agg=agg, fill=fill)
|
|
@@ -1343,12 +1348,17 @@ class ExpandDatasetsRule:
|
|
|
1343
1348
|
return [expression]
|
|
1344
1349
|
|
|
1345
1350
|
def _expand_relationship_path(
|
|
1346
|
-
self,
|
|
1351
|
+
self,
|
|
1352
|
+
dot: exp.Dot,
|
|
1353
|
+
ctx: ExpandContext,
|
|
1354
|
+
fill: exp.Ordered | None = None,
|
|
1355
|
+
*,
|
|
1356
|
+
agg: bool = False,
|
|
1347
1357
|
) -> exp.Expr:
|
|
1348
1358
|
head, *tail = path_names(dot)
|
|
1349
1359
|
if head.name != ctx.alias:
|
|
1350
1360
|
self._fail(ctx, "Unsupported relationship path", dot)
|
|
1351
|
-
return self._expand_path(tail, dot, ctx, fill=fill)
|
|
1361
|
+
return self._expand_path(tail, dot, ctx, fill=fill, agg=agg)
|
|
1352
1362
|
|
|
1353
1363
|
def _expand_path(
|
|
1354
1364
|
self,
|
|
@@ -1357,6 +1367,8 @@ class ExpandDatasetsRule:
|
|
|
1357
1367
|
ctx: ExpandContext,
|
|
1358
1368
|
key: bool = False,
|
|
1359
1369
|
fill: exp.Ordered | None = None,
|
|
1370
|
+
*,
|
|
1371
|
+
agg: bool = False,
|
|
1360
1372
|
) -> exp.Expr:
|
|
1361
1373
|
# The walk alternates namespaces: a segment resolves against a dataset's
|
|
1362
1374
|
# fields, or while a relationship is pending, against that entity's
|
|
@@ -1433,11 +1445,17 @@ class ExpandDatasetsRule:
|
|
|
1433
1445
|
self._spine(fill, ctx, dataset, dataset_alias, exp.column(name))
|
|
1434
1446
|
return self._column_value(ctx, dataset, column, dataset_alias)
|
|
1435
1447
|
case Calculation() as calc:
|
|
1448
|
+
# As a field of the query's own dataset, see _expand_col.
|
|
1449
|
+
if not agg and projection_has_aggregate(calc.expr):
|
|
1450
|
+
return exp.null()
|
|
1436
1451
|
# Unless joined, the body is the key, e.g. `sold_date.year`.
|
|
1437
1452
|
if fill is not None and dataset_alias != ctx.alias:
|
|
1438
1453
|
self._spine(fill, ctx, dataset, dataset_alias, exp.column(name))
|
|
1439
1454
|
fill = None
|
|
1440
|
-
|
|
1455
|
+
value = self._calculation_value(
|
|
1456
|
+
ctx, dataset, calc, dataset_alias, fill=fill
|
|
1457
|
+
)
|
|
1458
|
+
return exp.null() if not agg and projection_has_aggregate(value) else value
|
|
1441
1459
|
case _:
|
|
1442
1460
|
self._fail(ctx, "Unsupported relationship path", expr)
|
|
1443
1461
|
continue
|
|
@@ -1472,7 +1490,7 @@ class ExpandDatasetsRule:
|
|
|
1472
1490
|
)
|
|
1473
1491
|
if permission is not None and permission.mask is not None:
|
|
1474
1492
|
unmasked_ctx = replace(ctx, unmasked=ctx.unmasked | {attribute.id})
|
|
1475
|
-
value = self._expand_path(parts, expr, unmasked_ctx, key)
|
|
1493
|
+
value = self._expand_path(parts, expr, unmasked_ctx, key, agg=agg)
|
|
1476
1494
|
return apply_mask(permission.mask, attribute.name, value)
|
|
1477
1495
|
attr_expr = attribute.expr
|
|
1478
1496
|
if isinstance(attr_expr, exp.Column | exp.Dot):
|
|
@@ -1496,21 +1514,22 @@ class ExpandDatasetsRule:
|
|
|
1496
1514
|
source_dataset = self._dataset_for(ctx, source.dataset)
|
|
1497
1515
|
if fill is not None:
|
|
1498
1516
|
self._spine(fill, ctx, source_dataset, join_alias, attr_expr, source.dataset.this)
|
|
1499
|
-
|
|
1517
|
+
value = self._expand_expr(
|
|
1500
1518
|
attr_expr,
|
|
1501
1519
|
replace(ctx, dataset=source_dataset, alias=join_alias),
|
|
1502
1520
|
source_qualifier=source.dataset.this,
|
|
1503
1521
|
agg=True,
|
|
1504
1522
|
)
|
|
1523
|
+
# Whole, as a calculation is: e.g. COALESCE would turn a NULL inner field into 0.
|
|
1524
|
+
return exp.null() if not agg and projection_has_aggregate(value) else value
|
|
1505
1525
|
self._fail(ctx, "Unsupported relationship path", expr)
|
|
1506
1526
|
|
|
1507
1527
|
def _column_value(
|
|
1508
1528
|
self, ctx: ExpandContext, dataset: DatasetProxy, column: Column, alias: str
|
|
1509
1529
|
) -> exp.Expr:
|
|
1510
1530
|
mask = self._mask(ctx, dataset, column, alias)
|
|
1511
|
-
if mask is
|
|
1512
|
-
|
|
1513
|
-
return self._physical_column(column.name, alias, column.datatype)
|
|
1531
|
+
value = self._physical_column(column.name, alias, column.datatype) if mask is None else mask
|
|
1532
|
+
return _named(value, column.name)
|
|
1514
1533
|
|
|
1515
1534
|
def _calculation_value(
|
|
1516
1535
|
self,
|
|
@@ -1522,11 +1541,12 @@ class ExpandDatasetsRule:
|
|
|
1522
1541
|
) -> exp.Expr:
|
|
1523
1542
|
mask = self._mask(ctx, dataset, calculation, alias)
|
|
1524
1543
|
if mask is not None:
|
|
1525
|
-
return mask
|
|
1544
|
+
return _named(mask, calculation.name)
|
|
1526
1545
|
source_ctx = replace(ctx, dataset=dataset, alias=alias)
|
|
1527
|
-
|
|
1546
|
+
value = self._expand_expr(
|
|
1528
1547
|
calculation.expr, source_ctx, calculation=True, agg=True, fill=fill
|
|
1529
1548
|
)
|
|
1549
|
+
return _named(value, calculation.name)
|
|
1530
1550
|
|
|
1531
1551
|
def _mask(
|
|
1532
1552
|
self, ctx: ExpandContext, dataset: DatasetProxy, field: Column | Calculation, alias: str
|
|
@@ -1623,6 +1643,15 @@ class ExpandDatasetsRule:
|
|
|
1623
1643
|
key = self._key_columns(ctx, dataset, alias)
|
|
1624
1644
|
if key is not None:
|
|
1625
1645
|
return key
|
|
1646
|
+
# An entity join's ON is the entity's, so the key's conditions aren't applied there.
|
|
1647
|
+
if (
|
|
1648
|
+
alias in ctx.fanout_parents
|
|
1649
|
+
and alias not in ctx.grain_sources
|
|
1650
|
+
and dataset.key is not None
|
|
1651
|
+
):
|
|
1652
|
+
grain = self._conditioned_grain(ctx, dataset, alias)
|
|
1653
|
+
if grain is not None:
|
|
1654
|
+
return grain
|
|
1626
1655
|
source = ctx.grain_sources.get(alias)
|
|
1627
1656
|
if source is not None:
|
|
1628
1657
|
return self._entity_keys_from_on(ctx, source, alias)
|
|
@@ -1635,6 +1664,25 @@ class ExpandDatasetsRule:
|
|
|
1635
1664
|
}
|
|
1636
1665
|
return next(iter(grains.values())) if len(grains) == 1 else None
|
|
1637
1666
|
|
|
1667
|
+
def _conditioned_grain(
|
|
1668
|
+
self, ctx: ExpandContext, dataset: DatasetProxy, alias: str
|
|
1669
|
+
) -> tuple[exp.Expr, ...] | None:
|
|
1670
|
+
"""Grain of a one-to-many join through a key with conditions."""
|
|
1671
|
+
key = dataset.key
|
|
1672
|
+
assert key is not None
|
|
1673
|
+
parameters = {name for condition in key.conditions() for name in condition.parameters}
|
|
1674
|
+
if not parameters <= ctx.reached_with.get(alias, {}).keys():
|
|
1675
|
+
return None
|
|
1676
|
+
# Each placeholder value matches one row per field values, so these columns identify it.
|
|
1677
|
+
columns: dict[exp.Identifier, None] = dict.fromkeys(key.fields())
|
|
1678
|
+
for condition in key.conditions():
|
|
1679
|
+
for column in condition.on.find_all(exp.Column):
|
|
1680
|
+
if column.table or isinstance(column.parent, exp.Dot):
|
|
1681
|
+
return None
|
|
1682
|
+
columns[column.this] = None
|
|
1683
|
+
alias_ctx = replace(ctx, dataset=dataset, alias=alias, physical=True)
|
|
1684
|
+
return tuple(self._expand_col(name, alias_ctx, agg=False) for name in columns)
|
|
1685
|
+
|
|
1638
1686
|
def _dataset_at(self, ctx: ExpandContext, alias: str) -> DatasetProxy:
|
|
1639
1687
|
"""The dataset the source `alias` reads: the root, a generated join, or a user join."""
|
|
1640
1688
|
if alias == ctx.root_alias:
|
|
@@ -2062,6 +2110,12 @@ class ExpandDatasetsRule:
|
|
|
2062
2110
|
)
|
|
2063
2111
|
|
|
2064
2112
|
|
|
2113
|
+
def _named(value: exp.Expr, name: exp.Identifier) -> exp.Expr:
|
|
2114
|
+
"""`value`, the expansion of the field `name`, which a rule can name it with, e.g. `net`."""
|
|
2115
|
+
value.meta[FIELD_NAME] = name.copy()
|
|
2116
|
+
return value
|
|
2117
|
+
|
|
2118
|
+
|
|
2065
2119
|
def _is_alias(node: exp.Expr, select: exp.Select) -> bool:
|
|
2066
2120
|
"""Whether node reads one of select's projections by its alias."""
|
|
2067
2121
|
return (
|
{metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/rules/expand_fill.py
RENAMED
|
@@ -11,6 +11,8 @@ from metricflow_sql.utils.sql import (
|
|
|
11
11
|
internal_alias,
|
|
12
12
|
is_window_function,
|
|
13
13
|
normalize_group,
|
|
14
|
+
pick_aliases_for_subquery,
|
|
15
|
+
reserve_name,
|
|
14
16
|
)
|
|
15
17
|
|
|
16
18
|
FILL_COL = "_fill_col"
|
|
@@ -116,29 +118,42 @@ def expand_fill(select: exp.Select, dialect: DialectType = None) -> exp.Select:
|
|
|
116
118
|
}
|
|
117
119
|
select.set("order", None)
|
|
118
120
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
dimension: internal_alias(used, f"_fill_dimension_{index}")
|
|
122
|
-
for index, dimension in enumerate(dimensions)
|
|
123
|
-
}
|
|
124
|
-
value_aliases: dict[exp.Expr, str] = {}
|
|
125
|
-
grouping_aliases: dict[exp.Expr, str] = {}
|
|
121
|
+
values: list[exp.Expr] = []
|
|
122
|
+
groupings: list[exp.Expr] = []
|
|
126
123
|
for projection in select.expressions:
|
|
127
124
|
for node in walk_in_scope(
|
|
128
125
|
projection, prune=lambda node: _is_unit(node) or isinstance(node, exp.Grouping)
|
|
129
126
|
):
|
|
130
|
-
if isinstance(node, exp.Grouping)
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
)
|
|
134
|
-
|
|
135
|
-
|
|
127
|
+
if isinstance(node, exp.Grouping):
|
|
128
|
+
groupings.append(node)
|
|
129
|
+
elif _is_unit(node):
|
|
130
|
+
values.append(node)
|
|
131
|
+
aliases = pick_aliases_for_subquery(
|
|
132
|
+
[*dimensions, *values],
|
|
133
|
+
select,
|
|
134
|
+
lambda node: "_fill_dimension" if node in dimensions else "_fill_value",
|
|
135
|
+
)
|
|
136
|
+
dimension_aliases = {node: aliases[node] for node in dimensions}
|
|
137
|
+
value_aliases = {node: aliases[node] for node in values}
|
|
138
|
+
# The internal names, of columns and tables, avoid every column's.
|
|
139
|
+
used = {alias.name.lower() for alias in aliases.values()}
|
|
140
|
+
used |= {column.alias_or_name.lower() for spine in spines for column in spine.selects}
|
|
141
|
+
|
|
142
|
+
def internal(name: str) -> exp.Identifier:
|
|
143
|
+
return exp.to_identifier(internal_alias(used, name))
|
|
144
|
+
|
|
145
|
+
grouping_aliases = {
|
|
146
|
+
node: internal(f"_fill_grouping_{index}")
|
|
147
|
+
for index, node in enumerate(dict.fromkeys(groupings))
|
|
148
|
+
}
|
|
136
149
|
# Tells grouping sets apart, as a NULL key doesn't.
|
|
137
|
-
set_alias =
|
|
138
|
-
unfilled_name = internal_alias(used, "_unfilled")
|
|
150
|
+
set_alias = internal("_fill_set") if len(groups) > 1 else None
|
|
139
151
|
# A row the HAVING drops is kept when the spine adds it, with the values it has.
|
|
140
152
|
having = select.args.get("having")
|
|
141
|
-
having_alias =
|
|
153
|
+
having_alias = internal("_fill_having") if having is not None else None
|
|
154
|
+
unfilled_name, keys_name, filled_name, parts_name = (
|
|
155
|
+
internal_alias(used, name) for name in ("_unfilled", "keys", "filled", "parts")
|
|
156
|
+
)
|
|
142
157
|
key_aliases = [dimension_aliases[key] for key in fill_keys]
|
|
143
158
|
others = [alias for alias in dimension_aliases.values() if alias not in key_aliases]
|
|
144
159
|
|
|
@@ -159,17 +174,20 @@ def expand_fill(select: exp.Select, dialect: DialectType = None) -> exp.Select:
|
|
|
159
174
|
group=group,
|
|
160
175
|
)
|
|
161
176
|
|
|
162
|
-
def bound(function: str, alias:
|
|
177
|
+
def bound(function: str, alias: exp.Identifier) -> exp.Expr:
|
|
163
178
|
return exp.select(exp.func(function, exp.column(alias))).from_(unfilled_name).subquery()
|
|
164
179
|
|
|
165
|
-
spine_names = [
|
|
180
|
+
spine_names = [
|
|
181
|
+
internal_alias(used, "spine" if len(spines) == 1 else f"spine_{i}")
|
|
182
|
+
for i in range(len(spines))
|
|
183
|
+
]
|
|
166
184
|
spine_keys = [
|
|
167
185
|
exp.column(spine.selects[0].alias_or_name, table=name)
|
|
168
186
|
for spine, name in zip(spines, spine_names, strict=True)
|
|
169
187
|
]
|
|
170
188
|
# So a filled day gets its own month, e.g. DATE_TRUNC('MONTH', ds), not NULL.
|
|
171
189
|
# Each is read from a key's spine: the index of the key, and the value.
|
|
172
|
-
derived: dict[
|
|
190
|
+
derived: dict[exp.Identifier, tuple[int, exp.Expr]] = {}
|
|
173
191
|
for dimension, alias in dimension_aliases.items():
|
|
174
192
|
if alias in key_aliases:
|
|
175
193
|
continue
|
|
@@ -193,8 +211,10 @@ def expand_fill(select: exp.Select, dialect: DialectType = None) -> exp.Select:
|
|
|
193
211
|
if alias in derived or alias in key_aliases or not columns:
|
|
194
212
|
continue
|
|
195
213
|
if all(column.table == source for column in columns):
|
|
196
|
-
spine
|
|
197
|
-
|
|
214
|
+
# Apart from the spine's own column, which can have the dimension's name.
|
|
215
|
+
added = reserve_name(alias, {c.alias_or_name.lower() for c in spine.selects})
|
|
216
|
+
spine.select(exp.alias_(dimension.copy(), added), copy=False)
|
|
217
|
+
derived[alias] = (index, exp.column(added, table=name))
|
|
198
218
|
partitioned = {dimension_aliases[dimension] for dimension in partition}
|
|
199
219
|
|
|
200
220
|
def grouping(nodes: list[exp.Expr], grouping_set: tuple[exp.Expr, ...]) -> exp.Expr:
|
|
@@ -215,13 +235,13 @@ def expand_fill(select: exp.Select, dialect: DialectType = None) -> exp.Select:
|
|
|
215
235
|
and (alias in partitioned or alias in derived)
|
|
216
236
|
]
|
|
217
237
|
|
|
218
|
-
def filled_value(alias:
|
|
238
|
+
def filled_value(alias: exp.Identifier) -> exp.Expr:
|
|
219
239
|
if alias in key_aliases and key_aliases.index(alias) in filled:
|
|
220
240
|
return exp.alias_(spine_keys[key_aliases.index(alias)].copy(), alias)
|
|
221
241
|
if alias in aliases and alias in known:
|
|
222
242
|
return exp.alias_(known[alias].copy(), alias)
|
|
223
243
|
if alias in independent:
|
|
224
|
-
return exp.column(alias, table=
|
|
244
|
+
return exp.column(alias, table=parts_name)
|
|
225
245
|
# A NULL of the key's type, as Postgres reads a bare NULL as text.
|
|
226
246
|
return exp.alias_(
|
|
227
247
|
exp.select(exp.column(alias)).from_(unfilled_name).where(exp.false()).subquery(),
|
|
@@ -258,11 +278,11 @@ def expand_fill(select: exp.Select, dialect: DialectType = None) -> exp.Select:
|
|
|
258
278
|
where, exp.column(set_alias).eq(grouping(dimensions, grouping_set))
|
|
259
279
|
)
|
|
260
280
|
parts = (
|
|
261
|
-
exp.select(*independent)
|
|
281
|
+
exp.select(*map(exp.column, independent))
|
|
262
282
|
.distinct()
|
|
263
283
|
.from_(unfilled_name)
|
|
264
284
|
.where(where)
|
|
265
|
-
.subquery(
|
|
285
|
+
.subquery(parts_name)
|
|
266
286
|
)
|
|
267
287
|
keys = keys.join(parts, join_type="CROSS")
|
|
268
288
|
return keys
|
|
@@ -271,11 +291,11 @@ def expand_fill(select: exp.Select, dialect: DialectType = None) -> exp.Select:
|
|
|
271
291
|
for grouping_set in filled_sets[1:]:
|
|
272
292
|
keys = exp.union(keys, set_keys(grouping_set), distinct=False)
|
|
273
293
|
|
|
274
|
-
def unfilled_column(alias:
|
|
294
|
+
def unfilled_column(alias: exp.Identifier) -> exp.Column:
|
|
275
295
|
return exp.column(alias, table=unfilled_name)
|
|
276
296
|
|
|
277
|
-
def keys_column(alias:
|
|
278
|
-
return exp.column(alias, table=
|
|
297
|
+
def keys_column(alias: exp.Identifier) -> exp.Column:
|
|
298
|
+
return exp.column(alias, table=keys_name)
|
|
279
299
|
|
|
280
300
|
# A key some set doesn't fill is NULL there.
|
|
281
301
|
on = exp.and_(
|
|
@@ -300,7 +320,7 @@ def expand_fill(select: exp.Select, dialect: DialectType = None) -> exp.Select:
|
|
|
300
320
|
),
|
|
301
321
|
*(exp.alias_(unfilled_column(alias), alias) for alias in value_aliases.values()),
|
|
302
322
|
)
|
|
303
|
-
.from_(keys.subquery(
|
|
323
|
+
.from_(keys.subquery(keys_name))
|
|
304
324
|
.join(exp.to_table(unfilled_name), on=on, join_type="FULL OUTER")
|
|
305
325
|
)
|
|
306
326
|
if having_alias is not None:
|
|
@@ -313,14 +333,16 @@ def expand_fill(select: exp.Select, dialect: DialectType = None) -> exp.Select:
|
|
|
313
333
|
|
|
314
334
|
def from_filled(node: exp.Expr) -> exp.Expr:
|
|
315
335
|
alias = value_aliases.get(node) or dimension_aliases.get(node) or grouping_aliases.get(node)
|
|
316
|
-
return exp.column(alias, table=
|
|
336
|
+
return exp.column(alias, table=filled_name) if alias is not None else node
|
|
317
337
|
|
|
318
338
|
projections = [
|
|
319
339
|
exp.alias_(projection.this.transform(from_filled), projection.alias)
|
|
320
340
|
for projection in select.expressions
|
|
321
341
|
]
|
|
322
342
|
return (
|
|
323
|
-
exp.select(*projections)
|
|
343
|
+
exp.select(*projections)
|
|
344
|
+
.from_(filled.subquery(filled_name))
|
|
345
|
+
.with_(unfilled_name, as_=unfilled)
|
|
324
346
|
)
|
|
325
347
|
|
|
326
348
|
|
{metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/rules/expand_per.py
RENAMED
|
@@ -474,7 +474,9 @@ def _resolve_exclusion(
|
|
|
474
474
|
DATE_TRUNC('month', ordered_at) for ordered_at. A listed key derived from another listed
|
|
475
475
|
column stays in the partition, so EXCLUDING ordered_at, DATE_TRUNC('month', ordered_at)
|
|
476
476
|
restarts every month. When the grain has a derived key but not the column, the window's
|
|
477
|
-
ORDER BY column reads that key, so a month-grain window orders by month.
|
|
477
|
+
ORDER BY column reads that key, so a month-grain window orders by month. An excluded column
|
|
478
|
+
the grain lacks, with no key derived from it, is skipped unless the window orders by it, or
|
|
479
|
+
unless it reads no column.
|
|
478
480
|
"""
|
|
479
481
|
window = exclusion.find_ancestor(exp.Window)
|
|
480
482
|
assert window is not None
|
|
@@ -489,14 +491,17 @@ def _resolve_exclusion(
|
|
|
489
491
|
order = window.args.get("order")
|
|
490
492
|
for column in columns:
|
|
491
493
|
derived = [key for key in grain if _derives(key, column)]
|
|
494
|
+
ordered_by = order is not None and any(
|
|
495
|
+
ordered.this == column for ordered in order.expressions
|
|
496
|
+
)
|
|
492
497
|
if column not in grain and not derived:
|
|
493
|
-
|
|
498
|
+
# A column the grain lacks has nothing to remove, unless the window orders by it. An
|
|
499
|
+
# item that reads no column, e.g. a relationship, which expands to NULL, isn't one.
|
|
500
|
+
if ordered_by or column.find(exp.Column) is None:
|
|
501
|
+
_fail(dialect, missing, window)
|
|
502
|
+
continue
|
|
494
503
|
removed.extend((column, *derived))
|
|
495
|
-
if
|
|
496
|
-
column in grain
|
|
497
|
-
or order is None
|
|
498
|
-
or all(ordered.this != column for ordered in order.expressions)
|
|
499
|
-
):
|
|
504
|
+
if column in grain or not ordered_by:
|
|
500
505
|
continue
|
|
501
506
|
if len(derived) > 1:
|
|
502
507
|
_fail(
|
{metricflow_sql-0.5.6 → metricflow_sql-0.5.7}/src/metricflow_sql/compiler/rules/isolate_fragments.py
RENAMED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
from sqlglot import exp
|
|
4
|
-
from sqlglot.helper import
|
|
4
|
+
from sqlglot.helper import name_sequence
|
|
5
5
|
from sqlglot.optimizer.scope import (
|
|
6
6
|
Scope,
|
|
7
7
|
find_all_in_scope,
|
|
@@ -11,6 +11,7 @@ from sqlglot.optimizer.scope import (
|
|
|
11
11
|
)
|
|
12
12
|
|
|
13
13
|
from metricflow_sql.compiler.guard import TRUSTED
|
|
14
|
+
from metricflow_sql.utils.sql import reserve_name
|
|
14
15
|
|
|
15
16
|
# Clauses that are not part of the fragment. They move to the outer select.
|
|
16
17
|
_NON_FRAGMENT_ARGS = ("having", "qualify", "distinct", "order", "limit", "offset")
|
|
@@ -93,7 +94,7 @@ def _isolate(select: exp.Select, scope: Scope, fragment_alias: exp.Identifier) -
|
|
|
93
94
|
# Deduplicate projection names, so that they can be unambiguously referenced from outer scopes
|
|
94
95
|
for projection in select.expressions:
|
|
95
96
|
name = projection.args["alias"]
|
|
96
|
-
inner =
|
|
97
|
+
inner = reserve_name(name, taken)
|
|
97
98
|
projection.set("alias", inner)
|
|
98
99
|
inner_columns.setdefault(_key(projection.this), inner)
|
|
99
100
|
outer_projections.append(exp.alias_(exp.column(inner, fragment_alias), name, copy=False))
|
|
@@ -169,14 +170,15 @@ def _rewrite_for_outer(
|
|
|
169
170
|
|
|
170
171
|
# A column, a path and a measure keep their identifier, anything nameless
|
|
171
172
|
# is numbered like qualify does.
|
|
172
|
-
identifier =
|
|
173
|
+
identifier = reserve_name(
|
|
173
174
|
_output_identifier(node)
|
|
174
175
|
or exp.to_identifier(node.output_name or f"_col_{len(select.expressions)}"),
|
|
175
176
|
taken,
|
|
176
177
|
)
|
|
177
178
|
inner_columns[_key(node)] = identifier
|
|
178
179
|
node.replace(exp.column(identifier, fragment_alias))
|
|
179
|
-
|
|
180
|
+
# `exp.alias_` would set a subquery's own alias instead of wrapping it.
|
|
181
|
+
select.select(exp.Alias(this=node, alias=identifier.copy()), copy=False)
|
|
180
182
|
|
|
181
183
|
def move(node: exp.Expr, nested: bool) -> None:
|
|
182
184
|
root = node
|
|
@@ -216,15 +218,6 @@ def _grouping_expressions(group: exp.Group) -> set[exp.Expr]:
|
|
|
216
218
|
}
|
|
217
219
|
|
|
218
220
|
|
|
219
|
-
def _reserve_name(identifier: exp.Identifier, taken: set[str]) -> exp.Identifier:
|
|
220
|
-
name = find_new_name(taken, identifier.name)
|
|
221
|
-
taken.add(name)
|
|
222
|
-
if name == identifier.name:
|
|
223
|
-
return identifier
|
|
224
|
-
|
|
225
|
-
return exp.to_identifier(name, quoted=identifier.quoted)
|
|
226
|
-
|
|
227
|
-
|
|
228
221
|
def _output_identifier(node: exp.Expr) -> exp.Identifier | None:
|
|
229
222
|
"""
|
|
230
223
|
The identifier that names a column, the last field of a path or the measure
|
|
@@ -7,10 +7,10 @@ from sqlglot.helper import ensure_list, find_new_name
|
|
|
7
7
|
|
|
8
8
|
from metricflow_sql.utils.sql import (
|
|
9
9
|
get_groupings,
|
|
10
|
-
internal_alias,
|
|
11
10
|
is_aggregate,
|
|
12
11
|
is_shadowed,
|
|
13
12
|
nest_clauses,
|
|
13
|
+
pick_aliases_for_subquery,
|
|
14
14
|
)
|
|
15
15
|
|
|
16
16
|
logger = logging.getLogger(__name__)
|
|
@@ -27,12 +27,12 @@ def nest_grouped_subqueries(expression: exp.Expr) -> exp.Expr:
|
|
|
27
27
|
SELECT EXISTS(SELECT 1 FROM u WHERE u.k = t.k) AS e, SUM(t.x) AS x
|
|
28
28
|
FROM t GROUP BY EXISTS(SELECT 1 FROM u WHERE u.k = t.k)
|
|
29
29
|
=>
|
|
30
|
-
SELECT rows.
|
|
30
|
+
SELECT rows.e AS e, SUM(rows.x) AS x
|
|
31
31
|
FROM (
|
|
32
|
-
SELECT EXISTS(SELECT 1 FROM u WHERE u.k = t.k) AS
|
|
32
|
+
SELECT EXISTS(SELECT 1 FROM u WHERE u.k = t.k) AS e, t.x AS x
|
|
33
33
|
FROM t
|
|
34
34
|
) AS rows
|
|
35
|
-
GROUP BY rows.
|
|
35
|
+
GROUP BY rows.e
|
|
36
36
|
"""
|
|
37
37
|
selects = [
|
|
38
38
|
select
|
|
@@ -48,41 +48,17 @@ def nest_grouped_subqueries(expression: exp.Expr) -> exp.Expr:
|
|
|
48
48
|
|
|
49
49
|
|
|
50
50
|
def _nest(select: exp.Select, aliases: set[str]) -> None:
|
|
51
|
-
_alias_subqueries(select)
|
|
52
51
|
read = _row_reads(select)
|
|
53
52
|
if not read:
|
|
54
53
|
return
|
|
55
54
|
|
|
56
|
-
|
|
57
|
-
names: dict[exp.Expr, exp.Identifier] = {}
|
|
58
|
-
for index, node in enumerate(dict.fromkeys(read)):
|
|
59
|
-
if isinstance(node, exp.Column):
|
|
60
|
-
name = internal_alias(used, node.name)
|
|
61
|
-
names[node] = node.this if name == node.name else exp.to_identifier(name)
|
|
62
|
-
else:
|
|
63
|
-
names[node] = exp.to_identifier(internal_alias(used, f"_dim_{index}"))
|
|
64
|
-
|
|
55
|
+
names = pick_aliases_for_subquery(read, select, "_dim")
|
|
65
56
|
projected = {projection.alias_or_name for projection in select.expressions}
|
|
66
|
-
alias = find_new_name(aliases | projected, "rows")
|
|
57
|
+
alias = find_new_name({name.lower() for name in aliases | projected}, "rows")
|
|
67
58
|
aliases.add(alias)
|
|
68
59
|
nest_clauses(select, _ROW_ARGS, alias, read, names)
|
|
69
60
|
|
|
70
61
|
|
|
71
|
-
def _alias_subqueries(select: exp.Select) -> None:
|
|
72
|
-
"""
|
|
73
|
-
Names a projection that is a subquery with an alias around it, as any other projection is
|
|
74
|
-
named. An alias set on the subquery itself is part of it, so the projection wouldn't equal
|
|
75
|
-
the key it repeats.
|
|
76
|
-
"""
|
|
77
|
-
for projection in select.expressions:
|
|
78
|
-
name = projection.args.get("alias")
|
|
79
|
-
if isinstance(projection, exp.Subquery) and name is not None:
|
|
80
|
-
projection.set("alias", None)
|
|
81
|
-
alias = exp.Alias(alias=name.this if isinstance(name, exp.TableAlias) else name)
|
|
82
|
-
projection.replace(alias)
|
|
83
|
-
alias.set("this", projection)
|
|
84
|
-
|
|
85
|
-
|
|
86
62
|
def _row_reads(select: exp.Select) -> list[exp.Expr]:
|
|
87
63
|
"""
|
|
88
64
|
The GROUP BY keys holding a subquery and the columns of the select's sources that the rest of
|