-
Notifications
You must be signed in to change notification settings - Fork 2
Union relation macro (DNA-43127) #105
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ba9a333
feat: union relation for tables in database
MozhganUiPath f8660aa
feat: union relation macro and tests
MozhganUiPath d190acd
feat: sample input data added and tests updated
MozhganUiPath 7ce597c
feat: update integration test
MozhganUiPath e3e35c5
feat: update sources
MozhganUiPath c42352f
feat: update integration test
MozhganUiPath e4eb51f
feat: apply snowflake test first
MozhganUiPath 64da6c4
feat: updating integration tests and input_tables
MozhganUiPath f5fc3ee
feat: update dbt version
MozhganUiPath 69fdc9f
feat: update test scenario
MozhganUiPath fdf78a9
feat: update document and union macro
MozhganUiPath c156943
feat: update input table
MozhganUiPath 334e800
feat: update documents
MozhganUiPath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| name: 'pm_utils' | ||
| version: '2.3.2' | ||
| version: '2.4.0' | ||
| config-version: 2 | ||
|
|
||
| require-dbt-version: [">=1.0.0", "<2.0.0"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| {# This creates a table that can be used in test_union. #} | ||
| select | ||
| 1 as "RN", | ||
| 'A' as "Column_A", | ||
| {{ pm_utils.to_boolean('true') }} as "Column_B", | ||
| null as "Column_C" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| {# This creates a table that can be used in test_union. #} | ||
| select | ||
| 5 as "Column_C", | ||
| 3.5 as "Column_D", | ||
| null as "Column_E", | ||
| 2 as "RN", | ||
| 'B' as "Column_A" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| {# This creates a table that can be used in test_union. #} | ||
| select | ||
| 3 as "RN", | ||
| {{ pm_utils.to_boolean('false') }} as "Column_B", | ||
| 7 as "Column_C", | ||
| 2.3 as "Column_E", | ||
| null as "Column_F" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| with Union_Three as ( | ||
| {{ pm_utils.union([ref('input_table_1'), ref('input_table_2'), ref('input_table_3')]) }} | ||
| ), | ||
|
|
||
| Union_Three_Expected as ( | ||
| select | ||
| 1 as "RN", 'A' as "Column_A_Expected", {{pm_utils.to_boolean('true')}} as "Column_B_Expected", null as "Column_C_Expected", null as "Column_D_Expected", null as "Column_E_Expected", null as "Column_F_Expected" | ||
| union all | ||
| select 2, 'B', null, 5, 3.5, null, null | ||
| union all | ||
| select 3, null, {{pm_utils.to_boolean('false')}}, 7, null, {{ pm_utils.to_boolean('false') }}, null | ||
| ) | ||
|
|
||
| select | ||
| Union_Three."Column_A", | ||
| Union_Three."Column_B", | ||
| Union_Three."Column_C", | ||
| Union_Three."Column_D", | ||
| Union_Three."Column_E", | ||
| Union_Three."Column_F", | ||
| Union_Three_Expected."Column_A_Expected", | ||
| Union_Three_Expected."Column_B_Expected", | ||
| Union_Three_Expected."Column_C_Expected", | ||
| Union_Three_Expected."Column_D_Expected", | ||
| Union_Three_Expected."Column_E_Expected", | ||
| Union_Three_Expected."Column_F_Expected" | ||
| from Union_Three | ||
| left join Union_Three_Expected | ||
| on Union_Three."RN" = Union_Three_Expected."RN" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| {%- macro union(relations) -%} | ||
| {# | ||
| relations: a list of relations (model/ source tables) that exist in the database. | ||
| This macro unions any number of tables, aligning columns by name and filling missing columns with NULLs. | ||
| #} | ||
| {%- set joint_columns = [] -%} | ||
| {%- for relation in relations -%} | ||
| {%- set cols = adapter.get_columns_in_relation(relation) -%} | ||
| {%- for col in cols -%} | ||
| {%- if col.name not in joint_columns -%} | ||
| {%- do joint_columns.append(col.name) -%} | ||
| {%- endif -%} | ||
| {%- endfor -%} | ||
| {%- endfor -%} | ||
|
|
||
| {%- set selects = [] -%} | ||
| {%- for relation in relations -%} | ||
| {%- set cols = adapter.get_columns_in_relation(relation) -%} | ||
| {%- set col_names = cols | map(attribute='name') | list -%} | ||
| {%- set select_parts = [] -%} | ||
| {%- for col in joint_columns -%} | ||
| {%- if col in col_names -%} | ||
| {%- do select_parts.append('"' ~ col ~ '"') -%} | ||
| {%- else -%} | ||
| {%- do select_parts.append('NULL as "' ~ col ~ '"') -%} | ||
| {%- endif -%} | ||
| {%- endfor -%} | ||
| {%- set select_sql = 'select ' ~ select_parts | join(', ') ~ ' from ' ~ relation -%} | ||
| {%- do selects.append(select_sql) -%} | ||
| {%- endfor -%} | ||
|
|
||
| {{ selects | join('\nunion all\n') }} | ||
| {%- endmacro -%} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.