Three ways to load data into Snowflake, error tables that keep the rows a load rejects, and a dynamic table chain that turns it all into something useful, on a synthetic orders and clickstream schema.
- Choosing an ingestion pattern by freshness and arrival: a scheduled COPY for batch drops, Snowpipe with S3 events for continuous files, and where Snowpipe Streaming fits.
- Error tables for COPY INTO and Snowpipe: loads that keep going past bad rows, CHECK constraints enforced on load, an alert on rejections, and a correct-and-reload loop.
- Semantic view materializations: pre-aggregated metrics that queries on a semantic view are routed to, checked before and after with the rewrite-check functions, with history marked immutable to keep refreshes cheap.
- A silver and gold chain of dynamic tables on
REFRESH_MODE = ADAPTIVE, with a separate, largerINITIALIZATION_WAREHOUSEfor the occasional rebuild. - Deduplication downstream of at-least-once loading, using
QUALIFY ROW_NUMBER(), which still refreshes incrementally. - Custom incremental dynamic tables: an append-only stream-static join with
INSERT INTO SELF, and running totals withMERGE INTO SELF. - The monitoring queries that show what each refresh did, and when and why ADAPTIVE chose to reinitialise.
- Loading data into Snowflake, choosing between Snowpipe, COPY and Snowpark
- Dynamic tables grew up, adaptive refresh and custom incremental logic
- Snowflake now keeps the rows your loads reject
- Semantic view materializations, and the queries they do not speed up
Run the scripts in this order. Each one switches to the role it needs with USE ROLE.
| Order | Script | Roles used |
|---|---|---|
| 1 | ingestion/01_landing_setup.sql |
ACCOUNTADMIN (storage integration), SYSADMIN |
| 2 | ingestion/02_batch_copy.sql |
SYSADMIN |
| 3 | ingestion/03_snowpipe_auto_ingest.sql |
SYSADMIN |
| 4 | dynamic_tables/01_orders_chain.sql |
SYSADMIN |
| 5 | dynamic_tables/02_custom_incremental.sql |
SYSADMIN |
| any time | monitoring/dynamic_table_refreshes.sql |
a role with MONITOR on the dynamic tables |
| standalone | ingestion/05_error_tables.sql |
SYSADMIN; needs no S3, see below |
| standalone | semantic_views/01_revenue_materializations.sql |
SYSADMIN; uses SNOWFLAKE_SAMPLE_DATA, needs nothing else |
Snowpipe Streaming has no SQL to run; see ingestion/04_snowpipe_streaming.md.
ingestion/05_error_tables.sql stands on its own and runs in a trial account. It uploads sample_data/orders_with_errors.csv to an internal stage with PUT, so run it from SnowSQL or the Snowflake CLI in the root of this repository (worksheets cannot upload files). Replace wh_load, my_email_int and the alert address first.
Placeholders to replace before running:
<aws_role_arn>and<bucket>in01_landing_setup.sql. After creating the integration,DESC INTEGRATION s3_landing_intgives you the IAM user ARN and external ID to put in the AWS role's trust policy.wh_load,wh_transformandwh_transform_xl: your own warehouse names.01_orders_chain.sqlcreates the two transformation warehouses if they do not exist.- The cron schedule and time zone on the COPY task.
- After creating the pipe, point an S3 event notification for the
clickstream/prefix at the SQS queue shown in thenotification_channelcolumn ofSHOW PIPES.
The schema expects Parquet files under landing/orders/ and landing/clickstream/ with column names matching the raw tables, because both loads use MATCH_BY_COLUMN_NAME.
ingestion/ landing zone, batch COPY task, Snowpipe, Snowpipe Streaming notes, error tables
semantic_views/ a semantic view on the TPC-H sample data with materializations
sample_data/ a small CSV with deliberate errors for the error tables walkthrough
scripts/ statement-by-statement lint for files with very new syntax
dynamic_tables/ ADAPTIVE silver and gold chain, custom incremental examples
monitoring/ refresh history, reinitialisations and failures
- CI runs
sqlfluff lint(Snowflake dialect, sqlfluff 4.5.0) on every SQL file. That proves the SQL parses and follows a consistent style. It does not prove it runs. - Where the linter's dialect lags behind Snowflake, the exceptions are marked in the file with a
noqacomment and a reason: the->>pipe operator, and an alias on theCHANGESclause that the linter misreads. - Two files use syntax newer than sqlfluff 4.5.0 understands:
ingestion/05_error_tables.sql(ERROR_LOGGING, theSELECT ERROR TABLEprivilege,TRUNCATE ERROR_TABLE) andsemantic_views/01_revenue_materializations.sql(CREATE SEMANTIC VIEW,ADD MATERIALIZATION,SHOW MATERIALIZATIONS). Both are listed in.sqlfluffignore, and CI lints them one statement at a time withscripts/lint_statements.py, skipping only the seven statements marked-- lint: skip. Each of those was checked to be genuinely unparsable by sqlfluff, and follows the Snowflake documentation. - Syntax for the newer features (ADAPTIVE refresh,
INITIALIZATION_WAREHOUSE,REFRESH USING,CHANGES) follows the Snowflake documentation linked in each file. - None of these scripts have been run against a Snowflake account as part of this repository. Run them in a trial or sandbox account first, and adapt names, lags and warehouse sizes to yours.
- COPY INTO table
- Snowpipe introduction
- Dynamic table refresh modes
- Custom incrementalization
- Supported queries for incremental refresh
- Error tables for COPY INTO and Snowpipe
- Semantic view materializations
Built by Cloudcentrix, cloud and data engineering in South Africa. https://cloudcentrix.co.za