Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Regression] Unit tests generate an sql with wrong CTE this referrence on incremental model with alias #10754

Closed
2 tasks done
katsugeneration opened this issue Sep 22, 2024 · 2 comments · Fixed by #10755
Closed
2 tasks done
Labels
bug Something isn't working incremental Incremental modeling with dbt regression unit tests Issues related to built-in dbt unit testing functionality

Comments

@katsugeneration
Copy link
Contributor

Is this a new bug in dbt-core?

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

Current Behavior

Unit tests fail due to wrong this cte referrence on incremental model with alias.

Expected Behavior

I expect that the name of this referrence will be consistent with the cte, regardless of alias.

Steps To Reproduce

  1. I have a incremental model with alias
{{ 
    config(
        materialized='incremental',
        alias='aliased_example',
    )
}}

with source_data as (

    select 1 as id
    union all
    select null as id

)

select *
from source_data
{% if is_incremental() %}
where id > (select max(id) from {{ this }} )
{% endif %}
  1. And a unit test on a table which overrides is_incremental macro is true
unit_tests:
  - name: case_incremental
    model: example
    overrides:
      macros:
        is_incremental: true
    given:
      - input: this
        rows:
          - {id: 0}
    expect:
      rows:
        - {id: 1}
  1. Then I am running the unit test dbt test --s example, I am getting an error:
15:04:35  Finished running 1 unit test in 0 hours 0 minutes and 2.35 seconds (2.35s).
15:04:35  
15:04:35  Completed with 1 error and 0 warnings:
15:04:35  
15:04:35    Runtime Error in unit_test case_incremental (models/example/schema.yml)
  An error occurred during execution of unit test 'case_incremental'. There may be an error in the unit test definition: check the data types.
   Database Error
    Table "__dbt__cte__example" must be qualified with a dataset (e.g. dataset.table).

because the generated SQL has different name of the CTE and the from referrence:

select * from (
        

with  __dbt__cte__aliased_example as (

-- Fixture for example
select safe_cast(0 as INT64) as id
), source_data as (

    select 1 as id
    union all
    select null as id

)

select *
from source_data

where id > (select max(id) from __dbt__cte__example )

    ) as __dbt_sbq
    where false and current_timestamp() = current_timestamp()
    limit 0

    )

Relevant log output

No response

Environment

- OS: Ubuntu 24.04
- Python: 3.12.3
- dbt: 1.8.6

Which database adapter are you using with dbt?

bigquery

Additional Context

No response

@dbeatty10
Copy link
Contributor

@katsugeneration Thanks for reporting this and opening #10755 to fix it 🏆 !

I was able to reproduce the issue you described with the following commands:

dbt run -s example --empty
dbt build --select example

@dbeatty10 dbeatty10 removed the triage label Sep 23, 2024
@dbeatty10
Copy link
Contributor

Labeling this as a regression since this worked with dbt-core==1.8.5 dbt-adapters==1.4.0 but stopped working with dbt-core==1.8.6 dbt-adapters==1.4.1.

@dbeatty10 dbeatty10 changed the title [Bug] Unit tests generate an sql with wrong CTE this referrence on incremental model with alias [Regression] Unit tests generate an sql with wrong CTE this referrence on incremental model with alias Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working incremental Incremental modeling with dbt regression unit tests Issues related to built-in dbt unit testing functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants