metricflow-sql 0.5.0__tar.gz → 0.5.1__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.
Files changed (63) hide show
  1. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/PKG-INFO +1 -1
  2. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/pyproject.toml +1 -1
  3. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/converters/lookml.py +65 -15
  4. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/schemas/load.py +25 -1
  5. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/.gitignore +0 -0
  6. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/LICENSE +0 -0
  7. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/PYPI.md +0 -0
  8. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/README.md +0 -0
  9. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/__init__.py +0 -0
  10. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/compiler/__init__.py +0 -0
  11. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/compiler/catalog/__init__.py +0 -0
  12. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/compiler/catalog/cluster.py +0 -0
  13. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/compiler/catalog/information_schema.py +0 -0
  14. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/compiler/catalog/metadata.py +0 -0
  15. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/compiler/catalog/passthrough.py +0 -0
  16. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/compiler/catalog/protocols.py +0 -0
  17. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/compiler/catalog/semantic.py +0 -0
  18. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/compiler/compiler.py +0 -0
  19. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/compiler/rules/__init__.py +0 -0
  20. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/compiler/rules/expand_constellations.py +0 -0
  21. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/compiler/rules/expand_datasets.py +0 -0
  22. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/compiler/rules/expand_metadata.py +0 -0
  23. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/compiler/rules/expand_per.py +0 -0
  24. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/compiler/rules/isolate_fragments.py +0 -0
  25. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/compiler/rules/passthrough_tables.py +0 -0
  26. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/compiler/table_arguments.py +0 -0
  27. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/converters/__init__.py +0 -0
  28. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/converters/legacy.py +0 -0
  29. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/objects/__init__.py +0 -0
  30. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/objects/base.py +0 -0
  31. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/objects/constellation.py +0 -0
  32. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/objects/dataset.py +0 -0
  33. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/objects/entity.py +0 -0
  34. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/objects/reference.py +0 -0
  35. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/objects/wiki.py +0 -0
  36. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/py.typed +0 -0
  37. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/registry/__init__.py +0 -0
  38. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/registry/memory.py +0 -0
  39. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/registry/operation.py +0 -0
  40. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/registry/reference_resolution.py +0 -0
  41. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/registry/registry.py +0 -0
  42. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/registry/type_resolver.py +0 -0
  43. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/schemas/__init__.py +0 -0
  44. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/schemas/bundle.py +0 -0
  45. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/schemas/fragment.schema.json +0 -0
  46. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/schemas/generate.py +0 -0
  47. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/schemas/markdown.py +0 -0
  48. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/schemas/v1/__init__.py +0 -0
  49. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/schemas/v1/fragment.py +0 -0
  50. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/server/__init__.py +0 -0
  51. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/server/app.py +0 -0
  52. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/server/compile.py +0 -0
  53. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/server/errors.py +0 -0
  54. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/server/http.py +0 -0
  55. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/server/registry.py +0 -0
  56. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/server/serde.py +0 -0
  57. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/server/serve.py +0 -0
  58. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/utils/__init__.py +0 -0
  59. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/utils/composed_set.py +0 -0
  60. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/utils/normalize.py +0 -0
  61. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/utils/ordered_set.py +0 -0
  62. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/src/metricflow_sql/utils/sql.py +0 -0
  63. {metricflow_sql-0.5.0 → metricflow_sql-0.5.1}/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.0
3
+ Version: 0.5.1
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
@@ -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.0"
4
+ version = "0.5.1"
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"
@@ -98,6 +98,7 @@ _AGGREGATES: dict[str, _Template] = {
98
98
  "count_distinct": lambda sql: exp.replace_placeholders(_COUNT_DISTINCT, sql=sql),
99
99
  "average": lambda sql: exp.func("AVG", sql),
100
100
  "max": lambda sql: exp.func("MAX", sql),
101
+ "min": lambda sql: exp.func("MIN", sql),
101
102
  "median": lambda sql: exp.replace_placeholders(_MEDIAN, sql=sql),
102
103
  "number": lambda sql: sql,
103
104
  }
@@ -114,7 +115,7 @@ _COMPARISONS: dict[str, Callable[[exp.Expr, exp.Expr], exp.Expr]] = {
114
115
  }
115
116
  _PARAMETER_TYPES = {"date": "DATE", "number": "FLOAT64", "string": "STRING"}
116
117
  # The joins a relationship can express, and Looker's default.
117
- _RELATIONSHIPS = ("many_to_one", "one_to_one")
118
+ _RELATIONSHIPS = ("many_to_one", "one_to_one", "one_to_many")
118
119
  _DEFAULT_RELATIONSHIP = "many_to_one"
