metricflow-sql 0.5.2__tar.gz → 0.5.3__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.2 → metricflow_sql-0.5.3}/PKG-INFO +2 -2
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/pyproject.toml +2 -2
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/rules/expand_constellations.py +2 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/rules/expand_datasets.py +3 -4
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/converters/legacy.py +66 -13
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/converters/lookml.py +79 -14
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/.gitignore +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/LICENSE +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/PYPI.md +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/README.md +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/__init__.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/__init__.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/catalog/__init__.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/catalog/cluster.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/catalog/information_schema.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/catalog/metadata.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/catalog/passthrough.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/catalog/protocols.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/catalog/semantic.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/compiler.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/guard.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/rules/__init__.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/rules/constellation_permissions.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/rules/expand_fill.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/rules/expand_metadata.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/rules/expand_per.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/rules/isolate_fragments.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/rules/passthrough_tables.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/table_arguments.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/converters/__init__.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/objects/__init__.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/objects/base.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/objects/constellation.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/objects/dataset.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/objects/entity.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/objects/reference.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/objects/wiki.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/py.typed +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/registry/__init__.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/registry/dao.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/registry/dependency_resolution.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/registry/memory.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/registry/operation.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/registry/reference_resolution.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/registry/registry.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/registry/resolved.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/registry/type_resolver.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/schemas/__init__.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/schemas/bundle.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/schemas/fragment.schema.json +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/schemas/generate.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/schemas/load.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/schemas/markdown.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/schemas/v1/__init__.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/schemas/v1/fragment.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/server/__init__.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/server/app.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/server/compile.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/server/errors.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/server/http.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/server/registry.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/server/serde.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/server/serve.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/utils/__init__.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/utils/composed_set.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/utils/normalize.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/utils/ordered_set.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/utils/sql.py +0 -0
- {metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/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.3
|
|
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.32.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.3"
|
|
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.32.0",
|
|
21
21
|
"wenmode>=0.15.2",
|
|
22
22
|
]
|
|
23
23
|
|
|
@@ -1064,6 +1064,8 @@ class ExpandConstellationsRule:
|
|
|
1064
1064
|
return grouped(sets)
|
|
1065
1065
|
|
|
1066
1066
|
projections = [exp.alias_(expr, name) for name, expr in translated.items()] + leaves()
|
|
1067
|
+
if not projections:
|
|
1068
|
+
projections = [exp.Literal.number(1)]
|
|
1067
1069
|
groupings = [translated[name] for name in groupable]
|
|
1068
1070
|
group = exp.Group(expressions=groupings) if groupings and not ctx.stack else None
|
|
1069
1071
|
order = None
|
{metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/rules/expand_datasets.py
RENAMED
|
@@ -592,10 +592,9 @@ class ExpandDatasetsRule:
|
|
|
592
592
|
for alias in referenced_aliases
|
|
593
593
|
if alias in ctx.fanout_grain_by_alias
|
|
594
594
|
}
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
else having_grain or ctx.aggregate_grains.get(id(aggregate), ctx.root_alias)
|
|
595
|
+
own_alias = having_grain or ctx.aggregate_grains.get(id(aggregate), ctx.root_alias)
|
|
596
|
+
grain_alias = max(
|
|
597
|
+
(*fanout_aliases, own_alias), key=lambda alias: ctx.join_depths.get(alias, 0)
|
|
599
598
|
)
|
|
600
599
|
if referenced_aliases:
|
|
601
600
|
self._reject_fanout_aggregate(
|
|
@@ -18,13 +18,17 @@ The legacy concepts map onto the fragment's as follows:
|
|
|
18
18
|
- A semantic model is a dataset of its relation, with a column for each column its expressions
|
|
19
19
|
read, a calculation for each measure, e.g. `msr_revenue` of `SUM(amount)`, and a relationship
|
|
20
20
|
to each entity it references, e.g. `customer`. A relationship whose entity is named like one of
|
|
21
|
-
its columns is prefixed, e.g. `rel_customer_id`, as a field's name can't be a
|
|
21
|
+
its columns, ignoring case, is prefixed, e.g. `rel_customer_id`, as a field's name can't be a
|
|
22
|
+
column's. A semantic model named like an entity is suffixed, e.g. `orders_model`, as a dataset's
|
|
23
|
+
name can't be an entity's.
|
|
22
24
|
- An entity a semantic model is primary, unique or natural for is an entity of the same name, with
|
|
23
25
|
the semantic model's dataset as a source, keyed by `id`. A natural entity's validity dimensions
|
|
24
26
|
add an optional `as_of` key, e.g. `:as_of >= valid_from AND (valid_to IS NULL OR :as_of <
|
|
25
27
|
valid_to)`, which each relationship to it binds to its dataset's aggregation time.
|
|
26
28
|
- A semantic model's dimensions are attributes of its primary entity if it has the entity's key,
|
|
27
|
-
e.g. `customer.region`, else roles of the `metrics` constellation, e.g. `is_instant`.
|
|
29
|
+
e.g. `customer.region`, else roles of the `metrics` constellation, e.g. `is_instant`. A constant
|
|
30
|
+
attribute, e.g. `1`, is NULL for an entity without a row, e.g. `CASE WHEN refunds.order_id IS
|
|
31
|
+
NOT NULL THEN 1 END`.
|
|
28
32
|
- Every object is in the `legacy` namespace, apart from the project's own, e.g. `legacy.orders`.
|
|
29
33
|
- Every metric is a metric of the `legacy.metrics` constellation, whose roles are an entity reference
|
|
30
34
|
for each entity, e.g. `customer`, `metric_time` for each dataset's aggregation time, and the
|
|
@@ -82,6 +86,8 @@ _AS_OF = "as_of"
|
|
|
82
86
|
# relationship whose entity is named like a column, e.g. `customer_id` of the column `customer_id`.
|
|
83
87
|
_MEASURE = "msr_"
|
|
84
88
|
_RELATIONSHIP = "rel_"
|
|
89
|
+
# Suffixes a semantic model's dataset whose name an entity has, e.g. `orders_model`.
|
|
90
|
+
_DATASET = "_model"
|
|
85
91
|
# Names the dataset of the time spine of a grain, e.g. `time_spine__day`.
|
|
86
92
|
_TIME_SPINE = "time_spine"
|
|
87
93
|
# Separates the entities and element of a legacy name, e.g. `listing__user__home_state`.
|
|
@@ -192,7 +198,12 @@ class _Dataset:
|
|
|
192
198
|
"""Whether a metric reads it, so the constellation assigns it roles."""
|
|
193
199
|
|
|
194
200
|
def taken(self, name: str) -> bool:
|
|
195
|
-
|
|
201
|
+
# Names compare ignoring case, as the loader normalizes them, e.g. `Amount` is `amount`.
|
|
202
|
+
name = name.lower()
|
|
203
|
+
return any(
|
|
204
|
+
name == other.lower()
|
|
205
|
+
for other in (*self.columns, *self.calculations, *self.relationships)
|
|
206
|
+
)
|
|
196
207
|
|
|
197
208
|
def calculation(self, expr: str, name: str, **fields: Any) -> str:
|
|
198
209
|
"""The name of the calculation `expr`, defining it, named `name` if it's free."""
|
|
@@ -286,6 +297,21 @@ class _Converter:
|
|
|
286
297
|
"config": measure.get("config"),
|
|
287
298
|
"type_params": {"measure": {"name": measure["name"]}},
|
|
288
299
|
}
|
|
300
|
+
# Each semantic model's dataset name, by its name: its own, unless an entity or the
|
|
301
|
+
# constellation has it, as a dataset's name can't be theirs, e.g. `orders_model` beside the
|
|
302
|
+
# entity `orders`.
|
|
303
|
+
reserved = {name.lower() for name in (CONSTELLATION, *self._entity_names())}
|
|
304
|
+
taken = reserved | {name.lower() for name in self.models}
|
|
305
|
+
self.dataset_names: dict[str, str] = {}
|
|
306
|
+
for model_name in self.models:
|
|
307
|
+
name, index = model_name, 1
|
|
308
|
+
if name.lower() in reserved:
|
|
309
|
+
name = f"{model_name}{_DATASET}"
|
|
310
|
+
while name.lower() in taken:
|
|
311
|
+
index += 1
|
|
312
|
+
name = f"{model_name}{_DATASET}_{index}"
|
|
313
|
+
taken.add(name.lower())
|
|
314
|
+
self.dataset_names[model_name] = name
|
|
289
315
|
self.saved_queries: list[dict[str, Any]] = manifest.get("saved_queries") or []
|
|
290
316
|
self.spines = _time_spines(manifest)
|
|
291
317
|
# The spines' datasets, by grain.
|
|
@@ -306,6 +332,15 @@ class _Converter:
|
|
|
306
332
|
# Metrics converted or skipped so far, so each converts once whichever metric reads it.
|
|
307
333
|
self.converted: dict[str, str | None] = {}
|
|
308
334
|
|
|
335
|
+
def _entity_names(self) -> set[str]:
|
|
336
|
+
"""The names of the entities semantic models are sources of."""
|
|
337
|
+
return {
|
|
338
|
+
entity["name"]
|
|
339
|
+
for model in self.models.values()
|
|
340
|
+
for entity in model.get("entities") or []
|
|
341
|
+
if entity["type"] != "foreign"
|
|
342
|
+
}
|
|
343
|
+
|
|
309
344
|
def _check_model(self, model: dict[str, Any]) -> None:
|
|
310
345
|
"""Checks `model`'s SQL parses and its references resolve, so the conversion can read it."""
|
|
311
346
|
for element in (
|
|
@@ -454,9 +489,9 @@ class _Converter:
|
|
|
454
489
|
if key not in self.datasets:
|
|
455
490
|
model = self.models[model_name]
|
|
456
491
|
default = _default_time(model)
|
|
457
|
-
name = model_name
|
|
492
|
+
name = self.dataset_names[model_name]
|
|
458
493
|
if agg_time_dimension != default:
|
|
459
|
-
name = f"{
|
|
494
|
+
name = f"{name}{_SEPARATOR}{agg_time_dimension}"
|
|
460
495
|
if offset is not None:
|
|
461
496
|
if agg_time_dimension is None:
|
|
462
497
|
raise _Skip(f"semantic model '{model_name}' has no aggregation time")
|
|
@@ -474,7 +509,8 @@ class _Converter:
|
|
|
474
509
|
|
|
475
510
|
def _read_columns(self, dataset: _Dataset, expr: exp.Expr) -> None:
|
|
476
511
|
for column in expr.find_all(exp.Column):
|
|
477
|
-
|
|
512
|
+
# Once each, ignoring case, e.g. `Amount` and `amount` are one column.
|
|
513
|
+
if not column.table and not dataset.taken(column.name):
|
|
478
514
|
dataset.columns[column.name] = None
|
|
479
515
|
|
|
480
516
|
def _relationships(self, dataset: _Dataset) -> None:
|
|
@@ -507,7 +543,8 @@ class _Converter:
|
|
|
507
543
|
def _relationship(self, model: dict[str, Any], entity: str) -> str:
|
|
508
544
|
"""
|
|
509
545
|
The name of `model`'s relationship to `entity`: the entity's, unless one of the semantic
|
|
510
|
-
model's columns has it, e.g. `rel_customer_id` beside the column
|
|
546
|
+
model's columns has it ignoring case, e.g. `rel_customer_id` beside the column
|
|
547
|
+
`customer_id` or `Customer_Id`.
|
|
511
548
|
"""
|
|
512
549
|
if model["name"] not in self.model_columns:
|
|
513
550
|
elements = [
|
|
@@ -516,12 +553,12 @@ class _Converter:
|
|
|
516
553
|
*(model.get("measures") or []),
|
|
517
554
|
]
|
|
518
555
|
self.model_columns[model["name"]] = {
|
|
519
|
-
column.name
|
|
556
|
+
column.name.lower()
|
|
520
557
|
for element in elements
|
|
521
558
|
for column in self._expr(element).find_all(exp.Column)
|
|
522
559
|
if not column.table
|
|
523
560
|
}
|
|
524
|
-
if entity in self.model_columns[model["name"]]:
|
|
561
|
+
if entity.lower() in self.model_columns[model["name"]]:
|
|
525
562
|
return f"{_RELATIONSHIP}{entity}"
|
|
526
563
|
return entity
|
|
527
564
|
|
|
@@ -556,7 +593,9 @@ class _Converter:
|
|
|
556
593
|
else:
|
|
557
594
|
on = f"{on} AND {validity}"
|
|
558
595
|
self.scd_entities.add(entity["name"])
|
|
559
|
-
converted["sources"].append(
|
|
596
|
+
converted["sources"].append(
|
|
597
|
+
{"dataset": self.dataset_names[model["name"]], "on": on}
|
|
598
|
+
)
|
|
560
599
|
for name in self.scd_entities:
|
|
561
600
|
self.entities[name]["key"].append({"name": _AS_OF, "optional": True})
|
|
562
601
|
# Now that entities are known, relationships to them.
|
|
@@ -568,21 +607,22 @@ class _Converter:
|
|
|
568
607
|
continue
|
|
569
608
|
attributes = self.entities[entity]["attributes"]
|
|
570
609
|
names = {attribute["name"] for attribute in attributes}
|
|
610
|
+
dataset_name = self.dataset_names[model["name"]]
|
|
571
611
|
for dimension in model.get("dimensions") or []:
|
|
572
612
|
attribute = {
|
|
573
613
|
"name": dimension["name"],
|
|
574
614
|
"label": dimension.get("label"),
|
|
575
615
|
"description": dimension.get("description"),
|
|
576
|
-
"expr": self.
|
|
616
|
+
"expr": self._attribute_expr(model, dimension, entity),
|
|
577
617
|
}
|
|
578
618
|
self._add_attribute(entity, attributes, names, attribute)
|
|
579
619
|
for other in model.get("entities") or []:
|
|
580
620
|
if other["type"] != "foreign":
|
|
581
621
|
continue
|
|
582
622
|
if other["name"] in self.entities:
|
|
583
|
-
expr = f"{
|
|
623
|
+
expr = f"{dataset_name}.{self._relationship(model, other['name'])}"
|
|
584
624
|
else:
|
|
585
|
-
expr = self.
|
|
625
|
+
expr = self._attribute_expr(model, other, entity)
|
|
586
626
|
self._add_attribute(
|
|
587
627
|
entity, attributes, names, {"name": other["name"], "expr": expr}
|
|
588
628
|
)
|
|
@@ -595,6 +635,19 @@ class _Converter:
|
|
|
595
635
|
for attribute in converted["attributes"]
|
|
596
636
|
]
|
|
597
637
|
|
|
638
|
+
def _attribute_expr(self, model: dict[str, Any], element: dict[str, Any], entity: str) -> str:
|
|
639
|
+
"""
|
|
640
|
+
The expression of `element` as an attribute of `entity`, over `model`'s dataset's columns.
|
|
641
|
+
A constant, e.g. `1`, is the dataset's only where it has a row of the entity, e.g. `CASE
|
|
642
|
+
WHEN refunds.order_id IS NOT NULL THEN 1 END`, as legacy MetricFlow joins a dimension's
|
|
643
|
+
rows, so it's NULL for an entity without one.
|
|
644
|
+
"""
|
|
645
|
+
dataset = self.dataset_names[model["name"]]
|
|
646
|
+
if any(not column.table for column in self._expr(element).find_all(exp.Column)):
|
|
647
|
+
return self._qualified(element, dataset)
|
|
648
|
+
key = self._qualified(_entity(model, entity), dataset)
|
|
649
|
+
return f"CASE WHEN {key} IS NOT NULL THEN {self._text(element)} END"
|
|
650
|
+
|
|
598
651
|
def _add_attribute(
|
|
599
652
|
self,
|
|
600
653
|
entity: str,
|
|
@@ -60,6 +60,11 @@ _USER_ATTRIBUTE = re.compile(r"\{\{\s*_user_attributes\['(\w+)'\]\s*\}\}")
|
|
|
60
60
|
_PARAMETER = re.compile(r"\{%\s*parameter\s+(\w+)\s*%\}")
|
|
61
61
|
# Whether the query reads a field, which Looker knows, e.g. `accounts.region._in_query`.
|
|
62
62
|
_QUERY_FLAG = re.compile(r"\b\w+\.(\w+)\._(in_query|is_filtered|is_selected)\b")
|
|
63
|
+
# Whether a parameter has a value, which Looker knows, e.g.
|
|
64
|
+
# `accounts.mode._parameter_value == 'include'`. Other values stay Liquid, e.g. `'a-b'` or `'A'`,
|
|
65
|
+
# which would name their flags like `'a_b'`'s or `'a'`'s, as names don't tell case apart, and
|
|
66
|
+
# other spacing, which the view's JSON escapes.
|
|
67
|
+
_PARAMETER_VALUE = re.compile(r"\b(?:(\w+)\.)?(\w+)\._parameter_value *== *'([a-z0-9_]+)'")
|
|
63
68
|
# An `if` on query flags once they're parameters, e.g. `{% if :region_in_query %} a {% else %} b
|
|
64
69
|
# {% endif %}`.
|
|
65
70
|
_FLAG_IF = re.compile(
|
|
@@ -166,6 +171,9 @@ class _Converter:
|
|
|
166
171
|
the explores that join the view name them alike."""
|
|
167
172
|
timezone: str | None = None
|
|
168
173
|
"""Looker's query timezone, which converts the time dimension groups that don't opt out."""
|
|
174
|
+
looker_converts: bool = False
|
|
175
|
+
"""Whether Looker's connection has the timezone, so Looker converts the time fields a query
|
|
176
|
+
reads, and only the references in other fields' SQL convert here."""
|
|
169
177
|
base: str = field(init=False)
|
|
170
178
|
parents: dict[str, str] = field(init=False)
|
|
171
179
|
"""The view each joined view hangs off."""
|
|
@@ -355,22 +363,34 @@ class _Converter:
|
|
|
355
363
|
if "sql" not in definition:
|
|
356
364
|
raise _Skip("dimension without sql")
|
|
357
365
|
sql = self.translate(view, str(definition["sql"]), resolve)
|
|
358
|
-
|
|
366
|
+
# A date Looker converts stays a time, so Looker can take its date in the timezone.
|
|
367
|
+
if definition.get("type") == "date" and not (
|
|
368
|
+
self.looker_converts and self.converts(definition)
|
|
369
|
+
):
|
|
359
370
|
return _TIMEFRAMES["date"](sql)
|
|
360
|
-
if definition.get("type")
|
|
361
|
-
#
|
|
362
|
-
# matches what Looker renders for BigQuery.
|
|
363
|
-
if (
|
|
364
|
-
self.
|
|
365
|
-
and definition.get("convert_tz") != "no"
|
|
366
|
-
and definition.get("datatype") != "date"
|
|
367
|
-
):
|
|
368
|
-
return exp.func(
|
|
369
|
-
"DATETIME", sql, exp.Literal.string(self.timezone), dialect=_DIALECT
|
|
370
|
-
)
|
|
371
|
+
if definition.get("type") in ("date", "time"):
|
|
372
|
+
# Without a timezone on the connection, Looker converts nothing, so converting here
|
|
373
|
+
# matches what Looker renders for BigQuery.
|
|
374
|
+
if self.converts(definition) and not self.looker_converts:
|
|
375
|
+
return self.convert_tz(sql)
|
|
371
376
|
return exp.cast(sql, "DATETIME", dialect=_DIALECT)
|
|
372
377
|
return sql
|
|
373
378
|
|
|
379
|
+
def converts(self, definition: dict[str, Any]) -> bool:
|
|
380
|
+
"""Whether Looker converts a time field: a dimension group or a date that doesn't opt
|
|
381
|
+
out, and never a `datatype: date`."""
|
|
382
|
+
return bool(
|
|
383
|
+
self.timezone
|
|
384
|
+
and definition.get("type") in ("time", "date")
|
|
385
|
+
and definition.get("convert_tz") != "no"
|
|
386
|
+
and definition.get("datatype") != "date"
|
|
387
|
+
)
|
|
388
|
+
|
|
389
|
+
def convert_tz(self, sql: exp.Expr) -> exp.Expr:
|
|
390
|
+
"""A UTC time as a DATETIME in the timezone."""
|
|
391
|
+
assert self.timezone is not None
|
|
392
|
+
return exp.func("DATETIME", sql, exp.Literal.string(self.timezone), dialect=_DIALECT)
|
|
393
|
+
|
|
374
394
|
def filters(self, view: str, measure: dict[str, Any]) -> list[tuple[str, str]]:
|
|
375
395
|
"""A measure's `filters`, in either form: `{field, value}` or `[field: value, ...]`, by
|
|
376
396
|
the field's name in the explore, e.g. `revenue.tier`, and once each, as LookML can state
|
|
@@ -410,6 +430,7 @@ class _Converter:
|
|
|
410
430
|
"""Parses LookML `sql` into an expression on `view`'s dataset: its `${TABLE}.col` is the
|
|
411
431
|
column, its parameters and query flags placeholders, e.g. `:since`, an `if` on flags a
|
|
412
432
|
CASE, and a reference what `resolve` makes of it."""
|
|
433
|
+
sql = _PARAMETER_VALUE.sub(lambda match: f":{_value_flag(match)}", sql)
|
|
413
434
|
sql = _QUERY_FLAG.sub(lambda match: f":{_flag(match)}", _PARAMETER.sub(r":\1", sql))
|
|
414
435
|
# Each reference and `if`, by the placeholder standing in for it while the SQL parses.
|
|
415
436
|
nodes: dict[str, exp.Expr] = {}
|
|
@@ -499,6 +520,18 @@ class _Converter:
|
|
|
499
520
|
name, kind, template = group, "dimension_groups", _TIMEFRAMES[timeframe]
|
|
500
521
|
if isinstance(self.result(view, name), _Skip):
|
|
501
522
|
raise _Skip("references a skipped field")
|
|
523
|
+
# Looker's own model also converts the times other fields' SQL reads. That SQL runs here,
|
|
524
|
+
# out of the connection's reach, so it converts here, while the time field itself stays UTC
|
|
525
|
+
# for Looker. A date's field is its time too, see `dimension`; a `raw` one never converts.
|
|
526
|
+
definition = self.definition(view, name)[1]
|
|
527
|
+
if self.looker_converts and self.converts(definition):
|
|
528
|
+
if definition.get("type") == "date":
|
|
529
|
+
template = _TIMEFRAMES["date"]
|
|
530
|
+
if template is not _TIMEFRAMES["raw"]:
|
|
531
|
+
timeframe_template = template
|
|
532
|
+
template = lambda sql: timeframe_template( # noqa: E731
|
|
533
|
+
self.convert_tz(exp.func("TIMESTAMP", sql, dialect=_DIALECT))
|
|
534
|
+
)
|
|
502
535
|
return name, self.calculation(view, kind, name), template
|
|
503
536
|
|
|
504
537
|
def reference(self, view: str, target: str, name: str) -> exp.Expr:
|
|
@@ -541,7 +574,8 @@ class _Converter:
|
|
|
541
574
|
def parameters(self, view: str) -> dict[str, tuple[str, dict[str, Any]]]:
|
|
542
575
|
"""The parameters `view`'s LookML reads, by name: the Liquid Looker renders as their
|
|
543
576
|
argument, and their definition. A query flag is one too, e.g. `region_in_query` for
|
|
544
|
-
`accounts.region._in_query`,
|
|
577
|
+
`accounts.region._in_query`, or `mode_is_include` for
|
|
578
|
+
`accounts.mode._parameter_value == 'include'`, and NULL when false."""
|
|
545
579
|
lookml = json.dumps(self.views[view])
|
|
546
580
|
# The explore reads them, so by their view, e.g. `accounts.min_employees`.
|
|
547
581
|
parameters = {
|
|
@@ -556,6 +590,21 @@ class _Converter:
|
|
|
556
590
|
f"{{% if {match[0]} %}}TRUE{{% else %}}NULL{{% endif %}}",
|
|
557
591
|
{"datatype": "BOOL"},
|
|
558
592
|
)
|
|
593
|
+
# Looker renders a filter's Liquid itself, see `filter_sql`.
|
|
594
|
+
for match in _PARAMETER_VALUE.finditer(json.dumps(_without(self.views[view], "filters"))):
|
|
595
|
+
target, name, value = match[1] or view, match[2], match[3]
|
|
596
|
+
condition = f"{target}.{name}._parameter_value == '{value}'"
|
|
597
|
+
flag: dict[str, Any] = {"datatype": "BOOL"}
|
|
598
|
+
# Clients other than Looker leave it out, so it's TRUE for the parameter's default.
|
|
599
|
+
# Looker quotes a string parameter's value, e.g. `'include'`, so only an unquoted one's
|
|
600
|
+
# can match.
|
|
601
|
+
definition = self.definition(target, name)[1]
|
|
602
|
+
if definition.get("type") == "unquoted" and definition.get("default_value") == value:
|
|
603
|
+
flag["default"] = "TRUE"
|
|
604
|
+
parameters[_value_flag(match)] = (
|
|
605
|
+
f"{{% if {condition} %}}TRUE{{% else %}}NULL{{% endif %}}",
|
|
606
|
+
flag,
|
|
607
|
+
)
|
|
559
608
|
return parameters
|
|
560
609
|
|
|
561
610
|
def resolve_user_attributes(self, sql: str) -> str:
|
|
@@ -1095,6 +1144,12 @@ def _flag(match: re.Match[str]) -> str:
|
|
|
1095
1144
|
return f"{match[1]}_{match[2]}"
|
|
1096
1145
|
|
|
1097
1146
|
|
|
1147
|
+
def _value_flag(match: re.Match[str]) -> str:
|
|
1148
|
+
"""A parameter value's flag, e.g. `mode_is_include` for
|
|
1149
|
+
`accounts.mode._parameter_value == 'include'`."""
|
|
1150
|
+
return f"{match[2]}_is_{match[3]}"
|
|
1151
|
+
|
|
1152
|
+
|
|
1098
1153
|
def _docs(definition: dict[str, Any], **source: str) -> dict[str, Any]:
|
|
1099
1154
|
"""A LookML object's `label` and `description`, and its origin in `meta.lookml`: `source`
|
|
1100
1155
|
(e.g. its view and field), the field's name, and its `group_label`, `view_label`, `tags` and
|
|
@@ -1288,6 +1343,7 @@ def convert(
|
|
|
1288
1343
|
dashboards: tuple[Path, ...] = (),
|
|
1289
1344
|
catalog: str = "metrics",
|
|
1290
1345
|
timezone: str | None = None,
|
|
1346
|
+
looker_converts: bool = False,
|
|
1291
1347
|
) -> Conversion:
|
|
1292
1348
|
"""Converts `explores` of `model`, or all of them, reading the views it includes: from the
|
|
1293
1349
|
model's directory, and another project's, e.g. `//fivetran/*.view.lkml`, from `include`'s.
|
|
@@ -1337,7 +1393,9 @@ def convert(
|
|
|
1337
1393
|
views[join["name"]] = {**copy.deepcopy(views[join["from"]]), "name": join["name"]}
|
|
1338
1394
|
# What each explore's joins are, to name what explores join differently.
|
|
1339
1395
|
drafts = {
|
|
1340
|
-
definition["name"]: _Converter(
|
|
1396
|
+
definition["name"]: _Converter(
|
|
1397
|
+
views, definition, user_attributes, timezone=timezone, looker_converts=looker_converts
|
|
1398
|
+
)
|
|
1341
1399
|
for definition in selected
|
|
1342
1400
|
}
|
|
1343
1401
|
joins = {explore: draft.joins_by_view() for explore, draft in drafts.items()}
|
|
@@ -1402,6 +1460,7 @@ def convert(
|
|
|
1402
1460
|
entities=entity_names[explore],
|
|
1403
1461
|
renamed=renamed,
|
|
1404
1462
|
timezone=timezone,
|
|
1463
|
+
looker_converts=looker_converts,
|
|
1405
1464
|
)
|
|
1406
1465
|
# Converting every field first, so field lists can drop the skipped ones.
|
|
1407
1466
|
# Explores share views, so a view's skips would otherwise repeat.
|
|
@@ -1500,6 +1559,11 @@ def main() -> None:
|
|
|
1500
1559
|
"--timezone",
|
|
1501
1560
|
help="Looker's query timezone, e.g. America/Los_Angeles; none converts nothing.",
|
|
1502
1561
|
)
|
|
1562
|
+
parser.add_argument(
|
|
1563
|
+
"--looker-converts",
|
|
1564
|
+
action="store_true",
|
|
1565
|
+
help="Looker's connection has the timezone, so Looker converts the time fields it reads.",
|
|
1566
|
+
)
|
|
1503
1567
|
parser.add_argument("--out", type=Path, required=True)
|
|
1504
1568
|
args = parser.parse_args()
|
|
1505
1569
|
|
|
@@ -1514,6 +1578,7 @@ def main() -> None:
|
|
|
1514
1578
|
connection=args.connection,
|
|
1515
1579
|
catalog=args.catalog,
|
|
1516
1580
|
timezone=args.timezone,
|
|
1581
|
+
looker_converts=args.looker_converts,
|
|
1517
1582
|
)
|
|
1518
1583
|
args.out.mkdir(parents=True, exist_ok=True)
|
|
1519
1584
|
(args.out / "fragment.json").write_text(json.dumps(conversion.fragment, indent=2))
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/catalog/__init__.py
RENAMED
|
File without changes
|
{metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/catalog/cluster.py
RENAMED
|
File without changes
|
|
File without changes
|
{metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/catalog/metadata.py
RENAMED
|
File without changes
|
{metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/catalog/passthrough.py
RENAMED
|
File without changes
|
{metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/catalog/protocols.py
RENAMED
|
File without changes
|
{metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/catalog/semantic.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/rules/expand_fill.py
RENAMED
|
File without changes
|
{metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/rules/expand_metadata.py
RENAMED
|
File without changes
|
{metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/rules/expand_per.py
RENAMED
|
File without changes
|
{metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/rules/isolate_fragments.py
RENAMED
|
File without changes
|
|
File without changes
|
{metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/compiler/table_arguments.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/registry/dependency_resolution.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/registry/reference_resolution.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{metricflow_sql-0.5.2 → metricflow_sql-0.5.3}/src/metricflow_sql/schemas/fragment.schema.json
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|