Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fill nulls for multi-metric queries #850

Merged
merged 9 commits into from
Nov 8, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ table_snapshot:
- ["2020-01-02", "2020-01-02", "u0004114", ""]
- ["2020-01-02", "2020-01-02", "u0004114", "l7891283-incomplete"]
- ["2020-01-04", "2020-01-02", "u1612112", "l2718281"]
- ["2020-01-05", "2020-01-02", "u0004114", ""]
Empty file.
145 changes: 145 additions & 0 deletions metricflow/test/integration/query_output/test_fill_nulls_with_0.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
from __future__ import annotations

import datetime

import pytest
from _pytest.fixtures import FixtureRequest

from metricflow.engine.metricflow_engine import MetricFlowQueryRequest
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_object_snapshot_equal


@pytest.mark.sql_engine_snapshot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These snapshot tests are great, thanks for doing this!

def test_simple_fill_nulls_with_0_metric_time( # noqa: D
request: FixtureRequest,
mf_test_session_state: MetricFlowTestSessionState,
sql_client: SqlClient,
it_helpers: IntegrationTestHelpers,
) -> None:
query_result = it_helpers.mf_engine.query(
MetricFlowQueryRequest.create_with_random_request_id(
metric_names=["bookings_fill_nulls_with_0"],
group_by_names=["metric_time"],
order_by_names=["metric_time"],
time_constraint_start=datetime.datetime(2019, 11, 27),
time_constraint_end=datetime.datetime(2020, 1, 5),
)
)
assert query_result.result_df is not None, "Unexpected empty result."

assert_object_snapshot_equal(
request=request,
mf_test_session_state=mf_test_session_state,
obj_id="query_output",
obj=query_result.result_df.to_string(),
sql_client=sql_client,
)


@pytest.mark.sql_engine_snapshot
def test_simple_fill_nulls_with_0_month( # noqa: D
request: FixtureRequest,
mf_test_session_state: MetricFlowTestSessionState,
sql_client: SqlClient,
it_helpers: IntegrationTestHelpers,
) -> None:
query_result = it_helpers.mf_engine.query(
MetricFlowQueryRequest.create_with_random_request_id(
metric_names=["bookings_fill_nulls_with_0"],
group_by_names=["metric_time__month"],
order_by_names=["metric_time__month"],
time_constraint_start=datetime.datetime(2019, 1, 1),
time_constraint_end=datetime.datetime(2020, 12, 1),
)
)
assert query_result.result_df is not None, "Unexpected empty result."

assert_object_snapshot_equal(
request=request,
mf_test_session_state=mf_test_session_state,
obj_id="query_output",
obj=query_result.result_df.to_string(),
sql_client=sql_client,
)


@pytest.mark.sql_engine_snapshot
def test_simple_join_to_time_spine( # noqa: D
request: FixtureRequest,
mf_test_session_state: MetricFlowTestSessionState,
sql_client: SqlClient,
it_helpers: IntegrationTestHelpers,
) -> None:
query_result = it_helpers.mf_engine.query(
MetricFlowQueryRequest.create_with_random_request_id(
metric_names=["bookings_join_to_time_spine"],
group_by_names=["metric_time"],
time_constraint_start=datetime.datetime(2019, 11, 27),
time_constraint_end=datetime.datetime(2020, 1, 5),
order_by_names=["metric_time"],
)
)
assert query_result.result_df is not None, "Unexpected empty result."

assert_object_snapshot_equal(
request=request,
mf_test_session_state=mf_test_session_state,
obj_id="query_output",
obj=query_result.result_df.to_string(),
sql_client=sql_client,
)


@pytest.mark.sql_engine_snapshot
def test_fill_nulls_with_0_multi_metric_query( # noqa: D
request: FixtureRequest,
mf_test_session_state: MetricFlowTestSessionState,
sql_client: SqlClient,
it_helpers: IntegrationTestHelpers,
) -> None:
query_result = it_helpers.mf_engine.query(
MetricFlowQueryRequest.create_with_random_request_id(
metric_names=["bookings_fill_nulls_with_0", "views"],
group_by_names=["metric_time"],
order_by_names=["metric_time"],
time_constraint_start=datetime.datetime(2019, 11, 27),
time_constraint_end=datetime.datetime(2020, 1, 5),
)
)
assert query_result.result_df is not None, "Unexpected empty result."

