metricflow-sql 0.5.4__tar.gz → 0.5.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/PKG-INFO +1 -1
- metricflow_sql-0.5.5/pyproject.toml +123 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/compiler.py +2 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/guard.py +7 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/rules/constellation_permissions.py +2 -5
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/rules/expand_constellations.py +39 -9
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/rules/expand_datasets.py +101 -31
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/rules/expand_per.py +217 -13
- metricflow_sql-0.5.5/src/metricflow_sql/compiler/rules/nest_grouped_subqueries.py +125 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/rules/nest_windows.py +4 -24
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/converters/legacy.py +2 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/converters/lookml.py +111 -5
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/objects/base.py +15 -1
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/objects/constellation.py +4 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/objects/dataset.py +9 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/objects/entity.py +1 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/registry/memory.py +93 -21
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/registry/registry.py +15 -4
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/registry/type_resolver.py +15 -6
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/schemas/fragment.schema.json +5 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/schemas/load.py +1 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/schemas/v1/fragment.py +9 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/server/serde.py +1 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/utils/sql.py +83 -12
- metricflow_sql-0.5.4/README.md +0 -3
- metricflow_sql-0.5.4/pyproject.toml +0 -50
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/.gitignore +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/LICENSE +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/PYPI.md +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/__init__.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/__init__.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/catalog/__init__.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/catalog/cluster.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/catalog/information_schema.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/catalog/metadata.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/catalog/passthrough.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/catalog/protocols.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/catalog/semantic.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/rules/__init__.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/rules/expand_fill.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/rules/expand_metadata.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/rules/isolate_fragments.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/rules/passthrough_tables.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/table_arguments.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/converters/__init__.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/objects/__init__.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/objects/reference.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/objects/wiki.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/py.typed +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/registry/__init__.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/registry/dao.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/registry/dependency_resolution.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/registry/operation.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/registry/reference_resolution.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/registry/resolved.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/schemas/__init__.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/schemas/bundle.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/schemas/generate.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/schemas/markdown.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/schemas/v1/__init__.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/server/__init__.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/server/app.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/server/compile.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/server/errors.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/server/http.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/server/registry.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/server/serve.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/utils/__init__.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/utils/composed_set.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/utils/normalize.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/utils/ordered_set.py +0 -0
- {metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/utils/template.py +0 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "metricflow-sql"
|
|
3
|
+
# A placeholder: a release sets its tag's version, with scripts/set_version.py.
|
|
4
|
+
version = "0.5.5"
|
|
5
|
+
description = "MetricFlow's core semantic layer engine"
|
|
6
|
+
# Its PyPI page: README.md is for this repository.
|
|
7
|
+
readme = "PYPI.md"
|
|
8
|
+
# Fivetran's own, in LICENSE, which no SPDX identifier names.
|
|
9
|
+
license = "LicenseRef-Fivetran"
|
|
10
|
+
requires-python = ">=3.12"
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Programming Language :: Python :: 3",
|
|
13
|
+
"Programming Language :: Python :: 3.12",
|
|
14
|
+
"Programming Language :: Python :: 3.13",
|
|
15
|
+
"Programming Language :: Python :: 3.14",
|
|
16
|
+
]
|
|
17
|
+
dependencies = [
|
|
18
|
+
"jinja2>=3.1",
|
|
19
|
+
"msgspec>=0.19",
|
|
20
|
+
"sqlglot[c]>=30.33.0",
|
|
21
|
+
"wenmode>=0.15.2",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[project.scripts]
|
|
25
|
+
metricflow-serve = "metricflow_sql.server.serve:main"
|
|
26
|
+
|
|
27
|
+
[project.optional-dependencies]
|
|
28
|
+
lookml = [
|
|
29
|
+
"lkml>=1.3.7",
|
|
30
|
+
]
|
|
31
|
+
yaml = [
|
|
32
|
+
"ruamel-yaml>=0.18",
|
|
33
|
+
]
|
|
34
|
+
# With ruamel-yaml, so a server reads every bundle: YAML fragments and pages' frontmatter. The
|
|
35
|
+
# sidecar's executable and the hosted service install this extra.
|
|
36
|
+
server = [
|
|
37
|
+
"ruamel-yaml>=0.18",
|
|
38
|
+
"starlette>=0.47.0",
|
|
39
|
+
"uvicorn>=0.34.0",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
[project.urls]
|
|
43
|
+
Documentation = "https://docs-internal-git-mfsql-dbt-labs.vercel.app/metricflow"
|
|
44
|
+
|
|
45
|
+
[build-system]
|
|
46
|
+
requires = ["hatchling"]
|
|
47
|
+
build-backend = "hatchling.build"
|
|
48
|
+
|
|
49
|
+
[tool.hatch.build.targets.wheel]
|
|
50
|
+
packages = ["src/metricflow_sql"]
|
|
51
|
+
|
|
52
|
+
# This directory also holds MetricFlow SQL's tests, docs, demos and scripts, for all its packages; the sdist is
|
|
53
|
+
# the library alone.
|
|
54
|
+
[tool.hatch.build.targets.sdist]
|
|
55
|
+
only-include = ["src"]
|
|
56
|
+
|
|
57
|
+
# metricflow-testing brings metricflow-service and every test dependency, so
|
|
58
|
+
# `uv sync --package metricflow-sql` can run each workspace package's suite; and the tools, for all
|
|
59
|
+
# of MetricFlow SQL's packages.
|
|
60
|
+
[dependency-groups]
|
|
61
|
+
dev = [
|
|
62
|
+
"metricflow-testing",
|
|
63
|
+
"mypy>=1.11",
|
|
64
|
+
"pre-commit>=4.0",
|
|
65
|
+
"ruff>=0.14",
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
[tool.uv.sources]
|
|
69
|
+
metricflow-testing = { workspace = true }
|
|
70
|
+
|
|
71
|
+
# MetricFlow SQL's packages extend this; the rest of the workspace keeps the root's rules.
|
|
72
|
+
[tool.ruff]
|
|
73
|
+
line-length = 100
|
|
74
|
+
target-version = "py312"
|
|
75
|
+
src = [
|
|
76
|
+
"src",
|
|
77
|
+
"../metricflow-service/src",
|
|
78
|
+
"../metricflow-testing/src",
|
|
79
|
+
"../dbt-metricflow/src",
|
|
80
|
+
"../metricflow-sidecar/src",
|
|
81
|
+
]
|
|
82
|
+
|
|
83
|
+
[tool.ruff.lint]
|
|
84
|
+
select = ["E", "F", "I", "UP", "B"]
|
|
85
|
+
# E501 only fires where the formatter cannot reach, so it is always a manual fix.
|
|
86
|
+
ignore = ["E501"]
|
|
87
|
+
|
|
88
|
+
# Shared by MetricFlow SQL's workspace packages, which each check their own `src` and `tests`: each
|
|
89
|
+
# package's suite is a `tests` package, so one run can't hold two. `task mfsql:typecheck` runs them all.
|
|
90
|
+
[tool.mypy]
|
|
91
|
+
python_version = "3.12"
|
|
92
|
+
mypy_path = """$MYPY_CONFIG_FILE_DIR/src:$MYPY_CONFIG_FILE_DIR/../metricflow-service/src:\
|
|
93
|
+
$MYPY_CONFIG_FILE_DIR/../metricflow-testing/src"""
|
|
94
|
+
files = ["src", "tests"]
|
|
95
|
+
strict = true
|
|
96
|
+
pretty = true
|
|
97
|
+
disallow_any_generics = false
|
|
98
|
+
|
|
99
|
+
# pyarrow ships no type information.
|
|
100
|
+
[[tool.mypy.overrides]]
|
|
101
|
+
module = ["pyarrow", "pyarrow.*"]
|
|
102
|
+
ignore_missing_imports = true
|
|
103
|
+
|
|
104
|
+
# boto3 and botocore ship no type information.
|
|
105
|
+
[[tool.mypy.overrides]]
|
|
106
|
+
module = ["boto3", "botocore", "botocore.*"]
|
|
107
|
+
ignore_missing_imports = true
|
|
108
|
+
|
|
109
|
+
# Strawberry types `strawberry.field(...)` as a decorator `Any`.
|
|
110
|
+
[[tool.mypy.overrides]]
|
|
111
|
+
module = "metricflow_service.api.graphql"
|
|
112
|
+
disable_error_code = ["untyped-decorator"]
|
|
113
|
+
|
|
114
|
+
[[tool.mypy.overrides]]
|
|
115
|
+
module = "tests.*"
|
|
116
|
+
disallow_untyped_defs = false
|
|
117
|
+
|
|
118
|
+
# Each package's suite has these settings, apart from the root's: it imports its own helpers as
|
|
119
|
+
# `tests.*`, so runs on its own.
|
|
120
|
+
[tool.pytest.ini_options]
|
|
121
|
+
testpaths = ["tests"]
|
|
122
|
+
pythonpath = ["."]
|
|
123
|
+
addopts = ["--import-mode=importlib"]
|
|
@@ -34,6 +34,7 @@ from metricflow_sql.compiler.rules.expand_fill import expand_fills
|
|
|
34
34
|
from metricflow_sql.compiler.rules.expand_metadata import ENGINE, ExpandMetadataRule
|
|
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
|
+
from metricflow_sql.compiler.rules.nest_grouped_subqueries import nest_grouped_subqueries
|
|
37
38
|
from metricflow_sql.compiler.rules.nest_windows import nest_windows
|
|
38
39
|
from metricflow_sql.compiler.rules.passthrough_tables import name_passthrough_tables
|
|
39
40
|
from metricflow_sql.compiler.table_arguments import bind_table_arguments
|
|
@@ -99,6 +100,7 @@ PLANNER_RULES: Sequence[OptimizerFn] = (
|
|
|
99
100
|
ExpandDatasetsRule(),
|
|
100
101
|
expand_per,
|
|
101
102
|
expand_fills,
|
|
103
|
+
nest_grouped_subqueries,
|
|
102
104
|
nest_windows,
|
|
103
105
|
)
|
|
104
106
|
|
|
@@ -28,6 +28,13 @@ GOVERNED = "governed"
|
|
|
28
28
|
TRUSTED = "trusted"
|
|
29
29
|
|
|
30
30
|
|
|
31
|
+
def trust(expression: exp.Expr) -> exp.Expr:
|
|
32
|
+
"""Marks each node of `expression` `TRUSTED`, in place."""
|
|
33
|
+
for node in expression.walk():
|
|
34
|
+
node.meta[TRUSTED] = True
|
|
35
|
+
return expression
|
|
36
|
+
|
|
37
|
+
|
|
31
38
|
class Denied(ValueError):
|
|
32
39
|
"""What a guard raises for something the caller can't use."""
|
|
33
40
|
|
|
@@ -7,7 +7,7 @@ from sqlglot.optimizer.scope import find_in_scope
|
|
|
7
7
|
|
|
8
8
|
from metricflow_sql.compiler.catalog.cluster import Cluster
|
|
9
9
|
from metricflow_sql.compiler.catalog.semantic import SemanticCatalog
|
|
10
|
-
from metricflow_sql.compiler.guard import
|
|
10
|
+
from metricflow_sql.compiler.guard import Guard, trust
|
|
11
11
|
from metricflow_sql.utils.sql import column_path, path_names, table_scopes
|
|
12
12
|
|
|
13
13
|
logger = logging.getLogger(__name__)
|
|
@@ -72,7 +72,4 @@ def trusted_condition(condition: exp.Expr, alias: exp.Identifier) -> exp.Expr:
|
|
|
72
72
|
return column_path(alias.copy(), *(name.copy() for name in path_names(node)))
|
|
73
73
|
return node
|
|
74
74
|
|
|
75
|
-
|
|
76
|
-
for node in qualified.walk():
|
|
77
|
-
node.meta[TRUSTED] = True
|
|
78
|
-
return qualified
|
|
75
|
+
return trust(condition.transform(qualify))
|
|
@@ -15,19 +15,27 @@ from sqlglot.optimizer.simplify import simplify_parens
|
|
|
15
15
|
from metricflow_sql.compiler.catalog.cluster import Cluster
|
|
16
16
|
from metricflow_sql.compiler.catalog.protocols import ColumnLike
|
|
17
17
|
from metricflow_sql.compiler.catalog.semantic import SemanticCatalog
|
|
18
|
-
from metricflow_sql.compiler.guard import GOVERNED, TRUSTED, Guard
|
|
18
|
+
from metricflow_sql.compiler.guard import GOVERNED, TRUSTED, Guard, trust
|
|
19
19
|
from metricflow_sql.compiler.rules.constellation_permissions import trusted_condition
|
|
20
|
-
from metricflow_sql.compiler.rules.expand_datasets import
|
|
20
|
+
from metricflow_sql.compiler.rules.expand_datasets import (
|
|
21
|
+
ExpandDatasetsRule,
|
|
22
|
+
bare_relationship_error,
|
|
23
|
+
)
|
|
21
24
|
from metricflow_sql.compiler.rules.expand_per import expand_over
|
|
22
25
|
from metricflow_sql.compiler.table_arguments import TABLE_ARGUMENTS
|
|
23
|
-
from metricflow_sql.objects.base import apply_mask
|
|
26
|
+
from metricflow_sql.objects.base import apply_mask, in_namespace
|
|
24
27
|
from metricflow_sql.objects.constellation import Metric, Role
|
|
25
28
|
from metricflow_sql.objects.dataset import Relationship
|
|
26
29
|
from metricflow_sql.registry.reference_resolution import (
|
|
27
30
|
resolve_metric,
|
|
28
31
|
resolve_metric_references,
|
|
29
32
|
)
|
|
30
|
-
from metricflow_sql.registry.registry import
|
|
33
|
+
from metricflow_sql.registry.registry import (
|
|
34
|
+
ConstellationProxy,
|
|
35
|
+
DatasetTarget,
|
|
36
|
+
EntityTarget,
|
|
37
|
+
Registry,
|
|
38
|
+
)
|
|
31
39
|
from metricflow_sql.utils.sql import (
|
|
32
40
|
column_path,
|
|
33
41
|
group_all_keys,
|
|
@@ -38,6 +46,7 @@ from metricflow_sql.utils.sql import (
|
|
|
38
46
|
normalize_group,
|
|
39
47
|
path_names,
|
|
40
48
|
per_argument,
|
|
49
|
+
ref_target,
|
|
41
50
|
table_scopes,
|
|
42
51
|
)
|
|
43
52
|
|
|
@@ -1167,10 +1176,7 @@ class ExpandConstellationsRule:
|
|
|
1167
1176
|
# TODO: let a bare reference role stand for its key, e.g. `customer IN (SELECT ...)`
|
|
1168
1177
|
# filtering on `customer.c_customer_sk`, or on `ss_customer_sk` without a join.
|
|
1169
1178
|
if is_reference:
|
|
1170
|
-
|
|
1171
|
-
self._fail(
|
|
1172
|
-
ctx, f"Use an attribute of the entity role {name}, e.g. {name}.<attribute>"
|
|
1173
|
-
)
|
|
1179
|
+
self._fail(ctx, self._use_an_attribute(ctx, role, field))
|
|
1174
1180
|
trusted = bool(node.meta.get(TRUSTED))
|
|
1175
1181
|
mask = self._mask(field, ctx) if field is not None and not trusted else None
|
|
1176
1182
|
return _replace(
|
|
@@ -1185,7 +1191,15 @@ class ExpandConstellationsRule:
|
|
|
1185
1191
|
)
|
|
1186
1192
|
return exp.Dot.build([assigned, *path])
|
|
1187
1193
|
|
|
1188
|
-
|
|
1194
|
+
def carry(node: exp.Expr) -> exp.Expr:
|
|
1195
|
+
# What stands for a role a permission reads is the permission's too: a condition
|
|
1196
|
+
# that is only a role, e.g. `where: is_public`, has no other node to tell it by.
|
|
1197
|
+
translated = rewrite(node)
|
|
1198
|
+
if translated is not node and node.meta.get(TRUSTED):
|
|
1199
|
+
trust(translated)
|
|
1200
|
+
return translated
|
|
1201
|
+
|
|
1202
|
+
return expr.transform(carry)
|
|
1189
1203
|
|
|
1190
1204
|
def _dataset(self, fact: exp.Table, ctx: ConstellationContext) -> exp.Table:
|
|
1191
1205
|
"""The dataset of the fact named `fact`, e.g. `events` for `visits`."""
|
|
@@ -1320,6 +1334,22 @@ class ExpandConstellationsRule:
|
|
|
1320
1334
|
return None
|
|
1321
1335
|
return ctx.constellation.get_column(column.this)
|
|
1322
1336
|
|
|
1337
|
+
def _use_an_attribute(
|
|
1338
|
+
self, ctx: ConstellationContext, role: exp.Identifier, field: ColumnLike | None
|
|
1339
|
+
) -> str:
|
|
1340
|
+
"""The error for a bare reference role, naming its target and the target's key."""
|
|
1341
|
+
name = self._sql(ctx, role)
|
|
1342
|
+
fallback = f"Use an attribute of the entity role {name}, e.g. {name}.<attribute>"
|
|
1343
|
+
target = ref_target(field.datatype) if field is not None else None
|
|
1344
|
+
if target is None:
|
|
1345
|
+
return fallback
|
|
1346
|
+
target = in_namespace(target, ctx.constellation.name.args["db"])
|
|
1347
|
+
if (entity := ctx.registry.get_entity(target)) is not None:
|
|
1348
|
+
return bare_relationship_error(role, EntityTarget(entity), ctx.registry.dialect)
|
|
1349
|
+
if (dataset := ctx.registry.get_dataset(target)) is not None:
|
|
1350
|
+
return bare_relationship_error(role, DatasetTarget(dataset), ctx.registry.dialect)
|
|
1351
|
+
return fallback
|
|
1352
|
+
|
|
1323
1353
|
def _sql(self, ctx: ConstellationContext, name: exp.Expr) -> str:
|
|
1324
1354
|
return name.sql(ctx.registry.dialect)
|
|
1325
1355
|
|
{metricflow_sql-0.5.4 → metricflow_sql-0.5.5}/src/metricflow_sql/compiler/rules/expand_datasets.py
RENAMED
|
@@ -15,7 +15,7 @@ from sqlglot.optimizer.simplify import simplify, simplify_parens
|
|
|
15
15
|
|
|
16
16
|
from metricflow_sql.compiler.catalog.cluster import Cluster
|
|
17
17
|
from metricflow_sql.compiler.catalog.passthrough import WarehouseDefaults
|
|
18
|
-
from metricflow_sql.compiler.guard import GOVERNED, Guard
|
|
18
|
+
from metricflow_sql.compiler.guard import GOVERNED, TRUSTED, Guard, trust
|
|
19
19
|
from metricflow_sql.compiler.rules.expand_fill import FILL_COL
|
|
20
20
|
from metricflow_sql.compiler.rules.expand_per import expand_select
|
|
21
21
|
from metricflow_sql.compiler.table_arguments import TABLE_ARGUMENTS
|
|
@@ -47,6 +47,7 @@ from metricflow_sql.utils.normalize import resolve_name
|
|
|
47
47
|
from metricflow_sql.utils.sql import (
|
|
48
48
|
FINAL,
|
|
49
49
|
WAREHOUSE_TABLE,
|
|
50
|
+
conjuncts,
|
|
50
51
|
group_all_keys,
|
|
51
52
|
in_per,
|
|
52
53
|
internal_alias,
|
|
@@ -66,6 +67,35 @@ logger = logging.getLogger(__name__)
|
|
|
66
67
|
_HAVING_GRAIN = "having_grain"
|
|
67
68
|
|
|
68
69
|
|
|
70
|
+
def key_names(entity: EntityProxy) -> list[exp.Identifier]:
|
|
71
|
+
"""`entity`'s key fields a query names, e.g. `id`, without an optional one, e.g. `as_of`."""
|
|
72
|
+
return [name for name, column in entity.key.items() if not column.optional]
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def bare_relationship_error(
|
|
76
|
+
path: exp.Expr, target: RelationshipTarget, dialect: DialectType
|
|
77
|
+
) -> str:
|
|
78
|
+
"""
|
|
79
|
+
The error for a relationship read as a value, e.g. `customer`, which names what it targets
|
|
80
|
+
and the target's key to read instead.
|
|
81
|
+
"""
|
|
82
|
+
match target:
|
|
83
|
+
case EntityTarget(entity=entity):
|
|
84
|
+
kind, name, key = "entity", entity.name, key_names(entity)
|
|
85
|
+
case DatasetTarget(dataset=dataset):
|
|
86
|
+
kind, name = "dataset", dataset.name
|
|
87
|
+
key = list(dataset.key.fields()) if dataset.key is not None else []
|
|
88
|
+
written = path.sql(dialect)
|
|
89
|
+
message = (
|
|
90
|
+
f"Unsupported query - {written} is a relationship to the {kind} {name.sql(dialect)}, "
|
|
91
|
+
"not a value: read one of its fields"
|
|
92
|
+
)
|
|
93
|
+
if not key:
|
|
94
|
+
return f"{message}, e.g. {written}.<field>"
|
|
95
|
+
fields = ", ".join(f"{written}.{part.sql(dialect)}" for part in key)
|
|
96
|
+
return f"{message}, e.g. its key, {fields}"
|
|
97
|
+
|
|
98
|
+
|
|
69
99
|
def _unnested_path(select: exp.Select) -> exp.Column | exp.Dot | None:
|
|
70
100
|
"""The path `select` unnests, e.g. `c.store_sales` in `SELECT 1 FROM UNNEST(c.store_sales)`."""
|
|
71
101
|
match select.args.get("from_"):
|
|
@@ -463,17 +493,18 @@ class ExpandDatasetsRule:
|
|
|
463
493
|
select.set("order", order)
|
|
464
494
|
# The query's PERs, calculations' included, come first: the fan-out dedup is a PER on the
|
|
465
495
|
# scan's aggregates.
|
|
466
|
-
scan = expand_select(select, ctx.dialect)
|
|
496
|
+
grouped, scan = expand_select(select, ctx.dialect)
|
|
467
497
|
self._fanout_per(scan.expressions, ctx)
|
|
468
498
|
if havings:
|
|
469
499
|
# HAVING runs before windows, unlike a filter around the query, and its fan-out PERs
|
|
470
500
|
# must be expanded first, since only projections are.
|
|
471
|
-
expand_select(
|
|
501
|
+
expand_select(grouped, ctx.dialect)
|
|
472
502
|
names = {having.alias for having in havings}
|
|
473
|
-
hidden = [p for p in
|
|
474
|
-
|
|
503
|
+
hidden = [p for p in grouped.expressions if p.alias in names]
|
|
504
|
+
for query in (select, grouped):
|
|
505
|
+
query.set("expressions", [p for p in query.expressions if p.alias not in names])
|
|
475
506
|
condition = exp.and_(*(p.this for p in hidden), copy=False)
|
|
476
|
-
|
|
507
|
+
grouped.set("having", exp.Having(this=condition))
|
|
477
508
|
return select
|
|
478
509
|
|
|
479
510
|
def _spine(
|
|
@@ -655,7 +686,7 @@ class ExpandDatasetsRule:
|
|
|
655
686
|
if join.side in ("RIGHT", "FULL"):
|
|
656
687
|
ctx.outer_joins.add(alias)
|
|
657
688
|
on = join.args.get("on")
|
|
658
|
-
terms = [] if on is None else
|
|
689
|
+
terms = [] if on is None else conjuncts(on)
|
|
659
690
|
parents = self._key_matched(ctx, terms, dataset, alias) if dataset is not None else None
|
|
660
691
|
if parents is not None:
|
|
661
692
|
ctx.to_one_joins[alias] = next(iter(parents)) if len(parents) == 1 else None
|
|
@@ -701,7 +732,7 @@ class ExpandDatasetsRule:
|
|
|
701
732
|
"""
|
|
702
733
|
Writes each aggregate of the scan that reads a one-to-many join, or a joined dataset's
|
|
703
734
|
own calculation like AGG(store.total_employees) FROM store_sales, as a PER by its grain's
|
|
704
|
-
key, SUM(x) becomes SUM(
|
|
735
|
+
key, SUM(x) becomes SUM(ANY_VALUE(x) PER (key)), so expand_per counts each row of the grain once.
|
|
705
736
|
The query's own joins are read as generated ones are, and a permission's `having` counts
|
|
706
737
|
its own source's rows whenever a join repeats them.
|
|
707
738
|
"""
|
|
@@ -758,7 +789,7 @@ class ExpandDatasetsRule:
|
|
|
758
789
|
)
|
|
759
790
|
if grain_alias == unrepeated:
|
|
760
791
|
continue
|
|
761
|
-
#
|
|
792
|
+
# ANY_VALUE(x) PER (key) keeps each row's value, not the order an aggregate reads rows in.
|
|
762
793
|
ordered = isinstance(aggregate.this, (exp.Order, exp.Limit, exp.HavingMax))
|
|
763
794
|
within_group = isinstance(aggregate.parent, exp.WithinGroup)
|
|
764
795
|
if ordered or within_group:
|
|
@@ -792,9 +823,15 @@ class ExpandDatasetsRule:
|
|
|
792
823
|
value = exp.case().when(
|
|
793
824
|
key[0].copy().is_(exp.null()).not_(copy=False), then=value, copy=False
|
|
794
825
|
)
|
|
795
|
-
|
|
826
|
+
# A key's rows are copies of one row, so ANY_VALUE can return any of them, and
|
|
827
|
+
# unlike MAX it takes every type. A filter turns rows into NULLs that ANY_VALUE
|
|
828
|
+
# may return, so a filtered value uses MAX.
|
|
829
|
+
inner: exp.Expr = exp.AnyValue(this=value)
|
|
796
830
|
if isinstance(aggregate.parent, exp.Filter):
|
|
797
|
-
inner = exp.Filter(
|
|
831
|
+
inner = exp.Filter(
|
|
832
|
+
this=exp.func("MAX", value, copy=False),
|
|
833
|
+
expression=aggregate.parent.expression,
|
|
834
|
+
)
|
|
798
835
|
aggregate.parent.replace(aggregate)
|
|
799
836
|
per = exp.Per(this=inner, expressions=[column.copy() for column in key])
|
|
800
837
|
aggregate.set("this", exp.Distinct(expressions=[per]) if distinct else per)
|
|
@@ -980,7 +1017,7 @@ class ExpandDatasetsRule:
|
|
|
980
1017
|
return ordered
|
|
981
1018
|
|
|
982
1019
|
def _join_terms(self, expression: exp.Expr, dialect: DialectType) -> set[exp.Expr]:
|
|
983
|
-
return set(
|
|
1020
|
+
return set(conjuncts(simplify(expression, dialect=dialect)))
|
|
984
1021
|
|
|
985
1022
|
def _warn_unverified_join(
|
|
986
1023
|
self,
|
|
@@ -1243,6 +1280,10 @@ class ExpandDatasetsRule:
|
|
|
1243
1280
|
expanded = recurse(node, fill)
|
|
1244
1281
|
if expanded is node or isinstance(node, exp.Ordered | exp.Query):
|
|
1245
1282
|
return expanded
|
|
1283
|
+
# A field a constellation permission's condition reads is still the permission's
|
|
1284
|
+
# once expanded, so expand_per can tell its condition from the query's.
|
|
1285
|
+
if node.meta.get(TRUSTED):
|
|
1286
|
+
trust(expanded)
|
|
1246
1287
|
|
|
1247
1288
|
# Like SQLGlot alias expansion, preserve grouping (c * 2 -> (a + b) * 2)
|
|
1248
1289
|
# and let its simplifier remove parentheses that the surrounding SQL doesn't need.
|
|
@@ -1334,27 +1375,23 @@ class ExpandDatasetsRule:
|
|
|
1334
1375
|
if not remaining:
|
|
1335
1376
|
if not key:
|
|
1336
1377
|
return exp.null()
|
|
1337
|
-
# A
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
"Unsupported query - PER by a one-to-many relationship",
|
|
1343
|
-
expr,
|
|
1344
|
-
)
|
|
1345
|
-
values = self._key_values(
|
|
1346
|
-
ctx, field, per_target, dataset, dataset_alias
|
|
1378
|
+
# A bare relationship has no single value: name its key fields.
|
|
1379
|
+
bare_target, _ = self._relationship_target(ctx, field)
|
|
1380
|
+
self._fail(
|
|
1381
|
+
ctx,
|
|
1382
|
+
bare_relationship_error(expr, bare_target, ctx.registry.dialect),
|
|
1347
1383
|
)
|
|
1348
|
-
return exp.Tuple(expressions=list(values.values()))
|
|
1349
1384
|
match self._relationship_target(ctx, field):
|
|
1350
1385
|
case EntityTarget(entity=target_entity), target_cardinality:
|
|
1351
1386
|
self._reach_entity(ctx, target_entity)
|
|
1352
1387
|
relationship, entity = field, target_entity
|
|
1353
1388
|
cardinality = target_cardinality
|
|
1354
1389
|
case DatasetTarget(dataset=target), cardinality:
|
|
1355
|
-
#
|
|
1390
|
+
# If a row always matches, its key field is our key value: no
|
|
1391
|
+
# join. Otherwise join, so it's NULL when no row matches.
|
|
1356
1392
|
if (
|
|
1357
1393
|
cardinality is Cardinality.MANY_TO_ONE
|
|
1394
|
+
and field.always_matches
|
|
1358
1395
|
and len(remaining) == 1
|
|
1359
1396
|
and remaining[0] in field.key
|
|
1360
1397
|
and target.get_column(remaining[0]) is not None
|
|
@@ -1368,6 +1405,11 @@ class ExpandDatasetsRule:
|
|
|
1368
1405
|
dataset_alias,
|
|
1369
1406
|
physical=False,
|
|
1370
1407
|
)
|
|
1408
|
+
# Like a foreign key, a key with a NULL part matches nothing.
|
|
1409
|
+
if value is not None:
|
|
1410
|
+
value = self._unless_key_has_null(
|
|
1411
|
+
ctx, field, target, value, dataset, dataset_alias
|
|
1412
|
+
)
|
|
1371
1413
|
# Read without a join, but filled from the target.
|
|
1372
1414
|
if fill is not None:
|
|
1373
1415
|
self._spine(
|
|
@@ -1399,7 +1441,8 @@ class ExpandDatasetsRule:
|
|
|
1399
1441
|
case _:
|
|
1400
1442
|
self._fail(ctx, "Unsupported relationship path", expr)
|
|
1401
1443
|
continue
|
|
1402
|
-
#
|
|
1444
|
+
# An entity's key needs no join, matching or not: an entity is its key. A one-to-many
|
|
1445
|
+
# relationship's rows still need the join.
|
|
1403
1446
|
if name in relationship.key and cardinality is Cardinality.MANY_TO_ONE:
|
|
1404
1447
|
if remaining:
|
|
1405
1448
|
self._fail(ctx, "Unsupported relationship path", expr)
|
|
@@ -1417,7 +1460,11 @@ class ExpandDatasetsRule:
|
|
|
1417
1460
|
attribute = self._get_attribute(ctx, entity, name)
|
|
1418
1461
|
if attribute is None:
|
|
1419
1462
|
entity_name = self._sql(ctx, entity.name)
|
|
1420
|
-
|
|
1463
|
+
message = f"Unknown attribute: {entity_name}.{self._sql(ctx, name)}"
|
|
1464
|
+
if keys := key_names(entity):
|
|
1465
|
+
key_list = ", ".join(self._sql(ctx, key) for key in keys)
|
|
1466
|
+
message += f"; the key of {entity_name} is {key_list}"
|
|
1467
|
+
self._fail(ctx, message)
|
|
1421
1468
|
permission = (
|
|
1422
1469
|
self._permission(ctx, attribute.permissions)
|
|
1423
1470
|
if attribute.id not in ctx.unmasked and not remaining and not ctx.trusted
|
|
@@ -1869,6 +1916,34 @@ class ExpandDatasetsRule:
|
|
|
1869
1916
|
values[name] = value
|
|
1870
1917
|
return values
|
|
1871
1918
|
|
|
1919
|
+
def _unless_key_has_null(
|
|
1920
|
+
self,
|
|
1921
|
+
ctx: ExpandContext,
|
|
1922
|
+
relationship: Relationship,
|
|
1923
|
+
target: DatasetProxy,
|
|
1924
|
+
value: exp.Expr,
|
|
1925
|
+
dataset: DatasetProxy,
|
|
1926
|
+
alias: str,
|
|
1927
|
+
) -> exp.Expr:
|
|
1928
|
+
"""
|
|
1929
|
+
`value`, or NULL where any of the key values `relationship` compares with `=` is NULL. A
|
|
1930
|
+
value a key condition reads, e.g. `:as_of`, may match even when NULL.
|
|
1931
|
+
"""
|
|
1932
|
+
fields = {name.name for name in target.key.fields()} if target.key else set()
|
|
1933
|
+
names = [name for name in relationship.key if name.name in fields]
|
|
1934
|
+
if len(names) < 2:
|
|
1935
|
+
return value
|
|
1936
|
+
parts = [
|
|
1937
|
+
self._relationship_key_value(
|
|
1938
|
+
ctx, relationship, DatasetTarget(target), name, dataset, alias, physical=False
|
|
1939
|
+
)
|
|
1940
|
+
for name in names
|
|
1941
|
+
]
|
|
1942
|
+
conditions = [
|
|
1943
|
+
exp.Not(this=exp.Is(this=part, expression=exp.null())) for part in parts if part
|
|
1944
|
+
]
|
|
1945
|
+
return exp.case().when(exp.and_(*conditions), value)
|
|
1946
|
+
|
|
1872
1947
|
def _relationship_key_value(
|
|
1873
1948
|
self,
|
|
1874
1949
|
ctx: ExpandContext,
|
|
@@ -2003,11 +2078,6 @@ def _repeated_by_user_join(ctx: ExpandContext, alias: str) -> bool:
|
|
|
2003
2078
|
return alias in ctx.to_one_joins or alias in ctx.fanout_joins
|
|
2004
2079
|
|
|
2005
2080
|
|
|
2006
|
-
def _conjuncts(condition: exp.Expr) -> list[exp.Expr]:
|
|
2007
|
-
unnested = condition.unnest()
|
|
2008
|
-
return list(unnested.flatten()) if isinstance(unnested, exp.And) else [unnested]
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
2081
|
def _each_row(having: exp.Expr) -> exp.Expr:
|
|
2012
2082
|
"""
|
|
2013
2083
|
A query that doesn't aggregate treats each row as its own group. An aggregate of the outer
|