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

Use time spines to determine metric_time grain in no-metric queries #1355

Closed
wants to merge 4 commits into from
Closed
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,7 @@
from metricflow_semantics.model.semantics.linkable_element_set import LinkableElementSet
from metricflow_semantics.model.semantics.semantic_model_join_evaluator import SemanticModelJoinEvaluator
from metricflow_semantics.specs.time_dimension_spec import DEFAULT_TIME_GRANULARITY
from metricflow_semantics.time.time_spine_source import TimeSpineSource

if TYPE_CHECKING:
from metricflow_semantics.model.semantics.semantic_model_lookup import SemanticModelLookup
Expand Down Expand Up @@ -124,6 +125,7 @@ def __init__(
# Sort semantic models by name for consistency in building derived objects.
self._semantic_models = sorted(self._semantic_manifest.semantic_models, key=lambda x: x.name)
self._join_evaluator = SemanticModelJoinEvaluator(semantic_model_lookup)
self._time_spine_sources = TimeSpineSource.create_from_manifest(self._semantic_manifest)

assert max_entity_links >= 0
self._max_entity_links = max_entity_links
Expand Down Expand Up @@ -464,7 +466,8 @@ def _get_metric_time_elements(self, measure_reference: Optional[MeasureReference
time_dimension_reference=measure_agg_time_dimension_reference,
)
else:
defined_granularity = DEFAULT_TIME_GRANULARITY
# If querying metric_time without metrics, will query from time spines.
defined_granularity = min(self._time_spine_sources.keys())

# It's possible to aggregate measures to coarser time granularities (except with cumulative metrics).
possible_metric_time_granularities = tuple(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ def without_first_entity_link(self) -> TimeDimensionSpec: # noqa: D102

@property
def without_entity_links(self) -> TimeDimensionSpec: # noqa: D102
return TimeDimensionSpec.from_name(self.element_name)
return TimeDimensionSpec(
element_name=self.element_name,
time_granularity=self.time_granularity,
date_part=self.date_part,
entity_links=(),
)

@staticmethod
def from_name(name: str) -> TimeDimensionSpec: # noqa: D102
Expand Down Expand Up @@ -203,6 +208,3 @@ def generate_possible_specs_for_time_dimension(
@property
def is_metric_time(self) -> bool: # noqa: D102
return self.element_name == METRIC_TIME_ELEMENT_NAME


DEFAULT_TIME_GRANULARITY = TimeGranularity.DAY
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
# Shortcuts for referring to the metric time dimension.
MTD = MetricFlowReservedKeywords.METRIC_TIME.value
MTD_REFERENCE = TimeDimensionReference(element_name=MetricFlowReservedKeywords.METRIC_TIME.value)
MTD_SPEC_NANOSECOND = TimeDimensionSpec(
element_name=MetricFlowReservedKeywords.METRIC_TIME.value,
entity_links=(),
time_granularity=TimeGranularity.NANOSECOND,
)
MTD_SPEC_DAY = TimeDimensionSpec(
element_name=MetricFlowReservedKeywords.METRIC_TIME.value, entity_links=(), time_granularity=TimeGranularity.DAY
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,40 @@ project_configuration:
- location: $source_schema.mf_time_spine
column_name: ds
grain: day
time_spines:
- node_relation:
alias: mf_time_spine_nanosecond
schema_name: $source_schema
primary_column:
name: ts
time_granularity: nanosecond
- node_relation:
alias: mf_time_spine_microsecond
schema_name: $source_schema
primary_column:
name: ts
time_granularity: microsecond
- node_relation:
alias: mf_time_spine_millisecond
schema_name: $source_schema
primary_column:
name: ts
time_granularity: millisecond
- node_relation:
alias: mf_time_spine_second
schema_name: $source_schema
primary_column:
name: ts
time_granularity: second
- node_relation:
alias: mf_time_spine_minute
schema_name: $source_schema
primary_column:
name: ts
time_granularity: minute
- node_relation:
alias: mf_time_spine_hour
schema_name: $source_schema
primary_column:
name: ts
time_granularity: hour
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
MetricGroupByItemResolutionNode,
)
from metricflow_semantics.test_helpers.config_helpers import MetricFlowTestConfiguration
from metricflow_semantics.test_helpers.metric_time_dimension import MTD_SPEC_DAY, MTD_SPEC_MONTH, MTD_SPEC_YEAR
from metricflow_semantics.test_helpers.metric_time_dimension import MTD_SPEC_MONTH, MTD_SPEC_NANOSECOND, MTD_SPEC_YEAR
from metricflow_semantics.test_helpers.snapshot_helpers import assert_object_snapshot_equal

from tests_metricflow_semantics.query.group_by_item.conftest import AmbiguousResolutionQueryId
Expand Down Expand Up @@ -46,7 +46,7 @@ def test_ambiguous_metric_time_in_query( # noqa: D103
)

if case_id is AmbiguousResolutionQueryId.NO_METRICS:
assert result.spec == MTD_SPEC_DAY
assert result.spec == MTD_SPEC_NANOSECOND
elif case_id is AmbiguousResolutionQueryId.SIMPLE_METRIC:
assert result.spec == MTD_SPEC_MONTH
elif case_id is AmbiguousResolutionQueryId.METRICS_WITH_SAME_TIME_GRAINS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,55 @@
"TimeDimension('metric_time', 'day', date_part_name='month')",
"TimeDimension('metric_time', 'day', date_part_name='quarter')",
"TimeDimension('metric_time', 'day', date_part_name='year')",
"TimeDimension('metric_time', 'hour')",
"TimeDimension('metric_time', 'hour', date_part_name='day')",
"TimeDimension('metric_time', 'hour', date_part_name='dow')",
"TimeDimension('metric_time', 'hour', date_part_name='doy')",
"TimeDimension('metric_time', 'hour', date_part_name='month')",
"TimeDimension('metric_time', 'hour', date_part_name='quarter')",
"TimeDimension('metric_time', 'hour', date_part_name='year')",
"TimeDimension('metric_time', 'microsecond')",
"TimeDimension('metric_time', 'microsecond', date_part_name='day')",
"TimeDimension('metric_time', 'microsecond', date_part_name='dow')",
"TimeDimension('metric_time', 'microsecond', date_part_name='doy')",
"TimeDimension('metric_time', 'microsecond', date_part_name='month')",
"TimeDimension('metric_time', 'microsecond', date_part_name='quarter')",
"TimeDimension('metric_time', 'microsecond', date_part_name='year')",
"TimeDimension('metric_time', 'millisecond')",
"TimeDimension('metric_time', 'millisecond', date_part_name='day')",
"TimeDimension('metric_time', 'millisecond', date_part_name='dow')",
"TimeDimension('metric_time', 'millisecond', date_part_name='doy')",
"TimeDimension('metric_time', 'millisecond', date_part_name='month')",
"TimeDimension('metric_time', 'millisecond', date_part_name='quarter')",
"TimeDimension('metric_time', 'millisecond', date_part_name='year')",
"TimeDimension('metric_time', 'minute')",
"TimeDimension('metric_time', 'minute', date_part_name='day')",
"TimeDimension('metric_time', 'minute', date_part_name='dow')",
"TimeDimension('metric_time', 'minute', date_part_name='doy')",
"TimeDimension('metric_time', 'minute', date_part_name='month')",
"TimeDimension('metric_time', 'minute', date_part_name='quarter')",
"TimeDimension('metric_time', 'minute', date_part_name='year')",
"TimeDimension('metric_time', 'month')",
"TimeDimension('metric_time', 'month', date_part_name='month')",
"TimeDimension('metric_time', 'month', date_part_name='quarter')",
"TimeDimension('metric_time', 'month', date_part_name='year')",
"TimeDimension('metric_time', 'nanosecond')",
"TimeDimension('metric_time', 'nanosecond', date_part_name='day')",
"TimeDimension('metric_time', 'nanosecond', date_part_name='dow')",
"TimeDimension('metric_time', 'nanosecond', date_part_name='doy')",
"TimeDimension('metric_time', 'nanosecond', date_part_name='month')",
"TimeDimension('metric_time', 'nanosecond', date_part_name='quarter')",
"TimeDimension('metric_time', 'nanosecond', date_part_name='year')",
"TimeDimension('metric_time', 'quarter')",
"TimeDimension('metric_time', 'quarter', date_part_name='quarter')",
"TimeDimension('metric_time', 'quarter', date_part_name='year')",
"TimeDimension('metric_time', 'second')",
"TimeDimension('metric_time', 'second', date_part_name='day')",
"TimeDimension('metric_time', 'second', date_part_name='dow')",
"TimeDimension('metric_time', 'second', date_part_name='doy')",
"TimeDimension('metric_time', 'second', date_part_name='month')",
"TimeDimension('metric_time', 'second', date_part_name='quarter')",
"TimeDimension('metric_time', 'second', date_part_name='year')",
"TimeDimension('metric_time', 'week')",
"TimeDimension('metric_time', 'week', date_part_name='month')",
"TimeDimension('metric_time', 'week', date_part_name='quarter')",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
GroupByItemResolution(
spec=TimeDimensionSpec(element_name='metric_time', time_granularity=DAY),
spec=TimeDimensionSpec(element_name='metric_time', time_granularity=NANOSECOND),
linkable_element_set=LinkableElementSet(
path_key_to_linkable_dimensions={
ElementPathKey(
element_name='metric_time',
element_type=TIME_DIMENSION,
time_granularity=DAY,
time_granularity=NANOSECOND,
): (
LinkableDimension(
properties=(METRIC_TIME,),
Expand All @@ -16,7 +16,7 @@ GroupByItemResolution(
semantic_model_name='__VIRTUAL__',
),
),
time_granularity=DAY,
time_granularity=NANOSECOND,
),
),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@
'lux_listing__listing__lux_listing__twice_bookings_fill_nulls_with_0_without_time_spine',
'lux_listing__listing__lux_listing__views',
'lux_listing__listing__lux_listing__views_times_booking_value',
'metric_time__day',
'metric_time__nanosecond',
'revenue_instance__ds__day',
'revenue_instance__ds__extract_day',
'revenue_instance__ds__extract_dow',
Expand Down
11 changes: 7 additions & 4 deletions metricflow/dataflow/builder/node_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,23 +200,26 @@ def _find_joinable_candidate_nodes_that_can_satisfy_linkable_specs(
candidates_for_join: List[JoinLinkableInstancesRecipe] = []
left_node_spec_set = left_node_instance_set.spec_set
for right_node in self._nodes_available_for_joins:
data_set_in_right_node: SqlDataSet = self._node_data_set_resolver.get_output_data_set(right_node)
linkable_specs_in_right_node = data_set_in_right_node.instance_set.spec_set.linkable_specs

# If right node is time spine source node, use cross join.
if right_node in self._time_spine_nodes:
needed_metric_time_specs = group_specs_by_type(needed_linkable_specs).metric_time_specs
satisfiable_metric_time_specs = set(needed_linkable_specs).intersection(
set(linkable_specs_in_right_node)
)
candidates_for_join.append(
JoinLinkableInstancesRecipe(
node_to_join=right_node,
join_on_entity=None,
satisfiable_linkable_specs=list(needed_metric_time_specs),
satisfiable_linkable_specs=list(satisfiable_metric_time_specs),
join_on_partition_dimensions=(),
join_on_partition_time_dimensions=(),
join_type=SqlJoinType.CROSS_JOIN,
)
)
continue

data_set_in_right_node: SqlDataSet = self._node_data_set_resolver.get_output_data_set(right_node)
linkable_specs_in_right_node = data_set_in_right_node.instance_set.spec_set.linkable_specs
entity_specs_in_right_node = data_set_in_right_node.instance_set.spec_set.entity_specs

# For each unlinked entity in the data set, create a candidate for joining.
Expand Down
4 changes: 2 additions & 2 deletions metricflow/dataset/convert_semantic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def _create_time_dimension_instance(
self,
element_name: str,
entity_links: Tuple[EntityReference, ...],
time_granularity: TimeGranularity = DEFAULT_TIME_GRANULARITY,
time_granularity: TimeGranularity,
date_part: Optional[DatePart] = None,
semantic_model_name: Optional[str] = None,
) -> TimeDimensionInstance:
Expand Down Expand Up @@ -332,7 +332,7 @@ def _build_time_dimension_instances_and_columns(
) -> Tuple[List[TimeDimensionInstance], List[SqlSelectColumn]]:
time_dimension_instances: List[TimeDimensionInstance] = []
select_columns: List[SqlSelectColumn] = []
# Add time dimensions with a smaller granularity for ease in query resolution
# Add time dimensions with a larger granularity for ease in query resolution
for time_granularity in TimeGranularity:
if time_granularity.to_int() > defined_time_granularity.to_int():
time_dimension_instance = self._create_time_dimension_instance(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
table_snapshot:
table_name: mf_time_spine_hour
column_definitions:
- name: ts
type: TIME
rows:
- ["2020-01-01 01:00:00"]
- ["2020-01-01 02:00:00"]
- ["2020-01-01 03:00:00"]
- ["2020-01-01 04:00:00"]
- ["2020-01-01 05:00:00"]
- ["2020-01-01 06:00:00"]
- ["2020-01-01 07:00:00"]
- ["2020-01-01 08:00:00"]
- ["2020-01-01 09:00:00"]
- ["2020-01-01 010:00:00"]
- ["2020-01-01 11:00:00"]
- ["2020-01-01 12:00:00"]
- ["2020-01-02 01:00:00"]
- ["2020-01-02 02:00:00"]
- ["2020-01-02 03:00:00"]
- ["2020-01-02 04:00:00"]
- ["2020-01-02 05:00:00"]
- ["2020-01-02 06:00:00"]
- ["2020-01-02 07:00:00"]
- ["2020-01-02 08:00:00"]
- ["2020-01-02 09:00:00"]
- ["2020-01-02 010:00:00"]
- ["2020-01-02 11:00:00"]
- ["2020-01-02 12:00:00"]
- ["2020-01-03 01:00:00"]
- ["2020-01-03 02:00:00"]
- ["2020-01-03 03:00:00"]
- ["2020-01-03 04:00:00"]
- ["2020-01-03 05:00:00"]
- ["2020-01-03 06:00:00"]
- ["2020-01-03 07:00:00"]
- ["2020-01-03 08:00:00"]
- ["2020-01-03 09:00:00"]
- ["2020-01-03 010:00:00"]
- ["2020-01-03 11:00:00"]
- ["2020-01-03 12:00:00"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
table_snapshot:
table_name: mf_time_spine_microsecond
column_definitions:
- name: ts
type: TIME
rows:
- ["2020-01-01 00:00:00.000000"]
- ["2020-01-01 00:00:00.000001"]
- ["2020-01-01 00:00:00.000002"]
- ["2020-01-01 00:00:00.000003"]
- ["2020-01-01 00:00:00.000004"]
- ["2020-01-01 00:00:00.000005"]
- ["2020-01-01 00:00:00.000006"]
- ["2020-01-01 00:00:00.000007"]
- ["2020-01-01 00:00:00.000008"]
- ["2020-01-01 00:00:00.000009"]
- ["2020-01-01 00:00:00.000010"]
- ["2020-01-01 00:00:00.000011"]
- ["2020-01-01 00:00:00.000012"]
- ["2020-01-01 00:00:00.000013"]
- ["2020-01-01 00:00:00.000014"]
- ["2020-01-01 00:00:00.000015"]
- ["2020-01-01 00:00:00.000016"]
- ["2020-01-01 00:00:00.000017"]
- ["2020-01-01 00:00:00.000018"]
- ["2020-01-01 00:00:00.000019"]
- ["2020-01-01 00:00:00.000020"]
- ["2020-01-01 00:00:00.000021"]
- ["2020-01-01 00:00:00.000022"]
- ["2020-01-01 00:00:00.000023"]
- ["2020-01-01 00:00:00.000024"]
- ["2020-01-01 00:00:00.000025"]
- ["2020-01-01 00:00:00.000026"]
- ["2020-01-01 00:00:00.000027"]
- ["2020-01-01 00:00:00.000028"]
- ["2020-01-01 00:00:00.000029"]
- ["2020-01-01 00:00:00.000030"]

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
table_snapshot:
table_name: mf_time_spine_millisecond
column_definitions:
- name: ts
type: TIME
rows:
- ["2020-01-01 00:00:00.001"]
- ["2020-01-01 00:00:00.002"]
- ["2020-01-01 00:00:00.003"]
- ["2020-01-01 00:00:00.004"]
- ["2020-01-01 00:00:00.005"]
- ["2020-01-01 00:00:00.006"]
- ["2020-01-01 00:00:00.007"]
- ["2020-01-01 00:00:00.008"]
- ["2020-01-01 00:00:00.009"]
- ["2020-01-01 00:00:00.010"]
- ["2020-01-01 00:00:00.011"]
- ["2020-01-01 00:00:00.012"]
- ["2020-01-01 00:00:00.013"]
- ["2020-01-01 00:00:00.014"]
- ["2020-01-01 00:00:00.015"]
- ["2020-01-01 00:00:00.016"]
- ["2020-01-01 00:00:00.017"]
- ["2020-01-01 00:00:00.018"]
- ["2020-01-01 00:00:00.019"]
- ["2020-01-01 00:00:00.020"]
- ["2020-01-01 00:00:00.021"]
- ["2020-01-01 00:00:00.022"]
- ["2020-01-01 00:00:00.023"]
- ["2020-01-01 00:00:00.024"]
- ["2020-01-01 00:00:00.025"]
- ["2020-01-01 00:00:00.026"]
- ["2020-01-01 00:00:00.027"]
- ["2020-01-01 00:00:00.028"]
- ["2020-01-01 00:00:00.029"]
- ["2020-01-01 00:00:00.030"]

Loading
Loading