Add BLOCK column clustering for Snowflake tables #181
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds automatic clustering by the
BLOCKcolumn when creating new Snowflake tables, bringing Snowflake in line with BigQuery's existing clustering behavior for improved query performance.Background
Currently, BigQuery tables are created with explicit clustering on the
BLOCKcolumn (viaclustering_fields=["block"]inLoadJobConfig), which optimizes spatial queries. Snowflake tables were missing this optimization, leading to inconsistent query performance between the two platforms.Changes
add_clustering()method toSnowflakeConnectionclass that appliesCLUSTER BY (BLOCK)to tablesupload_raster()after all data and metadata are writtenappend_records)Technical Details
Why at the end of upload_raster()?
The clustering statement is intentionally placed after all data upload operations because:
upload_records()can be called multiple times when using chunk_size (batched uploads)overwrite=True, subsequent batches append dataSnowflake Clustering Background
Unlike BigQuery where clustering is configured at table creation time via the API, Snowflake's
write_pandas()function does not support a clustering parameter. The only way to add clustering in Snowflake is through SQL:CREATE TABLE ... CLUSTER BY (column)at creation timeALTER TABLE ... CLUSTER BY (column)after creationSince we use
auto_create_table=Trueinwrite_pandas(), we apply clustering viaALTER TABLEafter the initial data load.Testing
Related
This aligns Snowflake's behavior with BigQuery's implementation in
raster_loader/io/bigquery.py:91.🤖 Generated with Claude Code