Skip to content

Commit

Permalink
SQL tests for dimension-only sub-daily queries
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Jul 26, 2024
1 parent b11c539 commit b649bf1
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tests_metricflow/integration/test_cases/itest_dimensions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit b649bf1

Please sign in to comment.