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

Bug fix for derived offset filters #1531

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def contains_metric_time(self) -> bool:
"""Returns true if this set contains a spec referring to metric time at any grain."""
return len(self.metric_time_specs) > 0

@property
def contains_only_metric_time(self) -> bool:
"""If all specs are metric_time specs, return True.."""
return set(self.metric_time_specs) == set(self.as_tuple)

@property
def time_dimension_specs_with_custom_grain(self) -> Tuple[TimeDimensionSpec, ...]: # noqa: D102
return tuple([spec for spec in self.time_dimension_specs if spec.time_granularity.is_custom_granularity])
Expand Down
10 changes: 10 additions & 0 deletions metricflow-semantics/metricflow_semantics/specs/metric_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,13 @@ def has_time_offset(self) -> bool: # noqa: D102
def without_offset(self) -> MetricSpec:
"""Represents the metric spec with any time offsets removed."""
return MetricSpec(element_name=self.element_name, filter_spec_set=self.filter_spec_set, alias=self.alias)

def with_new_filter_spec_set(self, new_filter_spec_set: WhereFilterSpecSet) -> MetricSpec:
"""Represents the metric spec with a new filter spec set."""
return MetricSpec(
element_name=self.element_name,
filter_spec_set=new_filter_spec_set,
alias=self.alias,
offset_window=self.offset_window,
offset_to_grain=self.offset_to_grain,
)
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,13 @@ def merge(self, other: WhereFilterSpecSet) -> WhereFilterSpecSet:
metric_level_filter_specs=self.metric_level_filter_specs + other.metric_level_filter_specs,
query_level_filter_specs=self.query_level_filter_specs + other.query_level_filter_specs,
)

def replace_query_level_filter_specs(
self, new_query_level_filter_specs: Tuple[WhereFilterSpec, ...]
) -> WhereFilterSpecSet:
"""Return the same set but with the query level filter specs replaced."""
return WhereFilterSpecSet(
measure_level_filter_specs=self.measure_level_filter_specs,
metric_level_filter_specs=self.metric_level_filter_specs,
query_level_filter_specs=new_query_level_filter_specs,
)
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ metric:
metric:
name: "bookings_growth_since_start_of_month"
description: |
percentage growth of bookings since the start of the month,
number of bookings since the start of the month,
used to test derived metrics with an offset_to_grain.
type: derived
type_params:
Expand All @@ -506,6 +506,24 @@ metric:
offset_to_grain: month
alias: bookings_at_start_of_month
---
metric:
name: bookings_offset_to_grain_twice_with_tiered_filters
description: tests a nested offset_to_grain metric with filters at each level
type: derived
type_params:
expr: bookings_this_month_wtd - bookings
metrics:
- name: bookings
- name: bookings_growth_since_start_of_month
offset_to_grain: week
alias: bookings_this_month_wtd
filter:
- "{{ TimeDimension('booking__ds', 'year') }} >= '2019-01-01'"
- "{{ TimeDimension('listing__created_at', 'day') }} >= '2020-01-02'"
filter:
- "{{ TimeDimension('metric_time', 'month') }} >= '2020-01-01'"
- "{{ Dimension('booking__is_instant') }}"
---
metric:
name: "bookings_month_start_compared_to_1_month_prior"
description: |
Expand Down Expand Up @@ -700,7 +718,7 @@ metric:
alias: bookings_2_weeks_ago
---
metric:
name: "bookings_offset_once"
name: bookings_offset_once
description: bookings metric offset once.
type: derived
type_params:
Expand All @@ -710,7 +728,7 @@ metric:
offset_window: 5 days
---
metric:
name: "bookings_offset_twice"
name: bookings_offset_twice
description: bookings metric offset twice.
type: derived
type_params:
Expand All @@ -719,6 +737,22 @@ metric:
- name: bookings_offset_once
offset_window: 2 days
---
metric:
name: bookings_offset_twice_with_tiered_filters
description: bookings metric offset twice with tiered filters
type: derived
type_params:
expr: bookings_offset_once
metrics:
- name: bookings_offset_once
offset_window: 1 month
filter:
- "{{ TimeDimension('metric_time', 'month') }} >= '2019-12-01'"
- "{{ Dimension('booking__is_instant') }}"
filter:
- "{{ TimeDimension('metric_time', 'year') }} >= '2019-01-01'"
- "{{ Entity('listing') }} IS NOT NULL"
---
metric:
name: bookings_at_start_of_month
description: |
Expand Down
4 changes: 3 additions & 1 deletion metricflow/dataflow/builder/builder_cache.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Optional
from typing import Optional, Sequence

from metricflow_semantics.collection_helpers.lru_cache import LruCache
from metricflow_semantics.specs.linkable_spec_set import LinkableSpecSet
from metricflow_semantics.specs.metric_spec import MetricSpec
from metricflow_semantics.specs.where_filter.where_filter_spec import WhereFilterSpec
from metricflow_semantics.specs.where_filter.where_filter_transform import WhereSpecFactory

from metricflow.dataflow.builder.measure_spec_properties import MeasureSpecProperties
Expand Down Expand Up @@ -39,6 +40,7 @@ class BuildAnyMetricOutputNodeParameterSet:
filter_spec_factory: WhereSpecFactory
predicate_pushdown_state: PredicatePushdownState
for_group_by_source_node: bool
time_spine_filter_specs: Sequence[WhereFilterSpec] = ()


class DataflowPlanBuilderCache:
Expand Down
Loading
Loading