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

Removing Cartesian Join #31

Merged
merged 3 commits into from
Jun 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions macros/get_metric_sql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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.*/
Expand All @@ -84,32 +84,35 @@ 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 -%}
{% if dimensions is defined and dimensions|length > 0 %}
spine__values as (

{#- /*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
{%- for dim in dimensions -%}
{%- if metrics.is_dim_from_model(metric, dim) %}
{{ dim }}
{% if not loop.last %},{% endif %}
{% endif -%}
{%- endfor %}
from source_query

),
{% endif -%}

{%- 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 %}
{% if dimensions is defined and dimensions|length > 0 %}
cross join spine__values
{% endif -%}

),

Expand Down