|
2 | 2 |
|
3 | 3 | import contextlib |
4 | 4 | import logging |
5 | | -import re |
6 | 5 | import typing as t |
7 | 6 |
|
8 | | -from sqlglot import exp |
| 7 | +from sqlglot import exp, parse_one |
9 | 8 | from sqlglot.helper import ensure_list |
10 | 9 | from sqlglot.optimizer.normalize_identifiers import normalize_identifiers |
11 | 10 | from sqlglot.optimizer.qualify_columns import quote_identifiers |
@@ -683,30 +682,18 @@ def _record_execution_stats( |
683 | 682 | If so, we return early and do not record the row count. |
684 | 683 | """ |
685 | 684 | 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: |
691 | 697 | return |
692 | 698 |
|
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 |
712 | 699 | QueryExecutionTracker.record_execution(sql, rowcount, bytes_processed) |
0 commit comments