metricflow-sql 0.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. metricflow_sql/__init__.py +8 -0
  2. metricflow_sql/compiler/__init__.py +0 -0
  3. metricflow_sql/compiler/catalog/__init__.py +0 -0
  4. metricflow_sql/compiler/catalog/cluster.py +152 -0
  5. metricflow_sql/compiler/catalog/information_schema.py +47 -0
  6. metricflow_sql/compiler/catalog/metadata.py +65 -0
  7. metricflow_sql/compiler/catalog/passthrough.py +184 -0
  8. metricflow_sql/compiler/catalog/protocols.py +67 -0
  9. metricflow_sql/compiler/catalog/semantic.py +59 -0
  10. metricflow_sql/compiler/compiler.py +297 -0
  11. metricflow_sql/compiler/rules/__init__.py +0 -0
  12. metricflow_sql/compiler/rules/expand_constellations.py +790 -0
  13. metricflow_sql/compiler/rules/expand_datasets.py +1208 -0
  14. metricflow_sql/compiler/rules/expand_metadata.py +88 -0
  15. metricflow_sql/compiler/rules/isolate_fragments.py +244 -0
  16. metricflow_sql/compiler/rules/passthrough_tables.py +36 -0
  17. metricflow_sql/compiler/table_arguments.py +35 -0
  18. metricflow_sql/converters/__init__.py +0 -0
  19. metricflow_sql/converters/lookml.py +1041 -0
  20. metricflow_sql/objects/__init__.py +0 -0
  21. metricflow_sql/objects/base.py +308 -0
  22. metricflow_sql/objects/constellation.py +210 -0
  23. metricflow_sql/objects/dataset.py +275 -0
  24. metricflow_sql/objects/entity.py +131 -0
  25. metricflow_sql/objects/reference.py +35 -0
  26. metricflow_sql/objects/wiki.py +101 -0
  27. metricflow_sql/py.typed +0 -0
  28. metricflow_sql/registry/__init__.py +0 -0
  29. metricflow_sql/registry/memory.py +409 -0
  30. metricflow_sql/registry/operation.py +25 -0
  31. metricflow_sql/registry/reference_resolution.py +68 -0
  32. metricflow_sql/registry/registry.py +225 -0
  33. metricflow_sql/registry/type_resolver.py +460 -0
  34. metricflow_sql/schemas/__init__.py +0 -0
  35. metricflow_sql/schemas/bundle.py +327 -0
  36. metricflow_sql/schemas/fragment.schema.json +1165 -0
  37. metricflow_sql/schemas/generate.py +70 -0
  38. metricflow_sql/schemas/load.py +333 -0
  39. metricflow_sql/schemas/v1/__init__.py +51 -0
  40. metricflow_sql/schemas/v1/fragment.py +544 -0
  41. metricflow_sql/server/__init__.py +4 -0
  42. metricflow_sql/server/app.py +33 -0
  43. metricflow_sql/server/compile.py +69 -0
  44. metricflow_sql/server/errors.py +10 -0
  45. metricflow_sql/server/serve.py +161 -0
  46. metricflow_sql/utils/__init__.py +0 -0
  47. metricflow_sql/utils/composed_set.py +32 -0
  48. metricflow_sql/utils/normalize.py +74 -0
  49. metricflow_sql/utils/ordered_set.py +24 -0
  50. metricflow_sql/utils/sql.py +226 -0
  51. metricflow_sql/utils/template.py +57 -0
  52. metricflow_sql-0.1.1.dist-info/METADATA +30 -0
  53. metricflow_sql-0.1.1.dist-info/RECORD +56 -0
  54. metricflow_sql-0.1.1.dist-info/WHEEL +4 -0
  55. metricflow_sql-0.1.1.dist-info/entry_points.txt +2 -0
  56. metricflow_sql-0.1.1.dist-info/licenses/LICENSE +29 -0
