Skip to content

Commit 5087dee

Browse files
cayetanobvclaude
andauthored
Add BLOCK column clustering for Snowflake tables (#181)
Co-authored-by: Claude <[email protected]>
1 parent 63d7b12 commit 5087dee

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

raster_loader/io/snowflake.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ def __init__(
8686
def band_rename_function(self, band_name: str):
8787
return band_name
8888

89+
def add_clustering(self, fqn: str):
90+
"""Add clustering by BLOCK column for query optimization."""
91+
fqn = fqn.upper()
92+
try:
93+
cluster_query = f"ALTER TABLE {fqn} CLUSTER BY (BLOCK)"
94+
self.execute(cluster_query)
95+
except Exception:
96+
# Clustering might fail if table already has clustering,
97+
# so we silently ignore errors
98+
pass
99+
89100
def write_metadata(
90101
self,
91102
metadata,
@@ -310,6 +321,11 @@ def band_rename_function(x):
310321

311322
self.write_metadata(metadata, append_records, fqn)
312323

324+
# Add clustering on new tables for query optimization
325+
if not append_records:
326+
print("Adding clustering by BLOCK column...")
327+
self.add_clustering(fqn)
328+
313329
except IncompatibleRasterException as e:
314330
raise IOError("Error uploading to Snowflake: {}".format(e.message))
315331

0 commit comments

Comments
 (0)