Skip to content

Add semantic-view-patterns skill#2

Open
sfc-gh-jklahr wants to merge 15 commits intoSnowflake-Labs:mainfrom
sfc-gh-jklahr:add-semantic-view-patterns
Open

Add semantic-view-patterns skill#2
sfc-gh-jklahr wants to merge 15 commits intoSnowflake-Labs:mainfrom
sfc-gh-jklahr:add-semantic-view-patterns

Conversation

@sfc-gh-jklahr
Copy link
Copy Markdown

@sfc-gh-jklahr sfc-gh-jklahr commented Apr 21, 2026

Summary

  • Adds semantic-view-patterns — an interactive skill for 25 Snowflake Semantic View modeling patterns across two modes
  • Tutorial mode: deploys working seed data + DDL into the user's account, runs live SEMANTIC_VIEW() queries, and narrates the output
  • Apply mode: reads the user's existing SV, maps the pattern to their tables/columns, and generates adapted DDL — no example data needed
  • Bundles all 25 snippets inside the skill directory
  • Three snippets (inline_sv, materialization, scoped_dataset) are flagged as Private Preview with account-team enablement notes

Patterns included

range_join, asof_join, multi_path_metrics, shared_degenerate_dimension, semi_additive_metric, window_metrics, derived_metrics, time_intelligence, entity_facts, variables, multi_fact_table, ai_metadata, tags, introspection, fact_as_relationship_key, system_explain_semantic_query, caller_rights, standard_sql, inline_sv, materialization, scoped_dataset, row_access_policies, role_playing_dimensions, accumulating_snapshot, sv_diagnostics

Test plan

  • Load skill and trigger Tutorial mode: $sv-patterns walk me through time intelligence
  • Verify snippet deploys and live queries return results
  • Trigger Apply mode: $sv-patterns help me add year-over-year to my existing SV
  • Verify skill asks for existing SV DDL and generates adapted DDL
  • Trigger discovery: $sv-patterns what snippets are available
  • Test new patterns: role_playing_dimensions, accumulating_snapshot, sv_diagnostics

.... Generated with Cortex Code

sfc-gh-jklahr and others added 15 commits April 20, 2026 17:24
18 executable Snowflake Semantic View modeling patterns with two modes:
- Tutorial mode: deploys working examples, runs live queries, explains output
- Apply mode: adapts patterns to the user's own tables and generates DDL

