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

Add a sort parameter to star.sql #939

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

gfasano
Copy link

@gfasano gfasano commented Jul 24, 2024

Add a sort paramater that will sort in alphabetical order the return of the column name

Problem

My use case was to do UNION ALL on severall intermediary queries (not materialized data) with large amount of columns (arround 130) not ordered.

I tried to use the dbt_utils.union_relation but it work only on materialized data on I founded that overkilled to stock in database intermediary data just to do an UNION.

What I am currently doing to avoid that is as follow :

{% set column_list = dbt_utils.get_filtered_columns_in_relation( ref('original_data'))|sort %}

with partial_table_transformed as (
SELECT 
      *,
      [transformation to compute new_column_A and new_column_B, that changed the order of all the original column such as join or group by]
FROM {{ ref('original_data') }}
WHERE need_to_be_transformed = 1
),

partial_table_not_transformed as (
SELECT 
      *
FROM {{ ref('original_data') }}
WHERE need_to_be_transformed = 0
),

SELECT
    {{ column_list | join(', ') }}, 
    new_column_A, 
    new_column_B
FROM partial_table_transformed
UNION ALL
SELECT
    {{ column_list | join(', ') }},
    NULL as new_column_A, 
    NULL as new_column_B, 
FROM partial_table_not_transformed

Solution

It would be great to do as such :

SELECT
    {{ dbt_utils.star(ref('original_data'), sort = True) }}
    new_column_A, 
    new_column_B
FROM partial_table_transformed
UNION ALL
SELECT
    {{ dbt_utils.star(ref('original_data'), sort = True) }}
    NULL as new_column_A, 
    NULL as new_column_B, 
FROM partial_table_not_transformed

Checklist

  • This code is associated to this issue which has been triaged and accepted for development.
  • I have read the contributing guide and understand what's expected of me
  • I have run this code in development and it appears to resolve the stated issue
  • This PR includes tests, or tests are not required/relevant for this PR
  • I have updated the README.md (if applicable)

Add a sort paramater that will sort in alphabetical order the return of the column name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant