metricflow-sql 0.5.1__tar.gz → 0.5.2__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.1 → metricflow_sql-0.5.2}/PKG-INFO +3 -2
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/pyproject.toml +5 -2
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/compiler/catalog/cluster.py +21 -1
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/compiler/catalog/passthrough.py +20 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/compiler/compiler.py +72 -4
- metricflow_sql-0.5.2/src/metricflow_sql/compiler/guard.py +66 -0
- metricflow_sql-0.5.2/src/metricflow_sql/compiler/rules/constellation_permissions.py +78 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/compiler/rules/expand_constellations.py +221 -27
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/compiler/rules/expand_datasets.py +732 -71
- metricflow_sql-0.5.2/src/metricflow_sql/compiler/rules/expand_fill.py +344 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/compiler/rules/expand_per.py +174 -34
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/compiler/rules/isolate_fragments.py +25 -6
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/compiler/rules/passthrough_tables.py +6 -1
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/converters/legacy.py +162 -49
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/converters/lookml.py +287 -49
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/objects/base.py +76 -14
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/objects/constellation.py +23 -15
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/objects/dataset.py +26 -8
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/objects/entity.py +14 -9
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/objects/wiki.py +1 -1
- metricflow_sql-0.5.2/src/metricflow_sql/registry/dao.py +181 -0
- metricflow_sql-0.5.2/src/metricflow_sql/registry/dependency_resolution.py +303 -0
- metricflow_sql-0.5.2/src/metricflow_sql/registry/memory.py +878 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/registry/reference_resolution.py +30 -17
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/registry/registry.py +98 -10
- metricflow_sql-0.5.2/src/metricflow_sql/registry/resolved.py +137 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/registry/type_resolver.py +11 -16
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/schemas/bundle.py +16 -21
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/schemas/fragment.schema.json +347 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/schemas/load.py +118 -4
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/schemas/v1/__init__.py +6 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/schemas/v1/fragment.py +86 -9
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/server/registry.py +72 -15
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/server/serde.py +33 -14
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/utils/sql.py +70 -12
- metricflow_sql-0.5.1/src/metricflow_sql/registry/memory.py +0 -477
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/.gitignore +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/LICENSE +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/PYPI.md +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/README.md +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/__init__.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/compiler/__init__.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/compiler/catalog/__init__.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/compiler/catalog/information_schema.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/compiler/catalog/metadata.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/compiler/catalog/protocols.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/compiler/catalog/semantic.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/compiler/rules/__init__.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/compiler/rules/expand_metadata.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/compiler/table_arguments.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/converters/__init__.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/objects/__init__.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/objects/reference.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/py.typed +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/registry/__init__.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/registry/operation.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/schemas/__init__.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/schemas/generate.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/schemas/markdown.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/server/__init__.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/server/app.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/server/compile.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/server/errors.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/server/http.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/server/serve.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/utils/__init__.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/utils/composed_set.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/utils/normalize.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/utils/ordered_set.py +0 -0
- {metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/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.2
|
|
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,11 +12,12 @@ 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.31.0
|
|
16
16
|
Requires-Dist: wenmode>=0.15.2
|
|
17
17
|
Provides-Extra: lookml
|
|
18
18
|
Requires-Dist: lkml>=1.3.7; extra == 'lookml'
|
|
19
19
|
Provides-Extra: server
|
|
20
|
+
Requires-Dist: ruamel-yaml>=0.18; extra == 'server'
|
|
20
21
|
Requires-Dist: starlette>=0.47.0; extra == 'server'
|
|
21
22
|
Requires-Dist: uvicorn>=0.34.0; extra == 'server'
|
|
22
23
|
Provides-Extra: yaml
|
|
@@ -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.2"
|
|
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.31.0",
|
|
21
21
|
"wenmode>=0.15.2",
|
|
22
22
|
]
|
|
23
23
|
|
|
@@ -31,7 +31,10 @@ lookml = [
|
|
|
31
31
|
yaml = [
|
|
32
32
|
"ruamel-yaml>=0.18",
|
|
33
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.
|
|
34
36
|
server = [
|
|
37
|
+
"ruamel-yaml>=0.18",
|
|
35
38
|
"starlette>=0.47.0",
|
|
36
39
|
"uvicorn>=0.34.0",
|
|
37
40
|
]
|
{metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/compiler/catalog/cluster.py
RENAMED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import copy
|
|
3
4
|
from collections.abc import Callable, Sequence
|
|
4
5
|
from typing import TYPE_CHECKING, override
|
|
5
6
|
|
|
@@ -16,6 +17,9 @@ from metricflow_sql.utils.normalize import exact_identifier, normalizing_dialect
|
|
|
16
17
|
if TYPE_CHECKING:
|
|
17
18
|
from sqlglot.schema import ColumnMapping
|
|
18
19
|
|
|
20
|
+
# Whether the caller can read a field of a table, e.g. one of a dataset's.
|
|
21
|
+
type Allows = Callable[[exp.Table, exp.Identifier, ColumnLike], bool]
|
|
22
|
+
|
|
19
23
|
|
|
20
24
|
class Cluster(Schema):
|
|
21
25
|
"""
|
|
@@ -46,6 +50,13 @@ class Cluster(Schema):
|
|
|
46
50
|
self._fallback = fallback
|
|
47
51
|
self._fallen_back: dict[str, CatalogLike | None] = {}
|
|
48
52
|
self._normalized_names: dict[str, str] = {}
|
|
53
|
+
self.allows: Allows | None = None
|
|
54
|
+
|
|
55
|
+
def with_allows(self, allows: Allows | None) -> Cluster:
|
|
56
|
+
"""A copy whose `SELECT *` leaves out the fields the caller can't read."""
|
|
57
|
+
cluster = copy.copy(self)
|
|
58
|
+
cluster.allows = allows
|
|
59
|
+
return cluster
|
|
49
60
|
|
|
50
61
|
def get_catalog(self, name: str) -> CatalogLike | None:
|
|
51
62
|
"""The catalog named `name`, which is normalized: one of `catalogs`, else the fallback's."""
|
|
@@ -134,7 +145,16 @@ class Cluster(Schema):
|
|
|
134
145
|
dialect: DialectType = None,
|
|
135
146
|
normalize: bool | None = None,
|
|
136
147
|
) -> Sequence[str]:
|
|
137
|
-
|
|
148
|
+
found = self.get_table(table)
|
|
149
|
+
names = found.column_names()
|
|
150
|
+
if only_visible and (allows := self.allows) is not None:
|
|
151
|
+
qualified = exp.to_table(table, dialect=self._dialect)
|
|
152
|
+
names = [
|
|
153
|
+
name
|
|
154
|
+
for name in names
|
|
155
|
+
if (field := found.get_column(name)) is None or allows(qualified, name, field)
|
|
156
|
+
]
|
|
157
|
+
return [name.name for name in names]
|
|
138
158
|
|
|
139
159
|
@override
|
|
140
160
|
def get_column_type(
|
{metricflow_sql-0.5.1 → metricflow_sql-0.5.2}/src/metricflow_sql/compiler/catalog/passthrough.py
RENAMED
|
@@ -26,6 +26,26 @@ class PhysicalTable:
|
|
|
26
26
|
columns: Mapping[str, MetadataColumn]
|
|
27
27
|
|
|
28
28
|
|
|
29
|
+
@dataclass(frozen=True)
|
|
30
|
+
class WarehouseDefaults:
|
|
31
|
+
"""The catalog and schema a warehouse connection reads a table name's missing parts from."""
|
|
32
|
+
|
|
33
|
+
catalog: exp.Identifier
|
|
34
|
+
db: exp.Identifier | None = None
|
|
35
|
+
|
|
36
|
+
def qualify(self, table: exp.Table) -> None:
|
|
37
|
+
"""
|
|
38
|
+
`shop.orders` -> `analytics.shop.orders`. A part with no default stays missing, as the
|
|
39
|
+
warehouse can't read the table either.
|
|
40
|
+
"""
|
|
41
|
+
if not table.args.get("db"):
|
|
42
|
+
if self.db is None:
|
|
43
|
+
return
|
|
44
|
+
table.set("db", self.db.copy())
|
|
45
|
+
if not table.args.get("catalog"):
|
|
46
|
+
table.set("catalog", self.catalog.copy())
|
|
47
|
+
|
|
48
|
+
|
|
29
49
|
class TableLoader(Protocol):
|
|
30
50
|
"""Looks up warehouse tables for a `PassthroughCatalog`."""
|
|
31
51
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import functools
|
|
3
4
|
import inspect
|
|
4
5
|
import logging
|
|
5
6
|
from collections.abc import Mapping, Sequence
|
|
@@ -8,6 +9,7 @@ from dataclasses import dataclass
|
|
|
8
9
|
from sqlglot import Schema, parse
|
|
9
10
|
from sqlglot import expressions as exp
|
|
10
11
|
from sqlglot.dialects.dialect import DialectType, NormalizationStrategy
|
|
12
|
+
from sqlglot.errors import SqlglotError
|
|
11
13
|
from sqlglot.helper import find_new_name
|
|
12
14
|
from sqlglot.optimizer.normalize_identifiers import normalize_identifiers
|
|
13
15
|
from sqlglot.optimizer.optimizer import OptimizerFn
|
|
@@ -21,9 +23,14 @@ from sqlglot.optimizer.scope import traverse_scope
|
|
|
21
23
|
|
|
22
24
|
from metricflow_sql.compiler.catalog.cluster import Cluster
|
|
23
25
|
from metricflow_sql.compiler.catalog.metadata import INFORMATION_SCHEMA
|
|
26
|
+
from metricflow_sql.compiler.catalog.passthrough import WarehouseDefaults
|
|
27
|
+
from metricflow_sql.compiler.catalog.protocols import ColumnLike
|
|
24
28
|
from metricflow_sql.compiler.catalog.semantic import SemanticCatalog
|
|
29
|
+
from metricflow_sql.compiler.guard import Denied, Guard
|
|
30
|
+
from metricflow_sql.compiler.rules.constellation_permissions import apply_constellation_permissions
|
|
25
31
|
from metricflow_sql.compiler.rules.expand_constellations import ExpandConstellationsRule
|
|
26
32
|
from metricflow_sql.compiler.rules.expand_datasets import ExpandDatasetsRule
|
|
33
|
+
from metricflow_sql.compiler.rules.expand_fill import expand_fills
|
|
27
34
|
from metricflow_sql.compiler.rules.expand_metadata import ENGINE, ExpandMetadataRule
|
|
28
35
|
from metricflow_sql.compiler.rules.expand_per import expand_per
|
|
29
36
|
from metricflow_sql.compiler.rules.isolate_fragments import isolate_fragments
|
|
@@ -83,12 +90,14 @@ ANALYZER_RULES: Sequence[OptimizerFn] = (
|
|
|
83
90
|
)
|
|
84
91
|
|
|
85
92
|
PLANNER_RULES: Sequence[OptimizerFn] = (
|
|
93
|
+
apply_constellation_permissions,
|
|
86
94
|
isolate_fragments,
|
|
87
95
|
name_passthrough_tables,
|
|
88
96
|
ExpandMetadataRule(),
|
|
89
97
|
ExpandConstellationsRule(),
|
|
90
98
|
ExpandDatasetsRule(),
|
|
91
99
|
expand_per,
|
|
100
|
+
expand_fills,
|
|
92
101
|
)
|
|
93
102
|
|
|
94
103
|
|
|
@@ -130,9 +139,16 @@ class Compiler:
|
|
|
130
139
|
positional_columns: bool = False,
|
|
131
140
|
with_tables: bool = False,
|
|
132
141
|
db: str | None = None,
|
|
142
|
+
guard: Guard | None = None,
|
|
143
|
+
warehouse: WarehouseDefaults | None = None,
|
|
133
144
|
) -> Plan:
|
|
134
145
|
"""Compiles `sql`.
|
|
135
146
|
|
|
147
|
+
`guard` restricts what the query can read. Without one, nothing is restricted.
|
|
148
|
+
|
|
149
|
+
`warehouse` completes the names of the tables datasets read, so the compiled SQL names
|
|
150
|
+
each table in full. Without it, the warehouse completes them as it runs the SQL.
|
|
151
|
+
|
|
136
152
|
`db` names the db unqualified tables are in, as a client gave it, e.g. a Trino session
|
|
137
153
|
schema `Sales`, instead of the compiler's. It's normalized as a name in `sql` is, quoted
|
|
138
154
|
only if it must be, e.g. `my-schema`.
|
|
@@ -175,6 +191,11 @@ class Compiler:
|
|
|
175
191
|
case _:
|
|
176
192
|
raise ValueError("Only one query statement is supported")
|
|
177
193
|
|
|
194
|
+
if guard is not None:
|
|
195
|
+
for expression in expressions:
|
|
196
|
+
if expression is not None:
|
|
197
|
+
_drop_comment_meta(expression)
|
|
198
|
+
|
|
178
199
|
out = []
|
|
179
200
|
tables: dict[tuple[str, ...], None] = {}
|
|
180
201
|
engines: set[str | None] = set()
|
|
@@ -184,7 +205,11 @@ class Compiler:
|
|
|
184
205
|
if not expression:
|
|
185
206
|
continue
|
|
186
207
|
self._prepare(expression, input_dialect, default_db, read_only=read_only)
|
|
187
|
-
|
|
208
|
+
if guard is not None:
|
|
209
|
+
guard.check_query(expression)
|
|
210
|
+
planned = self._apply_rules(expression, input_dialect, default_db, guard, warehouse)
|
|
211
|
+
if guard is not None:
|
|
212
|
+
guard.check_plan(planned, self.cluster)
|
|
188
213
|
engine = planned.meta.get(ENGINE)
|
|
189
214
|
engines.add(engine)
|
|
190
215
|
if len(engines) > 1:
|
|
@@ -211,10 +236,21 @@ class Compiler:
|
|
|
211
236
|
tables=tuple(tables) if with_tables else None,
|
|
212
237
|
)
|
|
213
238
|
|
|
214
|
-
def _apply_rules(
|
|
239
|
+
def _apply_rules(
|
|
240
|
+
self,
|
|
241
|
+
expression: exp.Expr,
|
|
242
|
+
dialect: DialectType,
|
|
243
|
+
db: str | None,
|
|
244
|
+
guard: Guard | None,
|
|
245
|
+
warehouse: WarehouseDefaults | None,
|
|
246
|
+
) -> exp.Expr:
|
|
247
|
+
allows = None if guard is None else functools.partial(self._allows, guard=guard)
|
|
248
|
+
cluster = self.cluster.with_allows(allows)
|
|
215
249
|
arguments = {
|
|
216
|
-
"schema":
|
|
217
|
-
"cluster":
|
|
250
|
+
"schema": cluster,
|
|
251
|
+
"cluster": cluster,
|
|
252
|
+
"guard": guard,
|
|
253
|
+
"warehouse": warehouse,
|
|
218
254
|
"dialect": dialect,
|
|
219
255
|
"catalog": self.catalog,
|
|
220
256
|
"db": db,
|
|
@@ -226,6 +262,25 @@ class Compiler:
|
|
|
226
262
|
break
|
|
227
263
|
return expression
|
|
228
264
|
|
|
265
|
+
def _allows(
|
|
266
|
+
self, table: exp.Table, name: exp.Identifier, field: ColumnLike, *, guard: Guard
|
|
267
|
+
) -> bool:
|
|
268
|
+
"""
|
|
269
|
+
Whether `SELECT *` includes `field`: the caller can read it, as its expansion asks the
|
|
270
|
+
guard. Another error is left to the query, which reads the field and fails the same way.
|
|
271
|
+
"""
|
|
272
|
+
try:
|
|
273
|
+
if (found := self.cluster.get_dataset(table)) is not None:
|
|
274
|
+
ExpandDatasetsRule.read(*found, name, guard)
|
|
275
|
+
elif (found_constellation := self.cluster.get_constellation(table)) is not None:
|
|
276
|
+
registry, constellation = found_constellation
|
|
277
|
+
ExpandConstellationsRule.read(registry, constellation, field, self.cluster, guard)
|
|
278
|
+
except Denied:
|
|
279
|
+
return False
|
|
280
|
+
except (ValueError, SqlglotError):
|
|
281
|
+
return True
|
|
282
|
+
return True
|
|
283
|
+
|
|
229
284
|
def _dialect(self, engine: str | None, output_dialect: DialectType) -> DialectType:
|
|
230
285
|
if engine is None:
|
|
231
286
|
return output_dialect
|
|
@@ -260,6 +315,19 @@ class Compiler:
|
|
|
260
315
|
return False
|
|
261
316
|
|
|
262
317
|
|
|
318
|
+
_POSITION_KEYS = frozenset(("line", "col", "start", "end"))
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
def _drop_comment_meta(expression: exp.Expr) -> None:
|
|
322
|
+
"""
|
|
323
|
+
A `/* sqlglot.meta key=value */` comment sets node meta, which the rules trust, so a query
|
|
324
|
+
could mark its own tables, e.g. as governed.
|
|
325
|
+
"""
|
|
326
|
+
for node in expression.walk():
|
|
327
|
+
if node._meta:
|
|
328
|
+
node._meta = {key: value for key, value in node._meta.items() if key in _POSITION_KEYS}
|
|
329
|
+
|
|
330
|
+
|
|
263
331
|
def _physical_tables(expression: exp.Expr) -> list[tuple[str, ...]]:
|
|
264
332
|
tables = [
|
|
265
333
|
source
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The seam for access control. metricflow-sql applies the permissions a `Guard` picks, and asks it
|
|
3
|
+
about each object and field a query reads as it expands them, but enforces nothing itself: a
|
|
4
|
+
compile without a guard is unrestricted.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import logging
|
|
10
|
+
from typing import TYPE_CHECKING, Protocol
|
|
11
|
+
|
|
12
|
+
from sqlglot import exp
|
|
13
|
+
|
|
14
|
+
from metricflow_sql.objects.base import Permission
|
|
15
|
+
from metricflow_sql.registry.registry import DatasetProxy
|
|
16
|
+
|
|
17
|
+
if TYPE_CHECKING:
|
|
18
|
+
from metricflow_sql.compiler.catalog.cluster import Cluster
|
|
19
|
+
|
|
20
|
+
logger = logging.getLogger(__name__)
|
|
21
|
+
|
|
22
|
+
# Constellation expansion runs first, so it marks the tables whose dataset permissions it replaced.
|
|
23
|
+
GOVERNED = "governed"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# Set on each node of a permission's own condition, which reads what its author chose, unmasked,
|
|
27
|
+
# whatever the caller can use.
|
|
28
|
+
TRUSTED = "trusted"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class Denied(ValueError):
|
|
32
|
+
"""What a guard raises for something the caller can't use."""
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class Guard(Protocol):
|
|
36
|
+
def permission(self, permissions: tuple[Permission, ...]) -> Permission | None:
|
|
37
|
+
"""The one of `permissions` whose `where`, `having` and `mask` apply, if any."""
|
|
38
|
+
...
|
|
39
|
+
|
|
40
|
+
def governs(self, permissions: tuple[Permission, ...]) -> bool:
|
|
41
|
+
"""Whether an object's own permissions replace those of the datasets it reads."""
|
|
42
|
+
...
|
|
43
|
+
|
|
44
|
+
def require(self, permissions: tuple[Permission, ...], name: exp.Expr) -> None:
|
|
45
|
+
"""Raises `Denied` unless the caller can use the object or field `name`."""
|
|
46
|
+
...
|
|
47
|
+
|
|
48
|
+
def check_dataset(self, dataset: DatasetProxy) -> None:
|
|
49
|
+
"""For each dataset read other than through an object whose permissions replace its."""
|
|
50
|
+
...
|
|
51
|
+
|
|
52
|
+
def check_query(self, expression: exp.Expr) -> None:
|
|
53
|
+
"""Before planning, once table function arguments are bound into their tables' meta."""
|
|
54
|
+
...
|
|
55
|
+
|
|
56
|
+
def check_table(self, table: exp.Table) -> None:
|
|
57
|
+
"""For each passthrough table, before it's renamed to the warehouse's name."""
|
|
58
|
+
...
|
|
59
|
+
|
|
60
|
+
def check_scope(self, select: exp.Select, aliases: set[str]) -> None:
|
|
61
|
+
"""For each select of datasets, by their aliases, before it's expanded."""
|
|
62
|
+
...
|
|
63
|
+
|
|
64
|
+
def check_plan(self, expression: exp.Expr, cluster: Cluster) -> None:
|
|
65
|
+
"""After planning, when every table the plan reads should be a warehouse table."""
|
|
66
|
+
...
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
|
|
5
|
+
from sqlglot import exp
|
|
6
|
+
from sqlglot.optimizer.scope import find_in_scope
|
|
7
|
+
|
|
8
|
+
from metricflow_sql.compiler.catalog.cluster import Cluster
|
|
9
|
+
from metricflow_sql.compiler.catalog.semantic import SemanticCatalog
|
|
10
|
+
from metricflow_sql.compiler.guard import TRUSTED, Guard
|
|
11
|
+
from metricflow_sql.utils.sql import column_path, path_names, table_scopes
|
|
12
|
+
|
|
13
|
+
logger = logging.getLogger(__name__)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def apply_constellation_permissions(
|
|
17
|
+
expression: exp.Expr, cluster: Cluster, guard: Guard | None = None
|
|
18
|
+
) -> exp.Expr:
|
|
19
|
+
"""
|
|
20
|
+
ANDs the `where` and `having` of the permission that applies to each constellation query
|
|
21
|
+
into its WHERE and HAVING. Runs before isolate_fragments, so they expand like the query's own.
|
|
22
|
+
|
|
23
|
+
SELECT store, AGG(quantity) FROM sales GROUP BY 1
|
|
24
|
+
=>
|
|
25
|
+
SELECT store, AGG(quantity) FROM sales WHERE sales.store = 1 GROUP BY 1
|
|
26
|
+
HAVING AGG(sales.quantity) > 100
|
|
27
|
+
"""
|
|
28
|
+
if guard is None:
|
|
29
|
+
return expression
|
|
30
|
+
for _scope, select, table in table_scopes(expression):
|
|
31
|
+
found = cluster.get_constellation(table)
|
|
32
|
+
if found is None or not isinstance(cluster.get_catalog(table.catalog), SemanticCatalog):
|
|
33
|
+
continue
|
|
34
|
+
registry, constellation = found
|
|
35
|
+
permission = guard.permission(constellation.permissions)
|
|
36
|
+
if permission is None:
|
|
37
|
+
continue
|
|
38
|
+
alias = exp.to_identifier(table.alias_or_name, quoted=True)
|
|
39
|
+
if permission.where is not None:
|
|
40
|
+
# The permission's filter goes first, so the query's own condition can't raise an
|
|
41
|
+
# error on rows the caller isn't allowed to see.
|
|
42
|
+
where = select.args.get("where")
|
|
43
|
+
rows = trusted_condition(permission.where, alias)
|
|
44
|
+
condition = exp.and_(rows, *([where.this] if where else []), copy=False)
|
|
45
|
+
select.set("where", exp.Where(this=condition))
|
|
46
|
+
if permission.having is not None:
|
|
47
|
+
if not select.args.get("group") and not find_in_scope(select, exp.AggFunc):
|
|
48
|
+
raise ValueError(
|
|
49
|
+
f"{constellation.name.sql(registry.dialect)} can only be queried with "
|
|
50
|
+
"aggregates: its permission has `having`"
|
|
51
|
+
)
|
|
52
|
+
having = select.args.get("having")
|
|
53
|
+
results = trusted_condition(permission.having, alias)
|
|
54
|
+
condition = exp.and_(*([having.this] if having else []), results, copy=False)
|
|
55
|
+
select.set("having", exp.Having(this=condition))
|
|
56
|
+
return expression
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def trusted_condition(condition: exp.Expr, alias: exp.Identifier) -> exp.Expr:
|
|
60
|
+
"""
|
|
61
|
+
A permission's `condition`, qualified as a query of the constellation `alias` qualifies its
|
|
62
|
+
own, and marked `TRUSTED`.
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
def qualify(node: exp.Expr) -> exp.Expr:
|
|
66
|
+
# Only the outermost node of `customer.birth_year`, not each dot inside it.
|
|
67
|
+
if (
|
|
68
|
+
isinstance(node, exp.Column | exp.Dot)
|
|
69
|
+
and not isinstance(node.parent, exp.Dot)
|
|
70
|
+
and node.find(exp.Column) is not None
|
|
71
|
+
):
|
|
72
|
+
return column_path(alias.copy(), *(name.copy() for name in path_names(node)))
|
|
73
|
+
return node
|
|
74
|
+
|
|
75
|
+
qualified = condition.transform(qualify)
|
|
76
|
+
for node in qualified.walk():
|
|
77
|
+
node.meta[TRUSTED] = True
|
|
78
|
+
return qualified
|