.... Generated with [Cortex Code](https://docs.snowflake.com/en/user-guide/cortex-code/cortex-code)

Co-Authored-By: Cortex Code <noreply@snowflake.com>
- SKILL.md Step 3 now recommends SNOWFLAKE_LEARNING_DB.PUBLIC (pre-provisioned
  learning environment) as the default, with custom DB/schema as alternative
- run_snippet.py --db default changed from CORTEX_SNIPPETS to SNOWFLAKE_LEARNING_DB
- Skill notes SNOWFLAKE_LEARNING_ROLE and SNOWFLAKE_LEARNING_WH for users on
  the learning environment path

.... Generated with [Cortex Code](https://docs.snowflake.com/en/user-guide/cortex-code/cortex-code)

Co-Authored-By: Cortex Code <noreply@snowflake.com>
Step 3 now captures TARGET_CONNECTION before deployment so tutorials
can run against any Snowflake account (not just the active CoCo account).
All snowflake_sql_execute calls and run_snippet.py use that connection.

.... Generated with [Cortex Code](https://docs.snowflake.com/en/user-guide/cortex-code/cortex-code)

Co-Authored-By: Cortex Code <noreply@snowflake.com>
- SKILL.md Act 1 no longer presents the comparison table in the main flow;
  instead adds a prompt hint: "Ask me 'Tell me about other approaches'"
- New Step 5b handler responds to that prompt with the full table + narration
- Added Tableau row to 11 snippet READMEs (asof_join, derived_metrics,
  entity_facts, multi_fact_table, multi_path_metrics, range_join,
  scoped_dataset, semi_additive_metric, shared_degenerate_dimension,
  variables, window_metrics)
- time_intelligence, materialization, tags already had Tableau coverage

.... Generated with [Cortex Code](https://docs.snowflake.com/en/user-guide/cortex-code/cortex-code)

Co-Authored-By: Cortex Code <noreply@snowflake.com>
Adds two new patterns (20 total):
- fact_as_relationship_key: computed FK fact joining sales to fiscal_quarters via CONCAT(YEAR, '-Q', QUARTER) — no physical FK column needed
- system_explain_semantic_query: SYSTEM$EXPLAIN_SEMANTIC_QUERY demos on a support ticket SV, including PRIVATE fact diagnosis and pre-flight error debugging

.... Generated with [Cortex Code](https://docs.snowflake.com/en/user-guide/cortex-code/cortex-code)

Co-Authored-By: Cortex Code <noreply@snowflake.com>
Demonstrates that SEMANTIC_VIEW() uses caller rights (not owner rights
like standard views): users need SELECT on both the SV and all base
tables. Uses a 4-role pattern (SV_OWNER, SV_CREATOR, SV_USER,
SV_USER_NO_BASE_SELECT) with separate SV and DATA schemas to show a
succeeding and a deliberately failing query side-by-side.

Requires ACCOUNTADMIN. Creates dedicated SV_CALLER_TEST
database/warehouse/roles with a cleanup block.

.... Generated with [Cortex Code](https://docs.snowflake.com/en/user-guide/cortex-code/cortex-code)

Co-Authored-By: Cortex Code <noreply@snowflake.com>
SVs (like views) use owner rights by default. The trick is making the
SV owner a role with no base table access — so owner rights alone can't
reach the data, and the caller must supply their own table privileges.
Updated comparison table and four-role pattern table accordingly.

.... Generated with [Cortex Code](https://docs.snowflake.com/en/user-guide/cortex-code/cortex-code)

Co-Authored-By: Cortex Code <noreply@snowflake.com>
- New snippet: row_access_policies (22nd pattern) — demonstrates the NULL-row
  problem when a RAP is applied to the dimension table only, and two workarounds:
  inner-join helper view and applying the RAP directly to the fact table
- SKILL.md: probe-first SNOWFLAKE_LEARNING_DB check; direct snowflake_sql_execute
  execution instead of run_snippet.py; mandatory cleanup offer at end of tutorial;
  access-control snippets use existing warehouse instead of creating a dedicated one

.... Generated with [Cortex Code](https://docs.snowflake.com/en/user-guide/cortex-code/cortex-code)

Co-Authored-By: Cortex Code <noreply@snowflake.com>
One physical DIM_DATE aliased twice — order_date_dim and ship_date_dim — giving
each role independent logical dimension names (order_year, ship_year, etc.) with
no USING clause needed. Demonstrates revenue-by-order-month vs revenue-by-ship-month
and fulfillment lag cross-tab analysis.

Also fixes SV DIMENSIONS syntax throughout: entity.logical_name AS physical_column.

.... Generated with [Cortex Code](https://docs.snowflake.com/en/user-guide/cortex-code/cortex-code)

Co-Authored-By: Cortex Code <noreply@snowflake.com>
…SING syntax

- New snippet: accumulating_snapshot — Kimball Accumulating Snapshot Fact Table
  modeling a loan origination funnel (application → review → decision → funding);
  one DIM_DATE alias, four milestone relationships, USING per stage metric;
  derived conversion rate metrics (review_rate, decision_rate, funding_rate)
- Fix multi_path_metrics/semantic_view.sql: USING clause was inverted
  (AS expression USING rel → USING rel AS expression)
- Update SKILL.md: 23 → 24 patterns, add accumulating_snapshot to table

.... Generated with [Cortex Code](https://docs.snowflake.com/en/user-guide/cortex-code/cortex-code)

Co-Authored-By: Cortex Code <noreply@snowflake.com>
Four diagnostic scenarios with exact error messages and corrected models:
1. Ambiguous path relationship — two date FKs, no USING → query-time error
2. Fan trap — header-level metric routed through bridge to fine-grain dim
3. Table with no relationship — orphaned table in TABLES clause
4. Duplicate logical names (deploy error) + overlapping synonyms (CA ambiguity)

Includes pre-deployment checklist, diagnostic cheat sheet, and Cortex Analyst
ambiguity demo comparing broken vs fixed synonym sets.

.... Generated with [Cortex Code](https://docs.snowflake.com/en/user-guide/cortex-code/cortex-code)

Co-Authored-By: Cortex Code <noreply@snowflake.com>
…itive

Scenario 5a: Reversed relationship direction — deploy-time error when FK is on
the RHS of REFERENCES (not a PK/UK). Engine catches it immediately.

Scenario 5b: Wrong cardinality (lying PK declaration) — the most dangerous
diagnostic. Declaring PRIMARY KEY on an FK column (e.g. DEAL_ID on DEAL_ITEMS)
tells the engine the relationship is 1:1, disabling the fan trap guard. The
exact query that errors on a correctly-declared model runs silently and inflates
numbers by the average items-per-parent count. Detection: compare SV total to
raw SELECT SUM() on the table.

Scenario 6: Forgotten semi-additive behavior — checklist-only, no DDL. Snapshot
metrics (balance, headcount, inventory) should never use SUM across time.
References semi_additive_metric snippet for the NON ADDITIVE BY fix.

Also: add DELETE FROM guards to seed_data.sql to prevent double-insert inflation.

.... Generated with [Cortex Code](https://docs.snowflake.com/en/user-guide/cortex-code/cortex-code)

Co-Authored-By: Cortex Code <noreply@snowflake.com>
Each snippet now has both semantic_view.sql (DDL) and semantic_view.yaml (YAML)
giving users a choice of authoring format. SKILL.md updated to ask the format
question early and branch Tutorial/Apply steps accordingly.

YAML highlights:
- Full parity for 18 patterns with no DDL-only limitations
- USING → using_relationships, NON ADDITIVE BY → non_additive_dimensions,
  PRIVATE → access_modifier: private_access, AI_VERIFIED_QUERIES → verified_queries
- 7 patterns flagged with DDL-only notes (ASOF/range joins, VARIABLES, WITH TAG,
  MAX_STALENESS, inline SQL subqueries, caller-rights roles)
- verify_only=TRUE in SYSTEM$CREATE_SEMANTIC_VIEW_FROM_YAML documented as
  pre-deployment dry-run (closes sv_diagnostics "no dry-run" gap)
- SYSTEM$READ_YAML_FROM_SEMANTIC_VIEW documented for exporting existing DDL SVs

SKILL.md changes:
- Step 0b: ask authoring format (DDL vs YAML) before any deployment
- YAML/DDL feature map table for quick reference
- Tutorial Step 4/7: branch on AUTHORING_FORMAT for file reading and deployment
- Apply Steps A2/A3/A5: branch for adapted DDL vs adapted YAML generation

.... Generated with [Cortex Code](https://docs.snowflake.com/en/user-guide/cortex-code/cortex-code)

Co-Authored-By: Cortex Code <noreply@snowflake.com>
Track 1 (canonical exports from deployed SVs):
- range_join, accumulating_snapshot, role_playing_dimensions, sv_diagnostics
  replaced with ground-truth YAML from SYSTEM$READ_YAML_FROM_SEMANTIC_VIEW
- range_join: range join IS fully supported in YAML via type: range +
  right_range: {start_column, end_column} and constraints: [{distinct_range}]
- AI_SQL_GENERATION maps to module_custom_instructions: sql_generation: in YAML
  (previously flagged as DDL-only — correction applied to range_join)

Track 2 (spot-check 3 key YAML patterns):
- semi_additive_metric: non_additive_dimensions syntax valid ✓
- ai_metadata: verified_queries syntax valid ✓
- entity_facts: fixed value_segment dimension expr (must reference private fact
  by logical name: lifetime_value, not SUM(orders.AMOUNT)) ✓
  fixed lifetime_value fact expr (SUM(orders.order_amount) not SUM(orders.AMOUNT)) ✓

.... Generated with [Cortex Code](https://docs.snowflake.com/en/user-guide/cortex-code/cortex-code)

Co-Authored-By: Cortex Code <noreply@snowflake.com>
AI_SQL_GENERATION is NOT DDL-only — it maps to
module_custom_instructions: sql_generation: in YAML.
Removed from DDL-only list in SKILL.md; added to YAML ↔ DDL feature map.
Fixed comment in 7 snippet YAML files that incorrectly said DDL-only.

BETWEEN EXCLUSIVE range join is also NOT DDL-only — it maps to
type: range + right_range + constraints: [{distinct_range}] in YAML.
Added to YAML ↔ DDL feature map; removed from DDL-only list.
ASOF remains DDL-only.

.... Generated with [Cortex Code](https://docs.snowflake.com/en/user-guide/cortex-code/cortex-code)

Co-Authored-By: Cortex Code <noreply@snowflake.com>
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.

1 participant