diff --git a/tests_metricflow/integration/test_cases/itest_dimensions.yaml b/tests_metricflow/integration/test_cases/itest_dimensions.yaml index 54d6632927..08fccc059e 100644 --- a/tests_metricflow/integration/test_cases/itest_dimensions.yaml +++ b/tests_metricflow/integration/test_cases/itest_dimensions.yaml @@ -394,3 +394,10 @@ integration_test: FROM {{ source_schema }}.dim_users GROUP BY {{ render_date_trunc("bio_added_ts", TimeGranularity.SECOND) }} + +# - [ ] nanosecond - will need to skip for some engines +# - [ ] millisecond - will need to skip for some engines +# - [ ] microsecond +# - [ ] second +# - [ ] minute +# - [x] hour diff --git a/tests_metricflow/query_rendering/test_granularity_date_part_rendering.py b/tests_metricflow/query_rendering/test_granularity_date_part_rendering.py index ef9ba71f0b..e892783b97 100644 --- a/tests_metricflow/query_rendering/test_granularity_date_part_rendering.py +++ b/tests_metricflow/query_rendering/test_granularity_date_part_rendering.py @@ -8,10 +8,12 @@ import pytest from _pytest.fixtures import FixtureRequest +from dbt_semantic_interfaces.references import EntityReference from dbt_semantic_interfaces.type_enums.date_part import DatePart from dbt_semantic_interfaces.type_enums.time_granularity import TimeGranularity from metricflow_semantics.specs.metric_spec import MetricSpec from metricflow_semantics.specs.query_spec import MetricFlowQuerySpec +from metricflow_semantics.specs.time_dimension_spec import TimeDimensionSpec from metricflow_semantics.test_helpers.config_helpers import MetricFlowTestConfiguration from metricflow.dataflow.builder.dataflow_plan_builder import DataflowPlanBuilder @@ -99,3 +101,53 @@ def test_offset_window_with_date_part( # noqa: D103 dataflow_plan_builder=dataflow_plan_builder, query_spec=query_spec, ) + + +@pytest.mark.sql_engine_snapshot +def test_sub_daily_metric_time( # noqa: D103 + request: FixtureRequest, + mf_test_configuration: MetricFlowTestConfiguration, + dataflow_plan_builder: DataflowPlanBuilder, + dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter, + sql_client: SqlClient, +) -> None: + query_spec = MetricFlowQuerySpec( + time_dimension_specs=(DataSet.metric_time_dimension_spec(time_granularity=TimeGranularity.SECOND),), + ) + + render_and_check( + request=request, + mf_test_configuration=mf_test_configuration, + dataflow_to_sql_converter=dataflow_to_sql_converter, + sql_client=sql_client, + dataflow_plan_builder=dataflow_plan_builder, + query_spec=query_spec, + ) + + +@pytest.mark.sql_engine_snapshot +def test_sub_daily_dimension( # noqa: D103 + request: FixtureRequest, + mf_test_configuration: MetricFlowTestConfiguration, + dataflow_plan_builder: DataflowPlanBuilder, + dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter, + sql_client: SqlClient, +) -> None: + query_spec = MetricFlowQuerySpec( + time_dimension_specs=( + TimeDimensionSpec( + element_name="bio_added_ts", + time_granularity=TimeGranularity.DAY, + entity_links=(EntityReference("booking"),), + ), + ), + ) + + render_and_check( + request=request, + mf_test_configuration=mf_test_configuration, + dataflow_to_sql_converter=dataflow_to_sql_converter, + sql_client=sql_client, + dataflow_plan_builder=dataflow_plan_builder, + query_spec=query_spec, + ) diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_sub_daily_metric_time__plan0.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_sub_daily_metric_time__plan0.sql new file mode 100644 index 0000000000..4507843952 --- /dev/null +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_sub_daily_metric_time__plan0.sql @@ -0,0 +1,59 @@ +-- Pass Only Elements: ['metric_time__millisecond',] +SELECT + subq_1.metric_time__millisecond +FROM ( + -- Metric Time Dimension 'ts' + SELECT + subq_0.ts__millisecond + , subq_0.ts__second + , subq_0.ts__minute + , subq_0.ts__hour + , subq_0.ts__day + , subq_0.ts__week + , subq_0.ts__month + , subq_0.ts__quarter + , subq_0.ts__year + , subq_0.ts__extract_year + , subq_0.ts__extract_quarter + , subq_0.ts__extract_month + , subq_0.ts__extract_day + , subq_0.ts__extract_dow + , subq_0.ts__extract_doy + , subq_0.ts__millisecond AS metric_time__millisecond + , subq_0.ts__second AS metric_time__second + , subq_0.ts__minute AS metric_time__minute + , subq_0.ts__hour AS metric_time__hour + , subq_0.ts__day AS metric_time__day + , subq_0.ts__week AS metric_time__week + , subq_0.ts__month AS metric_time__month + , subq_0.ts__quarter AS metric_time__quarter + , subq_0.ts__year AS metric_time__year + , subq_0.ts__extract_year AS metric_time__extract_year + , subq_0.ts__extract_quarter AS metric_time__extract_quarter + , subq_0.ts__extract_month AS metric_time__extract_month + , subq_0.ts__extract_day AS metric_time__extract_day + , subq_0.ts__extract_dow AS metric_time__extract_dow + , subq_0.ts__extract_doy AS metric_time__extract_doy + FROM ( + -- Time Spine + SELECT + DATE_TRUNC('millisecond', time_spine_src_28002.ts) AS ts__millisecond + , DATE_TRUNC('second', time_spine_src_28002.ts) AS ts__second + , DATE_TRUNC('minute', time_spine_src_28002.ts) AS ts__minute + , DATE_TRUNC('hour', time_spine_src_28002.ts) AS ts__hour + , DATE_TRUNC('day', time_spine_src_28002.ts) AS ts__day + , DATE_TRUNC('week', time_spine_src_28002.ts) AS ts__week + , DATE_TRUNC('month', time_spine_src_28002.ts) AS ts__month + , DATE_TRUNC('quarter', time_spine_src_28002.ts) AS ts__quarter + , DATE_TRUNC('year', time_spine_src_28002.ts) AS ts__year + , EXTRACT(year FROM time_spine_src_28002.ts) AS ts__extract_year + , EXTRACT(quarter FROM time_spine_src_28002.ts) AS ts__extract_quarter + , EXTRACT(month FROM time_spine_src_28002.ts) AS ts__extract_month + , EXTRACT(day FROM time_spine_src_28002.ts) AS ts__extract_day + , EXTRACT(isodow FROM time_spine_src_28002.ts) AS ts__extract_dow + , EXTRACT(doy FROM time_spine_src_28002.ts) AS ts__extract_doy + FROM ***************************.mf_time_spine_millisecond time_spine_src_28002 + ) subq_0 +) subq_1 +GROUP BY + subq_1.metric_time__millisecond diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_sub_daily_metric_time__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_sub_daily_metric_time__plan0_optimized.sql new file mode 100644 index 0000000000..6bc0e3fe94 --- /dev/null +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_sub_daily_metric_time__plan0_optimized.sql @@ -0,0 +1,8 @@ +-- Time Spine +-- Metric Time Dimension 'ts' +-- Pass Only Elements: ['metric_time__millisecond',] +SELECT + DATE_TRUNC('millisecond', ts) AS metric_time__millisecond +FROM ***************************.mf_time_spine_millisecond time_spine_src_28002 +GROUP BY + DATE_TRUNC('millisecond', ts)