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

CTE no longer interpolated into target compiled query for ephemeral model #3056

Closed
1 of 5 tasks
cmcnicoll opened this issue Feb 6, 2021 · 2 comments · Fixed by #3139
Closed
1 of 5 tasks

CTE no longer interpolated into target compiled query for ephemeral model #3056

cmcnicoll opened this issue Feb 6, 2021 · 2 comments · Fixed by #3139
Assignees
Labels
bug Something isn't working regression

Comments

@cmcnicoll
Copy link

Describe the bug

Recently upgraded to 0.19. For some ephemeral models, I am no longer able to run the compiled query as-is. Sample error: Invalid operation: relation "__dbt__cte__xyz" does not exist;.

Steps To Reproduce

Issue occurs when an ephemeral model references another ephemeral model. The compiled query contains select * from __dbt__CTE__xyz, but the CTE query is missing.

Expected behavior

Be able to run all compiled queries as-is.

Screenshots and log output

N/A

System information

Which database are you using dbt with?

  • postgres
  • redshift
  • bigquery
  • snowflake
  • other (specify: ____________)

The output of dbt --version:

$ dbt --version
installed version: 0.19.0
   latest version: 0.19.0

Up to date!

Plugins:
  - bigquery: 0.19.0
  - postgres: 0.19.0
  - redshift: 0.19.0
  - snowflake: 0.19.0

The operating system you're using: Windows 10 Pro

The output of python --version: Python 3.8.7

Additional context

N/A

@cmcnicoll cmcnicoll added bug Something isn't working triage labels Feb 6, 2021
@jtcohen6 jtcohen6 added regression and removed triage labels Feb 8, 2021
@jtcohen6 jtcohen6 added this to the 0.19.1 (performance) milestone Feb 8, 2021
@jtcohen6
Copy link
Contributor

jtcohen6 commented Feb 8, 2021

Good catch @cmcnicoll, and thanks for the bug report. This is a regression in v0.19.0 associated with our rework of compiled_sql and removal of injected_sql (#2834). Handling for ephemeral models was the trickiest piece of that, and the ephemeral-of-ephemeral compiled_sql is still not quite right. We'll work on a fix for v0.19.1.

models/my_ephemeral_model:

{{ config(materialized = 'ephemeral') }}
select 1 as fun

models/another_ephemeral_model:

{{ config(materialized = 'ephemeral') }}
select * from {{ ref('my_ephemeral_model') }}

models/my_other_model:

select * from {{ ref('another_ephemeral_model')}}
union all
select 2 as fun

dbt v0.19.0

target/compiled/[project_name]/models/my_ephemeral_model:

select 1 as fun

target/compiled/[project_name]/models/another_ephemeral_model.sql

select * from __dbt__CTE__my_ephemeral_model

target/compiled/[project_name]/models/my_other_model.sql:

with __dbt__CTE__my_ephemeral_model as (


select 1 as fun
),  __dbt__CTE__another_ephemeral_model as (


select * from __dbt__CTE__my_ephemeral_model
)select * from __dbt__CTE__another_ephemeral_model
union all
select 2 as fun

dbt v0.18.1

target/compiled/[project_name]/models/another_ephemeral_model.sql

with __dbt__CTE__my_ephemeral_model as (

select 1 as fun
)select * from __dbt__CTE__my_ephemeral_model

@gshank
Copy link
Contributor

gshank commented Feb 8, 2021

Ephemeral models referencing other ephemeral models cannot be resolved to 'compiled_sql' at the same time as other models because the enclosing non-ephemeral models need the non-compiled sql. The compiled sql could be saved in some other place and updated at the end of compilation, or re-compiled at the end of compilation. This code is in core/dbt/compilation.py, in the '_recursively_prepend_ctes' method.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working regression
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants