Skip to content

Commit 2c67329

Browse files
committed
fix(dialect): make extend_sqlglot idempotent
_override saved whatever was currently installed under _<name>, so a second extend_sqlglot() call saved the override itself as the original. The wrapper then called itself and every subsequent type/CAST parse raised RecursionError, with sqlglot's original method lost. _override now returns early when the override is already installed. Adds tests/core/test_dialect.py::test_extend_sqlglot_is_idempotent, which fails with RecursionError without the fix. Signed-off-by: Sanjay Santhanam <51058514+Sanjays2402@users.noreply.github.com>
1 parent e91c9b9 commit 2c67329

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

‎sqlmesh/core/dialect.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,10 @@ def _parse_interval_span(self: Parser, this: exp.Expr) -> exp.Interval:
782782

783783
def _override(klass: t.Type[Tokenizer | Parser], func: t.Callable) -> None:
784784
name = func.__name__
785+
if getattr(klass, name, None) is func:
786+
# Already overridden. Re-applying would save the override itself as the
787+
# "original", making the wrapper call itself and recurse infinitely.
788+
return
785789
setattr(klass, f"_{name}", getattr(klass, name))
786790
setattr(klass, name, func)
787791

‎tests/core/test_dialect.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,3 +1185,11 @@ def test_pipe_syntax():
11851185
ast.sql("bigquery")
11861186
== "SELECT * FROM (WITH __tmp1 AS (SELECT id FROM t2) SELECT * FROM __tmp1)"
11871187
)
1188+
1189+
1190+
def test_extend_sqlglot_is_idempotent():
1191+
# extend_sqlglot() runs at import time; calling it again must not re-wrap the
1192+
# already-installed overrides, otherwise they call themselves (RecursionError).
1193+
d.extend_sqlglot()
1194+
1195+
assert parse_one("SELECT CAST(1 AS INT)").sql() == "SELECT CAST(1 AS INT)"

0 commit comments

Comments
 (0)