diff --git a/dbt-metricflow/dbt_metricflow/cli/dbt_connectors/adapter_backed_client.py b/dbt-metricflow/dbt_metricflow/cli/dbt_connectors/adapter_backed_client.py index 4650f94f30..082a67c594 100644 --- a/dbt-metricflow/dbt_metricflow/cli/dbt_connectors/adapter_backed_client.py +++ b/dbt-metricflow/dbt_metricflow/cli/dbt_connectors/adapter_backed_client.py @@ -136,6 +136,7 @@ def query( sql_bind_parameter_set: The parameter replacement mapping for filling in concrete values for SQL query parameters. """ + print("sql:", stmt) start = time.time() request_id = SqlRequestId(f"mf_rid__{random_id()}") if sql_bind_parameter_set.param_dict: diff --git a/metricflow-semantics/metricflow_semantics/specs/time_dimension_spec.py b/metricflow-semantics/metricflow_semantics/specs/time_dimension_spec.py index dec834adce..62211493d8 100644 --- a/metricflow-semantics/metricflow_semantics/specs/time_dimension_spec.py +++ b/metricflow-semantics/metricflow_semantics/specs/time_dimension_spec.py @@ -195,7 +195,7 @@ def with_aggregation_state(self, aggregation_state: AggregationState) -> TimeDim window_function=self.window_function, ) - def with_window_function(self, window_function: SqlWindowFunction) -> TimeDimensionSpec: # noqa: D102 + def with_window_function(self, window_function: Optional[SqlWindowFunction]) -> TimeDimensionSpec: # noqa: D102 return TimeDimensionSpec( element_name=self.element_name, entity_links=self.entity_links, diff --git a/metricflow-semantics/metricflow_semantics/sql/sql_exprs.py b/metricflow-semantics/metricflow_semantics/sql/sql_exprs.py index 7d432661fe..4ceb7aaab3 100644 --- a/metricflow-semantics/metricflow_semantics/sql/sql_exprs.py +++ b/metricflow-semantics/metricflow_semantics/sql/sql_exprs.py @@ -71,6 +71,14 @@ def as_window_function_expression(self) -> Optional[SqlWindowFunctionExpression] """If this is a window function expression, return self.""" return None + @property + def is_verbose(self) -> bool: + """Denotes if the statement is typically verbose, and therefore can be hard to read when optimized. + + This is helpful in determining if statements will be harder to read when collapsed. + """ + return False + @abstractmethod def rewrite( self, @@ -1017,7 +1025,7 @@ class SqlWindowFunction(Enum): LAST_VALUE = "LAST_VALUE" AVERAGE = "AVG" ROW_NUMBER = "ROW_NUMBER" - LAG = "LAG" + LEAD = "LEAD" @property def requires_ordering(self) -> bool: @@ -1026,7 +1034,7 @@ def requires_ordering(self) -> bool: self is SqlWindowFunction.FIRST_VALUE or self is SqlWindowFunction.LAST_VALUE or self is SqlWindowFunction.ROW_NUMBER - or self is SqlWindowFunction.LAG + or self is SqlWindowFunction.LEAD ): return True elif self is SqlWindowFunction.AVERAGE: @@ -1197,6 +1205,10 @@ def matches(self, other: SqlExpressionNode) -> bool: # noqa: D102 and self.sql_function_args == other.sql_function_args ) + @property + def is_verbose(self) -> bool: # noqa: D102 + return True + @dataclass(frozen=True, eq=False) class SqlNullExpression(SqlExpressionNode): @@ -1884,6 +1896,10 @@ def matches(self, other: SqlExpressionNode) -> bool: # noqa: D102 return False return self.when_to_then_exprs == other.when_to_then_exprs and self.else_expr == other.else_expr + @property + def is_verbose(self) -> bool: # noqa: D102 + return True + class SqlArithmeticOperator(Enum): """Arithmetic operator used to do math in a SQL expression.""" diff --git a/metricflow/dataflow/builder/dataflow_plan_builder.py b/metricflow/dataflow/builder/dataflow_plan_builder.py index 4d8056ba14..ec7cf59c14 100644 --- a/metricflow/dataflow/builder/dataflow_plan_builder.py +++ b/metricflow/dataflow/builder/dataflow_plan_builder.py @@ -1889,10 +1889,14 @@ def _build_time_spine_node( required_time_spine_specs = required_time_spine_spec_set.time_dimension_specs should_dedupe = False + filter_to_specs = tuple(queried_time_spine_specs) if offset_window and self._offset_window_is_custom(offset_window): time_spine_node = self._build_custom_offset_time_spine_node( offset_window=offset_window, required_time_spine_specs=required_time_spine_specs ) + filter_to_specs = self._node_data_set_resolver.get_output_data_set( + time_spine_node + ).instance_set.spec_set.time_dimension_specs else: # For simpler time spine queries, choose the appropriate time spine node and apply requested aliases. time_spine_source = self._choose_time_spine_source(required_time_spine_specs) @@ -1920,7 +1924,7 @@ def _build_time_spine_node( return self._build_pre_aggregation_plan( source_node=time_spine_node, - filter_to_specs=InstanceSpecSet(time_dimension_specs=tuple(queried_time_spine_specs)), + filter_to_specs=InstanceSpecSet(time_dimension_specs=filter_to_specs), time_range_constraint=time_range_constraint, where_filter_specs=where_filter_specs, distinct=should_dedupe, diff --git a/metricflow/plan_conversion/dataflow_to_sql.py b/metricflow/plan_conversion/dataflow_to_sql.py index 7fe36dc043..16d820076d 100644 --- a/metricflow/plan_conversion/dataflow_to_sql.py +++ b/metricflow/plan_conversion/dataflow_to_sql.py @@ -1447,47 +1447,74 @@ def visit_join_to_time_spine_node(self, node: JoinToTimeSpineNode) -> SqlDataSet time_spine_alias = self._next_unique_table_alias() required_agg_time_dimension_specs = tuple(node.requested_agg_time_dimension_specs) - if node.join_on_time_dimension_spec not in node.requested_agg_time_dimension_specs: + join_spec_was_requested = node.join_on_time_dimension_spec in node.requested_agg_time_dimension_specs + if not join_spec_was_requested: + # Why is this necessary? Really getting in the way here!! Can we remove it? required_agg_time_dimension_specs += (node.join_on_time_dimension_spec,) # Build join expression. - join_column_name = self._column_association_resolver.resolve_spec(node.join_on_time_dimension_spec).column_name + parent_join_column_name = self._column_association_resolver.resolve_spec( + node.join_on_time_dimension_spec + ).column_name + time_spine_jon_column_name = time_spine_data_set.instance_from_time_dimension_grain_and_date_part( + time_granularity_name=node.join_on_time_dimension_spec.time_granularity.name, date_part=None + ).associated_column.column_name join_description = SqlQueryPlanJoinBuilder.make_join_to_time_spine_join_description( node=node, time_spine_alias=time_spine_alias, - agg_time_dimension_column_name=join_column_name, + time_spine_column_name=time_spine_jon_column_name, + parent_column_name=parent_join_column_name, parent_sql_select_node=parent_data_set.checked_sql_select_node, parent_alias=parent_alias, ) - # Build combined instance set. + # Build new instances and columns. time_spine_required_spec_set = InstanceSpecSet(time_dimension_specs=required_agg_time_dimension_specs) - parent_instance_set = parent_data_set.instance_set.transform( + output_parent_instance_set = parent_data_set.instance_set.transform( FilterElements(exclude_specs=time_spine_required_spec_set) ) - time_spine_instance_set = time_spine_data_set.instance_set.transform( - FilterElements(include_specs=time_spine_required_spec_set) - ) - output_instance_set = InstanceSet.merge([parent_instance_set, time_spine_instance_set]) + output_time_spine_instances: Tuple[TimeDimensionInstance, ...] = () + output_time_spine_columns: Tuple[SqlSelectColumn, ...] = () + for old_instance in time_spine_data_set.instance_set.time_dimension_instances: + new_spec = old_instance.spec.with_window_function(None) + if new_spec not in required_agg_time_dimension_specs: + continue + if old_instance.spec.window_function: + new_instance = old_instance.with_new_spec( + new_spec=new_spec, column_association_resolver=self._column_association_resolver + ) + column = SqlSelectColumn( + expr=SqlColumnReferenceExpression.from_table_and_column_names( + table_alias=time_spine_alias, column_name=old_instance.associated_column.column_name + ), + column_alias=new_instance.associated_column.column_name, + ) + else: + new_instance = old_instance + column = SqlSelectColumn.from_table_and_column_names( + table_alias=time_spine_alias, column_name=old_instance.associated_column.column_name + ) + output_time_spine_instances += (new_instance,) + output_time_spine_columns += (column,) - # Build new simple select columns. - select_columns = create_simple_select_columns_for_instance_sets( + output_instance_set = InstanceSet.merge( + [output_parent_instance_set, InstanceSet(time_dimension_instances=output_time_spine_instances)] + ) + select_columns = output_time_spine_columns + create_simple_select_columns_for_instance_sets( self._column_association_resolver, - OrderedDict({parent_alias: parent_instance_set, time_spine_alias: time_spine_instance_set}), + OrderedDict({parent_alias: output_parent_instance_set}), ) # If offset_to_grain is used, will need to filter down to rows that match selected granularities. # Does not apply if one of the granularities selected matches the time spine column granularity. where_filter: Optional[SqlExpressionNode] = None - need_where_filter = ( - node.offset_to_grain and node.join_on_time_dimension_spec not in node.requested_agg_time_dimension_specs - ) + need_where_filter = node.offset_to_grain and not join_spec_was_requested # Filter down to one row per granularity period requested in the group by. Any other granularities # included here will be filtered out before aggregation and so should not be included in where filter. if need_where_filter: join_column_expr = SqlColumnReferenceExpression.from_table_and_column_names( - table_alias=time_spine_alias, column_name=join_column_name + table_alias=time_spine_alias, column_name=parent_join_column_name ) for requested_spec in node.requested_agg_time_dimension_specs: column_name = self._column_association_resolver.resolve_spec(requested_spec).column_name @@ -2178,7 +2205,7 @@ def visit_offset_by_custom_granularity_node(self, node: OffsetByCustomGranularit first_value_offset_column, last_value_offset_column = tuple( SqlSelectColumn( expr=SqlWindowFunctionExpression.create( - sql_function=SqlWindowFunction.LAG, + sql_function=SqlWindowFunction.LEAD, sql_function_args=( SqlColumnReferenceExpression.from_table_and_column_names( column_name=instance.associated_column.column_name, @@ -2203,9 +2230,6 @@ def visit_offset_by_custom_granularity_node(self, node: OffsetByCustomGranularit # Offset the base column by the requested window. If the offset date is not within the offset custom grain period, # default to the last value in that period. - new_custom_grain_column = SqlSelectColumn.from_table_and_column_names( - column_name=custom_grain_column_name, table_alias=bounds_data_set_alias - ) first_value_offset_expr, last_value_offset_expr = [ SqlColumnReferenceExpression.from_table_and_column_names( column_name=offset_column.column_alias, table_alias=offset_bounds_subquery_alias @@ -2228,12 +2252,20 @@ def visit_offset_by_custom_granularity_node(self, node: OffsetByCustomGranularit comparison=SqlComparison.LESS_THAN_OR_EQUALS, right_expr=last_value_offset_expr, ) + offset_base_instance = base_grain_instance.with_new_spec( + # LAG isn't quite accurate here, but this will differentiate the offset instance (and column) from the original one. + new_spec=base_grain_instance.spec.with_window_function(SqlWindowFunction.LEAD), + column_association_resolver=self._column_association_resolver, + ) offset_base_column = SqlSelectColumn( expr=SqlCaseExpression.create( when_to_then_exprs={is_below_last_value_expr: offset_base_grain_expr}, else_expr=last_value_offset_expr, ), - column_alias=base_grain_instance.associated_column.column_name, + column_alias=offset_base_instance.associated_column.column_name, + ) + original_base_grain_column = SqlSelectColumn.from_table_and_column_names( + column_name=base_grain_instance.associated_column.column_name, table_alias=bounds_data_set_alias ) join_desc = SqlJoinDescription( right_source=offset_bounds_subquery, @@ -2251,7 +2283,7 @@ def visit_offset_by_custom_granularity_node(self, node: OffsetByCustomGranularit ) offset_base_grain_subquery = SqlSelectStatementNode.create( description=node.description, - select_columns=(new_custom_grain_column, offset_base_column), + select_columns=(original_base_grain_column, offset_base_column), from_source=bounds_data_set.checked_sql_select_node, from_source_alias=bounds_data_set_alias, join_descs=(join_desc,), @@ -2261,41 +2293,42 @@ def visit_offset_by_custom_granularity_node(self, node: OffsetByCustomGranularit # Apply standard grains & date parts requested in the query. Use base grain for any custom grains. standard_grain_instances: Tuple[TimeDimensionInstance, ...] = () standard_grain_columns: Tuple[SqlSelectColumn, ...] = () - base_column = SqlSelectColumn( + offset_base_column_ref = SqlSelectColumn( expr=SqlColumnReferenceExpression.from_table_and_column_names( - column_name=base_grain_instance.associated_column.column_name, + column_name=offset_base_instance.associated_column.column_name, table_alias=offset_base_grain_subquery_alias, ), column_alias=base_grain_instance.associated_column.column_name, ) - base_grain_requested = False for spec in node.required_time_spine_specs: new_instance = base_grain_instance.with_new_spec( new_spec=spec, column_association_resolver=self._column_association_resolver ) standard_grain_instances += (new_instance,) if spec.date_part: - expr: SqlExpressionNode = SqlExtractExpression.create(date_part=spec.date_part, arg=base_column.expr) + expr: SqlExpressionNode = SqlExtractExpression.create( + date_part=spec.date_part, arg=offset_base_column_ref.expr + ) elif spec.time_granularity.base_granularity == base_grain.base_granularity: - expr = base_column.expr - base_grain_requested = True + expr = offset_base_column_ref.expr else: expr = SqlDateTruncExpression.create( - time_granularity=spec.time_granularity.base_granularity, arg=base_column.expr + time_granularity=spec.time_granularity.base_granularity, arg=offset_base_column_ref.expr ) standard_grain_columns += ( SqlSelectColumn(expr=expr, column_alias=new_instance.associated_column.column_name), ) - if not base_grain_requested: - assert 0 - standard_grain_instances = (base_grain_instance,) + standard_grain_instances - standard_grain_columns = (base_column,) + standard_grain_columns + + # Need to keep the non-offset base grain column in the output. This will be used to join to the source data set. + non_offset_base_grain_column = SqlSelectColumn.from_table_and_column_names( + column_name=base_grain_instance.associated_column.column_name, table_alias=offset_base_grain_subquery_alias + ) return SqlDataSet( - instance_set=InstanceSet(time_dimension_instances=standard_grain_instances), + instance_set=InstanceSet(time_dimension_instances=(base_grain_instance,) + standard_grain_instances), sql_select_node=SqlSelectStatementNode.create( description="Apply Requested Granularities", - select_columns=standard_grain_columns, + select_columns=(non_offset_base_grain_column,) + standard_grain_columns, from_source=offset_base_grain_subquery, from_source_alias=offset_base_grain_subquery_alias, ), diff --git a/metricflow/plan_conversion/sql_join_builder.py b/metricflow/plan_conversion/sql_join_builder.py index f80cdf2287..9f2e37743d 100644 --- a/metricflow/plan_conversion/sql_join_builder.py +++ b/metricflow/plan_conversion/sql_join_builder.py @@ -527,13 +527,14 @@ def make_cumulative_metric_time_range_join_description( def make_join_to_time_spine_join_description( node: JoinToTimeSpineNode, time_spine_alias: str, - agg_time_dimension_column_name: str, + time_spine_column_name: str, + parent_column_name: str, parent_sql_select_node: SqlSelectStatementNode, parent_alias: str, ) -> SqlJoinDescription: """Build join expression used to join a metric to a time spine dataset.""" left_expr: SqlExpressionNode = SqlColumnReferenceExpression.create( - col_ref=SqlColumnReference(table_alias=time_spine_alias, column_name=agg_time_dimension_column_name) + col_ref=SqlColumnReference(table_alias=time_spine_alias, column_name=time_spine_column_name) ) if node.offset_window: left_expr = SqlSubtractTimeIntervalExpression.create( @@ -551,7 +552,7 @@ def make_join_to_time_spine_join_description( left_expr=left_expr, comparison=SqlComparison.EQUALS, right_expr=SqlColumnReferenceExpression.create( - col_ref=SqlColumnReference(table_alias=parent_alias, column_name=agg_time_dimension_column_name) + col_ref=SqlColumnReference(table_alias=parent_alias, column_name=parent_column_name) ), ), join_type=node.join_type, diff --git a/metricflow/sql/optimizer/rewriting_sub_query_reducer.py b/metricflow/sql/optimizer/rewriting_sub_query_reducer.py index 82efa5dcd6..80aecc9f0f 100644 --- a/metricflow/sql/optimizer/rewriting_sub_query_reducer.py +++ b/metricflow/sql/optimizer/rewriting_sub_query_reducer.py @@ -241,6 +241,10 @@ def _current_node_can_be_reduced(self, node: SqlSelectStatementNode) -> bool: if len(from_source_node_as_select_node.group_bys) > 0 and len(node.group_bys) > 0: return False + # Skip this case for readability. + if any(col.expr.is_verbose for col in from_source_node_as_select_node.select_columns): + return False + # If there is a column in the parent group by that is not used in the current select statement, don't reduce or it # would leave an unselected column in the group by and change the meaning of the query. For example, in the SQL # below, reducing would remove the `is_instant` from the select statement. @@ -298,32 +302,6 @@ def _current_node_can_be_reduced(self, node: SqlSelectStatementNode) -> bool: ) > 0 and not SqlRewritingSubQueryReducerVisitor._select_columns_are_column_references(node.select_columns): return False - # If the group by is referencing a window function in the parent node, it can't be reduced. - # Window functions can't be included in group by. - parent_column_aliases_with_window_functions = { - select_column.column_alias - for select_column in SqlRewritingSubQueryReducerVisitor._select_columns_with_window_functions( - from_source_node_as_select_node.select_columns - ) - } - if len(node.group_bys) > 0 and [ - group_by - for group_by in node.group_bys - if ( - group_by.column_alias in parent_column_aliases_with_window_functions - or ( - group_by.expr.as_column_reference_expression - and group_by.expr.as_column_reference_expression.col_ref.column_name - in parent_column_aliases_with_window_functions - ) - or ( - group_by.expr.as_string_expression - and group_by.expr.as_string_expression.sql_expr in parent_column_aliases_with_window_functions - ) - ) - ]: - return False - # If the parent select node contains string columns, and this has a GROUP BY, don't reduce as string columns # can have special meanings in a GROUP BY. For example, # @@ -497,7 +475,11 @@ def _rewrite_node_with_join(self, node: SqlSelectStatementNode) -> SqlSelectStat join_select_node = join_desc.right_source.as_select_node # Verifying that it's simple makes it easier to reason about the logic. - if not join_select_node or not SqlRewritingSubQueryReducerVisitor._is_simple_source(join_select_node): + if ( + not join_select_node + or not SqlRewritingSubQueryReducerVisitor._is_simple_source(join_select_node) + or any(col.expr.is_verbose for col in join_select_node.select_columns) + ): new_join_descs.append(join_desc) continue diff --git a/tests_metricflow/integration/query_output/test_offset_metrics.py b/tests_metricflow/integration/query_output/test_offset_metrics.py index 3a84e6bbb8..72e9075e5a 100644 --- a/tests_metricflow/integration/query_output/test_offset_metrics.py +++ b/tests_metricflow/integration/query_output/test_offset_metrics.py @@ -64,17 +64,18 @@ def test_offset_to_grain_with_multiple_granularities( # noqa: D103 @pytest.mark.sql_engine_snapshot -def test_custom_offset_window_with_base_grain( # noqa: D103 +def test_custom_offset_window_with_base_grain( request: FixtureRequest, mf_test_configuration: MetricFlowTestConfiguration, sql_client: SqlClient, it_helpers: IntegrationTestHelpers, ) -> None: + """Gives a side by side comparison of bookings and bookings_offset_one_martian_day.""" query_result = it_helpers.mf_engine.query( MetricFlowQueryRequest.create_with_random_request_id( - metric_names=["bookings_offset_one_martian_day"], - group_by_names=["metric_time__day"], - order_by_names=["metric_time__day"], + metric_names=["bookings", "bookings_offset_one_martian_day"], + group_by_names=["metric_time__day", "metric_time__martian_day"], + order_by_names=["metric_time__day", "metric_time__martian_day"], ) ) assert query_result.result_df is not None, "Unexpected empty result." @@ -121,6 +122,7 @@ def test_custom_offset_window_with_grains_and_date_part( # noqa: D103 ) +# TODO: add test data to bookings source that spans multiple martian days @pytest.mark.sql_engine_snapshot def test_custom_offset_window_period_over_period( # noqa: D103 request: FixtureRequest, diff --git a/tests_metricflow/snapshots/test_cli.py/str/DuckDB/test_saved_query__cli_output.txt b/tests_metricflow/snapshots/test_cli.py/str/DuckDB/test_saved_query__cli_output.txt index dbb69a4fe1..a2eeed83e7 100644 --- a/tests_metricflow/snapshots/test_cli.py/str/DuckDB/test_saved_query__cli_output.txt +++ b/tests_metricflow/snapshots/test_cli.py/str/DuckDB/test_saved_query__cli_output.txt @@ -1,6 +1,43 @@ test_name: test_saved_query test_filename: test_cli.py --- +sql: -- Constrain Output with WHERE +-- Pass Only Elements: ['bookings', 'instant_bookings', 'listing__capacity_latest', 'metric_time__day'] +-- Aggregate Measures +-- Compute Metrics via Expressions +-- Order By ['metric_time__day', 'listing__capacity_latest'] +SELECT + metric_time__day + , listing__capacity_latest + , SUM(bookings) AS bookings + , SUM(instant_bookings) AS instant_bookings +FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_10000.capacity AS listing__capacity_latest + , subq_1.metric_time__day AS metric_time__day + , subq_1.bookings AS bookings + , subq_1.instant_bookings AS instant_bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_10000 + ) subq_1 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_10000 + ON + subq_1.listing = listings_latest_src_10000.listing_id +) subq_5 +WHERE listing__capacity_latest > 3 +GROUP BY + metric_time__day + , listing__capacity_latest +ORDER BY metric_time__day, listing__capacity_latest metric_time__day listing__capacity_latest bookings instant_bookings ------------------- -------------------------- ---------- ------------------ 2019-12-01T00:00:00 5 1 0 diff --git a/tests_metricflow/snapshots/test_cli.py/str/DuckDB/test_saved_query_with_cumulative_metric__cli_output.txt b/tests_metricflow/snapshots/test_cli.py/str/DuckDB/test_saved_query_with_cumulative_metric__cli_output.txt index 228f259ccb..275afcd759 100644 --- a/tests_metricflow/snapshots/test_cli.py/str/DuckDB/test_saved_query_with_cumulative_metric__cli_output.txt +++ b/tests_metricflow/snapshots/test_cli.py/str/DuckDB/test_saved_query_with_cumulative_metric__cli_output.txt @@ -1,6 +1,42 @@ test_name: test_saved_query_with_cumulative_metric test_filename: test_cli.py --- +sql: -- Join Self Over Time Range +-- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] +-- Pass Only Elements: ['txn_revenue', 'metric_time__day'] +-- Aggregate Measures +-- Compute Metrics via Expressions +-- Order By ['metric_time__day'] +SELECT + subq_3.metric_time__day AS metric_time__day + , SUM(subq_2.txn_revenue) AS trailing_2_months_revenue +FROM ( + -- Read From Time Spine 'mf_time_spine' + SELECT + ds AS metric_time__day + FROM ***************************.mf_time_spine subq_4 + WHERE ds BETWEEN '2020-01-01' AND '2020-01-01' +) subq_3 +INNER JOIN ( + -- Read Elements From Semantic Model 'revenue' + -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2019-11-01T00:00:00, 2020-01-01T00:00:00] + SELECT + DATE_TRUNC('day', created_at) AS metric_time__day + , revenue AS txn_revenue + FROM ***************************.fct_revenue revenue_src_10000 + WHERE DATE_TRUNC('day', created_at) BETWEEN '2019-11-01' AND '2020-01-01' +) subq_2 +ON + ( + subq_2.metric_time__day <= subq_3.metric_time__day + ) AND ( + subq_2.metric_time__day > subq_3.metric_time__day - INTERVAL 2 month + ) +WHERE subq_3.metric_time__day BETWEEN '2020-01-01' AND '2020-01-01' +GROUP BY + subq_3.metric_time__day +ORDER BY metric_time__day metric_time__day trailing_2_months_revenue ------------------- --------------------------- 2020-01-01T00:00:00 1000 diff --git a/tests_metricflow/snapshots/test_cli.py/str/DuckDB/test_saved_query_with_limit__cli_output.txt b/tests_metricflow/snapshots/test_cli.py/str/DuckDB/test_saved_query_with_limit__cli_output.txt index 246959c21c..6d53681755 100644 --- a/tests_metricflow/snapshots/test_cli.py/str/DuckDB/test_saved_query_with_limit__cli_output.txt +++ b/tests_metricflow/snapshots/test_cli.py/str/DuckDB/test_saved_query_with_limit__cli_output.txt @@ -1,6 +1,44 @@ test_name: test_saved_query_with_limit test_filename: test_cli.py --- +sql: -- Constrain Output with WHERE +-- Pass Only Elements: ['bookings', 'instant_bookings', 'listing__capacity_latest', 'metric_time__day'] +-- Aggregate Measures +-- Compute Metrics via Expressions +-- Order By ['metric_time__day', 'listing__capacity_latest'] Limit 3 +SELECT + metric_time__day + , listing__capacity_latest + , SUM(bookings) AS bookings + , SUM(instant_bookings) AS instant_bookings +FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_10000.capacity AS listing__capacity_latest + , subq_1.metric_time__day AS metric_time__day + , subq_1.bookings AS bookings + , subq_1.instant_bookings AS instant_bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_10000 + ) subq_1 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_10000 + ON + subq_1.listing = listings_latest_src_10000.listing_id +) subq_5 +WHERE listing__capacity_latest > 3 +GROUP BY + metric_time__day + , listing__capacity_latest +ORDER BY metric_time__day, listing__capacity_latest +LIMIT 3 metric_time__day listing__capacity_latest bookings instant_bookings ------------------- -------------------------- ---------- ------------------ 2019-12-01T00:00:00 5 1 0 diff --git a/tests_metricflow/snapshots/test_cli.py/str/DuckDB/test_saved_query_with_where__cli_output.txt b/tests_metricflow/snapshots/test_cli.py/str/DuckDB/test_saved_query_with_where__cli_output.txt index cc8f869e80..34b63a4ad3 100644 --- a/tests_metricflow/snapshots/test_cli.py/str/DuckDB/test_saved_query_with_where__cli_output.txt +++ b/tests_metricflow/snapshots/test_cli.py/str/DuckDB/test_saved_query_with_where__cli_output.txt @@ -1,6 +1,43 @@ test_name: test_saved_query_with_where test_filename: test_cli.py --- +sql: -- Constrain Output with WHERE +-- Pass Only Elements: ['bookings', 'instant_bookings', 'listing__capacity_latest', 'metric_time__day'] +-- Aggregate Measures +-- Compute Metrics via Expressions +-- Order By ['metric_time__day', 'listing__capacity_latest'] +SELECT + metric_time__day + , listing__capacity_latest + , SUM(bookings) AS bookings + , SUM(instant_bookings) AS instant_bookings +FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_10000.capacity AS listing__capacity_latest + , subq_1.metric_time__day AS metric_time__day + , subq_1.bookings AS bookings + , subq_1.instant_bookings AS instant_bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_10000 + ) subq_1 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_10000 + ON + subq_1.listing = listings_latest_src_10000.listing_id +) subq_5 +WHERE (listing__capacity_latest > 3) AND (listing__capacity_latest > 4) +GROUP BY + metric_time__day + , listing__capacity_latest +ORDER BY metric_time__day, listing__capacity_latest metric_time__day listing__capacity_latest bookings instant_bookings ------------------- -------------------------- ---------- ------------------ 2019-12-01T00:00:00 5 1 0 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlPlan/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0.xml b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlPlan/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0.xml index 6912f4b1de..298eed6bc2 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlPlan/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0.xml +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlPlan/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0.xml @@ -53,8 +53,8 @@ docstring: - - + + @@ -610,8 +610,8 @@ docstring: - - + + diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_custom_offset_window__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_custom_offset_window__plan0.sql index b99d9a1857..b4135dfc71 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_custom_offset_window__plan0.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_custom_offset_window__plan0.sql @@ -24,7 +24,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_1.ds__day AS ds__day + subq_11.metric_time__day AS metric_time__day + , subq_1.ds__day AS ds__day , subq_1.ds__week AS ds__week , subq_1.ds__month AS ds__month , subq_1.ds__quarter AS ds__quarter @@ -100,7 +101,6 @@ FROM ( , subq_1.metric_time__extract_day AS metric_time__extract_day , subq_1.metric_time__extract_dow AS metric_time__extract_dow , subq_1.metric_time__extract_doy AS metric_time__extract_doy - , subq_11.metric_time__day AS metric_time__day , subq_1.listing AS listing , subq_1.guest AS guest , subq_1.host AS host @@ -123,22 +123,24 @@ FROM ( , subq_1.approximate_continuous_booking_value_p99 AS approximate_continuous_booking_value_p99 , subq_1.approximate_discrete_booking_value_p99 AS approximate_discrete_booking_value_p99 FROM ( - -- Pass Only Elements: ['metric_time__day',] + -- Pass Only Elements: ['ds__day', 'metric_time__day'] SELECT - subq_10.metric_time__day + subq_10.ds__day + , subq_10.metric_time__day FROM ( -- Apply Requested Granularities SELECT - subq_9.ds__day AS metric_time__day + subq_9.ds__day + , subq_9.ds__day__lead AS metric_time__day FROM ( -- Offset Base Granularity By Custom Granularity Period(s) SELECT - subq_3.ds__martian_day AS ds__martian_day + subq_3.ds__day AS ds__day , CASE WHEN subq_8.ds__martian_day__first_value__offset + INTERVAL (subq_3.ds__day__row_number - 1) day <= subq_8.ds__martian_day__last_value__offset THEN subq_8.ds__martian_day__first_value__offset + INTERVAL (subq_3.ds__day__row_number - 1) day ELSE subq_8.ds__martian_day__last_value__offset - END AS ds__day + END AS ds__day__lead FROM ( -- Calculate Custom Granularity Bounds SELECT @@ -167,6 +169,7 @@ FROM ( , ROW_NUMBER() OVER ( PARTITION BY subq_2.ds__martian_day ORDER BY subq_2.ds__day + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day__row_number FROM ( -- Read From Time Spine 'mf_time_spine' @@ -190,8 +193,14 @@ FROM ( -- Offset Custom Granularity Bounds SELECT subq_6.ds__martian_day - , LAG(subq_6.ds__martian_day__first_value, 1) OVER (ORDER BY subq_6.ds__martian_day) AS ds__martian_day__first_value__offset - , LAG(subq_6.ds__martian_day__last_value, 1) OVER (ORDER BY subq_6.ds__martian_day) AS ds__martian_day__last_value__offset + , LEAD(subq_6.ds__martian_day__first_value, 1) OVER ( + ORDER BY subq_6.ds__martian_day + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__martian_day__first_value__offset + , LEAD(subq_6.ds__martian_day__last_value, 1) OVER ( + ORDER BY subq_6.ds__martian_day + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__martian_day__last_value__offset FROM ( -- Pass Only Elements: ['ds__martian_day', 'ds__martian_day__first_value', 'ds__martian_day__last_value'] SELECT @@ -226,6 +235,7 @@ FROM ( , ROW_NUMBER() OVER ( PARTITION BY subq_4.ds__martian_day ORDER BY subq_4.ds__day + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day__row_number FROM ( -- Read From Time Spine 'mf_time_spine' @@ -452,7 +462,7 @@ FROM ( ) subq_0 ) subq_1 ON - subq_11.metric_time__day = subq_1.metric_time__day + subq_11.ds__day = subq_1.metric_time__day ) subq_12 ) subq_13 GROUP BY diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_custom_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_custom_offset_window__plan0_optimized.sql index 5e069d8884..0d05cc3147 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_custom_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_custom_offset_window__plan0_optimized.sql @@ -7,7 +7,8 @@ WITH cgb_1_cte AS ( -- Read From Time Spine 'mf_time_spine' -- Calculate Custom Granularity Bounds SELECT - martian_day AS ds__martian_day + ds AS ds__day + , martian_day AS ds__martian_day , FIRST_VALUE(ds) OVER ( PARTITION BY martian_day ORDER BY ds @@ -21,6 +22,7 @@ WITH cgb_1_cte AS ( , ROW_NUMBER() OVER ( PARTITION BY martian_day ORDER BY ds + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day__row_number FROM ***************************.mf_time_spine time_spine_src_28006 ) @@ -34,35 +36,47 @@ FROM ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT - subq_26.metric_time__day AS metric_time__day + subq_24.ds__day__lead AS metric_time__day , SUM(subq_17.bookings) AS bookings FROM ( -- Offset Base Granularity By Custom Granularity Period(s) - -- Apply Requested Granularities - -- Pass Only Elements: ['metric_time__day',] SELECT - CASE - WHEN LAG(subq_21.ds__martian_day__first_value, 1) OVER (ORDER BY subq_21.ds__martian_day) + INTERVAL (cgb_1_cte.ds__day__row_number - 1) day <= LAG(subq_21.ds__martian_day__last_value, 1) OVER (ORDER BY subq_21.ds__martian_day) - THEN LAG(subq_21.ds__martian_day__first_value, 1) OVER (ORDER BY subq_21.ds__martian_day) + INTERVAL (cgb_1_cte.ds__day__row_number - 1) day - ELSE LAG(subq_21.ds__martian_day__last_value, 1) OVER (ORDER BY subq_21.ds__martian_day) - END AS metric_time__day + cgb_1_cte.ds__day AS ds__day + , CASE + WHEN subq_23.ds__martian_day__first_value__offset + INTERVAL (cgb_1_cte.ds__day__row_number - 1) day <= subq_23.ds__martian_day__last_value__offset + THEN subq_23.ds__martian_day__first_value__offset + INTERVAL (cgb_1_cte.ds__day__row_number - 1) day + ELSE subq_23.ds__martian_day__last_value__offset + END AS ds__day__lead FROM cgb_1_cte cgb_1_cte INNER JOIN ( - -- Read From CTE For node_id=cgb_1 - -- Pass Only Elements: ['ds__martian_day', 'ds__martian_day__first_value', 'ds__martian_day__last_value'] + -- Offset Custom Granularity Bounds SELECT - ds__martian_day__first_value - , ds__martian_day__last_value - , ds__martian_day - FROM cgb_1_cte cgb_1_cte - GROUP BY - ds__martian_day__first_value - , ds__martian_day__last_value - , ds__martian_day - ) subq_21 + ds__martian_day + , LEAD(ds__martian_day__first_value, 1) OVER ( + ORDER BY ds__martian_day + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__martian_day__first_value__offset + , LEAD(ds__martian_day__last_value, 1) OVER ( + ORDER BY ds__martian_day + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__martian_day__last_value__offset + FROM ( + -- Read From CTE For node_id=cgb_1 + -- Pass Only Elements: ['ds__martian_day', 'ds__martian_day__first_value', 'ds__martian_day__last_value'] + SELECT + ds__martian_day__first_value + , ds__martian_day__last_value + , ds__martian_day + FROM cgb_1_cte cgb_1_cte + GROUP BY + ds__martian_day__first_value + , ds__martian_day__last_value + , ds__martian_day + ) subq_21 + ) subq_23 ON - cgb_1_cte.ds__martian_day = subq_21.ds__martian_day - ) subq_26 + cgb_1_cte.ds__martian_day = subq_23.ds__martian_day + ) subq_24 INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' @@ -72,7 +86,7 @@ FROM ( FROM ***************************.fct_bookings bookings_source_src_28000 ) subq_17 ON - subq_26.metric_time__day = subq_17.metric_time__day + subq_24.ds__day = subq_17.metric_time__day GROUP BY - subq_26.metric_time__day + subq_24.ds__day__lead ) subq_30 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_custom_offset_window_with_granularity_and_date_part__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_custom_offset_window_with_granularity_and_date_part__plan0.sql index 5023fdfc56..95fc64c752 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_custom_offset_window_with_granularity_and_date_part__plan0.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_custom_offset_window_with_granularity_and_date_part__plan0.sql @@ -33,7 +33,10 @@ FROM ( -- Join to Time Spine Dataset -- Join to Custom Granularity Dataset SELECT - subq_1.ds__day AS ds__day + subq_11.booking__ds__month AS booking__ds__month + , subq_11.metric_time__extract_year AS metric_time__extract_year + , subq_11.metric_time__day AS metric_time__day + , subq_1.ds__day AS ds__day , subq_1.ds__week AS ds__week , subq_1.ds__month AS ds__month , subq_1.ds__quarter AS ds__quarter @@ -107,9 +110,6 @@ FROM ( , subq_1.metric_time__extract_day AS metric_time__extract_day , subq_1.metric_time__extract_dow AS metric_time__extract_dow , subq_1.metric_time__extract_doy AS metric_time__extract_doy - , subq_11.booking__ds__month AS booking__ds__month - , subq_11.metric_time__extract_year AS metric_time__extract_year - , subq_11.metric_time__day AS metric_time__day , subq_1.listing AS listing , subq_1.guest AS guest , subq_1.host AS host @@ -133,26 +133,28 @@ FROM ( , subq_1.approximate_discrete_booking_value_p99 AS approximate_discrete_booking_value_p99 , subq_12.martian_day AS metric_time__martian_day FROM ( - -- Pass Only Elements: ['booking__ds__month', 'metric_time__extract_year', 'metric_time__day'] + -- Pass Only Elements: ['ds__day', 'booking__ds__month', 'metric_time__extract_year', 'metric_time__day'] SELECT - subq_10.booking__ds__month + subq_10.ds__day + , subq_10.booking__ds__month , subq_10.metric_time__extract_year , subq_10.metric_time__day FROM ( -- Apply Requested Granularities SELECT - DATE_TRUNC('month', subq_9.ds__day) AS booking__ds__month - , EXTRACT(year FROM subq_9.ds__day) AS metric_time__extract_year - , subq_9.ds__day AS metric_time__day + subq_9.ds__day + , DATE_TRUNC('month', subq_9.ds__day__lead) AS booking__ds__month + , EXTRACT(year FROM subq_9.ds__day__lead) AS metric_time__extract_year + , subq_9.ds__day__lead AS metric_time__day FROM ( -- Offset Base Granularity By Custom Granularity Period(s) SELECT - subq_3.ds__martian_day AS ds__martian_day + subq_3.ds__day AS ds__day , CASE WHEN subq_8.ds__martian_day__first_value__offset + INTERVAL (subq_3.ds__day__row_number - 1) day <= subq_8.ds__martian_day__last_value__offset THEN subq_8.ds__martian_day__first_value__offset + INTERVAL (subq_3.ds__day__row_number - 1) day ELSE subq_8.ds__martian_day__last_value__offset - END AS ds__day + END AS ds__day__lead FROM ( -- Calculate Custom Granularity Bounds SELECT @@ -181,6 +183,7 @@ FROM ( , ROW_NUMBER() OVER ( PARTITION BY subq_2.ds__martian_day ORDER BY subq_2.ds__day + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day__row_number FROM ( -- Read From Time Spine 'mf_time_spine' @@ -204,8 +207,14 @@ FROM ( -- Offset Custom Granularity Bounds SELECT subq_6.ds__martian_day - , LAG(subq_6.ds__martian_day__first_value, 1) OVER (ORDER BY subq_6.ds__martian_day) AS ds__martian_day__first_value__offset - , LAG(subq_6.ds__martian_day__last_value, 1) OVER (ORDER BY subq_6.ds__martian_day) AS ds__martian_day__last_value__offset + , LEAD(subq_6.ds__martian_day__first_value, 1) OVER ( + ORDER BY subq_6.ds__martian_day + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__martian_day__first_value__offset + , LEAD(subq_6.ds__martian_day__last_value, 1) OVER ( + ORDER BY subq_6.ds__martian_day + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__martian_day__last_value__offset FROM ( -- Pass Only Elements: ['ds__martian_day', 'ds__martian_day__first_value', 'ds__martian_day__last_value'] SELECT @@ -240,6 +249,7 @@ FROM ( , ROW_NUMBER() OVER ( PARTITION BY subq_4.ds__martian_day ORDER BY subq_4.ds__day + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day__row_number FROM ( -- Read From Time Spine 'mf_time_spine' @@ -466,7 +476,7 @@ FROM ( ) subq_0 ) subq_1 ON - subq_11.metric_time__day = subq_1.metric_time__day + subq_11.ds__day = subq_1.metric_time__day LEFT OUTER JOIN ***************************.mf_time_spine subq_12 ON diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_custom_offset_window_with_granularity_and_date_part__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_custom_offset_window_with_granularity_and_date_part__plan0_optimized.sql index a461a2aab7..3ec4916249 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_custom_offset_window_with_granularity_and_date_part__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_custom_offset_window_with_granularity_and_date_part__plan0_optimized.sql @@ -7,7 +7,8 @@ WITH cgb_1_cte AS ( -- Read From Time Spine 'mf_time_spine' -- Calculate Custom Granularity Bounds SELECT - martian_day AS ds__martian_day + ds AS ds__day + , martian_day AS ds__martian_day , FIRST_VALUE(ds) OVER ( PARTITION BY martian_day ORDER BY ds @@ -21,6 +22,7 @@ WITH cgb_1_cte AS ( , ROW_NUMBER() OVER ( PARTITION BY martian_day ORDER BY ds + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS ds__day__row_number FROM ***************************.mf_time_spine time_spine_src_28006 ) @@ -38,46 +40,48 @@ FROM ( -- Compute Metrics via Expressions SELECT subq_28.martian_day AS metric_time__martian_day - , subq_27.booking__ds__month AS booking__ds__month - , subq_27.metric_time__extract_year AS metric_time__extract_year + , DATE_TRUNC('month', subq_25.ds__day__lead) AS booking__ds__month + , EXTRACT(year FROM subq_25.ds__day__lead) AS metric_time__extract_year , SUM(subq_18.bookings) AS bookings FROM ( -- Offset Base Granularity By Custom Granularity Period(s) - -- Apply Requested Granularities - -- Pass Only Elements: ['booking__ds__month', 'metric_time__extract_year', 'metric_time__day'] SELECT - DATE_TRUNC('month', CASE - WHEN LAG(subq_22.ds__martian_day__first_value, 1) OVER (ORDER BY subq_22.ds__martian_day) + INTERVAL (cgb_1_cte.ds__day__row_number - 1) day <= LAG(subq_22.ds__martian_day__last_value, 1) OVER (ORDER BY subq_22.ds__martian_day) - THEN LAG(subq_22.ds__martian_day__first_value, 1) OVER (ORDER BY subq_22.ds__martian_day) + INTERVAL (cgb_1_cte.ds__day__row_number - 1) day - ELSE LAG(subq_22.ds__martian_day__last_value, 1) OVER (ORDER BY subq_22.ds__martian_day) - END) AS booking__ds__month - , EXTRACT(year FROM CASE - WHEN LAG(subq_22.ds__martian_day__first_value, 1) OVER (ORDER BY subq_22.ds__martian_day) + INTERVAL (cgb_1_cte.ds__day__row_number - 1) day <= LAG(subq_22.ds__martian_day__last_value, 1) OVER (ORDER BY subq_22.ds__martian_day) - THEN LAG(subq_22.ds__martian_day__first_value, 1) OVER (ORDER BY subq_22.ds__martian_day) + INTERVAL (cgb_1_cte.ds__day__row_number - 1) day - ELSE LAG(subq_22.ds__martian_day__last_value, 1) OVER (ORDER BY subq_22.ds__martian_day) - END) AS metric_time__extract_year + cgb_1_cte.ds__day AS ds__day , CASE - WHEN LAG(subq_22.ds__martian_day__first_value, 1) OVER (ORDER BY subq_22.ds__martian_day) + INTERVAL (cgb_1_cte.ds__day__row_number - 1) day <= LAG(subq_22.ds__martian_day__last_value, 1) OVER (ORDER BY subq_22.ds__martian_day) - THEN LAG(subq_22.ds__martian_day__first_value, 1) OVER (ORDER BY subq_22.ds__martian_day) + INTERVAL (cgb_1_cte.ds__day__row_number - 1) day - ELSE LAG(subq_22.ds__martian_day__last_value, 1) OVER (ORDER BY subq_22.ds__martian_day) - END AS metric_time__day + WHEN subq_24.ds__martian_day__first_value__offset + INTERVAL (cgb_1_cte.ds__day__row_number - 1) day <= subq_24.ds__martian_day__last_value__offset + THEN subq_24.ds__martian_day__first_value__offset + INTERVAL (cgb_1_cte.ds__day__row_number - 1) day + ELSE subq_24.ds__martian_day__last_value__offset + END AS ds__day__lead FROM cgb_1_cte cgb_1_cte INNER JOIN ( - -- Read From CTE For node_id=cgb_1 - -- Pass Only Elements: ['ds__martian_day', 'ds__martian_day__first_value', 'ds__martian_day__last_value'] + -- Offset Custom Granularity Bounds SELECT - ds__martian_day__first_value - , ds__martian_day__last_value - , ds__martian_day - FROM cgb_1_cte cgb_1_cte - GROUP BY - ds__martian_day__first_value - , ds__martian_day__last_value - , ds__martian_day - ) subq_22 + ds__martian_day + , LEAD(ds__martian_day__first_value, 1) OVER ( + ORDER BY ds__martian_day + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__martian_day__first_value__offset + , LEAD(ds__martian_day__last_value, 1) OVER ( + ORDER BY ds__martian_day + ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING + ) AS ds__martian_day__last_value__offset + FROM ( + -- Read From CTE For node_id=cgb_1 + -- Pass Only Elements: ['ds__martian_day', 'ds__martian_day__first_value', 'ds__martian_day__last_value'] + SELECT + ds__martian_day__first_value + , ds__martian_day__last_value + , ds__martian_day + FROM cgb_1_cte cgb_1_cte + GROUP BY + ds__martian_day__first_value + , ds__martian_day__last_value + , ds__martian_day + ) subq_22 + ) subq_24 ON - cgb_1_cte.ds__martian_day = subq_22.ds__martian_day - ) subq_27 + cgb_1_cte.ds__martian_day = subq_24.ds__martian_day + ) subq_25 INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' @@ -87,13 +91,13 @@ FROM ( FROM ***************************.fct_bookings bookings_source_src_28000 ) subq_18 ON - subq_27.metric_time__day = subq_18.metric_time__day + subq_25.ds__day = subq_18.metric_time__day LEFT OUTER JOIN ***************************.mf_time_spine subq_28 ON - subq_27.metric_time__day = subq_28.ds + subq_25.ds__day__lead = subq_28.ds GROUP BY subq_28.martian_day - , subq_27.booking__ds__month - , subq_27.metric_time__extract_year + , DATE_TRUNC('month', subq_25.ds__day__lead) + , EXTRACT(year FROM subq_25.ds__day__lead) ) subq_32 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_offset_metric_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_offset_metric_with_custom_granularity__plan0.sql index dd4cbe20bc..9bb6394dab 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_offset_metric_with_custom_granularity__plan0.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_offset_metric_with_custom_granularity__plan0.sql @@ -25,7 +25,8 @@ FROM ( -- Join to Time Spine Dataset -- Join to Custom Granularity Dataset SELECT - subq_1.ds__day AS ds__day + subq_4.booking__ds__day AS booking__ds__day + , subq_1.ds__day AS ds__day , subq_1.ds__week AS ds__week , subq_1.ds__month AS ds__month , subq_1.ds__quarter AS ds__quarter @@ -101,7 +102,6 @@ FROM ( , subq_1.metric_time__extract_day AS metric_time__extract_day , subq_1.metric_time__extract_dow AS metric_time__extract_dow , subq_1.metric_time__extract_doy AS metric_time__extract_doy - , subq_4.booking__ds__day AS booking__ds__day , subq_1.listing AS listing , subq_1.guest AS guest , subq_1.host AS host diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0.sql index 3473cd4326..0da0eb1c62 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlPlan/DuckDB/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0.sql @@ -127,7 +127,8 @@ FROM ( -- Join to Time Spine Dataset -- Join to Custom Granularity Dataset SELECT - subq_1.ds__day AS ds__day + subq_4.metric_time__day AS metric_time__day + , subq_1.ds__day AS ds__day , subq_1.ds__week AS ds__week , subq_1.ds__month AS ds__month , subq_1.ds__quarter AS ds__quarter @@ -203,7 +204,6 @@ FROM ( , subq_1.metric_time__extract_day AS metric_time__extract_day , subq_1.metric_time__extract_dow AS metric_time__extract_dow , subq_1.metric_time__extract_doy AS metric_time__extract_doy - , subq_4.metric_time__day AS metric_time__day , subq_1.listing AS listing , subq_1.guest AS guest , subq_1.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_cumulative_time_offset_metric_with_time_constraint__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_cumulative_time_offset_metric_with_time_constraint__plan0.sql index cbe0b5170b..11ebd4ee2a 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_cumulative_time_offset_metric_with_time_constraint__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_cumulative_time_offset_metric_with_time_constraint__plan0.sql @@ -125,7 +125,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_4.ds__day AS ds__day + subq_7.metric_time__day AS metric_time__day + , subq_4.ds__day AS ds__day , subq_4.ds__week AS ds__week , subq_4.ds__month AS ds__month , subq_4.ds__quarter AS ds__quarter @@ -201,7 +202,6 @@ FROM ( , subq_4.metric_time__extract_day AS metric_time__extract_day , subq_4.metric_time__extract_dow AS metric_time__extract_dow , subq_4.metric_time__extract_doy AS metric_time__extract_doy - , subq_7.metric_time__day AS metric_time__day , subq_4.listing AS listing , subq_4.guest AS guest , subq_4.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_month_dimension_and_offset_window__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_month_dimension_and_offset_window__plan0.sql index 2aee922b30..1564aa5f2a 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_month_dimension_and_offset_window__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_month_dimension_and_offset_window__plan0.sql @@ -24,7 +24,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_1.ds__month AS ds__month + subq_4.metric_time__month AS metric_time__month + , subq_1.ds__month AS ds__month , subq_1.ds__quarter AS ds__quarter , subq_1.ds__year AS ds__year , subq_1.ds__extract_year AS ds__extract_year @@ -41,7 +42,6 @@ FROM ( , subq_1.metric_time__extract_year AS metric_time__extract_year , subq_1.metric_time__extract_quarter AS metric_time__extract_quarter , subq_1.metric_time__extract_month AS metric_time__extract_month - , subq_4.metric_time__month AS metric_time__month , subq_1.listing AS listing , subq_1.booking_monthly__listing AS booking_monthly__listing , subq_1.bookings_monthly AS bookings_monthly diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_to_grain__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_to_grain__plan0.sql index cbb9ee4065..3824e51f2c 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_to_grain__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_to_grain__plan0.sql @@ -245,7 +245,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_6.ds__day AS ds__day + subq_9.metric_time__day AS metric_time__day + , subq_6.ds__day AS ds__day , subq_6.ds__week AS ds__week , subq_6.ds__month AS ds__month , subq_6.ds__quarter AS ds__quarter @@ -321,7 +322,6 @@ FROM ( , subq_6.metric_time__extract_day AS metric_time__extract_day , subq_6.metric_time__extract_dow AS metric_time__extract_dow , subq_6.metric_time__extract_doy AS metric_time__extract_doy - , subq_9.metric_time__day AS metric_time__day , subq_6.listing AS listing , subq_6.guest AS guest , subq_6.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_to_grain_and_granularity__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_to_grain_and_granularity__plan0.sql index 675a437c72..e7ac33bbbf 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_to_grain_and_granularity__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_to_grain_and_granularity__plan0.sql @@ -245,7 +245,9 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_6.ds__day AS ds__day + subq_9.metric_time__day AS metric_time__day + , subq_9.metric_time__week AS metric_time__week + , subq_6.ds__day AS ds__day , subq_6.ds__week AS ds__week , subq_6.ds__month AS ds__month , subq_6.ds__quarter AS ds__quarter @@ -320,8 +322,6 @@ FROM ( , subq_6.metric_time__extract_day AS metric_time__extract_day , subq_6.metric_time__extract_dow AS metric_time__extract_dow , subq_6.metric_time__extract_doy AS metric_time__extract_doy - , subq_9.metric_time__day AS metric_time__day - , subq_9.metric_time__week AS metric_time__week , subq_6.listing AS listing , subq_6.guest AS guest , subq_6.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window__plan0.sql index 46c6801cfb..4220dcd2e0 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window__plan0.sql @@ -245,7 +245,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_6.ds__day AS ds__day + subq_9.metric_time__day AS metric_time__day + , subq_6.ds__day AS ds__day , subq_6.ds__week AS ds__week , subq_6.ds__month AS ds__month , subq_6.ds__quarter AS ds__quarter @@ -321,7 +322,6 @@ FROM ( , subq_6.metric_time__extract_day AS metric_time__extract_day , subq_6.metric_time__extract_dow AS metric_time__extract_dow , subq_6.metric_time__extract_doy AS metric_time__extract_doy - , subq_9.metric_time__day AS metric_time__day , subq_6.listing AS listing , subq_6.guest AS guest , subq_6.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_granularity__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_granularity__plan0.sql index 4ede9e08d2..331572a5af 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_granularity__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_granularity__plan0.sql @@ -245,7 +245,9 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_6.ds__day AS ds__day + subq_9.metric_time__day AS metric_time__day + , subq_9.metric_time__quarter AS metric_time__quarter + , subq_6.ds__day AS ds__day , subq_6.ds__week AS ds__week , subq_6.ds__month AS ds__month , subq_6.ds__quarter AS ds__quarter @@ -320,8 +322,6 @@ FROM ( , subq_6.metric_time__extract_day AS metric_time__extract_day , subq_6.metric_time__extract_dow AS metric_time__extract_dow , subq_6.metric_time__extract_doy AS metric_time__extract_doy - , subq_9.metric_time__day AS metric_time__day - , subq_9.metric_time__quarter AS metric_time__quarter , subq_6.listing AS listing , subq_6.guest AS guest , subq_6.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain__plan0.sql index 6f00c47348..814113fda2 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain__plan0.sql @@ -30,7 +30,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_1.ds__day AS ds__day + subq_4.metric_time__day AS metric_time__day + , subq_1.ds__day AS ds__day , subq_1.ds__week AS ds__week , subq_1.ds__month AS ds__month , subq_1.ds__quarter AS ds__quarter @@ -106,7 +107,6 @@ FROM ( , subq_1.metric_time__extract_day AS metric_time__extract_day , subq_1.metric_time__extract_dow AS metric_time__extract_dow , subq_1.metric_time__extract_doy AS metric_time__extract_doy - , subq_4.metric_time__day AS metric_time__day , subq_1.listing AS listing , subq_1.guest AS guest , subq_1.host AS host @@ -387,7 +387,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_10.ds__day AS ds__day + subq_13.metric_time__day AS metric_time__day + , subq_10.ds__day AS ds__day , subq_10.ds__week AS ds__week , subq_10.ds__month AS ds__month , subq_10.ds__quarter AS ds__quarter @@ -463,7 +464,6 @@ FROM ( , subq_10.metric_time__extract_day AS metric_time__extract_day , subq_10.metric_time__extract_dow AS metric_time__extract_dow , subq_10.metric_time__extract_doy AS metric_time__extract_doy - , subq_13.metric_time__day AS metric_time__day , subq_10.listing AS listing , subq_10.guest AS guest , subq_10.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0.sql index 1d5827eea4..178e5c4fbf 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0.sql @@ -30,7 +30,9 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_1.ds__day AS ds__day + subq_4.metric_time__day AS metric_time__day + , subq_4.metric_time__year AS metric_time__year + , subq_1.ds__day AS ds__day , subq_1.ds__week AS ds__week , subq_1.ds__month AS ds__month , subq_1.ds__quarter AS ds__quarter @@ -105,8 +107,6 @@ FROM ( , subq_1.metric_time__extract_day AS metric_time__extract_day , subq_1.metric_time__extract_dow AS metric_time__extract_dow , subq_1.metric_time__extract_doy AS metric_time__extract_doy - , subq_4.metric_time__day AS metric_time__day - , subq_4.metric_time__year AS metric_time__year , subq_1.listing AS listing , subq_1.guest AS guest , subq_1.host AS host @@ -389,7 +389,9 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_10.ds__day AS ds__day + subq_13.metric_time__day AS metric_time__day + , subq_13.metric_time__year AS metric_time__year + , subq_10.ds__day AS ds__day , subq_10.ds__week AS ds__week , subq_10.ds__month AS ds__month , subq_10.ds__quarter AS ds__quarter @@ -464,8 +466,6 @@ FROM ( , subq_10.metric_time__extract_day AS metric_time__extract_day , subq_10.metric_time__extract_dow AS metric_time__extract_dow , subq_10.metric_time__extract_doy AS metric_time__extract_doy - , subq_13.metric_time__day AS metric_time__day - , subq_13.metric_time__year AS metric_time__year , subq_10.listing AS listing , subq_10.guest AS guest , subq_10.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_time_filter__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_time_filter__plan0.sql index 444aa8d6ed..35b6aaf04b 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_time_filter__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_metric_with_offset_window_and_time_filter__plan0.sql @@ -449,7 +449,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_7.ds__day AS ds__day + subq_10.metric_time__day AS metric_time__day + , subq_7.ds__day AS ds__day , subq_7.ds__week AS ds__week , subq_7.ds__month AS ds__month , subq_7.ds__quarter AS ds__quarter @@ -525,7 +526,6 @@ FROM ( , subq_7.metric_time__extract_day AS metric_time__extract_day , subq_7.metric_time__extract_dow AS metric_time__extract_dow , subq_7.metric_time__extract_doy AS metric_time__extract_doy - , subq_10.metric_time__day AS metric_time__day , subq_7.listing AS listing , subq_7.guest AS guest , subq_7.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_offset_cumulative_metric__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_offset_cumulative_metric__plan0.sql index bbcea069fe..adf124e749 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_offset_cumulative_metric__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_offset_cumulative_metric__plan0.sql @@ -24,7 +24,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_4.ds__day AS ds__day + subq_7.metric_time__day AS metric_time__day + , subq_4.ds__day AS ds__day , subq_4.ds__week AS ds__week , subq_4.ds__month AS ds__month , subq_4.ds__quarter AS ds__quarter @@ -100,7 +101,6 @@ FROM ( , subq_4.metric_time__extract_day AS metric_time__extract_day , subq_4.metric_time__extract_dow AS metric_time__extract_dow , subq_4.metric_time__extract_doy AS metric_time__extract_doy - , subq_7.metric_time__day AS metric_time__day , subq_4.listing AS listing , subq_4.guest AS guest , subq_4.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_offset_metric_with_agg_time_dim__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_offset_metric_with_agg_time_dim__plan0.sql index a2cfc20182..2d8c9c40aa 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_offset_metric_with_agg_time_dim__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_offset_metric_with_agg_time_dim__plan0.sql @@ -30,7 +30,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_1.ds__day AS ds__day + subq_4.booking__ds__day AS booking__ds__day + , subq_1.ds__day AS ds__day , subq_1.ds__week AS ds__week , subq_1.ds__month AS ds__month , subq_1.ds__quarter AS ds__quarter @@ -106,7 +107,6 @@ FROM ( , subq_1.metric_time__extract_day AS metric_time__extract_day , subq_1.metric_time__extract_dow AS metric_time__extract_dow , subq_1.metric_time__extract_doy AS metric_time__extract_doy - , subq_4.booking__ds__day AS booking__ds__day , subq_1.listing AS listing , subq_1.guest AS guest , subq_1.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_offset_metric_with_one_input_metric__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_offset_metric_with_one_input_metric__plan0.sql index f4ad8b174b..3e703cabcd 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_offset_metric_with_one_input_metric__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_derived_offset_metric_with_one_input_metric__plan0.sql @@ -24,7 +24,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_1.ds__day AS ds__day + subq_4.metric_time__day AS metric_time__day + , subq_1.ds__day AS ds__day , subq_1.ds__week AS ds__week , subq_1.ds__month AS ds__month , subq_1.ds__quarter AS ds__quarter @@ -100,7 +101,6 @@ FROM ( , subq_1.metric_time__extract_day AS metric_time__extract_day , subq_1.metric_time__extract_dow AS metric_time__extract_dow , subq_1.metric_time__extract_doy AS metric_time__extract_doy - , subq_4.metric_time__day AS metric_time__day , subq_1.listing AS listing , subq_1.guest AS guest , subq_1.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0.sql index 98e30cf757..9e8dbfde1f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0.sql @@ -88,7 +88,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_1.ds__day AS ds__day + subq_4.metric_time__day AS metric_time__day + , subq_1.ds__day AS ds__day , subq_1.ds__week AS ds__week , subq_1.ds__month AS ds__month , subq_1.ds__quarter AS ds__quarter @@ -164,7 +165,6 @@ FROM ( , subq_1.metric_time__extract_day AS metric_time__extract_day , subq_1.metric_time__extract_dow AS metric_time__extract_dow , subq_1.metric_time__extract_doy AS metric_time__extract_doy - , subq_4.metric_time__day AS metric_time__day , subq_1.listing AS listing , subq_1.guest AS guest , subq_1.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_nested_offsets__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_nested_offsets__plan0.sql index 269c143dd4..b0a57338b2 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_nested_offsets__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_nested_offsets__plan0.sql @@ -72,7 +72,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_1.ds__day AS ds__day + subq_4.metric_time__day AS metric_time__day + , subq_1.ds__day AS ds__day , subq_1.ds__week AS ds__week , subq_1.ds__month AS ds__month , subq_1.ds__quarter AS ds__quarter @@ -148,7 +149,6 @@ FROM ( , subq_1.metric_time__extract_day AS metric_time__extract_day , subq_1.metric_time__extract_dow AS metric_time__extract_dow , subq_1.metric_time__extract_doy AS metric_time__extract_doy - , subq_4.metric_time__day AS metric_time__day , subq_1.listing AS listing , subq_1.guest AS guest , subq_1.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_nested_offsets_with_time_constraint__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_nested_offsets_with_time_constraint__plan0.sql index 1c43bd33e0..71a00693e1 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_nested_offsets_with_time_constraint__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_nested_offsets_with_time_constraint__plan0.sql @@ -77,7 +77,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_1.ds__day AS ds__day + subq_4.metric_time__day AS metric_time__day + , subq_1.ds__day AS ds__day , subq_1.ds__week AS ds__week , subq_1.ds__month AS ds__month , subq_1.ds__quarter AS ds__quarter @@ -153,7 +154,6 @@ FROM ( , subq_1.metric_time__extract_day AS metric_time__extract_day , subq_1.metric_time__extract_dow AS metric_time__extract_dow , subq_1.metric_time__extract_doy AS metric_time__extract_doy - , subq_4.metric_time__day AS metric_time__day , subq_1.listing AS listing , subq_1.guest AS guest , subq_1.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_nested_offsets_with_where_constraint__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_nested_offsets_with_where_constraint__plan0.sql index 7146a8aaf8..6701f6cd13 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_nested_offsets_with_where_constraint__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_nested_offsets_with_where_constraint__plan0.sql @@ -77,7 +77,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_1.ds__day AS ds__day + subq_4.metric_time__day AS metric_time__day + , subq_1.ds__day AS ds__day , subq_1.ds__week AS ds__week , subq_1.ds__month AS ds__month , subq_1.ds__quarter AS ds__quarter @@ -153,7 +154,6 @@ FROM ( , subq_1.metric_time__extract_day AS metric_time__extract_day , subq_1.metric_time__extract_dow AS metric_time__extract_dow , subq_1.metric_time__extract_doy AS metric_time__extract_doy - , subq_4.metric_time__day AS metric_time__day , subq_1.listing AS listing , subq_1.guest AS guest , subq_1.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0.sql index b0b6ac3fd0..81cdf83220 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0.sql @@ -127,7 +127,9 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_1.ds__day AS ds__day + subq_4.metric_time__day AS metric_time__day + , subq_4.metric_time__month AS metric_time__month + , subq_1.ds__day AS ds__day , subq_1.ds__week AS ds__week , subq_1.ds__month AS ds__month , subq_1.ds__quarter AS ds__quarter @@ -202,8 +204,6 @@ FROM ( , subq_1.metric_time__extract_day AS metric_time__extract_day , subq_1.metric_time__extract_dow AS metric_time__extract_dow , subq_1.metric_time__extract_doy AS metric_time__extract_doy - , subq_4.metric_time__day AS metric_time__day - , subq_4.metric_time__month AS metric_time__month , subq_1.listing AS listing , subq_1.guest AS guest , subq_1.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_to_grain_metric_multiple_granularities__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_to_grain_metric_multiple_granularities__plan0.sql index 7ff287fbf9..0b0df97858 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_to_grain_metric_multiple_granularities__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_to_grain_metric_multiple_granularities__plan0.sql @@ -34,7 +34,10 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_1.ds__day AS ds__day + subq_4.metric_time__day AS metric_time__day + , subq_4.metric_time__month AS metric_time__month + , subq_4.metric_time__year AS metric_time__year + , subq_1.ds__day AS ds__day , subq_1.ds__week AS ds__week , subq_1.ds__month AS ds__month , subq_1.ds__quarter AS ds__quarter @@ -108,9 +111,6 @@ FROM ( , subq_1.metric_time__extract_day AS metric_time__extract_day , subq_1.metric_time__extract_dow AS metric_time__extract_dow , subq_1.metric_time__extract_doy AS metric_time__extract_doy - , subq_4.metric_time__day AS metric_time__day - , subq_4.metric_time__month AS metric_time__month - , subq_4.metric_time__year AS metric_time__year , subq_1.listing AS listing , subq_1.guest AS guest , subq_1.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_to_grain_with_agg_time_dim__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_to_grain_with_agg_time_dim__plan0.sql index 794c43a76e..74ad0b6660 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_to_grain_with_agg_time_dim__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_to_grain_with_agg_time_dim__plan0.sql @@ -245,7 +245,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_6.ds__day AS ds__day + subq_9.booking__ds__day AS booking__ds__day + , subq_6.ds__day AS ds__day , subq_6.ds__week AS ds__week , subq_6.ds__month AS ds__month , subq_6.ds__quarter AS ds__quarter @@ -321,7 +322,6 @@ FROM ( , subq_6.metric_time__extract_day AS metric_time__extract_day , subq_6.metric_time__extract_dow AS metric_time__extract_dow , subq_6.metric_time__extract_doy AS metric_time__extract_doy - , subq_9.booking__ds__day AS booking__ds__day , subq_6.listing AS listing , subq_6.guest AS guest , subq_6.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_window_metric_filter_and_query_have_different_granularities__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_window_metric_filter_and_query_have_different_granularities__plan0.sql index 11695bd14c..e15b7df44b 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_window_metric_filter_and_query_have_different_granularities__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_window_metric_filter_and_query_have_different_granularities__plan0.sql @@ -133,7 +133,9 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_1.ds__day AS ds__day + subq_4.metric_time__day AS metric_time__day + , subq_4.metric_time__month AS metric_time__month + , subq_1.ds__day AS ds__day , subq_1.ds__week AS ds__week , subq_1.ds__month AS ds__month , subq_1.ds__quarter AS ds__quarter @@ -208,8 +210,6 @@ FROM ( , subq_1.metric_time__extract_day AS metric_time__extract_day , subq_1.metric_time__extract_dow AS metric_time__extract_dow , subq_1.metric_time__extract_doy AS metric_time__extract_doy - , subq_4.metric_time__day AS metric_time__day - , subq_4.metric_time__month AS metric_time__month , subq_1.listing AS listing , subq_1.guest AS guest , subq_1.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_window_metric_multiple_granularities__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_window_metric_multiple_granularities__plan0.sql index 9f27bbfd82..b5bdca268d 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_window_metric_multiple_granularities__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_window_metric_multiple_granularities__plan0.sql @@ -42,7 +42,10 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_1.ds__day AS ds__day + subq_4.metric_time__day AS metric_time__day + , subq_4.metric_time__month AS metric_time__month + , subq_4.metric_time__year AS metric_time__year + , subq_1.ds__day AS ds__day , subq_1.ds__week AS ds__week , subq_1.ds__month AS ds__month , subq_1.ds__quarter AS ds__quarter @@ -116,9 +119,6 @@ FROM ( , subq_1.metric_time__extract_day AS metric_time__extract_day , subq_1.metric_time__extract_dow AS metric_time__extract_dow , subq_1.metric_time__extract_doy AS metric_time__extract_doy - , subq_4.metric_time__day AS metric_time__day - , subq_4.metric_time__month AS metric_time__month - , subq_4.metric_time__year AS metric_time__year , subq_1.listing AS listing , subq_1.guest AS guest , subq_1.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_window_with_agg_time_dim__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_window_with_agg_time_dim__plan0.sql index d0a69d1936..84f8f72bd6 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_window_with_agg_time_dim__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_offset_window_with_agg_time_dim__plan0.sql @@ -245,7 +245,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_6.ds__day AS ds__day + subq_9.booking__ds__day AS booking__ds__day + , subq_6.ds__day AS ds__day , subq_6.ds__week AS ds__week , subq_6.ds__month AS ds__month , subq_6.ds__quarter AS ds__quarter @@ -321,7 +322,6 @@ FROM ( , subq_6.metric_time__extract_day AS metric_time__extract_day , subq_6.metric_time__extract_dow AS metric_time__extract_dow , subq_6.metric_time__extract_doy AS metric_time__extract_doy - , subq_9.booking__ds__day AS booking__ds__day , subq_6.listing AS listing , subq_6.guest AS guest , subq_6.host AS host diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_time_offset_metric_with_time_constraint__plan0.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_time_offset_metric_with_time_constraint__plan0.sql index e05b1169fd..a3938a64db 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_time_offset_metric_with_time_constraint__plan0.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlPlan/DuckDB/test_time_offset_metric_with_time_constraint__plan0.sql @@ -125,7 +125,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_1.ds__day AS ds__day + subq_4.metric_time__day AS metric_time__day + , subq_1.ds__day AS ds__day , subq_1.ds__week AS ds__week , subq_1.ds__month AS ds__month , subq_1.ds__quarter AS ds__quarter @@ -201,7 +202,6 @@ FROM ( , subq_1.metric_time__extract_day AS metric_time__extract_day , subq_1.metric_time__extract_dow AS metric_time__extract_dow , subq_1.metric_time__extract_doy AS metric_time__extract_doy - , subq_4.metric_time__day AS metric_time__day , subq_1.listing AS listing , subq_1.guest AS guest , subq_1.host AS host diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_derived_fill_nulls_for_one_input_metric__plan0.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_derived_fill_nulls_for_one_input_metric__plan0.sql index 0f78182476..a8cf1036ed 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_derived_fill_nulls_for_one_input_metric__plan0.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlPlan/DuckDB/test_derived_fill_nulls_for_one_input_metric__plan0.sql @@ -291,7 +291,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_10.ds__day AS ds__day + subq_13.metric_time__day AS metric_time__day + , subq_10.ds__day AS ds__day , subq_10.ds__week AS ds__week , subq_10.ds__month AS ds__month , subq_10.ds__quarter AS ds__quarter @@ -367,7 +368,6 @@ FROM ( , subq_10.metric_time__extract_day AS metric_time__extract_day , subq_10.metric_time__extract_dow AS metric_time__extract_dow , subq_10.metric_time__extract_doy AS metric_time__extract_doy - , subq_13.metric_time__day AS metric_time__day , subq_10.listing AS listing , subq_10.guest AS guest , subq_10.host AS host diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlPlan/DuckDB/test_offset_window_with_date_part__plan0.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlPlan/DuckDB/test_offset_window_with_date_part__plan0.sql index 81b8d3102d..cefd2d5b69 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlPlan/DuckDB/test_offset_window_with_date_part__plan0.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlPlan/DuckDB/test_offset_window_with_date_part__plan0.sql @@ -245,7 +245,9 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_6.ds__day AS ds__day + subq_9.metric_time__day AS metric_time__day + , subq_9.metric_time__extract_dow AS metric_time__extract_dow + , subq_6.ds__day AS ds__day , subq_6.ds__week AS ds__week , subq_6.ds__month AS ds__month , subq_6.ds__quarter AS ds__quarter @@ -320,8 +322,6 @@ FROM ( , subq_6.metric_time__extract_month AS metric_time__extract_month , subq_6.metric_time__extract_day AS metric_time__extract_day , subq_6.metric_time__extract_doy AS metric_time__extract_doy - , subq_9.metric_time__day AS metric_time__day - , subq_9.metric_time__extract_dow AS metric_time__extract_dow , subq_6.listing AS listing , subq_6.guest AS guest , subq_6.host AS host diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlPlan/DuckDB/test_subdaily_offset_to_grain_metric__plan0.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlPlan/DuckDB/test_subdaily_offset_to_grain_metric__plan0.sql index f50118713f..4a3277ad36 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlPlan/DuckDB/test_subdaily_offset_to_grain_metric__plan0.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlPlan/DuckDB/test_subdaily_offset_to_grain_metric__plan0.sql @@ -24,7 +24,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_1.ds__day AS ds__day + subq_4.metric_time__hour AS metric_time__hour + , subq_1.ds__day AS ds__day , subq_1.ds__week AS ds__week , subq_1.ds__month AS ds__month , subq_1.ds__quarter AS ds__quarter @@ -209,7 +210,6 @@ FROM ( , subq_1.metric_time__extract_day AS metric_time__extract_day , subq_1.metric_time__extract_dow AS metric_time__extract_dow , subq_1.metric_time__extract_doy AS metric_time__extract_doy - , subq_4.metric_time__hour AS metric_time__hour , subq_1.user AS user , subq_1.home_state AS home_state , subq_1.user__home_state AS user__home_state diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlPlan/DuckDB/test_subdaily_offset_window_metric__plan0.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlPlan/DuckDB/test_subdaily_offset_window_metric__plan0.sql index cb22006294..9bded1400b 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlPlan/DuckDB/test_subdaily_offset_window_metric__plan0.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlPlan/DuckDB/test_subdaily_offset_window_metric__plan0.sql @@ -24,7 +24,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_1.ds__day AS ds__day + subq_4.metric_time__hour AS metric_time__hour + , subq_1.ds__day AS ds__day , subq_1.ds__week AS ds__week , subq_1.ds__month AS ds__month , subq_1.ds__quarter AS ds__quarter @@ -209,7 +210,6 @@ FROM ( , subq_1.metric_time__extract_day AS metric_time__extract_day , subq_1.metric_time__extract_dow AS metric_time__extract_dow , subq_1.metric_time__extract_doy AS metric_time__extract_doy - , subq_4.metric_time__hour AS metric_time__hour , subq_1.user AS user , subq_1.home_state AS home_state , subq_1.user__home_state AS user__home_state diff --git a/tests_metricflow/snapshots/test_offset_metrics.py/str/DuckDB/test_custom_offset_window_period_over_period__query_output.txt b/tests_metricflow/snapshots/test_offset_metrics.py/str/DuckDB/test_custom_offset_window_period_over_period__query_output.txt index 29924960b3..f03116a7c4 100644 --- a/tests_metricflow/snapshots/test_offset_metrics.py/str/DuckDB/test_custom_offset_window_period_over_period__query_output.txt +++ b/tests_metricflow/snapshots/test_offset_metrics.py/str/DuckDB/test_custom_offset_window_period_over_period__query_output.txt @@ -1,12 +1,19 @@ test_name: test_custom_offset_window_period_over_period test_filename: test_offset_metrics.py --- -metric_time__day bookings_martian_day_over_martian_day +metric_time__day bookings_martian_day_over_martian_day ------------------- --------------------------------------- -2019-12-01T00:00:00 0 -2019-12-18T00:00:00 9 -2019-12-19T00:00:00 17 -2019-12-20T00:00:00 1 -2020-01-01T00:00:00 4 -2020-01-02T00:00:00 8 -2020-01-03T00:00:00 0 +2019-12-01T00:00:00 None +2019-12-18T00:00:00 None +2019-12-19T00:00:00 None +2019-12-20T00:00:00 None +2020-01-01T00:00:00 None +2020-01-02T00:00:00 None +2020-01-03T00:00:00 None +2022-08-27T00:00:00 None +2022-09-13T00:00:00 None +2022-09-14T00:00:00 None +2022-09-15T00:00:00 None +2022-09-27T00:00:00 None +2022-09-28T00:00:00 None +2022-09-29T00:00:00 None diff --git a/tests_metricflow/snapshots/test_offset_metrics.py/str/DuckDB/test_custom_offset_window_with_base_grain__query_output.txt b/tests_metricflow/snapshots/test_offset_metrics.py/str/DuckDB/test_custom_offset_window_with_base_grain__query_output.txt index 6be3b6d43c..f5cfd0c52d 100644 --- a/tests_metricflow/snapshots/test_offset_metrics.py/str/DuckDB/test_custom_offset_window_with_base_grain__query_output.txt +++ b/tests_metricflow/snapshots/test_offset_metrics.py/str/DuckDB/test_custom_offset_window_with_base_grain__query_output.txt @@ -1,12 +1,21 @@ test_name: test_custom_offset_window_with_base_grain test_filename: test_offset_metrics.py +docstring: + Gives a side by side comparison of bookings and bookings_offset_one_martian_day. --- -metric_time__day bookings_offset_one_martian_day -------------------- --------------------------------- -2019-12-01T00:00:00 1 -2019-12-18T00:00:00 10 -2019-12-19T00:00:00 18 -2019-12-20T00:00:00 2 -2020-01-01T00:00:00 5 -2020-01-02T00:00:00 9 -2020-01-03T00:00:00 1 +metric_time__martian_day metric_time__day bookings bookings_offset_one_martian_day +-------------------------- ------------------- ---------- --------------------------------- +2020-01-08T00:00:00 2019-12-01T00:00:00 1 None +2020-01-08T00:00:00 2019-12-18T00:00:00 10 None +2020-01-08T00:00:00 2019-12-19T00:00:00 18 None +2020-01-08T00:00:00 2019-12-20T00:00:00 2 None +2020-01-08T00:00:00 2020-01-01T00:00:00 5 None +2020-01-08T00:00:00 2020-01-02T00:00:00 9 None +2020-01-08T00:00:00 2020-01-03T00:00:00 1 None +2020-01-09T00:00:00 2022-08-27T00:00:00 None 1 +2020-01-09T00:00:00 2022-09-13T00:00:00 None 10 +2020-01-09T00:00:00 2022-09-14T00:00:00 None 18 +2020-01-09T00:00:00 2022-09-15T00:00:00 None 2 +2020-01-09T00:00:00 2022-09-27T00:00:00 None 5 +2020-01-09T00:00:00 2022-09-28T00:00:00 None 9 +2020-01-09T00:00:00 2022-09-29T00:00:00 None 1 diff --git a/tests_metricflow/snapshots/test_offset_metrics.py/str/DuckDB/test_custom_offset_window_with_grains_and_date_part__query_output.txt b/tests_metricflow/snapshots/test_offset_metrics.py/str/DuckDB/test_custom_offset_window_with_grains_and_date_part__query_output.txt index 9f37769433..07d2d112cd 100644 --- a/tests_metricflow/snapshots/test_offset_metrics.py/str/DuckDB/test_custom_offset_window_with_grains_and_date_part__query_output.txt +++ b/tests_metricflow/snapshots/test_offset_metrics.py/str/DuckDB/test_custom_offset_window_with_grains_and_date_part__query_output.txt @@ -3,5 +3,5 @@ test_filename: test_offset_metrics.py --- metric_time__martian_day booking__ds__month metric_time__extract_year bookings_offset_one_martian_day -------------------------- -------------------- --------------------------- --------------------------------- -2020-01-08T00:00:00 2019-12-01T00:00:00 2019 31 -2020-01-08T00:00:00 2020-01-01T00:00:00 2020 15 +2020-01-09T00:00:00 2022-08-01T00:00:00 2022 1 +2020-01-09T00:00:00 2022-09-01T00:00:00 2022 45 diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlPlan/DuckDB/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlPlan/DuckDB/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0.sql index c08bef2fa3..f78b7f13e1 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlPlan/DuckDB/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlPlan/DuckDB/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0.sql @@ -900,7 +900,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_15.ds__day AS ds__day + subq_18.metric_time__day AS metric_time__day + , subq_15.ds__day AS ds__day , subq_15.ds__week AS ds__week , subq_15.ds__month AS ds__month , subq_15.ds__quarter AS ds__quarter @@ -976,7 +977,6 @@ FROM ( , subq_15.metric_time__extract_day AS metric_time__extract_day , subq_15.metric_time__extract_dow AS metric_time__extract_dow , subq_15.metric_time__extract_doy AS metric_time__extract_doy - , subq_18.metric_time__day AS metric_time__day , subq_15.listing AS listing , subq_15.guest AS guest , subq_15.host AS host diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlPlan/DuckDB/test_offset_metric_with_query_time_filters__plan0.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlPlan/DuckDB/test_offset_metric_with_query_time_filters__plan0.sql index 3fbc062881..09e256fe5c 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlPlan/DuckDB/test_offset_metric_with_query_time_filters__plan0.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlPlan/DuckDB/test_offset_metric_with_query_time_filters__plan0.sql @@ -809,7 +809,8 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_11.ds__day AS ds__day + subq_14.metric_time__day AS metric_time__day + , subq_11.ds__day AS ds__day , subq_11.ds__week AS ds__week , subq_11.ds__month AS ds__month , subq_11.ds__quarter AS ds__quarter @@ -885,7 +886,6 @@ FROM ( , subq_11.metric_time__extract_day AS metric_time__extract_day , subq_11.metric_time__extract_dow AS metric_time__extract_dow , subq_11.metric_time__extract_doy AS metric_time__extract_doy - , subq_14.metric_time__day AS metric_time__day , subq_11.listing AS listing , subq_11.guest AS guest , subq_11.host AS host diff --git a/x.sql b/x.sql new file mode 100644 index 0000000000..5fcb21087e --- /dev/null +++ b/x.sql @@ -0,0 +1,87 @@ +-- Grouping by a grain that is NOT the same AS the custom grain used in the offset window +-------------------------------------------------- +-- Use the base grain of the custom grain's time spine in all initial subqueries, apply DATE_TRUNC in final query +-- This also works for custom grain, since we can just join it to the final subquery like usual. +-- Also works if there are multiple grains in the group by + +WITH cte AS ( -- CustomGranularityBoundsNode + SELECT + fiscal_quarter + , first_value(date_day) OVER (PARTITION BY fiscal_quarter ORDER BY date_day) AS ds__fiscal_quarter__first_value + , last_value(date_day) OVER (PARTITION BY fiscal_quarter ORDER BY date_day) AS ds__fiscal_quarter__last_value + , row_number() OVER (PARTITION BY fiscal_quarter ORDER BY date_day) AS ds__day__row_number + FROM ANALYTICS_DEV.DBT_JSTEIN.ALL_DAYS +) + +SELECT + metric_time__week, + fiscal_year AS metric_time__fiscal_year, + SUM(total_price) AS revenue_last_fiscal_quarter +FROM ANALYTICS_DEV.DBT_JSTEIN.STG_SALESFORCE__ORDER_ITEMS +INNER JOIN ( + -- OffsetByCustomGranularityNode + SELECT + offset_by_custom_grain.date_day, + DATE_TRUNC(week, offset_by_custom_grain.date_day) AS metric_time__week, + FROM ( + SELECT + date_day, + CASE + WHEN dateadd(day, ds__day__row_number - 1, ds__fiscal_quarter__first_value__offset) <= ds__fiscal_quarter__last_value__offset + THEN dateadd(day, ds__day__row_number - 1, ds__fiscal_quarter__first_value__offset) + ELSE ds__fiscal_quarter__last_value__offset + END AS metric_time__day + FROM cte + INNER JOIN ( + SELECT + fiscal_quarter, + lag(ds__fiscal_quarter__first_value, 1) OVER (ORDER BY fiscal_quarter) AS ds__fiscal_quarter__first_value__offset, + lag(ds__fiscal_quarter__last_value, 1) OVER (ORDER BY fiscal_quarter) AS ds__fiscal_quarter__last_value__offset + FROM ( + SELECT -- FilterElementsNode + fiscal_quarter, + ds__fiscal_quarter__first_value, + ds__fiscal_quarter__last_value + FROM cte + GROUP BY 1, 2, 3 + ) ts_distinct + ) ts_with_offset_intervals USING (fiscal_quarter) + ) AS offset_by_custom_grain +) ts_offset_dates ON ts_offset_dates.date_day = DATE_TRUNC(day, created_at)::date +LEFT JOIN ANALYTICS_DEV.DBT_JSTEIN.ALL_DAYS custom ON custom.date_day = ts_offset_dates.date_day -- JoinToCustomGranularityNode (only if needed) +GROUP BY 1, 2 +ORDER BY 1, 2; + + + + + + +-- Grouping by the just same custom grain AS what's used in the offset window (and only that grain) +-------------------------------------------------- +-- Could follow the same SQL AS above, but this would be a more optimized version (they appear to give the same results) +-- This is likely to be most common for period OVER period, so it might be good to optimize it + + +SELECT -- existing nodes! + metric_time__fiscal_quarter, + SUM(total_price) AS revenue +FROM ANALYTICS_DEV.DBT_JSTEIN.STG_SALESFORCE__ORDER_ITEMS +LEFT JOIN ( -- JoinToTimeSpineNode, no offset, join on custom grain spec + SELECT + -- JoinToTimeSpineNode + -- TransformTimeDimensionsNode?? + date_day, + fiscal_quarter_offset AS metric_time__fiscal_quarter + FROM ANALYTICS_DEV.DBT_JSTEIN.ALL_DAYS + INNER JOIN ( + -- OffsetCustomGranularityNode + SELECT + fiscal_quarter + , lag(fiscal_quarter, 1) OVER (ORDER BY fiscal_quarter) AS fiscal_quarter_offset + FROM ANALYTICS_DEV.DBT_JSTEIN.ALL_DAYS + GROUP BY 1 + ) ts_offset_dates USING (fiscal_quarter) +) ts ON date_day = DATE_TRUNC(day, created_at)::date +GROUP BY 1 +ORDER BY 1;