From cabdef7a6ab3955f9396535efb58eceab61625d8 Mon Sep 17 00:00:00 2001 From: Callum McCann Date: Fri, 20 May 2022 11:37:29 -0500 Subject: [PATCH 1/3] removing cartesian join --- macros/get_metric_sql.sql | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/macros/get_metric_sql.sql b/macros/get_metric_sql.sql index 1e2504a0..ccd0b260 100644 --- a/macros/get_metric_sql.sql +++ b/macros/get_metric_sql.sql @@ -71,7 +71,7 @@ with source_query as ( {%- endfor %} ), - spine__time as ( +spine__time as ( select /* this could be the same as date_day if grain is day. That's OK! They're used for different things: date_day for joining to the spine, period for aggregating.*/ @@ -84,32 +84,29 @@ with source_query as ( {% endfor %} date_day from {{ calendar_tbl }} - ), -{%- for dim in dimensions -%} - {%- if metrics.is_dim_from_model(metric, dim) %} - - spine__values__{{ dim }} as ( - - select distinct {{ dim }} - from source_query +), - ), - {% endif -%} +spine__values as ( + {#- /*On + it's unnecessary to pull through the whole table */ #} + select distinct + {%- for dim in dimensions -%} + {%- if metrics.is_dim_from_model(metric, dim) %} + {{ dim }} + {% if not loop.last %},{% endif %} + {% endif -%} + {%- endfor %} + from source_query -{%- endfor %} +), spine as ( select * from spine__time - {%- for dim in dimensions -%} - - {%- if metrics.is_dim_from_model(metric, dim) %} - cross join spine__values__{{ dim }} - {%- endif %} - {%- endfor %} + cross join spine__values ), From 2bdba9d9f6aca4c7fc0b96f9bef55e4f7220d84c Mon Sep 17 00:00:00 2001 From: Callum McCann Date: Fri, 20 May 2022 11:42:12 -0500 Subject: [PATCH 2/3] adding comments --- macros/get_metric_sql.sql | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/macros/get_metric_sql.sql b/macros/get_metric_sql.sql index ccd0b260..26315009 100644 --- a/macros/get_metric_sql.sql +++ b/macros/get_metric_sql.sql @@ -89,8 +89,9 @@ spine__time as ( spine__values as ( - {#- /*On - it's unnecessary to pull through the whole table */ #} + {#- /*This and the follwoing CTEs were changed on 5/20 in order to remove + the cartesian join behaviour that resulted in impossible combinations of + data. */ #} select distinct {%- for dim in dimensions -%} {%- if metrics.is_dim_from_model(metric, dim) %} From c9a3507e836d73b6878b1cb4e94059b98678c0b8 Mon Sep 17 00:00:00 2001 From: Callum McCann Date: Fri, 20 May 2022 15:53:02 -0500 Subject: [PATCH 3/3] fixing the off chance no dims selected --- macros/get_metric_sql.sql | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/macros/get_metric_sql.sql b/macros/get_metric_sql.sql index 26315009..71b99ccc 100644 --- a/macros/get_metric_sql.sql +++ b/macros/get_metric_sql.sql @@ -87,9 +87,10 @@ spine__time as ( ), +{% if dimensions is defined and dimensions|length > 0 %} spine__values as ( - {#- /*This and the follwoing CTEs were changed on 5/20 in order to remove + {#- /*This and the following CTEs were changed on 5/20 in order to remove the cartesian join behaviour that resulted in impossible combinations of data. */ #} select distinct @@ -102,12 +103,16 @@ spine__values as ( from source_query ), +{% endif -%} + spine as ( select * from spine__time + {% if dimensions is defined and dimensions|length > 0 %} cross join spine__values + {% endif -%} ),