Skip to content

Add Semantic View as Materialization#7

Open
sfc-gh-yutliu wants to merge 12 commits into
mainfrom
features/semantic-view/issue-1260
Open

Add Semantic View as Materialization#7
sfc-gh-yutliu wants to merge 12 commits into
mainfrom
features/semantic-view/issue-1260

Conversation

@sfc-gh-yutliu

@sfc-gh-yutliu sfc-gh-yutliu commented Aug 19, 2025

Copy link
Copy Markdown

resolves dbt-labs#1260
dbt-labs/docs.getdbt.com/#

Problem

dbt-snowflake did not support the new Snowflake "semantic view" relation type, so dbt users could not create or manage semantic views as first-class dbt resources or materializations. There was no materialization, build support, or test coverage for semantic views.

Solution

Snowflake Semantic Views in DBT

This pr adds support for "semantic view" as a new materialization in the dbt-snowflake adapter. It introduces the necessary logic, macros, SQL generation, and tests to enable creating, dropping, and renaming semantic views in Snowflake via dbt.

Test

tests/functional/semantic_view/test_semantic_view_basic.py

  • test_create_semantic_view: semantic view model refers to raw table (use source(<raw_table>)) and table model (use ref(<table_model>)).
  • test_table_refer_to_semantic_view: table model refers to a semantic view model (use ref(<semantic_view_model>)).
  • test_table_refer_to_raw_semantic_view: table model refers to raw semantic view (use source(<semantic_view>))
  • test_semantic_view_comment: test description is recorded by comment on semantic view command.
  • test_semantic_view_copy_grants_logic: test copy grants configuration is properly handled.
  • test_semantic_view_with_ca_extension: test Cortex Analyst Extension query is properly handled in DBT.
➜  dbt-snowflake git:(feature/semantic-view/issue-1260) ✗ hatch run unit-tests tests/functional/semantic_view/test_semantic_view_basic.py
======================================================== test session starts ========================================================
platform darwin -- Python 3.11.6, pytest-7.4.4, pluggy-1.6.0 -- /Users/yutliu/dbt-adapters/dbt-snowflake/.hatch/dbt-snowflake/bin/python
cachedir: .pytest_cache
rootdir: /Users/yutliu/dbt-adapters/dbt-snowflake
configfile: pyproject.toml
plugins: ddtrace-2.3.0, csv-3.0.0, logbook-1.2.0, xdist-3.8.0, dotenv-0.5.2
16 workers [6 items]      
scheduling tests via LoadScheduling

tests/functional/semantic_view/test_semantic_view_basic.py::TestSemanticViewBasic::test_table_refer_to_semantic_view 
tests/functional/semantic_view/test_semantic_view_basic.py::TestSemanticViewBasic::test_semantic_view_comment 
tests/functional/semantic_view/test_semantic_view_basic.py::TestSemanticViewBasic::test_create_semantic_view 
tests/functional/semantic_view/test_semantic_view_basic.py::TestSemanticViewBasic::test_table_refer_to_raw_semantic_view 
tests/functional/semantic_view/test_semantic_view_basic.py::TestSemanticViewBasic::test_semantic_view_copy_grants_logic 
tests/functional/semantic_view/test_semantic_view_basic.py::TestSemanticViewBasic::test_semantic_view_with_ca_extension 
[gw5] [ 16%] PASSED tests/functional/semantic_view/test_semantic_view_basic.py::TestSemanticViewBasic::test_semantic_view_with_ca_extension 
[gw3] [ 33%] PASSED tests/functional/semantic_view/test_semantic_view_basic.py::TestSemanticViewBasic::test_semantic_view_comment 
[gw0] [ 50%] PASSED tests/functional/semantic_view/test_semantic_view_basic.py::TestSemanticViewBasic::test_create_semantic_view 
[gw2] [ 66%] PASSED tests/functional/semantic_view/test_semantic_view_basic.py::TestSemanticViewBasic::test_table_refer_to_raw_semantic_view 
[gw4] [ 83%] PASSED tests/functional/semantic_view/test_semantic_view_basic.py::TestSemanticViewBasic::test_semantic_view_copy_grants_logic 
[gw1] [100%] PASSED tests/functional/semantic_view/test_semantic_view_basic.py::TestSemanticViewBasic::test_table_refer_to_semantic_view 

========================================================= warnings summary ==========================================================
.hatch/dbt-snowflake/lib/python3.11/site-packages/dbt/cli/options.py:6: 17 warnings
  /Users/yutliu/dbt-adapters/dbt-snowflake/.hatch/dbt-snowflake/lib/python3.11/site-packages/dbt/cli/options.py:6: DeprecationWarning: 'parser.OptionParser' is deprecated and will be removed in Click 9.0. The old parser is available in 'optparse'.
    from click.parser import OptionParser, ParsingState

.hatch/dbt-snowflake/lib/python3.11/site-packages/dbt/cli/options.py:6: 17 warnings
  /Users/yutliu/dbt-adapters/dbt-snowflake/.hatch/dbt-snowflake/lib/python3.11/site-packages/dbt/cli/options.py:6: DeprecationWarning: 'parser.ParsingState' is deprecated and will be removed in Click 9.0. The old parser is available in 'optparse'.
    from click.parser import OptionParser, ParsingState

tests/functional/semantic_view/test_semantic_view_basic.py: 38 warnings
  /Users/yutliu/dbt-adapters/dbt-snowflake/.hatch/dbt-snowflake/lib/python3.11/site-packages/snowflake/connector/__init__.py:54: DeprecationWarning: The 'insecure_mode' connection property is deprecated. Please use 'disable_ocsp_checks' instead
    return SnowflakeConnection(**kwargs)

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
================================================== 6 passed, 72 warnings in 15.29s ==================================================
➜  dbt-snowflake git:(feature/semantic-view/issue-1260) ✗ 

Checklist

  • I have read the contributing guide and understand what's expected of me
  • I have run this code in development and it appears to resolve the stated issue
  • This PR includes tests, or tests are not required/relevant for this PR
  • This PR has no interface changes (e.g. macros, cli, logs, json artifacts, config files, adapter interface, etc) or this PR has already received feedback and approval from Product or DX

@github-actions

Copy link
Copy Markdown

Thank you for your pull request! We could not find a changelog entry for this change in the dbt-snowflake package. For details on how to document a change, see the Contributing Guide.

@sfc-gh-yutliu sfc-gh-yutliu force-pushed the features/semantic-view/issue-1260 branch from 993aeff to 611fb1a Compare August 21, 2025 17:59
@sfc-gh-yutliu sfc-gh-yutliu force-pushed the features/semantic-view/issue-1260 branch from 006f465 to c6124be Compare August 21, 2025 23:20
@sfc-gh-yutliu sfc-gh-yutliu marked this pull request as ready for review August 22, 2025 07:08
@sfc-gh-yutliu sfc-gh-yutliu requested a review from a team as a code owner August 22, 2025 07:08

@sfc-gh-jliang sfc-gh-jliang left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

{%- set _sql_norm = _sql_norm[:-1] if _sql_norm[-1:] == ';' else _sql_norm -%}
{%- set _sql_norm = _sql_norm | trim -%}
{%- set _ends = (_sql_norm | lower)[-11:] -%}
{%- if _ends != 'copy grants' -%}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe using regex here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if 'copy grant' before comment works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Support Snowflake Semantic View as a materialization

2 participants