Skip to content

Commit dfacb59

Browse files
authored
Merge pull request #310 from swanderz/tsql_tweaks
t-sql hacks that shouldn't break mainline behavior
2 parents c64b520 + c84201f commit dfacb59

11 files changed

+34
-12
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Fix `equality` test when used with ephemeral models + explicit column set ([#321](https://github.com/fishtown-analytics/dbt-utils/pull/321))
99
- Fix `get_query_results_as_dict` integration test with consistent ordering ([#322](https://github.com/fishtown-analytics/dbt-utils/pull/322))
1010
- All macros are now properly dispatched, making it possible for non-core adapters to implement a shim package for dbt-utils ([#312](https://github.com/fishtown-analytics/dbt-utils/pull/312)) Thanks [@chaerinlee1](https://github.com/chaerinlee1) and [@swanderz](https://github.com/swanderz)
11+
- Small, non-breaking changes to accomdate TSQL (can't group by column number references, no real TRUE/FALSE values, aggregation CTEs need named columns) ([#310](https://github.com/fishtown-analytics/dbt-utils/pull/310)) Thanks [@swanderz](https://github.com/swanderz)
1112

1213
# dbt-utils v0.6.3
1314

integration_tests/dbt_project.yml

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ clean-targets: # directories to be removed by `dbt clean`
1919
- "target"
2020
- "dbt_modules"
2121

22+
vars:
23+
dbt_utils_dispatch_list: ['dbt_utils_integration_tests']
24+
2225
seeds:
2326

2427
+quote_columns: false
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% macro limit_zero() %}
2+
{{ return(adapter.dispatch('limit_zero', dbt_utils._get_utils_namespaces())()) }}
3+
{% endmacro %}
4+
5+
{% macro default__limit_zero() %}
6+
{{ return('limit 0') }}
7+
{% endmacro %}

integration_tests/tests/assert_get_query_results_as_dict_objects_equal.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,5 @@ Instead, we'll manually check that the values of these dictionaries are equivale
7777
{% endif %}
7878

7979
{{ log(ns.err_msg, info=True) }}
80-
select 1 {% if ns.pass %} limit 0 {% endif %}
80+
select 1 as col_name {% if ns.pass %} {{ limit_zero() }} {% endif %}
8181
{% endif %}

integration_tests/tests/logger/assert_pretty_output_msg_is_string.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{% if dbt_utils.pretty_log_format() is string %}
22
{# Return 0 rows for the test to pass #}
3-
select 1 limit 0
3+
select 1 as col_name {{ limit_zero() }}
44
{% else %}
55
{# Return >0 rows for the test to fail #}
66
select 1

integration_tests/tests/logger/assert_pretty_time_is_string.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{% if dbt_utils.pretty_time() is string %}
22
{# Return 0 rows for the test to pass #}
3-
select 1 limit 0
3+
select 1 as col_name {{ limit_zero() }}
44
{% else %}
55
{# Return >0 rows for the test to fail #}
66
select 1

macros/schema_tests/at_least_one.sql

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
select count(*)
1010
from (
1111
select
12-
13-
count({{ column_name }})
12+
{# In TSQL, subquery aggregate columns need aliases #}
13+
{# thus: a filler col name, 'filler_column' #}
14+
count({{ column_name }}) as filler_column
1415

1516
from {{ model }}
1617

macros/schema_tests/cardinality_equality.sql

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
{% macro test_cardinality_equality(model, to, field) %}
2+
23
{{ return(adapter.dispatch('test_cardinality_equality', packages = dbt_utils._get_utils_namespaces())(model, to, field, **kwargs)) }}
34

45
{% endmacro %}
56

67
{% macro default__test_cardinality_equality(model, to, field) %}
78

9+
{# T-SQL doesn't let you use numbers as aliases for columns #}
10+
{# Thus, no "GROUP BY 1" #}
11+
812
{% set column_name = kwargs.get('column_name', kwargs.get('from')) %}
913

1014

@@ -13,15 +17,15 @@ select
1317
{{ column_name }},
1418
count(*) as num_rows
1519
from {{ model }}
16-
group by 1
20+
group by {{ column_name }}
1721
),
1822

1923
table_b as (
2024
select
2125
{{ field }},
2226
count(*) as num_rows
2327
from {{ to }}
24-
group by 1
28+
group by {{ column_name }}
2529
),
2630

2731
except_a as (

macros/schema_tests/expression_is_true.sql

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
{% macro test_expression_is_true(model, condition='true') %}
1+
{% macro test_expression_is_true(model, condition='1=1') %}
2+
{# T-SQL has no boolean data type so we use 1=1 which returns TRUE #}
3+
{# ref https://stackoverflow.com/a/7170753/3842610 #}
24
{{ return(adapter.dispatch('test_expression_is_true', packages = dbt_utils._get_utils_namespaces())(model, condition, **kwargs)) }}
35
{% endmacro %}
46

5-
{% macro default__test_expression_is_true(model, condition='true') %}
7+
{% macro default__test_expression_is_true(model, condition) %}
68

79
{% set expression = kwargs.get('expression', kwargs.get('arg')) %}
810

macros/schema_tests/not_constant.sql

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ select count(*)
1212
from (
1313

1414
select
15-
count(distinct {{ column_name }})
15+
{# In TSQL, subquery aggregate columns need aliases #}
16+
{# thus: a filler col name, 'filler_column' #}
17+
count(distinct {{ column_name }}) as filler_column
1618

1719
from {{ model }}
1820

macros/schema_tests/relationships_where.sql

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
{% macro default__test_relationships_where(model, to, field) %}
66

77
{% set column_name = kwargs.get('column_name', kwargs.get('from')) %}
8-
{% set from_condition = kwargs.get('from_condition', "true") %}
9-
{% set to_condition = kwargs.get('to_condition', "true") %}
8+
{# T-SQL has no boolean data type so we use 1=1 which returns TRUE #}
9+
{# ref https://stackoverflow.com/a/7170753/3842610 #}
10+
{% set from_condition = kwargs.get('from_condition', "1=1") %}
11+
{% set to_condition = kwargs.get('to_condition', "1=1") %}
1012

1113
with left_table as (
1214

0 commit comments

Comments
 (0)