snowflake-semanticview
github/awesome-copilot
使用 Snowflake CLI(snow)建立、修改及驗證 Snowflake 語義檢視。當需要透過 CREATE/ALTER SEMANTIC VIEW 語句構建或排查語義檢視/語義層定義的問題、透過 CLI 對語義檢視的 DDL 語句進行驗證,或是指導 Snowflake CLI 的安裝與連線配置時,均可使用該工具。
...展開全部關於 snowflake-semanticview
本技能將指導助手使用官方 Snowflake CLI(snow)建立、修改及驗證 Snowflake 語義檢視。它透過遵循嚴格的“先驗證後操作”工作流程,解決了構建可靠語義層定義的問題:助手不會直接生成未經測試的 DDL,而是先起草 CREATE/ALTER SEMANTIC VIEW 語句,透過 CLI 使用臨時名稱在 Snowflake 中對其進行驗證,反覆調整直至透過驗證,最後才應用最終定義。此外,它還提供了安裝 Snowflake CLI 以及透過 snow connection add 配置連線的一次性設定指南,並會引用 Snowflake 的官方文件。
該工作流程十分嚴謹。它會要求助手確認目標資料庫、模式、角色、資料倉儲以及檢視名稱,驗證星型模型結構,並依據官方語法起草 DDL。對於每一個維度、事實表和度量指標,它都會要求填寫別名和註釋,優先使用 Snowflake 中已有的列註釋作為依據,在建立新註釋前會先徵得使用者許可。它還會使用帶有 DISTINCT 關鍵字且行數限制在 1000 行以內的只讀 SELECT 語句來識別資料關聯關係和列型別,透過帶有臨時字尾(如 __tmp_validate)的名稱對 DDL 進行驗證,以避免覆蓋真實檢視,隨後執行示例 SEMANTIC_VIEW 查詢來確認功能表現,最後清理臨時生成的物件。
該技能的目標使用者是那些需要構建 Snowflake 語義層或度量指標定義的資料工程師和分析工程師。憑證管理由 Snowflake 官方 CLI 的連線配置功能負責,而非由本技能處理;同時,透過使用臨時驗證名稱、限制行數上限以及在建立註釋前徵得許可等設計,體現了謹慎且低風險的資料處理理念。
常見問題
需要哪些前置條件?
必須已安裝 Snowflake CLI(snow),並透過 snow connection add 配置好連線。本技能會連結到 Snowflake 的官方文件以指導安裝和連線設定,這些步驟均為一次性操作。
憑證是如何處理的?
身份驗證工作由 Snowflake 官方 CLI 的連線配置功能負責。本技能會使用已配置的連線進行驗證和執行操作,本身不會儲存或管理任何憑證。
它是如何避免覆蓋現有檢視的?
它會使用臨時名稱對 DDL 進行驗證,例如在相同的資料庫和模式中新增 __tmp_validate 之類的字尾,只有在驗證成功後才會應用最終 DDL,並隨後清理臨時物件。
它會查詢我的實際資料嗎?
它會執行帶有 DISTINCT 關鍵字且行數限制在 1000 行以內的只讀 SELECT 語句,以此識別事實表與維度表之間的關聯關係以及列型別,從而生成有意義的註釋和別名。
它會自行建立別名或註釋嗎?
不會。它會優先使用 Snowflake 中已有的列註釋作為依據,在新增任何新內容之前,會先詢問使用者是否允許建立註釋、是否需要使用者提供具體文字,或是是否應先給出建議供使用者審批。
One-Time Setup
- Verify Snowflake CLI installation by opening a new terminal and running
snow --help. - If Snowflake CLI is missing or the user cannot install it, direct them to https://docs.snowflake.com/en/developer-guide/snowflake-cli/installation/installation.
- Configure a Snowflake connection with
snow connection addper https://docs.snowflake.com/en/developer-guide/snowflake-cli/connecting/configure-connections#add-a-connection. - Use the configured connection for all validation and execution steps.
Workflow For Each Semantic View Request
- Confirm the target database, schema, role, warehouse, and final semantic view name.
- Confirm the model follows a star schema (facts with conformed dimensions).
- Draft the semantic view DDL using the official syntax:
- https://docs.snowflake.com/en/sql-reference/sql/create-semantic-view
- Populate synonyms and comments for each dimension, fact, and metric:
- Read Snowflake table/view/column comments first (preferred source):
- https://docs.snowflake.com/en/sql-reference/sql/comment
- If comments or synonyms are missing, ask whether you can create them, whether the user wants to provide text, or whether you should draft suggestions for approval.
- Read Snowflake table/view/column comments first (preferred source):
- Use SELECT statements with DISTINCT and LIMIT (maximum 1000 rows) to discover relationships between fact and dimension tables, identify column data types, and create more meaningful comments and synonyms for columns.
- Create a temporary validation name (for example, append
__tmp_validate) while keeping the same database and schema. - Always validate by sending the DDL to Snowflake via Snowflake CLI before finalizing:
- Use
snow sqlto execute the statement with the configured connection. - If flags differ by version, check
snow sql --helpand use the connection option shown there.
- Use
- If validation fails, iterate on the DDL and re-run the validation step until it succeeds.
- Apply the final DDL (create or alter) using the real semantic view name.
- Run a sample query against the final semantic view to confirm it works as expected. It has a different SQL syntax as can be seen here: https://docs.snowflake.com/en/user-guide/views-semantic/querying#querying-a-semantic-viewExample:
SELECT * FROM SEMANTIC_VIEW( my_semview_name DIMENSIONS customer.customer_market_segment METRICS orders.order_average_value)ORDER BY customer_market_segment;
- Clean up any temporary semantic view created during validation.
Synonyms And Comments (Required)
- Use the semantic view syntax for synonyms and comments:
WITH SYNONYMS [ = ] ( 'synonym' [ , ... ] )COMMENT = 'comment_about_dim_fact_or_metric'- Treat synonyms as informational only; do not use them to reference dimensions, facts, or metrics elsewhere.
- Use Snowflake comments as the preferred and first source for synonyms and comments:
- https://docs.snowflake.com/en/sql-reference/sql/comment
- If Snowflake comments are missing, ask whether you can create them, whether the user wants to provide text, or whether you should draft suggestions for approval.
- Do not invent synonyms or comments without user approval.
Validation Pattern (Required)
- Never skip validation. Always execute the DDL against Snowflake with Snowflake CLI before presenting it as final.
- Prefer a temporary name for validation to avoid clobbering the real view.
Example CLI Validation (Template)
# Replace placeholders with real values.snow sql -q "<CREATE OR ALTER SEMANTIC VIEW ...>" --connection <connection_name>
If the CLI uses a different connection flag in your version, run:
snow sql --help
Notes
- Treat installation and connection setup as one-time steps, but confirm they are done before the first validation.
- Keep the final semantic view definition identical to the validated temporary definition except for the name.
- Do not omit synonyms or comments; consider them required for completeness even if optional in syntax.





首頁
