forked from dbt-labs/dbt-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
width_bucket.sql
59 lines (53 loc) · 1.8 KB
/
width_bucket.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{% macro width_bucket(expr, min_value, max_value, num_buckets) %}
{{ return(adapter.dispatch('width_bucket', 'dbt_utils') (expr, min_value, max_value, num_buckets)) }}
{% endmacro %}
{% macro default__width_bucket(expr, min_value, max_value, num_buckets) -%}
{% set bin_size -%}
(( {{ max_value }} - {{ min_value }} ) / {{ num_buckets }} )
{%- endset %}
(
-- to break ties when the amount is eaxtly at the bucket egde
case
when
mod(
{{ dbt_utils.safe_cast(expr, dbt_utils.type_numeric() ) }},
{{ dbt_utils.safe_cast(bin_size, dbt_utils.type_numeric() ) }}
) = 0
then 1
else 0
end
) +
-- Anything over max_value goes the N+1 bucket
least(
ceil(
({{ expr }} - {{ min_value }})/{{ bin_size }}
),
{{ num_buckets }} + 1
)
{%- endmacro %}
{% macro redshift__width_bucket(expr, min_value, max_value, num_buckets) -%}
{% set bin_size -%}
(( {{ max_value }} - {{ min_value }} ) / {{ num_buckets }} )
{%- endset %}
(
-- to break ties when the amount is exactly at the bucket edge
case
when
{{ dbt_utils.safe_cast(expr, dbt_utils.type_numeric() ) }} %
{{ dbt_utils.safe_cast(bin_size, dbt_utils.type_numeric() ) }}
= 0
then 1
else 0
end
) +
-- Anything over max_value goes the N+1 bucket
least(
ceil(
({{ expr }} - {{ min_value }})/{{ bin_size }}
),
{{ num_buckets }} + 1
)
{%- endmacro %}
{% macro snowflake__width_bucket(expr, min_value, max_value, num_buckets) %}
width_bucket({{ expr }}, {{ min_value }}, {{ max_value }}, {{ num_buckets }} )
{% endmacro %}