-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Helper macro to cast from array to string * Default implementations and tests for array macros * Trim Trailing Whitespace * Changelog entry * Remove dependence upon `cast_array_to_string` macro * pre-commit fixes * Remove `cast_array_to_string` macro * pre-commit fix * Trivial direct test; array_concat/append test non-triviallly indirectly * Remove vestigial `lstrip`
- Loading branch information
Showing
11 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
kind: Features | ||
body: Array macros | ||
time: 2022-09-12T22:22:27.475515-06:00 | ||
custom: | ||
Author: graciegoheen dbeatty10 | ||
Issue: "5520" | ||
PR: "5823" |
8 changes: 8 additions & 0 deletions
8
core/dbt/include/global_project/macros/utils/array_append.sql
This file contains 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,8 @@ | ||
{% macro array_append(array, new_element) -%} | ||
{{ return(adapter.dispatch('array_append', 'dbt')(array, new_element)) }} | ||
{%- endmacro %} | ||
|
||
{# new_element must be the same data type as elements in array to match postgres functionality #} | ||
{% macro default__array_append(array, new_element) -%} | ||
array_append({{ array }}, {{ new_element }}) | ||
{%- endmacro %} |
7 changes: 7 additions & 0 deletions
7
core/dbt/include/global_project/macros/utils/array_concat.sql
This file contains 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 @@ | ||
{% macro array_concat(array_1, array_2) -%} | ||
{{ return(adapter.dispatch('array_concat', 'dbt')(array_1, array_2)) }} | ||
{%- endmacro %} | ||
|
||
{% macro default__array_concat(array_1, array_2) -%} | ||
array_cat({{ array_1 }}, {{ array_2 }}) | ||
{%- endmacro %} |
12 changes: 12 additions & 0 deletions
12
core/dbt/include/global_project/macros/utils/array_construct.sql
This file contains 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,12 @@ | ||
{% macro array_construct(inputs=[], data_type=api.Column.translate_type('integer')) -%} | ||
{{ return(adapter.dispatch('array_construct', 'dbt')(inputs, data_type)) }} | ||
{%- endmacro %} | ||
|
||
{# all inputs must be the same data type to match postgres functionality #} | ||
{% macro default__array_construct(inputs, data_type) -%} | ||
{% if inputs|length > 0 %} | ||
array[ {{ inputs|join(' , ') }} ] | ||
{% else %} | ||
array[]::{{data_type}}[] | ||
{% endif %} | ||
{%- endmacro %} |
This file contains 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,22 @@ | ||
from dbt.tests.adapter.utils.base_utils import BaseUtils | ||
from dbt.tests.util import run_dbt, check_relations_equal, get_relation_columns | ||
|
||
|
||
class BaseArrayUtils(BaseUtils): | ||
def assert_columns_equal(self, project, expected_cols, actual_cols): | ||
assert ( | ||
expected_cols == actual_cols | ||
), f"Type difference detected: {expected_cols} vs. {actual_cols}" | ||
|
||
def test_expected_actual(self, project): | ||
run_dbt(["build"]) | ||
|
||
# check contents equal | ||
check_relations_equal(project.adapter, ["expected", "actual"]) | ||
|
||
# check types equal | ||
expected_cols = get_relation_columns(project.adapter, "expected") | ||
actual_cols = get_relation_columns(project.adapter, "actual") | ||
print(f"Expected: {expected_cols}") | ||
print(f"Actual: {actual_cols}") | ||
self.assert_columns_equal(project, expected_cols, actual_cols) |
12 changes: 12 additions & 0 deletions
12
tests/adapter/dbt/tests/adapter/utils/fixture_array_append.py
This file contains 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,12 @@ | ||
# array_append | ||
|
||
models__array_append_expected_sql = """ | ||
select 1 as id, {{ array_construct([1,2,3,4]) }} as array_col union all | ||
select 2 as id, {{ array_construct([4]) }} as array_col | ||
""" | ||
|
||
|
||
models__array_append_actual_sql = """ | ||
select 1 as id, {{ array_append(array_construct([1,2,3]), 4) }} as array_col union all | ||
select 2 as id, {{ array_append(array_construct([]), 4) }} as array_col | ||
""" |
14 changes: 14 additions & 0 deletions
14
tests/adapter/dbt/tests/adapter/utils/fixture_array_concat.py
This file contains 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,14 @@ | ||
# array_concat | ||
|
||
models__array_concat_expected_sql = """ | ||
select 1 as id, {{ array_construct([1,2,3,4,5,6]) }} as array_col union all | ||
select 2 as id, {{ array_construct([2]) }} as array_col union all | ||
select 3 as id, {{ array_construct([3]) }} as array_col | ||
""" | ||
|
||
|
||
models__array_concat_actual_sql = """ | ||
select 1 as id, {{ array_concat(array_construct([1,2,3]), array_construct([4,5,6])) }} as array_col union all | ||
select 2 as id, {{ array_concat(array_construct([]), array_construct([2])) }} as array_col union all | ||
select 3 as id, {{ array_concat(array_construct([3]), array_construct([])) }} as array_col | ||
""" |
12 changes: 12 additions & 0 deletions
12
tests/adapter/dbt/tests/adapter/utils/fixture_array_construct.py
This file contains 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,12 @@ | ||
# array_construct | ||
|
||
models__array_construct_expected_sql = """ | ||
select 1 as id, {{ array_construct([1,2,3]) }} as array_col union all | ||
select 2 as id, {{ array_construct([]) }} as array_col | ||
""" | ||
|
||
|
||
models__array_construct_actual_sql = """ | ||
select 1 as id, {{ array_construct([1,2,3]) }} as array_col union all | ||
select 2 as id, {{ array_construct([]) }} as array_col | ||
""" |
19 changes: 19 additions & 0 deletions
19
tests/adapter/dbt/tests/adapter/utils/test_array_append.py
This file contains 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,19 @@ | ||
import pytest | ||
from dbt.tests.adapter.utils.base_array_utils import BaseArrayUtils | ||
from dbt.tests.adapter.utils.fixture_array_append import ( | ||
models__array_append_actual_sql, | ||
models__array_append_expected_sql, | ||
) | ||
|
||
|
||
class BaseArrayAppend(BaseArrayUtils): | ||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return { | ||
"actual.sql": models__array_append_actual_sql, | ||
"expected.sql": models__array_append_expected_sql, | ||
} | ||
|
||
|
||
class TestArrayAppend(BaseArrayAppend): | ||
pass |
19 changes: 19 additions & 0 deletions
19
tests/adapter/dbt/tests/adapter/utils/test_array_concat.py
This file contains 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,19 @@ | ||
import pytest | ||
from dbt.tests.adapter.utils.base_array_utils import BaseArrayUtils | ||
from dbt.tests.adapter.utils.fixture_array_concat import ( | ||
models__array_concat_actual_sql, | ||
models__array_concat_expected_sql, | ||
) | ||
|
||
|
||
class BaseArrayConcat(BaseArrayUtils): | ||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return { | ||
"actual.sql": models__array_concat_actual_sql, | ||
"expected.sql": models__array_concat_expected_sql, | ||
} | ||
|
||
|
||
class TestArrayConcat(BaseArrayConcat): | ||
pass |
19 changes: 19 additions & 0 deletions
19
tests/adapter/dbt/tests/adapter/utils/test_array_construct.py
This file contains 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,19 @@ | ||
import pytest | ||
from dbt.tests.adapter.utils.base_array_utils import BaseArrayUtils | ||
from dbt.tests.adapter.utils.fixture_array_construct import ( | ||
models__array_construct_actual_sql, | ||
models__array_construct_expected_sql, | ||
) | ||
|
||
|
||
class BaseArrayConstruct(BaseArrayUtils): | ||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return { | ||
"actual.sql": models__array_construct_actual_sql, | ||
"expected.sql": models__array_construct_expected_sql, | ||
} | ||
|
||
|
||
class TestArrayConstruct(BaseArrayConstruct): | ||
pass |