Generate Snowflake SQL from YAML configuration for ID unification
Generates Snowflake SQL from YAML configuration for ID unification workflows.
/plugin marketplace add treasure-data/aps_claude_tools/plugin install treasure-data-cdp-hybrid-idu-plugins-cdp-hybrid-idu@treasure-data/aps_claude_toolsGenerate production-ready Snowflake SQL workflow from your unify.yml configuration file. This command creates Snowflake-native SQL files with proper clustering, VARIANT support, and platform-specific function conversions.
unify.ymlsnowflake_sql/)unify.yml exists and is validI'll call the snowflake-sql-generator agent to:
yaml_unification_to_snowflake.py Python scriptGenerate complete SQL workflow in this structure:
snowflake_sql/unify/
├── 01_create_graph.sql # Initialize graph table
├── 02_extract_merge.sql # Extract identities with validation
├── 03_source_key_stats.sql # Source statistics with GROUPING SETS
├── 04_unify_loop_iteration_*.sql # Loop iterations (auto-calculated count)
├── 05_canonicalize.sql # Canonical ID creation with key masks
├── 06_result_key_stats.sql # Result statistics with histograms
├── 10_enrich_*.sql # Enrich each source table
├── 20_master_*.sql # Master tables with attribute aggregation
├── 30_unification_metadata.sql # Metadata tables
├── 31_filter_lookup.sql # Validation rules lookup
└── 32_column_lookup.sql # Column mapping lookup
Provide:
/cdp-hybrid-idu:hybrid-generate-snowflake
I'll prompt you for:
- YAML file path
- Target database
- Target schema
Provide all parameters upfront:
YAML file: /path/to/unify.yml
Target database: my_database
Target schema: my_schema
Source database: source_database (optional)
Source schema: PUBLIC (optional, defaults to PUBLIC)
Output directory: custom_output/ (optional)
CLUSTER BY (follower_id) on graph tablesDynamic Iteration Count: Auto-calculates based on:
Key-Specific Hashing: Each key uses unique cryptographic mask:
Key Type 1 (email): 0ffdbcf0c666ce190d
Key Type 2 (customer_id): 61a821f2b646a4e890
Key Type 3 (phone): acd2206c3f88b3ee27
Validation Rules:
valid_regexp: REGEXP_LIKE pattern filteringinvalid_texts: NOT IN clause with proper NULL handlingMaster Table Attributes:
MAX_BY(attr, order) with COALESCEARRAY_SLICE(ARRAY_CAT(arrays), 0, N)The generator automatically converts:
unify.yml)name: customer_unification
keys:
- name: email
valid_regexp: ".*@.*"
invalid_texts: ['', 'N/A', 'null']
- name: customer_id
invalid_texts: ['', 'N/A']
tables:
- table: customer_profiles
key_columns:
- {column: email_std, key: email}
- {column: customer_id, key: customer_id}
canonical_ids:
- name: unified_id
merge_by_keys: [email, customer_id]
merge_iterations: 15
master_tables:
- name: customer_master
canonical_id: unified_id
attributes:
- name: best_email
source_columns:
- {table: customer_profiles, column: email_std, priority: 1}
snowflake_sql/unify/
├── 01_create_graph.sql # Creates unified_id_graph_unify_loop_0
├── 02_extract_merge.sql # Merges customer_profiles keys
├── 03_source_key_stats.sql # Stats by table
├── 04_unify_loop_iteration_01.sql # First iteration
├── 04_unify_loop_iteration_02.sql # Second iteration
├── ... # Up to iteration_05
├── 05_canonicalize.sql # Creates unified_id_lookup
├── 06_result_key_stats.sql # Final statistics
├── 10_enrich_customer_profiles.sql # Adds unified_id column
├── 20_master_customer_master.sql # Creates customer_master table
├── 30_unification_metadata.sql # Metadata
├── 31_filter_lookup.sql # Validation rules
└── 32_column_lookup.sql # Column mappings
Use the execution command:
/cdp-hybrid-idu:hybrid-execute-snowflake
The agent executes:
python3 scripts/snowflake/yaml_unification_to_snowflake.py \
unify.yml \
-d my_database \
-s my_schema \
-sd source_database \
-ss source_schema \
-o snowflake_sql
01-09: Setup and initialization10-19: Source table enrichment20-29: Master table creation30-39: Metadata and lookup tables04_*_NN: Loop iterations (auto-numbered)Each loop iteration includes:
-- Check if graph changed
SELECT COUNT(*) FROM (
SELECT leader_ns, leader_id, follower_ns, follower_id
FROM iteration_N
EXCEPT
SELECT leader_ns, leader_id, follower_ns, follower_id
FROM iteration_N_minus_1
) diff
Stops when count = 0
Issue: YAML validation error Solution: Check YAML syntax, ensure proper indentation, verify all required fields
Issue: Table not found error Solution: Verify source database/schema, check table names in YAML
Issue: Python script error Solution: Ensure Python 3.7+ installed, check pyyaml dependency
Issue: Too many/few iterations
Solution: Adjust merge_iterations in canonical_ids section of YAML
Issue: VARIANT column errors Solution: Snowflake VARIANT type handling is automatic, ensure proper casting in custom SQL
Generated SQL will:
Ready to generate Snowflake SQL from your YAML configuration?
Provide your YAML file path and target database/schema to begin!