Skip to content

Commit

Permalink
Update snapshots with DuckDB.
Browse files Browse the repository at this point in the history
  • Loading branch information
plypaul committed Nov 14, 2024
1 parent 47bc9f2 commit f53fb2e
Show file tree
Hide file tree
Showing 66 changed files with 3,320 additions and 2,375 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,60 @@ docstring:
Test querying a derived metric with a cumulative input metric using non-default grains.
sql_engine: DuckDB
---
-- Compute Metrics via Expressions
SELECT
metric_time__week
, t2mr - 10 AS trailing_2_months_revenue_sub_10
FROM (
-- Re-aggregate Metric via Group By
-- Read From CTE For node_id=cm_5
WITH cm_4_cte AS (
-- Compute Metrics via Expressions
SELECT
metric_time__week
, txn_revenue AS t2mr
FROM (
-- Join Self Over Time Range
-- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day']
-- Aggregate Measures
SELECT
subq_13.ds AS metric_time__day
, DATE_TRUNC('week', subq_13.ds) AS metric_time__week
, SUM(revenue_src_28000.revenue) AS txn_revenue
FROM ***************************.mf_time_spine subq_13
INNER JOIN
***************************.fct_revenue revenue_src_28000
ON
(
DATE_TRUNC('day', revenue_src_28000.created_at) <= subq_13.ds
) AND (
DATE_TRUNC('day', revenue_src_28000.created_at) > subq_13.ds - INTERVAL 2 month
)
GROUP BY
subq_13.ds
, DATE_TRUNC('week', subq_13.ds)
) subq_16
)

, cm_5_cte AS (
-- Compute Metrics via Expressions
SELECT
metric_time__week
, t2mr
, t2mr - 10 AS trailing_2_months_revenue_sub_10
FROM (
-- Compute Metrics via Expressions
-- Window Function for Metric Re-aggregation
-- Re-aggregate Metric via Group By
SELECT
metric_time__week
, AVG(txn_revenue) OVER (PARTITION BY metric_time__week) AS t2mr
, t2mr
FROM (
-- Join Self Over Time Range
-- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day']
-- Aggregate Measures
-- Read From CTE For node_id=cm_4
-- Window Function for Metric Re-aggregation
SELECT
subq_13.ds AS metric_time__day
, DATE_TRUNC('week', subq_13.ds) AS metric_time__week
, SUM(revenue_src_28000.revenue) AS txn_revenue
FROM ***************************.mf_time_spine subq_13
INNER JOIN
***************************.fct_revenue revenue_src_28000
ON
(
DATE_TRUNC('day', revenue_src_28000.created_at) <= subq_13.ds
) AND (
DATE_TRUNC('day', revenue_src_28000.created_at) > subq_13.ds - INTERVAL 2 month
)
GROUP BY
subq_13.ds
, DATE_TRUNC('week', subq_13.ds)
) subq_16
) subq_18
GROUP BY
metric_time__week
, t2mr
) subq_19
metric_time__week
, AVG(t2mr) OVER (PARTITION BY metric_time__week) AS t2mr
FROM cm_4_cte cm_4_cte
) subq_18
GROUP BY
metric_time__week
, t2mr
) subq_19
)

SELECT
metric_time__week AS metric_time__week
, trailing_2_months_revenue_sub_10 AS trailing_2_months_revenue_sub_10
FROM cm_5_cte cm_5_cte
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_custom_granularity
test_filename: test_custom_granularity.py
sql_engine: DuckDB
---
-- Compute Metrics via Expressions
SELECT
booking__ds__martian_day
, booking_value * 0.05 / bookers AS booking_fees_per_booker
FROM (
-- Read From CTE For node_id=cm_9
WITH cm_8_cte AS (
-- Metric Time Dimension 'ds'
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__martian_day']
Expand All @@ -23,4 +20,24 @@ FROM (
DATE_TRUNC('day', bookings_source_src_28000.ds) = subq_14.ds
GROUP BY
subq_14.martian_day
) subq_18
)

, cm_9_cte AS (
-- Compute Metrics via Expressions
SELECT
booking__ds__martian_day
, booking_value * 0.05 / bookers AS booking_fees_per_booker
FROM (
-- Read From CTE For node_id=cm_8
SELECT
booking__ds__martian_day
, booking_value
, bookers
FROM cm_8_cte cm_8_cte
) subq_18
)

SELECT
booking__ds__martian_day AS booking__ds__martian_day
, booking_fees_per_booker AS booking_fees_per_booker
FROM cm_9_cte cm_9_cte
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ test_filename: test_custom_granularity.py
sql_engine: DuckDB
---
-- Combine Aggregated Outputs
SELECT
COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) AS metric_time__martian_day
, MAX(subq_17.bookings) AS bookings
, MAX(subq_23.listings) AS listings
FROM (
WITH cm_4_cte AS (
-- Metric Time Dimension 'ds'
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['bookings', 'metric_time__martian_day']
Expand All @@ -29,8 +25,9 @@ FROM (
subq_12.ds__day = subq_13.ds
GROUP BY
subq_13.martian_day
) subq_17
FULL OUTER JOIN (
)

, cm_5_cte AS (
-- Metric Time Dimension 'ds'
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['listings', 'metric_time__martian_day']
Expand All @@ -52,8 +49,16 @@ FULL OUTER JOIN (
subq_18.ds__day = subq_19.ds
GROUP BY
subq_19.martian_day
) subq_23
)

SELECT
COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) AS metric_time__martian_day
, MAX(cm_4_cte.bookings) AS bookings
, MAX(cm_5_cte.listings) AS listings
FROM cm_4_cte cm_4_cte
FULL OUTER JOIN
cm_5_cte cm_5_cte
ON
subq_17.metric_time__martian_day = subq_23.metric_time__martian_day
cm_4_cte.metric_time__martian_day = cm_5_cte.metric_time__martian_day
GROUP BY
COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day)
COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day)
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity
test_filename: test_custom_granularity.py
sql_engine: DuckDB
---
-- Compute Metrics via Expressions
SELECT
booking__ds__martian_day
, bookings_5_days_ago AS bookings_5_day_lag
FROM (
-- Read From CTE For node_id=cm_5
WITH cm_4_cte AS (
-- Join to Time Spine Dataset
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['bookings', 'booking__ds__martian_day']
Expand All @@ -32,4 +29,23 @@ FROM (
subq_12.ds = subq_13.ds
GROUP BY
subq_13.martian_day
) subq_17
)

, cm_5_cte AS (
-- Compute Metrics via Expressions
SELECT
booking__ds__martian_day
, bookings_5_days_ago AS bookings_5_day_lag
FROM (
-- Read From CTE For node_id=cm_4
SELECT
booking__ds__martian_day
, bookings_5_days_ago
FROM cm_4_cte cm_4_cte
) subq_17
)

SELECT
booking__ds__martian_day AS booking__ds__martian_day
, bookings_5_day_lag AS bookings_5_day_lag
FROM cm_5_cte cm_5_cte
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity_filter_not_in_group_by
test_filename: test_custom_granularity.py
sql_engine: DuckDB
---
-- Compute Metrics via Expressions
SELECT
metric_time__day
, bookings_5_days_ago AS bookings_5_day_lag
FROM (
-- Read From CTE For node_id=cm_5
WITH cm_4_cte AS (
-- Constrain Output with WHERE
-- Pass Only Elements: ['bookings', 'metric_time__day']
-- Aggregate Measures
Expand Down Expand Up @@ -40,4 +37,23 @@ FROM (
WHERE metric_time__martian_day = '2020-01-01'
GROUP BY
metric_time__day
) subq_19
)

, cm_5_cte AS (
-- Compute Metrics via Expressions
SELECT
metric_time__day
, bookings_5_days_ago AS bookings_5_day_lag
FROM (
-- Read From CTE For node_id=cm_4
SELECT
metric_time__day
, bookings_5_days_ago
FROM cm_4_cte cm_4_cte
) subq_19
)

SELECT
metric_time__day AS metric_time__day
, bookings_5_day_lag AS bookings_5_day_lag
FROM cm_5_cte cm_5_cte
Loading

0 comments on commit f53fb2e

Please sign in to comment.