Skip to content

Commit

Permalink
Add test for ID enumeration.
Browse files Browse the repository at this point in the history
  • Loading branch information
plypaul committed Jan 31, 2024
1 parent 6774de2 commit b32103a
Show file tree
Hide file tree
Showing 22 changed files with 1,050 additions and 1 deletion.
106 changes: 105 additions & 1 deletion metricflow/test/integration/test_rendered_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

import pytest
from _pytest.fixtures import FixtureRequest
from dbt_semantic_interfaces.test_utils import as_datetime

from metricflow.dataflow.sql_table import SqlTable
from metricflow.engine.metricflow_engine import MetricFlowQueryRequest
from metricflow.engine.metricflow_engine import MetricFlowEngine, MetricFlowQueryRequest
from metricflow.model.semantic_manifest_lookup import SemanticManifestLookup
from metricflow.plan_conversion.column_resolver import DunderColumnAssociationResolver
from metricflow.protocols.sql_client import SqlClient
from metricflow.test.fixtures.setup_fixtures import MetricFlowTestSessionState
from metricflow.test.integration.conftest import IntegrationTestHelpers
from metricflow.test.snapshot_utils import (
assert_sql_snapshot_equal,
)
from metricflow.test.time.configurable_time_source import ConfigurableTimeSource


@pytest.mark.sql_engine_snapshot
Expand Down Expand Up @@ -51,3 +56,102 @@ def test_render_write_to_table_query( # noqa: D
sql=result.rendered_sql.sql_query,
sql_engine=it_helpers.sql_client.sql_engine_type,
)


@pytest.mark.sql_engine_snapshot
def test_id_enumeration( # noqa: D
request: FixtureRequest,
mf_test_session_state: MetricFlowTestSessionState,
simple_semantic_manifest_lookup: SemanticManifestLookup,
sql_client: SqlClient,
) -> None:
mf_engine = MetricFlowEngine(
semantic_manifest_lookup=simple_semantic_manifest_lookup,
sql_client=sql_client,
column_association_resolver=DunderColumnAssociationResolver(
semantic_manifest_lookup=simple_semantic_manifest_lookup
),
time_source=ConfigurableTimeSource(as_datetime("2020-01-01")),
consistent_id_enumeration=True,
)

result = mf_engine.explain(
MetricFlowQueryRequest.create_with_random_request_id(
metric_names=["bookings", "listings"],
group_by_names=["metric_time"],
)
)

assert_sql_snapshot_equal(
request=request,
mf_test_session_state=mf_test_session_state,
snapshot_id="query",
sql=result.rendered_sql.sql_query,
sql_engine=sql_client.sql_engine_type,
)

# The resulting snapshot should be the same since mf_engine was created with consistent_id_enumeration=True
result = mf_engine.explain(
MetricFlowQueryRequest.create_with_random_request_id(
metric_names=["bookings", "listings"],
group_by_names=["metric_time"],
)
)

assert_sql_snapshot_equal(
request=request,
mf_test_session_state=mf_test_session_state,
snapshot_id="query",
sql=result.rendered_sql.sql_query,
sql_engine=sql_client.sql_engine_type,
)


@pytest.mark.sql_engine_snapshot
def test_id_enumeration_without_consistency_flag(
request: FixtureRequest,
mf_test_session_state: MetricFlowTestSessionState,
simple_semantic_manifest_lookup: SemanticManifestLookup,
sql_client: SqlClient,
) -> None:
"""Test generated SQL with consistent_id_enumeration=False."""
mf_engine = MetricFlowEngine(
semantic_manifest_lookup=simple_semantic_manifest_lookup,
sql_client=sql_client,
column_association_resolver=DunderColumnAssociationResolver(
semantic_manifest_lookup=simple_semantic_manifest_lookup
),
time_source=ConfigurableTimeSource(as_datetime("2020-01-01")),
consistent_id_enumeration=False,
)

result = mf_engine.explain(
MetricFlowQueryRequest.create_with_random_request_id(
metric_names=["bookings", "listings"],
group_by_names=["metric_time"],
)
)

assert_sql_snapshot_equal(
request=request,
mf_test_session_state=mf_test_session_state,
snapshot_id="first_query",
sql=result.rendered_sql.sql_query,
sql_engine=sql_client.sql_engine_type,
)

