elementary-data / elementary

The dbt-native data observability solution for data & analytics engineers. Monitor your data pipelines in minutes. Available as self-hosted or cloud service with premium features.
https://www.elementary-data.com/
Apache License 2.0
1.94k stars 165 forks source link

Data test broken when switching to materialized views on postgres #1684

Closed mateusz closed 2 months ago

mateusz commented 3 months ago

Describe the bug

On postgres, when using elementary.column_anomalies test on a column that is part of a table using materialized views, an "Unable to find column" error occurs during the elemenatary data test run.

To Reproduce

Create a table using materialized view materialization:

{{ config(materialized='materialized_view') }}

SELECT bill_usd FROM table

Configure test in DBT table spec:

models:
  - name: table
    columns:
      - name: bill_usd
        tests:
          - elementary.column_anomalies:
              detection_period:
                period: day
                count: 2
              training_period:
                period: day
                count: 35
              detection_delay:
                period: day
                count: 5
              column_anomalies:
                - sum
              config:
                severity: warn

Run elementary:

2024-08-27 12:05:46,796 | INFO | root | 00:05:42    Compilation Error in test ...bill_usd__day__5__day__2__day__35 (models/....yml)
2024-08-27 12:05:46,796 | INFO | root |   Unable to find column `bill_usd` in `....TABLE`
2024-08-27 12:05:46,796 | INFO | root |   
2024-08-27 12:05:46,796 | INFO | root |   > in macro test_column_anomalies (macros/edr/tests/test_column_anomalies.sql)
2024-08-27 12:05:46,797 | INFO | root |   > called by test ...bill_usd__day__5__day__2__day__35 (...)

This problem goes away when I remove the materialization, or switch to table materialization.

Expected behavior

I expect data tests to continue working when I switch to materialized_view materialization.

Screenshots

n/a

Environment (please complete the following information):

ofek1weiss commented 2 months ago

Hey @mateusz This issue seems to be caused by the postgres dbt adapter itself... 😞 Elementary uses the macro adapter.get_columns_in_relation, to find metadata about the monitored column, and the adapter itself returns an empty list for materialized views. I recommend opening an issue in the dbt-postgres repo for this issue.

mateusz commented 2 months ago

Thanks for looking, will do.