Skip to content

Commit 5d834df

Browse files
committed
Replace regular expression in VIEW converter with sqlglot tokenizer
1 parent 5341694 commit 5d834df

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## [0.60.0] - 2025-11-11
4+
5+
- Replaced regular expression in `VIEW` converter with sqlglot tokenizer.
6+
- Introduced new category for optional dependencies: "convert". Example: `pip install snowddl[convert]`. It is only required if you plan to use converters.
7+
38
## [0.59.1] - 2025-10-25
49

510
- Fixed issue with optional `mfa_enrollment` parameter for `AUTHENTICATION_POLICY`.

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ file = "README.md"
3636
content-type = "text/markdown"
3737

3838
[project.optional-dependencies]
39+
convert = [
40+
"sqlglot"
41+
]
3942
dev = [
4043
"black",
4144
"pytest",

snowddl/converter/view.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pathlib import Path
2-
from re import compile, DOTALL
2+
from sqlglot import Tokenizer, TokenType
33

44
from snowddl.blueprint import ObjectType
55
from snowddl.converter.abc_converter import ConvertResult
@@ -8,9 +8,6 @@
88
from snowddl.parser.view import view_json_schema
99

1010

11-
view_text_re = compile(r"^.*?\sas(\n|\s)+(.*)$", DOTALL)
12-
13-
1411
class ViewConverter(AbstractSchemaObjectConverter):
1512
def get_object_type(self) -> ObjectType:
1613
return ObjectType.VIEW
@@ -77,13 +74,8 @@ def _get_columns(self, row):
7774
return cols
7875

7976
def _get_text_or_include(self, object_path: Path, row: dict):
80-
# TODO: replace with better implementation when available
81-
cur = self.engine.execute_meta(
82-
"SELECT GET_DDL('VIEW', {ident}) AS view_ddl", {"ident": f"{row['database']}.{row['schema']}.{row['name']}"}
83-
)
84-
85-
view_ddl = cur.fetchone()["VIEW_DDL"]
86-
view_text = view_text_re.sub(r"\2", view_ddl).rstrip(";").strip(" \n\r\t")
77+
view_text = self._extract_view_text_after_as(row)
78+
view_text = view_text.rstrip(";").strip(" \n\r\t")
8779

8880
# Remove trailing spaces from each line to prevent output formatting issues
8981
# https://github.com/yaml/pyyaml/issues/411
@@ -96,3 +88,12 @@ def _get_text_or_include(self, object_path: Path, row: dict):
9688
return YamlIncludeStr(f"sql/{file_name}")
9789

9890
return YamlLiteralStr(view_text)
91+
92+
def _extract_view_text_after_as(self, row: dict):
93+
tokenizer = Tokenizer()
94+
95+
for token in tokenizer.tokenize(row["text"]):
96+
if token.token_type == TokenType.ALIAS:
97+
return row["text"][token.end+2:]
98+
99+
raise ValueError(f"Could not extract view text after 'AS' keyword using sqlglot tokenizer")

snowddl/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.59.1"
1+
__version__ = "0.60.0"

0 commit comments

Comments
 (0)