# The second set of queries should have IDs that are sequentially numbered with respect to the previous query.
result = mf_engine.explain(
MetricFlowQueryRequest.create_with_random_request_id(
metric_names=["bookings", "listings"],
group_by_names=["metric_time"],
)
)

assert_sql_snapshot_equal(
request=request,
mf_test_session_state=mf_test_session_state,
snapshot_id="second_query",
sql=result.rendered_sql.sql_query,
sql_engine=sql_client.sql_engine_type,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- Combine Aggregated Outputs
SELECT
COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) AS metric_time__day
, MAX(subq_4.bookings) AS bookings
, MAX(subq_9.listings) AS listings
FROM (
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
metric_time__day
, SUM(bookings) AS bookings
FROM (
-- Read Elements From Semantic Model 'bookings_source'
-- Metric Time Dimension 'ds'
-- Pass Only Elements: ['bookings', 'metric_time__day']
SELECT
DATE_TRUNC(ds, day) AS metric_time__day
, 1 AS bookings
FROM ***************************.fct_bookings bookings_source_src_10000
) subq_2
GROUP BY
metric_time__day
) subq_4
FULL OUTER JOIN (
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
metric_time__day
, SUM(listings) AS listings
FROM (
-- Read Elements From Semantic Model 'listings_latest'
-- Metric Time Dimension 'ds'
-- Pass Only Elements: ['listings', 'metric_time__day']
SELECT
DATE_TRUNC(created_at, day) AS metric_time__day
, 1 AS listings
FROM ***************************.dim_listings_latest listings_latest_src_10000
) subq_7
GROUP BY
metric_time__day
) subq_9
ON
subq_4.metric_time__day = subq_9.metric_time__day
GROUP BY
metric_time__day
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- Combine Aggregated Outputs
SELECT
COALESCE(subq_10013.metric_time__day, subq_10018.metric_time__day) AS metric_time__day
, MAX(subq_10013.bookings) AS bookings
, MAX(subq_10018.listings) AS listings
FROM (
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
metric_time__day
, SUM(bookings) AS bookings
FROM (
-- Read Elements From Semantic Model 'bookings_source'
-- Metric Time Dimension 'ds'
-- Pass Only Elements: ['bookings', 'metric_time__day']
SELECT
DATE_TRUNC(ds, day) AS metric_time__day
, 1 AS bookings
FROM ***************************.fct_bookings bookings_source_src_10000
) subq_10011
GROUP BY
metric_time__day
) subq_10013
FULL OUTER JOIN (
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
metric_time__day
, SUM(listings) AS listings
FROM (
-- Read Elements From Semantic Model 'listings_latest'
-- Metric Time Dimension 'ds'
-- Pass Only Elements: ['listings', 'metric_time__day']
SELECT
DATE_TRUNC(created_at, day) AS metric_time__day
, 1 AS listings
FROM ***************************.dim_listings_latest listings_latest_src_10000
) subq_10016
GROUP BY
metric_time__day
) subq_10018
ON
subq_10013.metric_time__day = subq_10018.metric_time__day
GROUP BY
metric_time__day
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- Combine Aggregated Outputs
SELECT
COALESCE(subq_10023.metric_time__day, subq_10028.metric_time__day) AS metric_time__day
, MAX(subq_10023.bookings) AS bookings
, MAX(subq_10028.listings) AS listings
FROM (
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
metric_time__day
, SUM(bookings) AS bookings
FROM (
-- Read Elements From Semantic Model 'bookings_source'
-- Metric Time Dimension 'ds'
-- Pass Only Elements: ['bookings', 'metric_time__day']
SELECT
DATE_TRUNC(ds, day) AS metric_time__day
, 1 AS bookings
FROM ***************************.fct_bookings bookings_source_src_10000
) subq_10021
GROUP BY
metric_time__day
) subq_10023
FULL OUTER JOIN (
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
metric_time__day
, SUM(listings) AS listings
FROM (
-- Read Elements From Semantic Model 'listings_latest'
-- Metric Time Dimension 'ds'
-- Pass Only Elements: ['listings', 'metric_time__day']
SELECT
DATE_TRUNC(created_at, day) AS metric_time__day
, 1 AS listings
FROM ***************************.dim_listings_latest listings_latest_src_10000
) subq_10026
GROUP BY
metric_time__day
) subq_10028
ON
subq_10023.metric_time__day = subq_10028.metric_time__day
GROUP BY
metric_time__day
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- Combine Aggregated Outputs
SELECT
COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) AS metric_time__day
, MAX(subq_4.bookings) AS bookings
, MAX(subq_9.listings) AS listings
FROM (
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
metric_time__day
, SUM(bookings) AS bookings
FROM (
-- Read Elements From Semantic Model 'bookings_source'
-- Metric Time Dimension 'ds'
-- Pass Only Elements: ['bookings', 'metric_time__day']
SELECT
DATE_TRUNC('day', ds) AS metric_time__day
, 1 AS bookings
FROM ***************************.fct_bookings bookings_source_src_10000
) subq_2
GROUP BY
metric_time__day
) subq_4
FULL OUTER JOIN (
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
metric_time__day
, SUM(listings) AS listings
FROM (
-- Read Elements From Semantic Model 'listings_latest'
-- Metric Time Dimension 'ds'
-- Pass Only Elements: ['listings', 'metric_time__day']
SELECT
DATE_TRUNC('day', created_at) AS metric_time__day
, 1 AS listings
FROM ***************************.dim_listings_latest listings_latest_src_10000
) subq_7
GROUP BY
metric_time__day
) subq_9
ON
subq_4.metric_time__day = subq_9.metric_time__day
GROUP BY
COALESCE(subq_4.metric_time__day, subq_9.metric_time__day)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- Combine Aggregated Outputs
SELECT
COALESCE(subq_10013.metric_time__day, subq_10018.metric_time__day) AS metric_time__day
, MAX(subq_10013.bookings) AS bookings
, MAX(subq_10018.listings) AS listings
FROM (
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
metric_time__day
, SUM(bookings) AS bookings
FROM (
-- Read Elements From Semantic Model 'bookings_source'
-- Metric Time Dimension 'ds'
-- Pass Only Elements: ['bookings', 'metric_time__day']
SELECT
DATE_TRUNC('day', ds) AS metric_time__day
, 1 AS bookings
FROM ***************************.fct_bookings bookings_source_src_10000
) subq_10011
GROUP BY
metric_time__day
) subq_10013
FULL OUTER JOIN (
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
metric_time__day
, SUM(listings) AS listings
FROM (
-- Read Elements From Semantic Model 'listings_latest'
-- Metric Time Dimension 'ds'
-- Pass Only Elements: ['listings', 'metric_time__day']
SELECT
DATE_TRUNC('day', created_at) AS metric_time__day
, 1 AS listings
FROM ***************************.dim_listings_latest listings_latest_src_10000
) subq_10016
GROUP BY
metric_time__day
) subq_10018
ON
subq_10013.metric_time__day = subq_10018.metric_time__day
GROUP BY
COALESCE(subq_10013.metric_time__day, subq_10018.metric_time__day)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- Combine Aggregated Outputs
SELECT
COALESCE(subq_10023.metric_time__day, subq_10028.metric_time__day) AS metric_time__day
, MAX(subq_10023.bookings) AS bookings
, MAX(subq_10028.listings) AS listings
FROM (
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
metric_time__day
, SUM(bookings) AS bookings
FROM (
-- Read Elements From Semantic Model 'bookings_source'
-- Metric Time Dimension 'ds'
-- Pass Only Elements: ['bookings', 'metric_time__day']
SELECT
DATE_TRUNC('day', ds) AS metric_time__day
, 1 AS bookings
FROM ***************************.fct_bookings bookings_source_src_10000
) subq_10021
GROUP BY
metric_time__day
) subq_10023
FULL OUTER JOIN (
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
metric_time__day
, SUM(listings) AS listings
FROM (
-- Read Elements From Semantic Model 'listings_latest'
-- Metric Time Dimension 'ds'
-- Pass Only Elements: ['listings', 'metric_time__day']
SELECT
DATE_TRUNC('day', created_at) AS metric_time__day
, 1 AS listings
FROM ***************************.dim_listings_latest listings_latest_src_10000
) subq_10026
GROUP BY
metric_time__day
) subq_10028
ON
subq_10023.metric_time__day = subq_10028.metric_time__day
GROUP BY
COALESCE(subq_10023.metric_time__day, subq_10028.metric_time__day)
Loading

0 comments on commit b32103a

Please sign in to comment.