fix(ibis): preserve RECURSIVE when rebuilding WITH clause in .sql() - #12118
Open
developer-rpai wants to merge 1 commit into
Open
developer-rpai wants to merge 1 commit into
developer-rpai wants to merge 1 commit into
Conversation
Fixes ibis-project#11922. SQLGlotCompiler.translate() pops the WITH clause off a parsed raw-SQL query and rebuilds it via .with_() when merging CTEs. Rebuilding dropped the RECURSIVE keyword, so a recursive query passed through .sql() (e.g. WITH RECURSIVE power ... ) compiled to a non-recursive WITH and DuckDB rejected it with a circular CTE reference error. Remember whether the original WITH clause was recursive before popping it and pass it through to .with_() so the keyword is preserved.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #11922.
Table.sql()parses raw SQL with sqlglot andSQLGlotCompiler.translate()then pops the query'sWITHclause off and rebuilds it via.with_()while merging CTEs. Rebuilding the clause from scratch silently dropped theRECURSIVEkeyword, so a recursive query likeWITH RECURSIVE power(a, b, c) AS (...)compiled to a plainWITH, and DuckDB rejected it with a circular CTE reference error (the same query works through.raw_sql(), which skips compilation).The fix remembers whether the original
WITHclause was recursive before popping it and passes that flag through to.with_(), soRECURSIVEis preserved. Non-recursive queries are unaffected.Tests:
test_sql_recursive_cteinibis/backends/duckdb/tests/test_client.pyusing the issue's repro query; it executes and returns the expected powers of 2..sql(), plain.sql()without CTEs, regular ibis expressions, and mixed recursive + non-recursive CTEs all still compile and execute correctly.ruff checkandruff format --checkpass on both changed files.