Skip to content

Commit 46fcdf6

Browse files
joellabesjlcotoepapineauElize Papineaujoestemarie
authored
Add any value (#501)
* Add link for fewer_rows_than schema test in docs (#465) * Update get_query_results_as_dict example to demonstrate accessing columnar results as dictionary values (#474) * Update get_qu ery_results_as_dict example to demonstrate accessing columnar results as dictionary values * Use slugify in example * Fix slugify example with dbt_utils. package prefix Co-authored-by: Elize Papineau <elize.papineau@dbtlabs.com> * Add note about not_null_where deprecation to Readme (#477) * Add note about not_null_where deprecation to Readme * Add docs to unique_where test * Update pull_request_template.md to reference `main` vs `master` (#496) * Correct coalesce -> concatenation typo (#495) * add any_value cross-db macro * Missing colon in test * Update CHANGELOG.md Co-authored-by: José Coto <jlcoto@users.noreply.github.com> Co-authored-by: Elize Papineau <elizepapineau@gmail.com> Co-authored-by: Elize Papineau <elize.papineau@dbtlabs.com> Co-authored-by: Joe Ste.Marie <stemarie.joe@gmail.com> Co-authored-by: Niall Woodward <niall@niallrees.com>
1 parent 14127aa commit 46fcdf6

File tree

6 files changed

+50
-1
lines changed

6 files changed

+50
-1
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# dbt-utils v0.8.1
22

3+
## New features
4+
- A cross-database implementation of `any_value()` ([#497](https://github.com/dbt-labs/dbt-utils/issues/497), [#501](https://github.com/dbt-labs/dbt-utils/pull/501))
5+
36
## Under the hood
47
- also ignore `dbt_packages/` directory [#463](https://github.com/dbt-labs/dbt-utils/pull/463)
58

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ in isolation.
461461
We generally recommend testing this uniqueness condition by either:
462462
* generating a [surrogate_key](#surrogate_key-source) for your model and testing
463463
the uniqueness of said key, OR
464-
* passing the `unique` test a coalesce of the columns (as discussed [here](https://docs.getdbt.com/docs/building-a-dbt-project/testing-and-documentation/testing/#testing-expressions)).
464+
* passing the `unique` test a concatenation of the columns (as discussed [here](https://docs.getdbt.com/docs/building-a-dbt-project/testing-and-documentation/testing/#testing-expressions)).
465465

466466
However, these approaches can become non-perfomant on large data sets, in which
467467
case we recommend using this test instead.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
key_name,static_col,num_rows
2+
abc,dbt,2
3+
jkl,dbt,3
4+
xyz,test,1

integration_tests/models/cross_db_utils/schema.yml

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
version: 2
22

33
models:
4+
- name: test_any_value
5+
tests:
6+
- dbt_utils.equality:
7+
compare_model: ref('data_any_value_expected')
8+
9+
410
- name: test_concat
511
tests:
612
- assert_equal:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
with some_model as (
2+
select 1 as id, 'abc' as key_name, 'dbt' as static_col union all
3+
select 2 as id, 'abc' as key_name, 'dbt' as static_col union all
4+
select 3 as id, 'jkl' as key_name, 'dbt' as static_col union all
5+
select 4 as id, 'jkl' as key_name, 'dbt' as static_col union all
6+
select 5 as id, 'jkl' as key_name, 'dbt' as static_col union all
7+
select 6 as id, 'xyz' as key_name, 'test' as static_col
8+
),
9+
10+
final as (
11+
select
12+
key_name,
13+
{{ dbt_utils.any_value('static_col') }} as static_col,
14+
count(id) as num_rows
15+
from some_model
16+
group by key_name
17+
)
18+
19+
select * from final

macros/cross_db_utils/any_value.sql

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{% macro any_value(expression) -%}
2+
{{ return(adapter.dispatch('any_value', 'dbt_utils') (expression)) }}
3+
{% endmacro %}
4+
5+
6+
{% macro default__any_value(expression) -%}
7+
8+
any_value({{ expression }})
9+
10+
{%- endmacro %}
11+
12+
13+
{% macro postgres__any_value(expression) -%}
14+
{#- /*Postgres doesn't support any_value, so we're using min() to get the same result*/ -#}
15+
min({{ expression }})
16+
17+
{%- endmacro %}

0 commit comments

Comments
 (0)