dbt-labs / dbt-utils

Utility functions for dbt projects.
https://hub.getdbt.com/dbt-labs/dbt_utils/latest/
Apache License 2.0
1.38k stars 499 forks source link

`dbt_utils.not_null_proportion` does not work with clickhouse #952

Open camerondavison opened 2 months ago

camerondavison commented 2 months ago

Describe the bug

dbt_utils.not_null_proportion is returning 0 in clickhouse because it is not using floats for the division

Steps to reproduce

select
    sum(case when baz is null then 0 else 1 end) / cast(count(*) as numeric) as not_null_proportion
from `foo`.`bar`

which is basically

select 100/cast(200 as numeric)

returns 0 in clickhouse

Expected results

.5

Actual results

0

Which database are you using dbt with?

The output of dbt --version:

Core:
  - installed: 1.8.6
  - latest:    1.8.6 - Up to date!

Plugins:
  - clickhouse: 1.8.3

I think that the fix could just be to sum by 1.0 and 0.0 instead of 1 and 0 which should be the same across database, and just tells dbs to use float math correctly.

select 100.0/cast(200 as numeric)

works as expected