@@ -0,0 +1,225 @@
1
+ from __future__ import annotations
2
+
3
+ import logging
4
+ from collections.abc import Collection, Mapping
5
+ from dataclasses import dataclass
6
+ from typing import Protocol
7
+
8
+ from sqlglot import exp
9
+ from sqlglot.dialects.dialect import NormalizationStrategy
10
+ from sqlglot.errors import SchemaError
11
+
12
+ from metricflow_sql.compiler.catalog.protocols import TableLike
13
+ from metricflow_sql.objects.base import Backlink, BaseObject, Id, Link
14
+ from metricflow_sql.objects.constellation import Assignment, Metric, Role
15
+ from metricflow_sql.objects.dataset import (
16
+ Calculation,
17
+ Cardinality,
18
+ Column,
19
+ DatasetKey,
20
+ Parameter,
21
+ Relationship,
22
+ )
23
+ from metricflow_sql.objects.entity import Attribute, KeyColumn, Source
24
+ from metricflow_sql.registry.operation import Batch
25
+
26
+ logger = logging.getLogger(__name__)
27
+
28
+
29
+ @dataclass(frozen=True, slots=True)
30
+ class SearchHit:
31
+ """A chunk of an object matching a search query."""
32
+
33
+ id: Id
34
+ # Heading trail of the matching chunk within its object, as on `Chunk.section`.
35
+ section: tuple[str, ...]
36
+ text: str
37
+ # Relevance, higher is better; only comparable within one search.
38
+ score: float
39
+
40
+
41
+ class Registry(Protocol):
42
+ """Graph of all objects in the semantic layer"""
43
+
44
+ # The main dialect, which the registry's names are read and written in.
45
+ dialect: str
46
+
47
+ @property
48
+ def normalization_strategy(self) -> NormalizationStrategy:
49
+ """
50
+ How the registry's names were normalized when loaded, e.g. `LOWERCASE` reads `Amount` as
51
+ `amount` and `"Amount"` as `Amount`. Queries read names the same way.
52
+ """
53
+ ...
54
+
55
+ def apply(self, batch: Batch) -> None: ...
56
+
57
+ def get_objects(self, ids: Collection[Id]) -> dict[Id, BaseObject | None]:
58
+ """Objects by id; None for ids without an object."""
59
+ ...
60
+
61
+ def get_links(self, ids: Collection[Id]) -> dict[Id, list[Link]]:
62
+ """Outgoing links from each object"""
63
+ ...
64
+
65
+ def get_backlinks(self, ids: Collection[Id]) -> dict[Id, list[Backlink]]:
66
+ """
67
+ Incoming links to each object.
68
+ """
69
+ ...
70
+
71
+ def get_dataset_field_ids(self, dataset: exp.Table) -> list[Id]: ...
72
+
73
+ def search(self, query: str, limit: int = 10) -> list[SearchHit]:
74
+ """Chunks of objects matching `query`, best first."""
75
+ ...
76
+
77
+ def get_dataset(self, name: exp.Table) -> DatasetProxy | None: ...
78
+
79
+ def get_entity(self, name: exp.Table) -> EntityProxy | None: ...
80
+
81
+ def get_constellation(self, name: exp.Table) -> ConstellationProxy | None: ...
82
+
83
+
84
+ class DatasetProxy(TableLike[Column | Calculation | Relationship], Protocol):
85
+ """
86
+ Proxy to a dataset.
87
+
88
+ Datasets are federated into many objects - this acts as if they are a single object.
89
+ """
90
+
91
+ @property
92
+ def name(self) -> exp.Table: ...
93
+
94
+ @property
95
+ def parameters(self) -> dict[exp.Identifier, Parameter]: ...
96
+
97
+ @property
98
+ def key(self) -> DatasetKey | None: ...
99
+
100
+ def from_(self, arguments: dict[str, exp.Expr]) -> exp.Expr:
101
+ """What the dataset reads from, given the query's table function arguments."""
102
+ ...
103
+
104
+ def get_entity_sources(self) -> Mapping[exp.Table, Source]: ...
105
+
106
+
107
+ class EntityProxy(TableLike[Attribute], Protocol):
108
+ """
109
+ Proxy to an entity.
110
+
111
+ Entities are federated into many objects - this acts as if they are a single object.
112
+ """
113
+
114
+ @property
115
+ def name(self) -> exp.Table: ...
116
+
117
+ @property
118
+ def key(self) -> dict[exp.Identifier, KeyColumn]: ...
119
+
120
+ def get_source(self, name: exp.Table) -> Source | None: ...
121
+
122
+
123
+ class ConstellationProxy(TableLike[Metric | Role], Protocol):
124
+ """
125
+ Proxy to a constellation.
126
+
127
+ Constellations are federated into many objects - this acts as if they are a single object.
128
+ """
129
+
130
+ @property
131
+ def name(self) -> exp.Table: ...
132
+
133
+ def get_assignment(self, dataset: exp.Table, role: exp.Identifier) -> Assignment | None:
134
+ """Returns how `dataset` provides `role`, or None if it doesn't."""
135
+ ...
136
+
137
+ def assigned_datasets(self) -> Collection[exp.Table]:
138
+ """Returns the datasets that provide any of the constellation's roles."""
139
+ ...
140
+
141
+
142
+ @dataclass(frozen=True)
143
+ class EntityTarget:
144
+ """A relationship's target that is an entity."""
145
+
146
+ entity: EntityProxy
147
+
148
+
149
+ @dataclass(frozen=True)
150
+ class DatasetTarget:
151
+ """A relationship's target that is a dataset."""
152
+
153
+ dataset: DatasetProxy
154
+
155
+
156
+ # Wrapped, as the proxies are protocols, which a match can't tell apart.
157
+ RelationshipTarget = EntityTarget | DatasetTarget
158
+
159
+
160
+ def relationship_target(registry: Registry, relationship: Relationship) -> RelationshipTarget:
161
+ """
162
+ The entity or dataset `relationship` targets, looked up in that order.
163
+
164
+ Raises `SchemaError` for a constellation, which isn't supported yet, or for no target.
165
+ """
166
+ entity = registry.get_entity(relationship.target)
167
+ if entity is not None:
168
+ return EntityTarget(entity)
169
+ dataset = registry.get_dataset(relationship.target)
170
+ if dataset is not None:
171
+ return DatasetTarget(dataset)
172
+ where = _where(relationship, registry.dialect)
173
+ if registry.get_constellation(relationship.target) is not None:
174
+ raise SchemaError(f"Relationship {where} targets a constellation, not supported yet")
175
+ target = relationship.target.sql(registry.dialect)
176
+ raise SchemaError(f"Unknown target of relationship {where}: {target}")
177
+
178
+
179
+ def _where(relationship: Relationship, dialect: str) -> str:
180
+ """`relationship`'s name for errors, e.g. `public.orders.customer`."""
181
+ return f"{relationship.dataset.sql(dialect)}.{relationship.name.sql(dialect)}"
182
+
183
+
184
+ def relationship_cardinality(
185
+ relationship: Relationship, target: RelationshipTarget, dialect: str
186
+ ) -> Cardinality:
187
+ """
188
+ The cardinality `relationship`'s key implies: many-to-one when it binds all of the target's
189
+ key, else one-to-many. For an entity, the key may bind key columns or attributes, and binding
190
+ its required key columns is enough, e.g. `{sk: ss_customer_sk}` for a `customer` keyed by
191
+ `sk`. For a dataset, it may bind its `DatasetKey` names or fields.
192
+
193
+ Raises `SchemaError` when the key binds anything else, or when the cardinality disagrees with
194
+ a declared one. Names are written in `dialect`, the registry's.
195
+ """
196
+ where = _where(relationship, dialect)
197
+ bound = relationship.key.keys()
198
+ match target:
199
+ case EntityTarget(entity=entity):
200
+ keys = set(entity.key)
201
+ required = {name for name, column in entity.key.items() if not column.optional}
202
+ unknown = [
203
+ name for name in bound if name not in keys and entity.get_column(name) is None
204
+ ]
205
+ allowed = f"key columns or attributes of entity {entity.name.sql(dialect)}"
206
+ case DatasetTarget(dataset=dataset):
207
+ keys = set(dataset.key.names()) if dataset.key is not None else set()
208
+ required = keys
209
+ unknown = [
210
+ name for name in bound if name not in keys and dataset.get_column(name) is None
211
+ ]
212
+ allowed = f"key names or fields of dataset {dataset.name.sql(dialect)}"
213
+ if unknown:
214
+ raise SchemaError(
215
+ f"Relationship {where} binds {sorted(name.name for name in unknown)}, which aren't "
216
+ f"{allowed}"
217
+ )
218
+ covered = bool(keys) and required <= bound
219
+ inferred = Cardinality.MANY_TO_ONE if covered else Cardinality.ONE_TO_MANY
220
+ if relationship.cardinality is not None and relationship.cardinality != inferred:
221
+ raise SchemaError(
222
+ f"Relationship {where} is declared {relationship.cardinality}, but its key makes it "
223
+ f"{inferred}"
224
+ )
225
+ return inferred
@@ -0,0 +1,460 @@
1
+ from __future__ import annotations
2
+
3
+ from collections.abc import Collection, Mapping
4
+ from functools import partial
5
+ from typing import cast, override
6
+
7
+ from sqlglot import MappingSchema, exp
8
+ from sqlglot.dialects.dialect import DialectType
9
+ from sqlglot.errors import SchemaError
10
+ from sqlglot.optimizer.annotate_types import TypeAnnotator, annotate_types
11
+ from sqlglot.optimizer.qualify import qualify
12
+ from sqlglot.optimizer.qualify_columns import qualify_outputs, quote_identifiers
13
+ from sqlglot.optimizer.resolver import Resolver
14
+ from sqlglot.optimizer.scope import find_all_in_scope, traverse_scope
15
+
16
+ from metricflow_sql.objects.base import Id, Prefix, in_namespace, resolve_dataset_path
17
+ from metricflow_sql.objects.constellation import Metric, Role
18
+ from metricflow_sql.objects.dataset import Calculation, Column, Relationship
19
+ from metricflow_sql.objects.entity import Attribute
20
+ from metricflow_sql.registry.reference_resolution import (
21
+ resolve_attribute_references,
22
+ resolve_metric,
23
+ )
24
+ from metricflow_sql.registry.registry import DatasetProxy, EntityProxy, Registry
25
+ from metricflow_sql.utils.normalize import exact_identifier, resolve_name
26
+ from metricflow_sql.utils.ordered_set import OrderedSet
27
+ from metricflow_sql.utils.sql import UNKNOWN, path_names, ref_target, type_or_unknown
28
+
29
+
30
+ class TypeInferenceCycleError(SchemaError):
31
+ """A field's inferred type depends on itself."""
32
+
33
+
34
+ class TypeResolver:
35
+ def __init__(self, registry: Registry) -> None:
36
+ self.registry = registry
37
+
38
+ self._inferred_types: dict[tuple[Id, exp.Table | None], exp.DataType] = {}
39
+ self._schemas: dict[str, InferenceSchema] = {}
40
+ self._type_path: OrderedSet[tuple[Id, exp.Table | None]] = OrderedSet()
41
+
42
+ # Cache expanded STRUCTs separately from inferred types that name entities or datasets.
43
+ self._expanded_types: dict[tuple[exp.Table, int], exp.DataType] = {}
44
+
45
+ def clear(self) -> None:
46
+ self._inferred_types.clear()
47
+ self._schemas.clear()
48
+ self._expanded_types.clear()
49
+
50
+ def infer(self, field_id: Id) -> exp.DataType:
51
+ """Infer from declarations and dependencies; unsupported objects return UNKNOWN."""
52
+ return self._infer(field_id)
53
+
54
+ def expand_type(self, datatype: exp.DataType, *, max_depth: int = 1) -> exp.DataType:
55
+ """Expand entity and dataset types into STRUCTs: keys at depth 0, fields at depth 1.
56
+
57
+ Nested entities and datasets consume depth; STRUCT/ARRAY containers do not.
58
+ """
59
+ if max_depth < 0:
60
+ raise ValueError("max_depth must be non-negative")
61
+
62
+ return self._expand_type(datatype, max_depth)
63
+
64
+ def _infer(self, field_id: Id, *, constellation: exp.Table | None = None) -> exp.DataType:
65
+ """Preserve the queried constellation when following imported metric dependencies."""
66
+ cache_key = (field_id, constellation)
67
+ cached = self._inferred_types.get(cache_key)
68
+ if cached is not None:
69
+ return cached.copy()
70
+
71
+ # TODO: this may need to be refactored; it can lead to an N+1 problem for db registries
72
+ field = self.registry.get_objects([field_id])[field_id]
73
+
74
+ match field:
75
+ case Column() | Relationship() | Role():
76
+ return _declared_type(field.datatype, field.id)
77
+ case Calculation() | Attribute() | Metric():
78
+ if field.datatype is not None:
79
+ return _declared_type(field.datatype, field.id)
80
+ case None:
81
+ # Entity keys and imported fields have no separate stored definition on this owner
82
+ match field_id:
83
+ case Id(
84
+ prefix=Prefix.FIELD, parts=(exp.Table() as owner, exp.Identifier() as name)
85
+ ):
86
+ entity = self.registry.get_entity(owner)
87
+ if entity is not None and (key := entity.key.get(name)) is not None:
88
+ return _declared_type(key.datatype, field_id)
89
+
90
+ constellation_proxy = self.registry.get_constellation(owner)
91
+ if (
92
+ constellation_proxy is not None
93
+ and (member := constellation_proxy.get_column(name)) is not None
94
+ ):
95
+ inferred = self._infer(member.id, constellation=owner)
96
+ self._inferred_types[cache_key] = inferred.copy()
97
+ return inferred
98
+
99
+ return UNKNOWN.copy()
100
+ case _:
101
+ return UNKNOWN.copy()
102
+
103
+ constellation = (
104
+ (constellation or field.constellation) if isinstance(field, Metric) else None
105
+ )
106
+
107
+ # Imports can give one definition several names; detect cycles by its resolved identity.
108
+ cycle_key = (field.id, constellation)
109
+ if cycle_key in self._type_path:
110
+ path = list(self._type_path)
111
+ start = path.index(cycle_key)
112
+ cycle = (*path[start:], cycle_key)
113
+ names = " -> ".join(item.text() for item, _ in cycle)
114
+ raise TypeInferenceCycleError(f"Type inference cycle: {names}")
115
+
116
+ self._type_path.add(cycle_key)
117
+ try:
118
+ dialect = str(field.expr.meta.get("dialect") or self.registry.dialect)
119
+ schema = self._schemas.get(dialect)
120
+ if schema is None:
121
+ schema = self._schemas[dialect] = InferenceSchema(self, dialect)
122
+
123
+ qualified = qualify(
124
+ schema.query_for(field),
125
+ schema=schema,
126
+ dialect=dialect,
127
+ validate_qualify_columns=False,
128
+ allow_partial_qualification=True,
129
+ )
130
+
131
+ metadata = {
132
+ **schema.dialect.EXPRESSION_METADATA,
133
+ exp.Dot: {"annotator": self._annotate_dot},
134
+ }
135
+ match field:
136
+ case Calculation() if qualified.find(exp.Placeholder) is not None:
137
+ dataset = self.registry.get_dataset(field.dataset)
138
+ assert dataset is not None
139
+
140
+ metadata[exp.Placeholder] = {
141
+ "annotator": partial(
142
+ self._annotate_placeholder,
143
+ parameter_types={
144
+ name.name: parameter.datatype
145
+ for name, parameter in dataset.parameters.items()
146
+ },
147
+ )
148
+ }
149
+ case Metric():
150
+ assert constellation is not None
151
+ metadata[exp.Agg] = {
152
+ "annotator": partial(
153
+ self._annotate_agg, metric=field, constellation=constellation
154
+ )
155
+ }
156
+
157
+ annotated = annotate_types(qualified, schema=schema, expression_metadata=metadata)
158
+ self._inferred_types[cache_key] = inferred = type_or_unknown(annotated.selects[0].type)
159
+ return inferred.copy()
160
+ finally:
161
+ self._type_path.discard(cycle_key)
162
+
163
+ def _expand_type(self, datatype: exp.DataType, depth: int) -> exp.DataType:
164
+ def expand(node: exp.Expr) -> exp.Expr:
165
+ if isinstance(node, exp.DataType) and (target := ref_target(node)) is not None:
166
+ # At the depth limit, include keys but don't expand entity/dataset-valued keys
167
+ return self._struct_type(target, depth) if depth >= 0 else UNKNOWN.copy()
168
+
169
+ return node
170
+
171
+ return cast(exp.DataType, datatype.transform(expand))
172
+
173
+ def _struct_type(self, target: exp.Table, depth: int) -> exp.DataType:
174
+ cached = self._expanded_types.get((target, depth))
175
+ if cached is not None:
176
+ return cached.copy()
177
+
178
+ entity = self.registry.get_entity(target)
179
+
180
+ if entity is not None:
181
+ fields: dict[exp.Identifier, exp.DataType] = {
182
+ name: _declared_type(key.datatype, Id(Prefix.FIELD, (target, name)))
183
+ for name, key in entity.key.items()
184
+ }
185
+ names = entity.column_names() if depth else ()
186
+ else:
187
+ dataset = self.registry.get_dataset(target)
188
+ if dataset is None:
189
+ return UNKNOWN.copy()
190
+
191
+ # Key-condition parameters bind predicates; they aren't fields of the target's rows.
192
+ fields = {}
193
+ if depth:
194
+ names = dataset.column_names()
195
+ else:
196
+ names = dataset.key.fields() if dataset.key else ()
197
+
198
+ for name in names:
199
+ try:
200
+ fields[name] = self.infer(Id(Prefix.FIELD, (target, name)))
201
+ except TypeInferenceCycleError:
202
+ # An unused cyclic calculation must not hide the object's other fields.
203
+ fields[name] = UNKNOWN.copy()
204
+
205
+ datatype = (
206
+ exp.DataType(
207
+ this=exp.DType.STRUCT,
208
+ expressions=[
209
+ exp.ColumnDef(this=name.copy(), kind=self._expand_type(kind, depth - 1))
210
+ for name, kind in fields.items()
211
+ ],
212
+ )
213
+ if fields
214
+ else UNKNOWN.copy()
215
+ )
216
+ self._expanded_types[target.copy(), depth] = datatype
217
+ return datatype.copy()
218
+
219
+ def _annotate_placeholder(
220
+ self,
221
+ annotator: TypeAnnotator,
222
+ expression: exp.Placeholder,
223
+ *,
224
+ parameter_types: Mapping[str, exp.DataType | None],
225
+ ) -> exp.Placeholder:
226
+ """Use declared parameter types; do not infer types from default values."""
227
+ name = resolve_name(expression.name, parameter_types)
228
+ datatype = type_or_unknown(parameter_types.get(name) if name is not None else None)
229
+ annotator._set_type(expression, datatype)
230
+ return expression
231
+
232
+ def _annotate_agg(
233
+ self,
234
+ annotator: TypeAnnotator,
235
+ expression: exp.Agg,
236
+ *,
237
+ metric: Metric,
238
+ constellation: exp.Table,
239
+ ) -> exp.Agg:
240
+ """AGG preserves its dataset calculation's type, or resolves a composed metric."""
241
+ datatype = type_or_unknown(expression.this.type)
242
+ match expression.this:
243
+ case exp.Identifier() as name:
244
+ queried = self.registry.get_constellation(constellation)
245
+ if queried is not None:
246
+ dependency = resolve_metric(self.registry, queried, name, metric.constellation)
247
+ if dependency is not None:
248
+ datatype = self._infer(dependency.id, constellation=constellation)
249
+
250
+ annotator._set_type(expression, datatype)
251
+ return expression
252
+
253
+ def _annotate_dot(self, annotator: TypeAnnotator, expression: exp.Expr) -> exp.Expr:
254
+ """Resolve one relationship member; SQLGlot handles ordinary struct access."""
255
+ match expression:
256
+ case exp.Dot(expression=exp.Identifier() as member):
257
+ target = ref_target(expression.this.type)
258
+ if target is not None:
259
+ datatype = self.infer(Id(Prefix.FIELD, (target, exact_identifier(member.name))))
260
+ annotator._set_type(expression, datatype)
261
+ return expression
262
+
263
+ annotator.dialect.EXPRESSION_METADATA[exp.Dot]["annotator"](annotator, expression)
264
+ return expression
265
+
266
+
267
+ class InferenceSchema(MappingSchema):
268
+ def __init__(self, resolver: TypeResolver, dialect: str) -> None:
269
+ super().__init__(dialect=dialect, normalize=False)
270
+
271
+ self.resolver = resolver
272
+ self._tables: set[exp.Table] = set()
273
+
274
+ def query_for(self, field: Calculation | Attribute | Metric) -> exp.Select:
275
+ registry = self.resolver.registry
276
+ proxy: DatasetProxy | EntityProxy | None
277
+ entity = None
278
+
279
+ match field:
280
+ case Metric():
281
+ return self._metric_query(field)
282
+ case Calculation():
283
+ owner = field.dataset
284
+ proxy = registry.get_dataset(owner)
285
+ case Attribute():
286
+ owner = field.entity
287
+ proxy = entity = registry.get_entity(owner)
288
+
289
+ if proxy is None:
290
+ raise SchemaError(f"Unknown inference scope: {owner.sql(registry.dialect)}")
291
+
292
+ expression = field.expr.copy()
293
+
294
+ references = (
295
+ list(resolve_attribute_references(entity, expression)) if entity is not None else []
296
+ )
297
+ table = self.table_for(proxy, keys=entity.key if entity is not None else ())
298
+ query = exp.select(expression, copy=False).from_(table, copy=False)
299
+
300
+ if entity is not None:
301
+ local_column_ids = _local_column_ids(query, self)
302
+
303
+ # Bare names belong to the entity, even if a source also has that field
304
+ for column in find_all_in_scope(query, exp.Column):
305
+ if not column.table:
306
+ column.set("table", owner.this.copy())
307
+ column.set("db", owner.args["db"].copy())
308
+
309
+ sources: set[exp.Table] = set()
310
+ for reference in references:
311
+ # Leave columns bound by a subquery's own sources alone, e.g. q.profile.city.
312
+ if id(reference.column.find(exp.Column)) in local_column_ids:
313
+ continue
314
+
315
+ if reference.source is not None:
316
+ table = reference.source.dataset
317
+ path = reference.path
318
+
319
+ if table not in sources:
320
+ dataset = registry.get_dataset(table)
321
+ if dataset is None:
322
+ raise SchemaError(
323
+ f"Unknown inference scope: {table.sql(registry.dialect)}"
324
+ )
325
+
326
+ sources.add(table)
327
+ query.join(self.table_for(dataset), join_type="CROSS", copy=False)
328
+ else:
329
+ # profile.city can name a sibling attribute even if a source has profile too.
330
+ path = path_names(reference.column)
331
+ if entity.get_column(path[0]) is None and path[0] not in entity.key:
332
+ continue
333
+
334
+ table = owner
335
+
336
+ # COUNT(people.*) needs its source registered, but has no field path to rebuild.
337
+ if reference.column.is_star:
338
+ continue
339
+
340
+ # Bind the column before struct access: ns.people.profile.city
341
+ # means (people.profile).city, not a four-part SQL column name.
342
+ reference.column.replace(_field_access(table, path))
343
+
344
+ return query
345
+
346
+ def _metric_query(self, metric: Metric) -> exp.Select:
347
+ query = exp.select(metric.expr.copy(), copy=False)
348
+
349
+ # Replace the Column argument in AGG(metric) with the enclosed Identifier to avoid
350
+ # qualification issues. The AGG annotator resolves it in the queried constellation,
351
+ # including overrides of imported dependencies.
352
+ for aggregate in find_all_in_scope(query, exp.Agg):
353
+ match aggregate.this:
354
+ case exp.Column(table="") as column:
355
+ aggregate.set("this", column.this.copy())
356
+
357
+ sources: set[exp.Table] = set()
358
+ registry = self.resolver.registry
359
+ for column in list(find_all_in_scope(query, exp.Column)):
360
+ reference: exp.Column | exp.Dot = column
361
+ while True:
362
+ match reference.parent:
363
+ case exp.Dot() as parent:
364
+ reference = parent
365
+ case _:
366
+ break
367
+
368
+ path = path_names(reference)
369
+ if len(path) < 2:
370
+ continue
371
+
372
+ table, members = resolve_dataset_path(
373
+ path, metric.namespace, lambda name: registry.get_dataset(name) is not None
374
+ )
375
+ dataset = registry.get_dataset(table)
376
+ if dataset is None:
377
+ continue
378
+
379
+ if table not in sources:
380
+ source = self.table_for(dataset)
381
+
382
+ if not sources:
383
+ query.from_(source, copy=False)
384
+ else:
385
+ query.join(source, join_type="CROSS", copy=False)
386
+
387
+ sources.add(table)
388
+
389
+ if not reference.is_star:
390
+ reference.replace(_field_access(table, members))
391
+
392
+ return query
393
+
394
+ def table_for(
395
+ self, proxy: DatasetProxy | EntityProxy, *, keys: Collection[exp.Identifier] = ()
396
+ ) -> exp.Table:
397
+ name = proxy.name
398
+ if name not in self._tables:
399
+ self.add_table(name, [column.name for column in (*proxy.column_names(), *keys)])
400
+ self._tables.add(name)
401
+
402
+ return name.copy()
403
+
404
+ @override
405
+ def get_column_type(
406
+ self,
407
+ table: exp.Table | str,
408
+ column: exp.Column | str,
409
+ dialect: DialectType = None,
410
+ normalize: bool | None = None,
411
+ ) -> exp.DataType:
412
+ table = exp.to_table(table, dialect=self.dialect, copy=False)
413
+ db = table.args.get("db")
414
+ if db is None:
415
+ return UNKNOWN.copy()
416
+ owner = quote_identifiers(
417
+ exp.table_(table.this, db=db), dialect=self.resolver.registry.dialect
418
+ )
419
+ return self.resolver.infer(Id(Prefix.FIELD, (owner, exact_identifier(column))))
420
+
421
+
422
+ def _declared_type(datatype: exp.DataType | None, field_id: Id) -> exp.DataType:
423
+ """Qualify unqualified REF(employee) types using the declaring field's namespace."""
424
+ resolved = type_or_unknown(datatype)
425
+ owner = field_id.parts[0]
426
+ for node in resolved.find_all(exp.DataType):
427
+ if (target := ref_target(node)) is not None:
428
+ node.set("expressions", [in_namespace(target, owner.args["db"])])
429
+ return resolved
430
+
431
+
432
+ def _field_access(table: exp.Table, path: tuple[exp.Identifier, ...]) -> exp.Expr:
433
+ """Bind a source column before traversing its relationship or struct fields."""
434
+ access: exp.Expr = exp.column(
435
+ path[0].copy(), table=table.this.copy(), db=table.args["db"].copy()
436
+ )
437
+ for member in path[1:]:
438
+ access = exp.Dot(this=access, expression=member.copy())
439
+ return access
440
+
441
+
442
+ def _local_column_ids(query: exp.Select, schema: MappingSchema) -> set[int]:
443
+ """Return the ids of columns in query's subqueries that resolve to a local source.
444
+
445
+ In (SELECT profile.city FROM (...) q), profile can be a local struct column. The outer
446
+ query is skipped, but scope.columns also includes references from correlated subqueries.
447
+ """
448
+ local_column_ids: set[int] = set()
449
+ for scope in traverse_scope(query):
450
+ if scope.expression is query:
451
+ continue
452
+ # Apply CTE/derived-table column aliases before their parent inspects them.
453
+ if scope.outer_columns:
454
+ qualify_outputs(scope, schema.dialect)
455
+ resolver = Resolver(scope, schema)
456
+ for column in scope.columns:
457
+ if column.parts[0].name in resolver.all_columns:
458
+ local_column_ids.add(id(column))
459
+
460
+ return local_column_ids
File without changes