Skip to content

[BUG] Division by Zero in expect_column_proportion_of_unique_values_to_be_between with empty tables #19

@rloredo

Description

@rloredo

Is this a new bug in dbt-expectations?

  • I believe this is a new bug in dbt-expectations
  • I have searched the existing issues, and I could not find an existing issue for this bug

Current Behavior

The test expect_column_proportion_of_unique_values_to_be_between fails with empty tables (mostly used in CI runs) with division by zero.

Expected Behavior

Should return 1 (all values are unique because there are no values)

Steps To Reproduce

run the test in a table with zero rows.

Which database adapter are you using with dbt?

BigQuery

Additional Context

I think a simple fix can be implemented here with no breaking changes, using ANSI SQL:

case 
   when count({{ column_name }}) = 0 then 1  -- Return 1 when division by zero
   else cast(count(distinct {{ column_name }}) as {{ dbt.type_float() }})/count({{ column_name }})
end

I did in this PR #20

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions