Skip to content

Commit 63b4f6a

Browse files
committed
Query info schema for snowflake CTAS num rows
1 parent 95d163d commit 63b4f6a

File tree

2 files changed

+16
-36
lines changed

2 files changed

+16
-36
lines changed

sqlmesh/core/engine_adapter/snowflake.py

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
import contextlib
44
import logging
5-
import re
65
import typing as t
76

8-
from sqlglot import exp
7+
from sqlglot import exp, parse_one
98
from sqlglot.helper import ensure_list
109
from sqlglot.optimizer.normalize_identifiers import normalize_identifiers
1110
from sqlglot.optimizer.qualify_columns import quote_identifiers
@@ -684,30 +683,18 @@ def _record_execution_stats(
684683
If so, we return early and do not record the row count.
685684
"""
686685
if rowcount == 1:
687-
results = self.cursor.fetchall()
688-
if results and len(results) == 1:
689-
try:
690-
results_str = str(results[0][0])
691-
except (ValueError, TypeError):
686+
query_parsed = parse_one(sql, dialect=self.dialect)
687+
if isinstance(query_parsed, exp.Create):
688+
if query_parsed.expression and isinstance(query_parsed.expression, exp.Select):
689+
table = query_parsed.find(exp.Table)
690+
if table:
691+
row_query = f"SELECT ROW_COUNT as row_count FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{table.db}' AND TABLE_NAME = '{table.name}'"
692+
row_query_results = self.fetchone(row_query, quote_identifiers=True)
693+
if row_query_results:
694+
rowcount = row_query_results[0]
695+
else:
696+
return
697+
else:
692698
return
693699

694-
# Snowflake identifiers may be:
695-
# - An unquoted contiguous set of [a-zA-Z0-9_$] characters
696-
# - A double-quoted string that may contain spaces and nested double-quotes represented by `""`. Example: " my ""table"" name "
697-
# - Regex:
698-
# - [a-zA-Z0-9_$]+ matches one or more character in the set
699-
# - "(?:[^"]|"")+" matches a double-quoted string that may contain spaces and nested double-quotes
700-
# - ?: non-capturing group
701-
# - [^"] matches any single character except a double-quote
702-
# - | or
703-
# - "" matches two sequential double-quotes
704-
is_created = re.match(
705-
r'Table ([a-zA-Z0-9_$]+|"(?:[^"]|"")+") successfully created\.', results_str
706-
)
707-
is_already_exists = re.match(
708-
r'([a-zA-Z0-9_$]+|"(?:[^"]|"")+") already exists, statement succeeded\.',
709-
results_str,
710-
)
711-
if is_created or is_already_exists:
712-
return
713700
QueryExecutionTracker.record_execution(sql, rowcount, bytes_processed)

tests/core/engine_adapter/integration/test_integration.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,14 +2457,10 @@ def capture_execution_stats(
24572457
assert len(physical_layer_results.tables) == len(physical_layer_results.non_temp_tables) == 3
24582458

24592459
if ctx.engine_adapter.SUPPORTS_QUERY_EXECUTION_TRACKING:
2460-
assert actual_execution_stats["seed_model"].total_rows_processed == (
2461-
None if ctx.mark.startswith("snowflake") else 7
2462-
)
2460+
assert actual_execution_stats["seed_model"].total_rows_processed == 7
24632461
assert actual_execution_stats["incremental_model"].total_rows_processed == 7
24642462
# snowflake doesn't track rows for CTAS
2465-
assert actual_execution_stats["full_model"].total_rows_processed == (
2466-
None if ctx.mark.startswith("snowflake") else 3
2467-
)
2463+
assert actual_execution_stats["full_model"].total_rows_processed == 3
24682464

24692465
if ctx.mark.startswith("bigquery") or ctx.mark.startswith("databricks"):
24702466
assert actual_execution_stats["incremental_model"].total_bytes_processed is not None
@@ -2482,10 +2478,7 @@ def capture_execution_stats(
24822478

24832479
if ctx.engine_adapter.SUPPORTS_QUERY_EXECUTION_TRACKING:
24842480
assert actual_execution_stats["incremental_model"].total_rows_processed == 0
2485-
# snowflake doesn't track rows for CTAS
2486-
assert actual_execution_stats["full_model"].total_rows_processed == (
2487-
None if ctx.mark.startswith("snowflake") else 3
2488-
)
2481+
assert actual_execution_stats["full_model"].total_rows_processed == 3
24892482

24902483
# make and validate unmodified dev environment
24912484
no_change_plan: Plan = context.plan_builder(

0 commit comments

Comments
 (0)