Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Dependencies to dbt-semantic-interfaces~=0.3.0 #809

Merged
merged 3 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/Dependencies-20231012-141930.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Dependencies
body: Update to dbt-semantic-interfaces~=0.3.0.
time: 2023-10-12T14:19:30.799362-07:00
custom:
Author: plypaul
PR: "809"
4 changes: 2 additions & 2 deletions metricflow/engine/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from dbt_semantic_interfaces.protocols.metadata import Metadata
from dbt_semantic_interfaces.protocols.metric import Metric as SemanticManifestMetric
from dbt_semantic_interfaces.protocols.metric import MetricInputMeasure, MetricType, MetricTypeParams
from dbt_semantic_interfaces.protocols.where_filter import WhereFilter
from dbt_semantic_interfaces.protocols.where_filter import WhereFilterIntersection
from dbt_semantic_interfaces.transformations.add_input_metric_measures import AddInputMetricMeasuresRule
from dbt_semantic_interfaces.type_enums.aggregation_type import AggregationType
from dbt_semantic_interfaces.type_enums.entity_type import EntityType
Expand All @@ -34,7 +34,7 @@ class Metric:
description: Optional[str]
type: MetricType
type_params: MetricTypeParams
filter: Optional[WhereFilter]
filter: Optional[WhereFilterIntersection]
metadata: Optional[Metadata]
dimensions: List[Dimension]

Expand Down
21 changes: 21 additions & 0 deletions metricflow/filters/merge_where.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from __future__ import annotations

from typing import Optional

from dbt_semantic_interfaces.implementations.filters.where_filter import PydanticWhereFilter
from dbt_semantic_interfaces.protocols import WhereFilter, WhereFilterIntersection


def merge_to_single_where_filter(where_filter_intersection: WhereFilterIntersection) -> Optional[WhereFilter]:
"""Returns a single where filter that is equivalent to the given intersection."""
if len(where_filter_intersection.where_filters) == 0:
return None

if len(where_filter_intersection.where_filters) == 1:
return where_filter_intersection.where_filters[0]

each_where_filter_condition = [
"( " + where_filter.where_sql_template + " )" for where_filter in where_filter_intersection.where_filters
]

return PydanticWhereFilter(where_sql_template=" AND ".join(each_where_filter_condition))
28 changes: 11 additions & 17 deletions metricflow/model/semantics/metric_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import logging
from typing import Dict, FrozenSet, List, Sequence

from dbt_semantic_interfaces.implementations.filters.where_filter import PydanticWhereFilterIntersection
from dbt_semantic_interfaces.implementations.metric import PydanticMetricTimeWindow
from dbt_semantic_interfaces.protocols import WhereFilter
from dbt_semantic_interfaces.protocols.metric import Metric, MetricType
from dbt_semantic_interfaces.protocols.semantic_manifest import SemanticManifest
from dbt_semantic_interfaces.references import MetricReference
Expand Down Expand Up @@ -123,9 +125,7 @@ def measures_for_metric(
measure_spec=measure_spec,
constraint=WhereSpecFactory(
column_association_resolver=column_association_resolver,
).create_from_where_filter(input_measure.filter)
if input_measure.filter is not None
else None,
).create_from_where_filter_intersection(input_measure.filter),
alias=input_measure.alias,
)
input_measure_specs.append(spec)
Expand Down Expand Up @@ -156,27 +156,21 @@ def metric_input_specs_for_metric(
for input_metric in metric.input_metrics:
original_metric_obj = self.get_metric(input_metric.as_reference)

where_filters: List[WhereFilter] = []

# This is the constraint parameter added to the input metric in the derived metric definition
combined_filter = (
WhereSpecFactory(
column_association_resolver=column_association_resolver,
).create_from_where_filter(input_metric.filter)
if input_metric.filter is not None
else None
)
if input_metric.filter:
where_filters.extend(input_metric.filter.where_filters)

# This is the constraint parameter included in the original input metric definition
if original_metric_obj.filter:
original_metric_filter = WhereSpecFactory(
column_association_resolver=column_association_resolver,
).create_from_where_filter(original_metric_obj.filter)
combined_filter = (
combined_filter.combine(original_metric_filter) if combined_filter else original_metric_filter
)
where_filters.extend(original_metric_obj.filter.where_filters)

spec = MetricSpec(
element_name=input_metric.name,
constraint=combined_filter,
constraint=WhereSpecFactory(column_association_resolver).create_from_where_filter_intersection(
PydanticWhereFilterIntersection(where_filters=where_filters)
),
alias=input_metric.alias,
offset_window=PydanticMetricTimeWindow(
count=input_metric.offset_window.count,
Expand Down
25 changes: 13 additions & 12 deletions metricflow/query/query_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def _construct_metric_specs_for_query(
# add constraint to MetricSpec
metric_where_constraint = WhereSpecFactory(
column_association_resolver=self._column_association_resolver,
).create_from_where_filter(metric.filter)
).create_from_where_filter_intersection(metric.filter)
# TODO: Directly initializing Spec object instead of using a factory method since
# importing WhereConstraintConverter is a problem in specs.py
metric_specs.append(
Expand Down Expand Up @@ -444,19 +444,20 @@ def _parse_and_validate_query(

# Combine the group by elements from the query with the group by elements that are required by the
# metric filter to see if that's a valid set that could be queried.
where_spec = WhereSpecFactory(
column_association_resolver=self._column_association_resolver
).create_from_where_filter_intersection(metric.filter)
self._validate_linkable_specs_for_metrics(
metric_references=(metric_reference,),
all_linkable_specs=QueryTimeLinkableSpecSet.combine(
(
group_by_specs_for_one_metric,
QueryTimeLinkableSpecSet.create_from_linkable_spec_set(
(
WhereSpecFactory(
column_association_resolver=self._column_association_resolver
).create_from_where_filter(metric.filter)
).linkable_spec_set
),
),
all_linkable_specs=(
QueryTimeLinkableSpecSet.combine(
(
group_by_specs_for_one_metric,
QueryTimeLinkableSpecSet.create_from_linkable_spec_set(where_spec.linkable_spec_set),
)
)
if where_spec is not None
else group_by_specs_for_one_metric
),
time_dimension_specs=time_dimension_specs,
)
Expand Down
9 changes: 7 additions & 2 deletions metricflow/specs/dimension_spec_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from dbt_semantic_interfaces.references import DimensionReference, EntityReference, TimeDimensionReference
from dbt_semantic_interfaces.type_enums import TimeGranularity

from metricflow.specs.specs import DimensionSpec, TimeDimensionSpec
from metricflow.specs.specs import DEFAULT_TIME_GRANULARITY, DimensionSpec, TimeDimensionSpec


class DimensionSpecResolver:
Expand Down Expand Up @@ -50,5 +50,10 @@ def resolve_time_dimension_spec(
return TimeDimensionSpec(
element_name=call_parameter_set.time_dimension_reference.element_name,
entity_links=call_parameter_set.entity_path,
time_granularity=call_parameter_set.time_granularity,
time_granularity=(
call_parameter_set.time_granularity
# TODO: This should be updated once resolution of unspecified grain is supported.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. DAY could be wrong, and then it'll throw a less helpful error, but at least it'll work for daily granularities and error out otherwise.

if call_parameter_set.time_granularity is not None
else DEFAULT_TIME_GRANULARITY
),
)
15 changes: 15 additions & 0 deletions metricflow/specs/where_filter_transform.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from __future__ import annotations

import logging
from typing import Optional

import jinja2
from dbt_semantic_interfaces.protocols import WhereFilterIntersection
from dbt_semantic_interfaces.protocols.where_filter import WhereFilter

from metricflow.filters.merge_where import merge_to_single_where_filter
from metricflow.specs.column_assoc import ColumnAssociationResolver
from metricflow.specs.specs import LinkableSpecSet, WhereFilterSpec
from metricflow.specs.where_filter_dimension import WhereFilterDimensionFactory
Expand All @@ -30,6 +33,18 @@ def __init__( # noqa: D
self._column_association_resolver = column_association_resolver
self._bind_parameters = bind_parameters

def create_from_where_filter_intersection( # noqa: D
self, where_filter_intersection: Optional[WhereFilterIntersection]
) -> Optional[WhereFilterSpec]:
if where_filter_intersection is None:
return None

where_filter = merge_to_single_where_filter(where_filter_intersection)
if where_filter is None:
return None

return self.create_from_where_filter(where_filter)

def create_from_where_filter(self, where_filter: WhereFilter) -> WhereFilterSpec: # noqa: D
# Used to check that call parameter sets generated by DSI match those generated below.
call_parameter_sets = where_filter.call_parameter_sets
Expand Down
12 changes: 8 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ dependencies = [
"MarkupSafe==2.0.1", # pandas implicitly requires this version
"PyYAML~=6.0",
"click>=7.1.2",
"dbt-core~=1.6.0",
"dbt-semantic-interfaces~=0.2.0",
"dbt-core~=1.7.0rc1",
"dbt-semantic-interfaces~=0.3.0",
"graphviz>=0.18.2, <0.21",
"halo~=0.0.31",
"more-itertools>=8.10.0, <10.2.0",
Expand Down Expand Up @@ -75,7 +75,7 @@ sql-client-packages = [
]

dbt-postgres = [
"dbt-postgres~=1.6.0",
"dbt-postgres~=1.7.0rc1",
]

dbt-bigquery = [
Expand Down Expand Up @@ -114,9 +114,13 @@ exclude = [
description = "Environment for development. Includes a DuckDB-backed client."
features = [
"dev-packages",
"dbt-duckdb",
"sql-client-packages",
]
# Temporary workaround for installing the dbt-duckdb adapter until it's updated to support dbt-core~=1.7.0rc1.
post-install-commands = [
"pip install duckdb>=0.7.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, we need to manually install the adapter dependencies? That's a bummer.

"pip install --no-deps dbt-duckdb~=1.6.0",
]

[tool.hatch.envs.dev-env.env-vars]
MF_TEST_ADAPTER_TYPE="duckdb"
Expand Down