Skip to content

Commit 85b47c7

Browse files
committed
Query info schema for snowflake CTAS num rows
1 parent 216d874 commit 85b47c7

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
@@ -2418,14 +2418,10 @@ def capture_execution_stats(
24182418
assert len(physical_layer_results.tables) == len(physical_layer_results.non_temp_tables) == 3
24192419

24202420
if ctx.engine_adapter.SUPPORTS_QUERY_EXECUTION_TRACKING:
2421-
assert actual_execution_stats["seed_model"].total_rows_processed == (
2422-
None if ctx.mark.startswith("snowflake") else 7
2423-
)
2421+
assert actual_execution_stats["seed_model"].total_rows_processed == 7
24242422
assert actual_execution_stats["incremental_model"].total_rows_processed == 7
24252423
# snowflake doesn't track rows for CTAS
2426-
assert actual_execution_stats["full_model"].total_rows_processed == (
2427-
None if ctx.mark.startswith("snowflake") else 3
2428-
)
2424+
assert actual_execution_stats["full_model"].total_rows_processed == 3
24292425

24302426
if ctx.mark.startswith("bigquery") or ctx.mark.startswith("databricks"):
24312427
assert actual_execution_stats["incremental_model"].total_bytes_processed is not None
@@ -2443,10 +2439,7 @@ def capture_execution_stats(
24432439

24442440
if ctx.engine_adapter.SUPPORTS_QUERY_EXECUTION_TRACKING:
24452441
assert actual_execution_stats["incremental_model"].total_rows_processed == 0
2446-
# snowflake doesn't track rows for CTAS
2447-
assert actual_execution_stats["full_model"].total_rows_processed == (
2448-
None if ctx.mark.startswith("snowflake") else 3
2449-
)
2442+
assert actual_execution_stats["full_model"].total_rows_processed == 3
24502443

24512444
# make and validate unmodified dev environment
24522445
no_change_plan: Plan = context.plan_builder(

0 commit comments

Comments
 (0)