From 7eb8d3a5ff74e09e917a1592cbb503a27e4b72af Mon Sep 17 00:00:00 2001 From: cayetanobv Date: Thu, 27 Nov 2025 16:19:47 +0100 Subject: [PATCH] Add BLOCK column clustering for Snowflake tables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change adds automatic clustering by the BLOCK column when creating new Snowflake tables, bringing Snowflake in line with BigQuery's existing clustering behavior for improved query performance on spatial lookups. Changes: - Added add_clustering() method to apply CLUSTER BY (BLOCK) to tables - Clustering is applied once at the end of upload_raster() after all data and metadata are written - Only applies to new tables (not when appending to existing tables) - Gracefully handles errors if clustering already exists 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- raster_loader/io/snowflake.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/raster_loader/io/snowflake.py b/raster_loader/io/snowflake.py index 04699b6..9b28f93 100644 --- a/raster_loader/io/snowflake.py +++ b/raster_loader/io/snowflake.py @@ -86,6 +86,17 @@ def __init__( def band_rename_function(self, band_name: str): return band_name + def add_clustering(self, fqn: str): + """Add clustering by BLOCK column for query optimization.""" + fqn = fqn.upper() + try: + cluster_query = f"ALTER TABLE {fqn} CLUSTER BY (BLOCK)" + self.execute(cluster_query) + except Exception: + # Clustering might fail if table already has clustering, + # so we silently ignore errors + pass + def write_metadata( self, metadata, @@ -310,6 +321,11 @@ def band_rename_function(x): self.write_metadata(metadata, append_records, fqn) + # Add clustering on new tables for query optimization + if not append_records: + print("Adding clustering by BLOCK column...") + self.add_clustering(fqn) + except IncompatibleRasterException as e: raise IOError("Error uploading to Snowflake: {}".format(e.message))