sqlmesh icon indicating copy to clipboard operation
sqlmesh copied to clipboard

Snowflake - Correctly handle creation of materialized views in the physical layer and dev previews

Open nickmuoh opened this issue 10 months ago • 0 comments

So given this example model:

MODEL (
  name MY_SCHEMA.MY_MATERIALIZED_VIEW,
  kind VIEW (
    materialized true
  ),
  tags (example, app),
  description 'Materialized view of **only** example app events based on app_name',
  blueprints (
    (
      app_name := 'example-app1',
      table_name_part := EXAMPLE,
      table_description := 'Materialized view of **only** example1 app events based on app_name',
      start_ := '2020-02-01',
      end_ := '2025-01-01'
    ),
    (
      app_name := 'example-app2',
      table_name_part := EXAMPLE,
      table_description := 'Materialized view of **only** example2 app events based on app_name',
      start_ := '2020-01-01',
      end_ := '2025-01-01'
    )
  ),
  start @start_,
  end @end_,
  enabled true
);

SELECT
  r.event_id,
  r.user_id,
  @app_name AS app_name
FROM MY_SCHEMA.MY_SOURCE_TABLE AS r
WHERE r.user_properties:"app_name" = @app_name;

Will result in the following create or replace statement:

CREATE OR REPLACE MATERIALIZED VIEW "MY_SCHEMA"."MY_MATERIALIZED_VIEW" 
(
    "EVENT_ID",
    "USER_ID",
    "APP_NAME"
) COPY GRANTS COMMENT = 'Materialized view of **only** example2 app events based on app_name' AS
SELECT
    "R"."EVENT_ID" AS "EVENT_ID",
    "R"."USER_ID" AS "USER_ID",
    'example-app' AS "APP_NAME"
FROM
    "MY_SCHEMA"."MY_SOURCE_TABLE" AS "R"
WHERE
    "R"."USER_PROPERTIES"::json:"app_name" = 'example-app'

Snowflake will throw the following error: Syntax error: unexpected '"EVENT_ID"'. (line 2)

I believe omitting the column list would resolve the issue, so the create or replace would look like this:

CREATE OR REPLACE MATERIALIZED VIEW "MY_SCHEMA"."MY_MATERIALIZED_VIEW"  
COPY GRANTS COMMENT = 'Materialized view of **only** example2 app events based on app_name' AS
SELECT
    "R"."EVENT_ID" AS "EVENT_ID",
    "R"."USER_ID" AS "USER_ID",
    'example-app' AS "APP_NAME"
FROM
    "MY_SCHEMA"."MY_SOURCE_TABLE" AS "R"
WHERE
    "R"."USER_PROPERTIES"::json:"app_name" = 'example-app'

nickmuoh avatar Apr 18 '25 16:04 nickmuoh