Skip to content

About

Snowflake ingestion with COPY and Snowpipe, error tables for rejected rows, dynamic tables with adaptive refresh, and semantic view materializations

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

Snowflake data pipelines

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.

What this shows

  • 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, larger INITIALIZATION_WAREHOUSE for 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 with MERGE INTO SELF.
  • The monitoring queries that show what each refresh did, and when and why ADAPTIVE chose to reinitialise.

Read the article

Quick start

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> in 01_landing_setup.sql. After creating the integration, DESC INTEGRATION s3_landing_int gives you the IAM user ARN and external ID to put in the AWS role's trust policy.
  • wh_load, wh_transform and wh_transform_xl: your own warehouse names. 01_orders_chain.sql creates 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 the notification_channel column of SHOW 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.

Repository layout

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

What is tested

  • 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 noqa comment and a reason: the ->> pipe operator, and an alias on the CHANGES clause that the linter misreads.
  • Two files use syntax newer than sqlfluff 4.5.0 understands: ingestion/05_error_tables.sql (ERROR_LOGGING, the SELECT ERROR TABLE privilege, TRUNCATE ERROR_TABLE) and semantic_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 with scripts/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.

Further reading

Built by Cloudcentrix, cloud and data engineering in South Africa. https://cloudcentrix.co.za

About

Snowflake ingestion with COPY and Snowpipe, error tables for rejected rows, dynamic tables with adaptive refresh, and semantic view materializations

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages