Skip to content

Commit d425f85

Browse files
committed
Query info schema for snowflake CTAS num rows
1 parent cf75287 commit d425f85

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
@@ -683,30 +682,18 @@ def _record_execution_stats(
683682
If so, we return early and do not record the row count.
684683
"""
685684
if rowcount == 1:
686-
results = self.cursor.fetchall()
687-
if results and len(results) == 1:
688-
try:
689-
results_str = str(results[0][0])
690-
except (ValueError, TypeError):
685+
query_parsed = parse_one(sql, dialect=self.dialect)
686+
if isinstance(query_parsed, exp.Create):
687+
if query_parsed.expression and isinstance(query_parsed.expression, exp.Select):
688+
table = query_parsed.find(exp.Table)
689+
if table:
690+
row_query = f"SELECT ROW_COUNT as row_count FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{table.db}' AND TABLE_NAME = '{table.name}'"
691+
row_query_results = self.fetchone(row_query, quote_identifiers=True)
692+
if row_query_results:
693+
rowcount = row_query_results[0]
694+
else:
695+
return
696+
else:
691697
return
692698

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