Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion sqlmesh/core/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,14 @@ def text_diff(
return "\n".join(unified_diff(a_sql, b_sql))


WS_OR_COMMENT = r"(?:\s|--[^\n]*\n|/\*.*?\*/)"
# Each '*/' closes exactly one '/*', so a run of comments matches in exactly one way. A lazy
# '/\*.*?\*/' under DOTALL can stretch across several comments, and a failed search then
# backtracks through every grouping of them. SQLGlot reads block comments as nested, so this
# matches nesting up to three levels deep; a regex cannot match arbitrary depth.
BLOCK_COMMENT = r"/\*(?:[^*]|\*(?!/))*\*/"
for _ in range(2):
BLOCK_COMMENT = rf"/\*(?:[^*/]|\*(?!/)|/(?!\*)|{BLOCK_COMMENT})*\*/"
WS_OR_COMMENT = rf"(?:\s|--[^\n]*\n|{BLOCK_COMMENT})"
HEADER = r"\b(?:model|audit)\b(?=\s*\()"
KEY_BOUNDARY = r"(?:\(|,)" # key is preceded by either '(' or ','
DIALECT_VALUE = r"['\"]?(?P<dialect>[a-z][a-z0-9]*)['\"]?"
Expand Down
20 changes: 20 additions & 0 deletions tests/core/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3151,6 +3151,26 @@ def assert_match(test_sql: str, expected_value: t.Optional[str] = "duckdb"):
"""
)

# a run of block comments before the dialect key
comments = "\n".join(f"/* note {i} */" for i in range(40))
assert_match(make_test_sql(f"tag my_tag, {comments}\ndialect duckdb"))

# a run of block comments and no dialect key: the search fails without backtracking
# through every grouping of the comments
test_sql = make_test_sql(f"tag my_tag, {comments}\ncron '@daily'")
matches = list(d.DIALECT_PATTERN.finditer(test_sql))
assert not matches

# nested block comments before the dialect key
assert_match(
make_test_sql(
"""
tag my_tag, /* outer /* inner /* innermost */ */ still outer */
dialect duckdb
"""
)
)

# full parse
sql = """
MODEL (
Expand Down