assert_object_snapshot_equal(
request=request,
mf_test_session_state=mf_test_session_state,
obj_id="query_output",
obj=query_result.result_df.to_string(),
sql_client=sql_client,
)


@pytest.mark.sql_engine_snapshot
def test_fill_nulls_with_0_multi_metric_query_with_categorical_dimension( # noqa: D
request: FixtureRequest,
mf_test_session_state: MetricFlowTestSessionState,
sql_client: SqlClient,
it_helpers: IntegrationTestHelpers,
) -> None:
query_result = it_helpers.mf_engine.query(
MetricFlowQueryRequest.create_with_random_request_id(
metric_names=["bookings_fill_nulls_with_0_without_time_spine", "views"],
group_by_names=["metric_time", "listing__is_lux_latest"],
order_by_names=["metric_time", "listing__is_lux_latest"],
)
)
assert query_result.result_df is not None, "Unexpected empty result."

assert_object_snapshot_equal(
request=request,
mf_test_session_state=mf_test_session_state,
obj_id="query_output",
obj=query_result.result_df.to_string(),
sql_client=sql_client,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
metric_time__day bookings_fill_nulls_with_0 views
0 2019-11-27 0 NaN
1 2019-11-28 0 NaN
2 2019-11-29 0 NaN
3 2019-11-30 0 NaN
4 2019-12-01 1 NaN
5 2019-12-02 0 NaN
6 2019-12-03 0 NaN
7 2019-12-04 0 NaN
8 2019-12-05 0 NaN
9 2019-12-06 0 NaN
10 2019-12-07 0 NaN
11 2019-12-08 0 NaN
12 2019-12-09 0 NaN
13 2019-12-10 0 NaN
14 2019-12-11 0 NaN
15 2019-12-12 0 NaN
16 2019-12-13 0 NaN
17 2019-12-14 0 NaN
18 2019-12-15 0 NaN
19 2019-12-16 0 NaN
20 2019-12-17 0 NaN
21 2019-12-18 10 NaN
22 2019-12-19 18 NaN
23 2019-12-20 2 NaN
24 2019-12-21 0 NaN
25 2019-12-22 0 NaN
26 2019-12-23 0 NaN
27 2019-12-24 0 NaN
28 2019-12-25 0 NaN
29 2019-12-26 0 NaN
30 2019-12-27 0 NaN
31 2019-12-28 0 NaN
32 2019-12-29 0 NaN
33 2019-12-30 0 NaN
34 2019-12-31 0 NaN
35 2020-01-01 5 2.0
36 2020-01-02 9 5.0
37 2020-01-03 1 NaN
38 2020-01-04 0 1.0
39 2020-01-05 0 1.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
metric_time__day listing__is_lux_latest bookings_fill_nulls_with_0_without_time_spine views
0 2019-12-01 True 1 NaN
1 2019-12-18 False 4 NaN
2 2019-12-18 True 6 NaN
3 2019-12-19 None 6 NaN
4 2019-12-19 False 6 NaN
5 2019-12-19 True 6 NaN
6 2019-12-20 True 2 NaN
7 2020-01-01 False 2 NaN
8 2020-01-01 True 3 2.0
9 2020-01-02 None 3 1.0
10 2020-01-02 False 3 3.0
11 2020-01-02 True 3 1.0
12 2020-01-03 True 1 NaN
13 2020-01-04 False 0 1.0
14 2020-01-05 None 0 1.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
metric_time__day bookings_fill_nulls_with_0
0 2019-11-27 0
1 2019-11-28 0
2 2019-11-29 0
3 2019-11-30 0
4 2019-12-01 1
5 2019-12-02 0
6 2019-12-03 0
7 2019-12-04 0
8 2019-12-05 0
9 2019-12-06 0
10 2019-12-07 0
11 2019-12-08 0
12 2019-12-09 0
13 2019-12-10 0
14 2019-12-11 0
15 2019-12-12 0
16 2019-12-13 0
17 2019-12-14 0
18 2019-12-15 0
19 2019-12-16 0
20 2019-12-17 0
21 2019-12-18 10
22 2019-12-19 18
23 2019-12-20 2
24 2019-12-21 0
25 2019-12-22 0
26 2019-12-23 0
27 2019-12-24 0
28 2019-12-25 0
29 2019-12-26 0
30 2019-12-27 0
31 2019-12-28 0
32 2019-12-29 0
33 2019-12-30 0
34 2019-12-31 0
35 2020-01-01 5
36 2020-01-02 9
37 2020-01-03 1
38 2020-01-04 0
39 2020-01-05 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
metric_time__month bookings_fill_nulls_with_0
0 2019-01-01 0
1 2019-02-01 0
2 2019-03-01 0
3 2019-04-01 0
4 2019-05-01 0
5 2019-06-01 0
6 2019-07-01 0
7 2019-08-01 0
8 2019-09-01 0
9 2019-10-01 0
10 2019-11-01 0
11 2019-12-01 31
12 2020-01-01 15
13 2020-02-01 0
14 2020-03-01 0
15 2020-04-01 0
16 2020-05-01 0
17 2020-06-01 0
18 2020-07-01 0
19 2020-08-01 0
20 2020-09-01 0
21 2020-10-01 0
22 2020-11-01 0
23 2020-12-01 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
metric_time__day bookings_join_to_time_spine
0 2019-11-27 NaN
1 2019-11-28 NaN
2 2019-11-29 NaN
3 2019-11-30 NaN
4 2019-12-01 1.0
5 2019-12-02 NaN
6 2019-12-03 NaN
7 2019-12-04 NaN
8 2019-12-05 NaN
9 2019-12-06 NaN
10 2019-12-07 NaN
11 2019-12-08 NaN
12 2019-12-09 NaN
13 2019-12-10 NaN
14 2019-12-11 NaN
15 2019-12-12 NaN
16 2019-12-13 NaN
17 2019-12-14 NaN
18 2019-12-15 NaN
19 2019-12-16 NaN
20 2019-12-17 NaN
21 2019-12-18 10.0
22 2019-12-19 18.0
23 2019-12-20 2.0
24 2019-12-21 NaN
25 2019-12-22 NaN
26 2019-12-23 NaN
27 2019-12-24 NaN
28 2019-12-25 NaN
29 2019-12-26 NaN
30 2019-12-27 NaN
31 2019-12-28 NaN
32 2019-12-29 NaN
33 2019-12-30 NaN
34 2019-12-31 NaN
35 2020-01-01 5.0
36 2020-01-02 9.0
37 2020-01-03 1.0
38 2020-01-04 NaN
39 2020-01-05 NaN
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
metric_time__day bookings_fill_nulls_with_0 views
0 2019-11-27 0 NaN
1 2019-11-28 0 NaN
2 2019-11-29 0 NaN
3 2019-11-30 0 NaN
4 2019-12-01 1 NaN
5 2019-12-02 0 NaN
6 2019-12-03 0 NaN
7 2019-12-04 0 NaN
8 2019-12-05 0 NaN
9 2019-12-06 0 NaN
10 2019-12-07 0 NaN
11 2019-12-08 0 NaN
12 2019-12-09 0 NaN
13 2019-12-10 0 NaN
14 2019-12-11 0 NaN
15 2019-12-12 0 NaN
16 2019-12-13 0 NaN
17 2019-12-14 0 NaN
18 2019-12-15 0 NaN
19 2019-12-16 0 NaN
20 2019-12-17 0 NaN
21 2019-12-18 10 NaN
22 2019-12-19 18 NaN
23 2019-12-20 2 NaN
24 2019-12-21 0 NaN
25 2019-12-22 0 NaN
26 2019-12-23 0 NaN
27 2019-12-24 0 NaN
28 2019-12-25 0 NaN
29 2019-12-26 0 NaN
30 2019-12-27 0 NaN
31 2019-12-28 0 NaN
32 2019-12-29 0 NaN
33 2019-12-30 0 NaN
34 2019-12-31 0 NaN
35 2020-01-01 5 2.0
36 2020-01-02 9 5.0
37 2020-01-03 1 NaN
38 2020-01-04 0 1.0
39 2020-01-05 0 1.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
metric_time__day listing__is_lux_latest bookings_fill_nulls_with_0_without_time_spine views
0 2019-12-01 True 1 NaN
1 2019-12-18 False 4 NaN
2 2019-12-18 True 6 NaN
3 2019-12-19 False 6 NaN
4 2019-12-19 True 6 NaN
5 2019-12-19 None 6 NaN
6 2019-12-20 True 2 NaN
7 2020-01-01 False 2 NaN
8 2020-01-01 True 3 2.0
9 2020-01-02 False 3 3.0
10 2020-01-02 True 3 1.0
11 2020-01-02 None 3 1.0
12 2020-01-03 True 1 NaN
13 2020-01-04 False 0 1.0
14 2020-01-05 None 0 1.0
Loading