Skip to content

Commit

Permalink
Don't apply unnecessary DATE_TRUNC to base time spine column (#1547)
Browse files Browse the repository at this point in the history
This will enable more efficient queries. We require the described grain
to be accurate to the grain of the column, so this `DATE_TRUNC` is
unnecessary.
  • Loading branch information
courtneyholcomb authored Dec 9, 2024
1 parent 648cee4 commit cd330b8
Show file tree
Hide file tree
Showing 131 changed files with 221 additions and 217 deletions.
10 changes: 4 additions & 6 deletions metricflow/dataset/convert_semantic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,13 +528,11 @@ def build_time_spine_source_data_set(self, time_spine_source: TimeSpineSource) -
time_granularity=ExpandedTimeGranularity.from_time_granularity(base_granularity),
)
time_dimension_instances.append(base_time_dimension_instance)
base_dimension_select_expr = SemanticModelToDataSetConverter._make_element_sql_expr(
table_alias=from_source_alias, element_name=base_column_name
base_dimension_select_expr = SqlColumnReferenceExpression.from_table_and_column_names(
table_alias=from_source_alias, column_name=base_column_name
)
base_select_column = self._build_column_for_standard_time_granularity(
time_granularity=base_granularity,
expr=base_dimension_select_expr,
column_alias=base_time_dimension_instance.associated_column.column_name,
base_select_column = SqlSelectColumn(
expr=base_dimension_select_expr, column_alias=base_time_dimension_instance.associated_column.column_name
)
select_columns.append(base_select_column)
new_base_instances, new_base_columns = self._build_time_dimension_instances_and_columns(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ FROM (
FROM (
-- Read From Time Spine 'mf_time_spine'
SELECT
DATETIME_TRUNC(time_spine_src_28006.ds, day) AS ds__day
time_spine_src_28006.ds AS ds__day
, DATETIME_TRUNC(time_spine_src_28006.ds, isoweek) AS ds__week
, DATETIME_TRUNC(time_spine_src_28006.ds, month) AS ds__month
, DATETIME_TRUNC(time_spine_src_28006.ds, quarter) AS ds__quarter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ SELECT
subq_12.martian_day AS user__bio_added_ts__martian_day
, subq_11.martian_day AS metric_time__martian_day
, DATETIME_TRUNC(users_ds_source_src_28000.bio_added_ts, month) AS user__bio_added_ts__month
, DATETIME_TRUNC(time_spine_src_28006.ds, day) AS metric_time__day
, time_spine_src_28006.ds AS metric_time__day
FROM ***************************.dim_users users_ds_source_src_28000
CROSS JOIN
***************************.mf_time_spine time_spine_src_28006
LEFT OUTER JOIN
***************************.mf_time_spine subq_11
ON
DATETIME_TRUNC(time_spine_src_28006.ds, day) = subq_11.ds
time_spine_src_28006.ds = subq_11.ds
LEFT OUTER JOIN
***************************.mf_time_spine subq_12
ON
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ FROM (
FROM (
-- Read From Time Spine 'mf_time_spine'
SELECT
DATETIME_TRUNC(time_spine_src_28006.ds, day) AS ds__day
time_spine_src_28006.ds AS ds__day
, DATETIME_TRUNC(time_spine_src_28006.ds, isoweek) AS ds__week
, DATETIME_TRUNC(time_spine_src_28006.ds, month) AS ds__month
, DATETIME_TRUNC(time_spine_src_28006.ds, quarter) AS ds__quarter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ FROM ***************************.mf_time_spine time_spine_src_28006
LEFT OUTER JOIN
***************************.mf_time_spine subq_4
ON
DATETIME_TRUNC(time_spine_src_28006.ds, day) = subq_4.ds
time_spine_src_28006.ds = subq_4.ds
GROUP BY
metric_time__martian_day
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ FROM (
FROM (
-- Read From Time Spine 'mf_time_spine'
SELECT
DATE_TRUNC('day', time_spine_src_28006.ds) AS ds__day
time_spine_src_28006.ds AS ds__day
, DATE_TRUNC('week', time_spine_src_28006.ds) AS ds__week
, DATE_TRUNC('month', time_spine_src_28006.ds) AS ds__month
, DATE_TRUNC('quarter', time_spine_src_28006.ds) AS ds__quarter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ SELECT
subq_12.martian_day AS user__bio_added_ts__martian_day
, subq_11.martian_day AS metric_time__martian_day
, DATE_TRUNC('month', users_ds_source_src_28000.bio_added_ts) AS user__bio_added_ts__month
, DATE_TRUNC('day', time_spine_src_28006.ds) AS metric_time__day
, time_spine_src_28006.ds AS metric_time__day
FROM ***************************.dim_users users_ds_source_src_28000
CROSS JOIN
***************************.mf_time_spine time_spine_src_28006
LEFT OUTER JOIN
***************************.mf_time_spine subq_11
ON
DATE_TRUNC('day', time_spine_src_28006.ds) = subq_11.ds
time_spine_src_28006.ds = subq_11.ds
LEFT OUTER JOIN
***************************.mf_time_spine subq_12
ON
Expand All @@ -26,4 +26,4 @@ GROUP BY
subq_12.martian_day
, subq_11.martian_day
, DATE_TRUNC('month', users_ds_source_src_28000.bio_added_ts)
, DATE_TRUNC('day', time_spine_src_28006.ds)
, time_spine_src_28006.ds
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ FROM (
FROM (
-- Read From Time Spine 'mf_time_spine'
SELECT
DATE_TRUNC('day', time_spine_src_28006.ds) AS ds__day
time_spine_src_28006.ds AS ds__day
, DATE_TRUNC('week', time_spine_src_28006.ds) AS ds__week
, DATE_TRUNC('month', time_spine_src_28006.ds) AS ds__month
, DATE_TRUNC('quarter', time_spine_src_28006.ds) AS ds__quarter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ FROM ***************************.mf_time_spine time_spine_src_28006
LEFT OUTER JOIN
***************************.mf_time_spine subq_4
ON
DATE_TRUNC('day', time_spine_src_28006.ds) = subq_4.ds
time_spine_src_28006.ds = subq_4.ds
GROUP BY
subq_4.martian_day
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ FROM (
FROM (
-- Read From Time Spine 'mf_time_spine'
SELECT
DATE_TRUNC('day', time_spine_src_28006.ds) AS ds__day
time_spine_src_28006.ds AS ds__day
, DATE_TRUNC('week', time_spine_src_28006.ds) AS ds__week
, DATE_TRUNC('month', time_spine_src_28006.ds) AS ds__month
, DATE_TRUNC('quarter', time_spine_src_28006.ds) AS ds__quarter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ SELECT
subq_12.martian_day AS user__bio_added_ts__martian_day
, subq_11.martian_day AS metric_time__martian_day
, DATE_TRUNC('month', users_ds_source_src_28000.bio_added_ts) AS user__bio_added_ts__month
, DATE_TRUNC('day', time_spine_src_28006.ds) AS metric_time__day
, time_spine_src_28006.ds AS metric_time__day
FROM ***************************.dim_users users_ds_source_src_28000
CROSS JOIN
***************************.mf_time_spine time_spine_src_28006
LEFT OUTER JOIN
***************************.mf_time_spine subq_11
ON
DATE_TRUNC('day', time_spine_src_28006.ds) = subq_11.ds
time_spine_src_28006.ds = subq_11.ds
LEFT OUTER JOIN
***************************.mf_time_spine subq_12
ON
Expand All @@ -26,4 +26,4 @@ GROUP BY
subq_12.martian_day
, subq_11.martian_day
, DATE_TRUNC('month', users_ds_source_src_28000.bio_added_ts)
, DATE_TRUNC('day', time_spine_src_28006.ds)
, time_spine_src_28006.ds
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ FROM (
FROM (
-- Read From Time Spine 'mf_time_spine'
SELECT
DATE_TRUNC('day', time_spine_src_28006.ds) AS ds__day
time_spine_src_28006.ds AS ds__day
, DATE_TRUNC('week', time_spine_src_28006.ds) AS ds__week
, DATE_TRUNC('month', time_spine_src_28006.ds) AS ds__month
, DATE_TRUNC('quarter', time_spine_src_28006.ds) AS ds__quarter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ FROM ***************************.mf_time_spine time_spine_src_28006
LEFT OUTER JOIN
***************************.mf_time_spine subq_4
ON
DATE_TRUNC('day', time_spine_src_28006.ds) = subq_4.ds
time_spine_src_28006.ds = subq_4.ds
GROUP BY
subq_4.martian_day
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ FROM (
FROM (
-- Read From Time Spine 'mf_time_spine'
SELECT
DATE_TRUNC('day', time_spine_src_28006.ds) AS ds__day
time_spine_src_28006.ds AS ds__day
, DATE_TRUNC('week', time_spine_src_28006.ds) AS ds__week
, DATE_TRUNC('month', time_spine_src_28006.ds) AS ds__month
, DATE_TRUNC('quarter', time_spine_src_28006.ds) AS ds__quarter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ SELECT
subq_12.martian_day AS user__bio_added_ts__martian_day
, subq_11.martian_day AS metric_time__martian_day
, DATE_TRUNC('month', users_ds_source_src_28000.bio_added_ts) AS user__bio_added_ts__month
, DATE_TRUNC('day', time_spine_src_28006.ds) AS metric_time__day
, time_spine_src_28006.ds AS metric_time__day
FROM ***************************.dim_users users_ds_source_src_28000
CROSS JOIN
***************************.mf_time_spine time_spine_src_28006
LEFT OUTER JOIN
***************************.mf_time_spine subq_11
ON
DATE_TRUNC('day', time_spine_src_28006.ds) = subq_11.ds
time_spine_src_28006.ds = subq_11.ds
LEFT OUTER JOIN
***************************.mf_time_spine subq_12
ON
Expand All @@ -26,4 +26,4 @@ GROUP BY
subq_12.martian_day
, subq_11.martian_day
, DATE_TRUNC('month', users_ds_source_src_28000.bio_added_ts)
, DATE_TRUNC('day', time_spine_src_28006.ds)
, time_spine_src_28006.ds
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ FROM (
FROM (
-- Read From Time Spine 'mf_time_spine'
SELECT
DATE_TRUNC('day', time_spine_src_28006.ds) AS ds__day
time_spine_src_28006.ds AS ds__day
, DATE_TRUNC('week', time_spine_src_28006.ds) AS ds__week
, DATE_TRUNC('month', time_spine_src_28006.ds) AS ds__month
, DATE_TRUNC('quarter', time_spine_src_28006.ds) AS ds__quarter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ FROM ***************************.mf_time_spine time_spine_src_28006
LEFT OUTER JOIN
***************************.mf_time_spine subq_4
ON
DATE_TRUNC('day', time_spine_src_28006.ds) = subq_4.ds
time_spine_src_28006.ds = subq_4.ds
GROUP BY
subq_4.martian_day
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ FROM (
FROM (
-- Read From Time Spine 'mf_time_spine'
SELECT
DATE_TRUNC('day', time_spine_src_28006.ds) AS ds__day
time_spine_src_28006.ds AS ds__day
, DATE_TRUNC('week', time_spine_src_28006.ds) AS ds__week
, DATE_TRUNC('month', time_spine_src_28006.ds) AS ds__month
, DATE_TRUNC('quarter', time_spine_src_28006.ds) AS ds__quarter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ SELECT
subq_12.martian_day AS user__bio_added_ts__martian_day
, subq_11.martian_day AS metric_time__martian_day
, DATE_TRUNC('month', users_ds_source_src_28000.bio_added_ts) AS user__bio_added_ts__month
, DATE_TRUNC('day', time_spine_src_28006.ds) AS metric_time__day
, time_spine_src_28006.ds AS metric_time__day
FROM ***************************.dim_users users_ds_source_src_28000
CROSS JOIN
***************************.mf_time_spine time_spine_src_28006
LEFT OUTER JOIN
***************************.mf_time_spine subq_11
ON
DATE_TRUNC('day', time_spine_src_28006.ds) = subq_11.ds
time_spine_src_28006.ds = subq_11.ds
LEFT OUTER JOIN
***************************.mf_time_spine subq_12
ON
Expand All @@ -26,4 +26,4 @@ GROUP BY
subq_12.martian_day
, subq_11.martian_day
, DATE_TRUNC('month', users_ds_source_src_28000.bio_added_ts)
, DATE_TRUNC('day', time_spine_src_28006.ds)
, time_spine_src_28006.ds
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ FROM (
FROM (
-- Read From Time Spine 'mf_time_spine'
SELECT
DATE_TRUNC('day', time_spine_src_28006.ds) AS ds__day
time_spine_src_28006.ds AS ds__day
, DATE_TRUNC('week', time_spine_src_28006.ds) AS ds__week
, DATE_TRUNC('month', time_spine_src_28006.ds) AS ds__month
, DATE_TRUNC('quarter', time_spine_src_28006.ds) AS ds__quarter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ FROM ***************************.mf_time_spine time_spine_src_28006
LEFT OUTER JOIN
***************************.mf_time_spine subq_4
ON
DATE_TRUNC('day', time_spine_src_28006.ds) = subq_4.ds
time_spine_src_28006.ds = subq_4.ds
GROUP BY
subq_4.martian_day
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ FROM (
FROM (
-- Read From Time Spine 'mf_time_spine'
SELECT
DATE_TRUNC('day', time_spine_src_28006.ds) AS ds__day
time_spine_src_28006.ds AS ds__day
, DATE_TRUNC('week', time_spine_src_28006.ds) AS ds__week
, DATE_TRUNC('month', time_spine_src_28006.ds) AS ds__month
, DATE_TRUNC('quarter', time_spine_src_28006.ds) AS ds__quarter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ SELECT
subq_12.martian_day AS user__bio_added_ts__martian_day
, subq_11.martian_day AS metric_time__martian_day
, DATE_TRUNC('month', users_ds_source_src_28000.bio_added_ts) AS user__bio_added_ts__month
, DATE_TRUNC('day', time_spine_src_28006.ds) AS metric_time__day
, time_spine_src_28006.ds AS metric_time__day
FROM ***************************.dim_users users_ds_source_src_28000
CROSS JOIN
***************************.mf_time_spine time_spine_src_28006
LEFT OUTER JOIN
***************************.mf_time_spine subq_11
ON
DATE_TRUNC('day', time_spine_src_28006.ds) = subq_11.ds
time_spine_src_28006.ds = subq_11.ds
LEFT OUTER JOIN
***************************.mf_time_spine subq_12
ON
Expand All @@ -26,4 +26,4 @@ GROUP BY
subq_12.martian_day
, subq_11.martian_day
, DATE_TRUNC('month', users_ds_source_src_28000.bio_added_ts)
, DATE_TRUNC('day', time_spine_src_28006.ds)
, time_spine_src_28006.ds
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ FROM (
FROM (
-- Read From Time Spine 'mf_time_spine'
SELECT
DATE_TRUNC('day', time_spine_src_28006.ds) AS ds__day
time_spine_src_28006.ds AS ds__day
, DATE_TRUNC('week', time_spine_src_28006.ds) AS ds__week
, DATE_TRUNC('month', time_spine_src_28006.ds) AS ds__month
, DATE_TRUNC('quarter', time_spine_src_28006.ds) AS ds__quarter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ FROM ***************************.mf_time_spine time_spine_src_28006
LEFT OUTER JOIN
***************************.mf_time_spine subq_4
ON
DATE_TRUNC('day', time_spine_src_28006.ds) = subq_4.ds
time_spine_src_28006.ds = subq_4.ds
GROUP BY
subq_4.martian_day
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ FROM (
FROM (
-- Read From Time Spine 'mf_time_spine'
SELECT
DATE_TRUNC('day', time_spine_src_28006.ds) AS ds__day
time_spine_src_28006.ds AS ds__day
, DATE_TRUNC('week', time_spine_src_28006.ds) AS ds__week
, DATE_TRUNC('month', time_spine_src_28006.ds) AS ds__month
, DATE_TRUNC('quarter', time_spine_src_28006.ds) AS ds__quarter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ SELECT
subq_12.martian_day AS user__bio_added_ts__martian_day
, subq_11.martian_day AS metric_time__martian_day
, DATE_TRUNC('month', users_ds_source_src_28000.bio_added_ts) AS user__bio_added_ts__month
, DATE_TRUNC('day', time_spine_src_28006.ds) AS metric_time__day
, time_spine_src_28006.ds AS metric_time__day
FROM ***************************.dim_users users_ds_source_src_28000
CROSS JOIN
***************************.mf_time_spine time_spine_src_28006
LEFT OUTER JOIN
***************************.mf_time_spine subq_11
ON
DATE_TRUNC('day', time_spine_src_28006.ds) = subq_11.ds
time_spine_src_28006.ds = subq_11.ds
LEFT OUTER JOIN
***************************.mf_time_spine subq_12
ON
Expand All @@ -26,4 +26,4 @@ GROUP BY
subq_12.martian_day
, subq_11.martian_day
, DATE_TRUNC('month', users_ds_source_src_28000.bio_added_ts)
, DATE_TRUNC('day', time_spine_src_28006.ds)
, time_spine_src_28006.ds
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ FROM (
FROM (
-- Read From Time Spine 'mf_time_spine'
SELECT
DATE_TRUNC('day', time_spine_src_28006.ds) AS ds__day
time_spine_src_28006.ds AS ds__day
, DATE_TRUNC('week', time_spine_src_28006.ds) AS ds__week
, DATE_TRUNC('month', time_spine_src_28006.ds) AS ds__month
, DATE_TRUNC('quarter', time_spine_src_28006.ds) AS ds__quarter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ FROM ***************************.mf_time_spine time_spine_src_28006
LEFT OUTER JOIN
***************************.mf_time_spine subq_4
ON
DATE_TRUNC('day', time_spine_src_28006.ds) = subq_4.ds
time_spine_src_28006.ds = subq_4.ds
GROUP BY
subq_4.martian_day
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ FROM (
FROM (
-- Read From Time Spine 'mf_time_spine_millisecond'
SELECT
DATETIME_TRUNC(time_spine_src_28002.ts, millisecond) AS ts__millisecond
time_spine_src_28002.ts AS ts__millisecond
, DATETIME_TRUNC(time_spine_src_28002.ts, second) AS ts__second
, DATETIME_TRUNC(time_spine_src_28002.ts, minute) AS ts__minute
, DATETIME_TRUNC(time_spine_src_28002.ts, hour) AS ts__hour
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sql_engine: BigQuery
-- Metric Time Dimension 'ts'
-- Pass Only Elements: ['metric_time__millisecond',]
SELECT
DATETIME_TRUNC(ts, millisecond) AS metric_time__millisecond
ts AS metric_time__millisecond
FROM ***************************.mf_time_spine_millisecond time_spine_src_28002
GROUP BY
metric_time__millisecond
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ FROM (
FROM (
-- Read From Time Spine 'mf_time_spine_second'
SELECT
DATETIME_TRUNC(time_spine_src_28003.ts, second) AS ts__second
time_spine_src_28003.ts AS ts__second
, DATETIME_TRUNC(time_spine_src_28003.ts, minute) AS ts__minute
, DATETIME_TRUNC(time_spine_src_28003.ts, hour) AS ts__hour
, DATETIME_TRUNC(time_spine_src_28003.ts, day) AS ts__day
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ sql_engine: BigQuery
-- Constrain Time Range to [2020-01-01T00:00:02, 2020-01-01T00:00:08]
-- Pass Only Elements: ['metric_time__second',]
SELECT
DATETIME_TRUNC(ts, second) AS metric_time__second
ts AS metric_time__second
FROM ***************************.mf_time_spine_second time_spine_src_28003
WHERE DATETIME_TRUNC(ts, second) BETWEEN '2020-01-01 00:00:02' AND '2020-01-01 00:00:08'
WHERE ts BETWEEN '2020-01-01 00:00:02' AND '2020-01-01 00:00:08'
GROUP BY
metric_time__second
Loading

0 comments on commit cd330b8

Please sign in to comment.