Skip to content

Creating materialized views using TO table syntax does not work with ephemeral models #521

@jorgeparavicini

Description

@jorgeparavicini

Describe the bug

We have set up some staging tables that are materialized as ephemeral, i.e., not materialized at all. We then would like to create a materialized view on this staging model. This works if we have a single materialized view. However, if we use the -- mv:begin and -- mv:end syntax, the ephemeral model is referenced, but the CTE is skipped.

Steps to reproduce

  1. Have an ephemeral materialized view
  2. Create a materialized view model that selects from the model in step 1. Running this will succeed.
  3. Now wrap the select statement of the materialized view in -- mv:begin and -- mv:end. Running this model will now fail.

Expected behaviour

Running multiple materialized views in the same model should succeed if they reference an ephemeral model.

Code examples, such as models or profile settings

Example staging model:

{{ config(materialization="ephemeral") }}

select
    _id
from {{ source("rapid_service", "responses") }}

Now, let's create the materialized view

{{ config(materialization="ephemeral") }}

select
    _id
from {{ source("rapid_service", "responses") }}

Running this will run the following SQL query:

create materialized view if not exists `analysis`.`int_responses_aggregated_to_campaigns_mv`
to `analysis`.`int_responses_aggregated_to_campaigns` 
as with __dbt__cte__stg_rapid_service__responses as (
    select _id
    from `mongodb`.`responses`
)
select _id
from `__dbt__cte__stg_rapid_service__responses`

If we change our model to the following:

{{
    config(
        materialized="materialized_view",
    )
}}

-- mv:begin
select _id
from {{ ref("stg_rapid_service__responses") }}
-- mv:end

The executed query is:

create materialized view if not exists `analysis`.`int_responses_aggregated_to_campaigns_mv` 
to `analysis`.`int_responses_aggregated_to_campaigns`
    as
select _id
from `__dbt__cte__stg_rapid_service__responses`

This query now references a CTE that has not been copied and therefore fails to execute.

dbt and/or ClickHouse server logs

Configuration

Environment

  • dbt version: 1.10.11
  • dbt-clickhouse version: 1.9.3
  • clickhouse-driver version (if using native): 0.2.9
  • clickhouse-connect version (if using http): 0.8.18
  • Python version: 3.13
  • Operating system: MacOS Tahoe

ClickHouse server

  • ClickHouse Server version: 25.8.1.5101

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions