Skip to content

Commit

Permalink
Change order of descriptions in SQL output
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Sep 24, 2024
1 parent ea8bbfc commit 4e728b6
Show file tree
Hide file tree
Showing 27 changed files with 245 additions and 36 deletions.
18 changes: 9 additions & 9 deletions metricflow/plan_conversion/dataflow_to_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1087,9 +1087,9 @@ def visit_metric_time_dimension_transform_node(self, node: MetricTimeDimensionTr
spec=metric_time_dimension_spec,
)
)
output_column_to_input_column[
metric_time_dimension_column_association.column_name
] = matching_time_dimension_instance.associated_column.column_name
output_column_to_input_column[metric_time_dimension_column_association.column_name] = (
matching_time_dimension_instance.associated_column.column_name
)

output_instance_set = InstanceSet(
measure_instances=tuple(output_measure_instances),
Expand Down Expand Up @@ -1324,11 +1324,11 @@ def visit_join_to_time_spine_node(self, node: JoinToTimeSpineNode) -> SqlDataSet
and len(time_spine_dataset.checked_sql_select_node.select_columns) == 1
), "Time spine dataset not configured properly. Expected exactly one column."
original_time_spine_dim_instance = time_spine_dataset.instance_set.time_dimension_instances[0]
time_spine_column_select_expr: Union[
SqlColumnReferenceExpression, SqlDateTruncExpression
] = SqlColumnReferenceExpression.create(
SqlColumnReference(
table_alias=time_spine_alias, column_name=original_time_spine_dim_instance.spec.qualified_name
time_spine_column_select_expr: Union[SqlColumnReferenceExpression, SqlDateTruncExpression] = (
SqlColumnReferenceExpression.create(
SqlColumnReference(
table_alias=time_spine_alias, column_name=original_time_spine_dim_instance.spec.qualified_name
)
)
)

Expand Down Expand Up @@ -1492,7 +1492,7 @@ def visit_join_to_custom_granularity_node(self, node: JoinToCustomGranularityNod
return SqlDataSet(
instance_set=InstanceSet.merge([time_spine_instance_set, parent_data_set.instance_set]),
sql_select_node=SqlSelectStatementNode.create(
description=node.description + "\n" + parent_data_set.checked_sql_select_node.description,
description=parent_data_set.checked_sql_select_node.description + "\n" + node.description,
select_columns=parent_data_set.checked_sql_select_node.select_columns + time_spine_select_columns,
from_source=parent_data_set.checked_sql_select_node.from_source,
from_source_alias=parent_alias,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
-- Re-aggregate Metric via Group By
SELECT
subq_9.metric_time__martian_day
, subq_9.trailing_2_months_revenue
FROM (
-- Window Function for Metric Re-aggregation
SELECT
subq_8.metric_time__martian_day
, AVG(subq_8.trailing_2_months_revenue) OVER (PARTITION BY subq_8.metric_time__martian_day) AS trailing_2_months_revenue
FROM (
-- Compute Metrics via Expressions
SELECT
subq_7.metric_time__martian_day
, subq_7.metric_time__day
, subq_7.txn_revenue AS trailing_2_months_revenue
FROM (
-- Aggregate Measures
SELECT
subq_6.metric_time__martian_day
, subq_6.metric_time__day
, SUM(subq_6.txn_revenue) AS txn_revenue
FROM (
-- Pass Only Elements: ['txn_revenue', 'metric_time__day', 'metric_time__day']
-- Join to Custom Granularity Dataset
SELECT
subq_4.metric_time__day AS metric_time__day
, subq_4.txn_revenue AS txn_revenue
, subq_5.martian_day AS metric_time__martian_day
FROM (
-- Join Self Over Time Range
SELECT
subq_2.metric_time__day AS metric_time__day
, subq_1.ds__day AS ds__day
, subq_1.ds__week AS ds__week
, subq_1.ds__month AS ds__month
, subq_1.ds__quarter AS ds__quarter
, subq_1.ds__year AS ds__year
, subq_1.ds__extract_year AS ds__extract_year
, subq_1.ds__extract_quarter AS ds__extract_quarter
, subq_1.ds__extract_month AS ds__extract_month
, subq_1.ds__extract_day AS ds__extract_day
, subq_1.ds__extract_dow AS ds__extract_dow
, subq_1.ds__extract_doy AS ds__extract_doy
, subq_1.revenue_instance__ds__day AS revenue_instance__ds__day
, subq_1.revenue_instance__ds__week AS revenue_instance__ds__week
, subq_1.revenue_instance__ds__month AS revenue_instance__ds__month
, subq_1.revenue_instance__ds__quarter AS revenue_instance__ds__quarter
, subq_1.revenue_instance__ds__year AS revenue_instance__ds__year
, subq_1.revenue_instance__ds__extract_year AS revenue_instance__ds__extract_year
, subq_1.revenue_instance__ds__extract_quarter AS revenue_instance__ds__extract_quarter
, subq_1.revenue_instance__ds__extract_month AS revenue_instance__ds__extract_month
, subq_1.revenue_instance__ds__extract_day AS revenue_instance__ds__extract_day
, subq_1.revenue_instance__ds__extract_dow AS revenue_instance__ds__extract_dow
, subq_1.revenue_instance__ds__extract_doy AS revenue_instance__ds__extract_doy
, subq_1.metric_time__week AS metric_time__week
, subq_1.metric_time__month AS metric_time__month
, subq_1.metric_time__quarter AS metric_time__quarter
, subq_1.metric_time__year AS metric_time__year
, subq_1.metric_time__extract_year AS metric_time__extract_year
, subq_1.metric_time__extract_quarter AS metric_time__extract_quarter
, subq_1.metric_time__extract_month AS metric_time__extract_month
, subq_1.metric_time__extract_day AS metric_time__extract_day
, subq_1.metric_time__extract_dow AS metric_time__extract_dow
, subq_1.metric_time__extract_doy AS metric_time__extract_doy
, subq_1.user AS user
, subq_1.revenue_instance__user AS revenue_instance__user
, subq_1.txn_revenue AS txn_revenue
FROM (
-- Time Spine
SELECT
subq_3.ds AS metric_time__day
FROM ***************************.mf_time_spine subq_3
) subq_2
INNER JOIN (
-- Metric Time Dimension 'ds'
SELECT
subq_0.ds__day
, subq_0.ds__week
, subq_0.ds__month
, subq_0.ds__quarter
, subq_0.ds__year
, subq_0.ds__extract_year
, subq_0.ds__extract_quarter
, subq_0.ds__extract_month
, subq_0.ds__extract_day
, subq_0.ds__extract_dow
, subq_0.ds__extract_doy
, subq_0.revenue_instance__ds__day
, subq_0.revenue_instance__ds__week
, subq_0.revenue_instance__ds__month
, subq_0.revenue_instance__ds__quarter
, subq_0.revenue_instance__ds__year
, subq_0.revenue_instance__ds__extract_year
, subq_0.revenue_instance__ds__extract_quarter
, subq_0.revenue_instance__ds__extract_month
, subq_0.revenue_instance__ds__extract_day
, subq_0.revenue_instance__ds__extract_dow
, subq_0.revenue_instance__ds__extract_doy
, subq_0.ds__day AS metric_time__day
, subq_0.ds__week AS metric_time__week
, subq_0.ds__month AS metric_time__month
, subq_0.ds__quarter AS metric_time__quarter
, subq_0.ds__year AS metric_time__year
, subq_0.ds__extract_year AS metric_time__extract_year
, subq_0.ds__extract_quarter AS metric_time__extract_quarter
, subq_0.ds__extract_month AS metric_time__extract_month
, subq_0.ds__extract_day AS metric_time__extract_day
, subq_0.ds__extract_dow AS metric_time__extract_dow
, subq_0.ds__extract_doy AS metric_time__extract_doy
, subq_0.user
, subq_0.revenue_instance__user
, subq_0.txn_revenue
FROM (
-- Read Elements From Semantic Model 'revenue'
SELECT
revenue_src_28000.revenue AS txn_revenue
, DATE_TRUNC('day', revenue_src_28000.created_at) AS ds__day
, DATE_TRUNC('week', revenue_src_28000.created_at) AS ds__week
, DATE_TRUNC('month', revenue_src_28000.created_at) AS ds__month
, DATE_TRUNC('quarter', revenue_src_28000.created_at) AS ds__quarter
, DATE_TRUNC('year', revenue_src_28000.created_at) AS ds__year
, EXTRACT(year FROM revenue_src_28000.created_at) AS ds__extract_year
, EXTRACT(quarter FROM revenue_src_28000.created_at) AS ds__extract_quarter
, EXTRACT(month FROM revenue_src_28000.created_at) AS ds__extract_month
, EXTRACT(day FROM revenue_src_28000.created_at) AS ds__extract_day
, EXTRACT(isodow FROM revenue_src_28000.created_at) AS ds__extract_dow
, EXTRACT(doy FROM revenue_src_28000.created_at) AS ds__extract_doy
, DATE_TRUNC('day', revenue_src_28000.created_at) AS revenue_instance__ds__day
, DATE_TRUNC('week', revenue_src_28000.created_at) AS revenue_instance__ds__week
, DATE_TRUNC('month', revenue_src_28000.created_at) AS revenue_instance__ds__month
, DATE_TRUNC('quarter', revenue_src_28000.created_at) AS revenue_instance__ds__quarter
, DATE_TRUNC('year', revenue_src_28000.created_at) AS revenue_instance__ds__year
, EXTRACT(year FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_year
, EXTRACT(quarter FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_quarter
, EXTRACT(month FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_month
, EXTRACT(day FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_day
, EXTRACT(isodow FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_dow
, EXTRACT(doy FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_doy
, revenue_src_28000.user_id AS user
, revenue_src_28000.user_id AS revenue_instance__user
FROM ***************************.fct_revenue revenue_src_28000
) subq_0
) subq_1
ON
(
subq_1.metric_time__day <= subq_2.metric_time__day
) AND (
subq_1.metric_time__day > subq_2.metric_time__day - INTERVAL 2 month
)
) subq_4
LEFT OUTER JOIN
***************************.mf_time_spine subq_5
ON
subq_4.metric_time__day = subq_5.ds
) subq_6
GROUP BY
subq_6.metric_time__martian_day
, subq_6.metric_time__day
) subq_7
) subq_8
) subq_9
GROUP BY
subq_9.metric_time__martian_day
, subq_9.trailing_2_months_revenue
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- Re-aggregate Metric via Group By
SELECT
metric_time__martian_day
, trailing_2_months_revenue
FROM (
-- Compute Metrics via Expressions
-- Window Function for Metric Re-aggregation
SELECT
metric_time__martian_day
, AVG(txn_revenue) OVER (PARTITION BY metric_time__martian_day) AS trailing_2_months_revenue
FROM (
-- Pass Only Elements: ['txn_revenue', 'metric_time__day', 'metric_time__day']
-- Join to Custom Granularity Dataset
-- Aggregate Measures
SELECT
subq_15.martian_day AS metric_time__martian_day
, subq_14.metric_time__day AS metric_time__day
, SUM(subq_14.txn_revenue) AS txn_revenue
FROM (
-- Join Self Over Time Range
SELECT
subq_13.ds AS metric_time__day
, 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
)
) subq_14
LEFT OUTER JOIN
***************************.mf_time_spine subq_15
ON
subq_14.metric_time__day = subq_15.ds
GROUP BY
subq_15.martian_day
, subq_14.metric_time__day
) subq_17
) subq_19
GROUP BY
metric_time__martian_day
, trailing_2_months_revenue
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ FROM (
subq_3.booking__ds__martian_day
, subq_3.booking_value
FROM (
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['booking_value', 'booking__ds__day']
-- Join to Custom Granularity Dataset
SELECT
subq_1.booking__ds__day AS booking__ds__day
, subq_1.booking_value AS booking_value
Expand Down Expand Up @@ -251,8 +251,8 @@ FROM (
subq_10.booking__ds__martian_day
, subq_10.bookers
FROM (
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['bookers', 'booking__ds__day']
-- Join to Custom Granularity Dataset
SELECT
subq_8.booking__ds__day AS booking__ds__day
, subq_8.bookers AS bookers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ SELECT
booking__ds__martian_day
, booking_value * 0.05 / bookers AS booking_fees_per_booker
FROM (
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__day']
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__martian_day']
-- Aggregate Measures
-- Compute Metrics via Expressions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ FROM (
, subq_3.listing__ds__month
, subq_3.listings
FROM (
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['listings', 'metric_time__day', 'listing__ds__month']
-- Join to Custom Granularity Dataset
SELECT
subq_1.listing__ds__month AS listing__ds__month
, subq_1.metric_time__day AS metric_time__day
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['listings', 'metric_time__day', 'listing__ds__month']
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['listings', 'metric_time__martian_day', 'listing__ds__month']
-- Aggregate Measures
-- Compute Metrics via Expressions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ FROM (
subq_3.metric_time__martian_day
, subq_3.bookings
FROM (
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['bookings', 'metric_time__day']
-- Join to Custom Granularity Dataset
SELECT
subq_1.metric_time__day AS metric_time__day
, subq_1.bookings AS bookings
Expand Down Expand Up @@ -246,8 +246,8 @@ FULL OUTER JOIN (
subq_10.metric_time__martian_day
, subq_10.listings
FROM (
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['listings', 'metric_time__day']
-- Join to Custom Granularity Dataset
SELECT
subq_8.metric_time__day AS metric_time__day
, subq_8.listings AS listings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ SELECT
, MAX(subq_20.bookings) AS bookings
, MAX(subq_27.listings) AS listings
FROM (
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['bookings', 'metric_time__day']
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['bookings', 'metric_time__martian_day']
-- Aggregate Measures
-- Compute Metrics via Expressions
Expand All @@ -28,8 +28,8 @@ FROM (
subq_16.martian_day
) subq_20
FULL OUTER JOIN (
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['listings', 'metric_time__day']
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['listings', 'metric_time__martian_day']
-- Aggregate Measures
-- Compute Metrics via Expressions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ SELECT
, subq_6.user__bio_added_ts__month
, subq_6.metric_time__day
FROM (
-- Join Standard Outputs
-- Join to Custom Granularity Dataset
-- Join to Custom Granularity Dataset
-- Join Standard Outputs
SELECT
subq_0.ds__day AS ds__day
, subq_0.ds__week AS ds__week
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- Join Standard Outputs
-- Join to Custom Granularity Dataset
-- Join to Custom Granularity Dataset
-- Join Standard Outputs
-- Pass Only Elements: ['metric_time__day', 'metric_time__martian_day', 'user__bio_added_ts__martian_day', 'user__bio_added_ts__month']
SELECT
subq_12.martian_day AS user__bio_added_ts__martian_day
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
SELECT
subq_2.metric_time__martian_day
FROM (
-- Join to Custom Granularity Dataset
-- Metric Time Dimension 'ds'
-- Join to Custom Granularity Dataset
SELECT
subq_0.ds__day AS ds__day
, subq_0.ds__week AS ds__week
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Join to Custom Granularity Dataset
-- Metric Time Dimension 'ds'
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['metric_time__martian_day',]
SELECT
subq_4.martian_day AS metric_time__martian_day
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
SELECT
subq_1.booking__ds__martian_day
FROM (
-- Join to Custom Granularity Dataset
-- Read Elements From Semantic Model 'bookings_source'
-- Join to Custom Granularity Dataset
SELECT
1 AS bookings
, CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Join to Custom Granularity Dataset
-- Read Elements From Semantic Model 'bookings_source'
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['booking__ds__martian_day',]
SELECT
subq_2.martian_day AS booking__ds__martian_day
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ FROM (
, subq_1.largest_listing
, subq_1.smallest_listing
FROM (
-- Join to Custom Granularity Dataset
-- Read Elements From Semantic Model 'listings_latest'
-- Join to Custom Granularity Dataset
SELECT
1 AS listings
, listings_latest_src_28000.capacity AS largest_listing
Expand Down
Loading

0 comments on commit 4e728b6

Please sign in to comment.