119
120
  _FIELD_KINDS = ("parameters", "filters", "dimensions", "dimension_groups", "measures")
120
121
  # Each explore can add to these, e.g. a relationship to a view only it joins, or a LookML view's
@@ -153,6 +154,9 @@ class _Converter:
153
154
  """By joined view, e.g. `{region_history: current_regions__region_history}`; else its name."""
154
155
  entities: dict[str, str] = field(default_factory=dict)
155
156
  """By joined view; else e.g. `accounts_entity`."""
157
+ renamed: dict[str, set[str]] = field(default_factory=dict)
158
+ """By view, the fields an explore on it renames as they're named like what it joins, so
159
+ the explores that join the view name them alike."""
156
160
  timezone: str | None = None
157
161
  """Looker's query timezone, which converts the time dimension groups that don't opt out."""
158
162
  base: str = field(init=False)
@@ -175,6 +179,11 @@ class _Converter:
175
179
  if _relationship(join) not in _RELATIONSHIPS:
176
180
  self.dropped[view] = f"{_relationship(join)} join"
177
181
  continue
182
+ # The compiler keeps a one-to-many join from double counting by telling the joined
183
+ # rows apart by their key.
184
+ if _relationship(join) == "one_to_many" and not self.primary_key(view):
185
+ self.dropped[view] = "one_to_many join without a primary key"
186
+ continue
178
187
  parents = {match[1] for match in _REF.finditer(join["sql_on"])} - {view}
179
188
  # A relationship hangs off one view, so a key read from several, e.g.
180
189
  # `coalesce(${a.account_id}, ${b.account_id}) = ${accounts.id}`, has no relationship.
@@ -233,15 +242,16 @@ class _Converter:
233
242
 
234
243
  def measure(self, view: str, definition: dict[str, Any]) -> exp.Expr:
235
244
  measure_type = definition["type"]
245
+ distinct_key = None
236
246
  if measure_type in _DISTINCT_AGGREGATES:
237
247
  # Each dataset aggregates at its key's grain before it joins, which is the dedupe
238
248
  # Looker does on the primary key; any other key dedupes within the view.
239
- if not self.key(view):
240
- raise _Skip(f"{measure_type} without a primary key")
241
- if "sql_distinct_key" in definition and not self.is_primary_key(
242
- view, definition["sql_distinct_key"]
249
+ if "sql_distinct_key" in definition and not (
250
+ self.key(view) and self.is_primary_key(view, definition["sql_distinct_key"])
243
251
  ):
244
- raise _Skip("sql_distinct_key other than the primary key")
252
+ distinct_key = self.translate(view, definition["sql_distinct_key"], self.reference)
253
+ elif not self.key(view):
254
+ raise _Skip(f"{measure_type} without a primary key")
245
255
  measure_type = measure_type.removesuffix("_distinct")
246
256
  aggregate = _AGGREGATES.get(measure_type)
247
257
  if aggregate is None:
@@ -257,6 +267,9 @@ class _Converter:
257
267
  ]
258
268
  if conditions:
259
269
  sql = exp.case().when(exp.and_(*conditions), sql).else_(exp.null())
270
+ if distinct_key is not None:
271
+ # Looker counts each key's value once, taking it to be the same on all its rows.
272
+ sql = exp.Per(this=exp.func("MAX", sql), expressions=[distinct_key])
260
273
  return aggregate(sql)
261
274
 
262
275
  def is_primary_key(self, view: str, sql: str) -> bool:
@@ -395,12 +408,17 @@ class _Converter:
395
408
 
396
409
  return _REF.sub(reference, sql)
397
410
 
411
+ def joined_names(self, view: str) -> set[str]:
412
+ """The names of what the explore joins, when `view` is its base."""
413
+ if view != self.base:
414
+ return set()
415
+ return {*self.paths, *self.relationships.values()} - {self.base}
416
+
398
417
  def calculation(self, view: str, kind: str, name: str) -> str:
399
418
  """The dataset field a LookML field converts to, renamed when it would shadow a column,
400
419
  or, e.g. `orders__users`, a view the explore joins, as the relationship to it and its
401
420
  role are named like it."""
402
- joined = {*self.paths, *self.relationships.values()} - {self.base}
403
- if view == self.base and name.lower() in joined:
421
+ if name.lower() in self.renamed.get(view, set()) | self.joined_names(view):
404
422
  return f"{view}__{name.lower()}"
405
423
  if kind == "dimensions" and _is_column(self.result(view, name), name):
406
424
  return name
@@ -500,6 +518,12 @@ class _Converter:
500
518
  continue
501
519
  name = self.calculation(view, kind, definition["name"])
502
520
  docs[name] = _docs(definition, view=view, kind=kind.removesuffix("s"))
521
+ # A dimension that reads its column documents it in every explore, so the views
522
+ # explores share agree even where one renames the field, e.g. `items__products`.
523
+ if kind == "dimensions" and _is_column(
524
+ self.result(view, definition["name"]), definition["name"]
525
+ ):
526
+ docs.setdefault(definition["name"], docs[name])
503
527
  return docs
504
528
 
505
529
  def dataset(self, view: str) -> dict[str, Any]:
@@ -536,14 +560,19 @@ class _Converter:
536
560
  ],
537
561
  }
538
562
 
539
- def key(self, view: str) -> list[str]:
540
- """The field of the view's `primary_key: yes` dimension, or none when it's skipped."""
563
+ def primary_key(self, view: str) -> str | None:
564
+ """The name of the view's `primary_key: yes` dimension."""
541
565
  for kind, definition in self.fields(view):
542
566
  if kind == "dimensions" and definition.get("primary_key") == "yes":
543
- if isinstance(self.result(view, definition["name"]), _Skip):
544
- return []
545
- return [self.calculation(view, kind, definition["name"])]
546
- return []
567
+ return str(definition["name"])
568
+ return None
569
+
570
+ def key(self, view: str) -> list[str]:
571
+ """The field of the view's `primary_key: yes` dimension, or none when it's skipped."""
572
+ name = self.primary_key(view)
573
+ if name is None or isinstance(self.result(view, name), _Skip):
574
+ return []
575
+ return [self.calculation(view, "dimensions", name)]
547
576
 
548
577
  def join(self, join: dict[str, Any]) -> tuple[dict[str, Any], list[dict[str, Any]]]:
549
578
  """
@@ -609,8 +638,16 @@ class _Converter:
609
638
  )
610
639
 
611
640
  on = self.translate(view, join["sql_on"], bind)
641
+ entity_key = list(key)
642
+ if _relationship(join) == "one_to_many":
643
+ # The joined view's key, which the relationship leaves unbound: so the compiler takes
644
+ # it as one-to-many, and groups by the key to count each joined row once.
645
+ primary_key = str(self.primary_key(view))
646
+ key_name = f"{view}__{primary_key}"
647
+ entity_key.append(key_name)
648
+ on = exp.and_(on, exp.var(f":{key_name}").eq(self.inline(view, view, primary_key)))
612
649
  entity = {
613
- "key": [{"name": name} for name in key],
650
+ "key": [{"name": name} for name in entity_key],
614
651
  "sources": [{"dataset": view, "on": on.sql(_DIALECT)}],
615
652
  }
616
653
  return entity, [{"name": name, "expr": expr.sql(_DIALECT)} for name, expr in key.items()]
@@ -871,6 +908,12 @@ def _merged(old: dict[str, Any], new: dict[str, Any], name: str) -> dict[str, An
871
908
  if key in _UNITED:
872
909
  members = {member["name"]: member for member in old.get(key, [])}
873
910
  for member in new.get(key, []):
911
+ old_member = members.get(member["name"])
912
+ if key == "sets" and old_member is not None:
913
+ # A field an explore skips, e.g. as it filters on a view the explore doesn't
914
+ # join, is left out of its sets there, but the view, and so its sets, keep it.
915
+ fields = list(dict.fromkeys([*old_member["fields"], *member["fields"]]))
916
+ member = members[member["name"]] = {**member, "fields": fields}
874
917
  if members.setdefault(member["name"], member) != member:
875
918
  raise ValueError(
876
919
  f"Explores convert `{name}.{key}.{member['name']}` differently"
@@ -1119,6 +1162,12 @@ def convert(
1119
1162
  },
1120
1163
  relationship_name,
1121
1164
  )
1165
+ renamed: dict[str, set[str]] = {}
1166
+ for explore, draft in drafts.items():
1167
+ draft.relationships = {
1168
+ view: name for (_, view), name in relationship_names[explore].items()
1169
+ }
1170
+ renamed.setdefault(draft.base, set()).update(draft.joined_names(draft.base))
1122
1171
  datasets: dict[str, Any] = {}
1123
1172
  entities: dict[str, Any] = {}
1124
1173
  constellations: dict[str, Any] = {}
@@ -1133,6 +1182,7 @@ def convert(
1133
1182
  user_attributes,
1134
1183
  relationships={view: name for (_, view), name in relationship_names[explore].items()},
1135
1184
  entities=entity_names[explore],
1185
+ renamed=renamed,
1136
1186
  timezone=timezone,
1137
1187
  )
1138
1188
  # Converting every field first, so field lists can drop the skipped ones.
@@ -164,6 +164,7 @@ class Loader:
164
164
  key=build_key(dataset),
165
165
  label=dataset.label,
166
166
  description=dataset.description,
167
+ meta=dataset.meta,
167
168
  )
168
169
  # Render once with the defaults, so a broken template fails at load.
169
170
  view.from_({})
@@ -181,6 +182,7 @@ class Loader:
181
182
  key=build_key(dataset),
182
183
  label=dataset.label,
183
184
  description=dataset.description,
185
+ meta=dataset.meta,
184
186
  )
185
187
 
186
188
  for dataset in fragment.datasets:
@@ -202,6 +204,7 @@ class Loader:
202
204
  datatype=build_datatype(column.datatype),
203
205
  label=column.label,
204
206
  description=column.description,
207
+ meta=column.meta,
205
208
  )
206
209
  for column in (dataset.source.columns if dataset.source else [])
207
210
  )
@@ -213,6 +216,7 @@ class Loader:
213
216
  datatype=build_datatype(calculation.datatype),
214
217
  label=calculation.label,
215
218
  description=calculation.description,
219
+ meta=calculation.meta,
216
220
  )
217
221
  for calculation in dataset.calculations
218
222
  )
@@ -229,6 +233,7 @@ class Loader:
229
233
  ),
230
234
  label=relationship.label,
231
235
  description=relationship.description,
236
+ meta=relationship.meta,
232
237
  )
233
238
  for relationship in dataset.relationships
234
239
  )
@@ -245,6 +250,9 @@ class Loader:
245
250
  )
246
251
  for key_column in entity.key
247
252
  },
253
+ label=entity.label,
254
+ description=entity.description,
255
+ meta=entity.meta,
248
256
  )
249
257
  )
250
258
  objects.extend(
@@ -252,6 +260,7 @@ class Loader:
252
260
  dataset=reference(source.dataset, name),
253
261
  entity=name,
254
262
  on=sql(source.on),
263
+ meta=source.meta,
255
264
  )
256
265
  for source in entity.sources
257
266
  )
@@ -263,6 +272,7 @@ class Loader:
263
272
  datatype=build_datatype(attribute.datatype),
264
273
  label=attribute.label,
265
274
  description=attribute.description,
275
+ meta=attribute.meta,
266
276
  )
267
277
  for attribute in entity.attributes
268
278
  )
@@ -274,6 +284,7 @@ class Loader:
274
284
  name=name,
275
285
  label=constellation.label,
276
286
  description=constellation.description,
287
+ meta=constellation.meta,
277
288
  imports=[
278
289
  Import(
279
290
  constellation=reference(import_.constellation, name),
@@ -290,6 +301,7 @@ class Loader:
290
301
  datatype=build_datatype(role.datatype),
291
302
  label=role.label,
292
303
  description=role.description,
304
+ meta=role.meta,
293
305
  )
294
306
  for role in constellation.roles
295
307
  )
@@ -301,6 +313,7 @@ class Loader:
301
313
  datatype=build_datatype(metric.datatype),
302
314
  label=metric.label,
303
315
  description=metric.description,
316
+ meta=metric.meta,
304
317
  )
305
318
  for metric in constellation.metrics
306
319
  )
@@ -313,13 +326,23 @@ class Loader:
313
326
  else exp.table_(n(constellation_fact.alias), db=name.args["db"].copy())
314
327
  )
315
328
  # Metrics can read a fact even when it has no roles.
316
- objects.append(Fact(constellation=name, name=fact_name, dataset=assigned))
329
+ objects.append(
330
+ Fact(
331
+ constellation=name,
332
+ name=fact_name,
333
+ dataset=assigned,
334
+ meta=constellation_fact.meta,
335
+ )
336
+ )
317
337
  objects.extend(
318
338
  Assignment(
319
339
  constellation=name,
320
340
  fact=fact_name,
321
341
  role=n(assignment.name),
322
342
  expr=sql(assignment.expr),
343
+ label=assignment.label,
344
+ description=assignment.description,
345
+ meta=assignment.meta,
323
346
  )
324
347
  for assignment in constellation_fact.roles
325
348
  )
@@ -339,6 +362,7 @@ class Loader:
339
362
  normalization_strategy=strategy,
340
363
  label=page.label,
341
364
  description=page.description,
365
+ meta=page.meta,
342
366
  )
343
367
  )
344
368
 
File without changes
File without changes
File without changes