Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,40 @@ TABLES(
[ FACTS ( semanticExpression [ , ... ] ) ]
[ DIMENSIONS ( semanticExpression [ , ... ] ) ]
[ METRICS ( semanticExpression [ , ... ] ) ]
[ WITH EXTENSION ( CA = '<json>' ) ]
[ COMMENT = '<comment>' ]
[ COPY GRANTS ]
```

#### Verified queries (Cortex Analyst extension)
Snowflake Semantic Views can store “verified queries” in the `CA` extension. This package supports a `verified_queries` model config that will append a `WITH EXTENSION (CA = '<json>')` clause at materialization time.

Example:
```
{{ config(
materialized='semantic_view',
verified_queries=[
{
"name": "Orders by day",
"question": "How many orders per day?",
"sql": "select * from orders_by_day"
},
{
"name": "Revenue by month",
"question": "What is revenue by month?",
"sql": "select * from revenue_by_month"
}
]
) }}

TABLES(...)
DIMENSIONS(...)
METRICS(...)
```

Notes:
- If your model SQL already includes a `WITH EXTENSION (...)` clause, this package will **not** attempt to merge extensions.

Reference a Semantic View from another model:
```
{{ config(materialized='table') }}
Expand Down
12 changes: 10 additions & 2 deletions integration_tests/models/semantic_view_with_ca_extension.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@
-- See the License for the specific language governing permissions and
-- limitations under the License.

{{ config(materialized='semantic_view') }}
{{ config(
materialized='semantic_view',
verified_queries=[
{
"name": "hi",
"question": "hello",
"sql": "select 1"
}
]
) }}
TABLES(t1 AS {{ ref('base_table') }}, t2 as {{ source('seed_sources', 'base_table2') }})
DIMENSIONS(t1.count as value, t2.volume as value)
METRICS(t1.total_rows AS SUM(t1.count), t2.max_volume as max(t2.volume))
with extension (CA = '{"verified_queries":[{"name":"hi", "question": "hello"}]')
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
select 'CA extension missing or values mismatch for SEMANTIC_VIEW_WITH_CA_EXTENSION' as error_message
where not (
position('ca' in lower(get_ddl('SEMANTIC_VIEW', '{{ ref('semantic_view_with_ca_extension') }}'))) > 0
and position('"verified_queries":[{"name":"hi", "question": "hello"}]' in lower(get_ddl('SEMANTIC_VIEW', '{{ ref('semantic_view_with_ca_extension') }}'))) > 0
and position('"verified_queries":[{"name":"hi","question":"hello"}]' in lower(get_ddl('SEMANTIC_VIEW', '{{ ref('semantic_view_with_ca_extension') }}'))) > 0
)


26 changes: 26 additions & 0 deletions macros/relations/semantic_view/create.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@
{%- endmacro %}


{% macro append_ca_extension_if_missing(sql, ca_json) -%}
{%- set s = (sql | trim) -%}
{%- if not s -%}
{{- s -}}
{%- else -%}
{%- set had_semicolon = (s[-1:] == ';') -%}
{%- if had_semicolon -%}
{%- set s = (s[:-1] | trim) -%}
{%- endif -%}

{# If the model SQL already includes a WITH EXTENSION clause, do not try to merge. #}
{%- if 'with extension' in (s | lower) -%}
{{- s -}}
{%- else -%}
{%- set out = s ~ "\nWITH EXTENSION (CA = '" ~ ca_json ~ "')" -%}
{{- out -}}
{%- endif -%}
{%- endif -%}
{%- endmacro %}

{% macro append_copy_grants_if_missing(sql) -%}
{%- set s = (sql | trim) -%}
{%- set had_semicolon = (s[-1:] == ';') -%}
Expand Down Expand Up @@ -59,6 +79,7 @@
{%- set identifier = model['alias'] -%}

{%- set copy_grants = config.get('copy_grants', default=false) -%}
{%- set verified_queries = config.get('verified_queries', default=none) -%}

{%- set target_relation = api.Relation.create(
identifier=identifier, schema=schema, database=database,
Expand All @@ -68,6 +89,11 @@
{%- set sql = dbt_semantic_view.append_copy_grants_if_missing(sql) -%}
{%- endif -%}

{%- if verified_queries is not none -%}
{%- set ca_json = {'verified_queries': verified_queries} | tojson -%}
{%- set sql = dbt_semantic_view.append_ca_extension_if_missing(sql, ca_json) -%}
{%- endif -%}

{{ run_hooks(pre_hooks) }}

-- build model
Expand Down