Skip to content

fix(ibis): preserve RECURSIVE when rebuilding WITH clause in .sql() - #12118

Open
developer-rpai wants to merge 1 commit into
ibis-project:mainfrom
developer-rpai:fix/ibis-cte-collision-11922
Open

developer-rpai wants to merge 1 commit into
ibis-project:mainfrom
developer-rpai:fix/ibis-cte-collision-11922

Conversation

@developer-rpai

Copy link
Copy Markdown

Fixes #11922.

Table.sql() parses raw SQL with sqlglot and SQLGlotCompiler.translate() then pops the query's WITH clause off and rebuilds it via .with_() while merging CTEs. Rebuilding the clause from scratch silently dropped the RECURSIVE keyword, so a recursive query like WITH RECURSIVE power(a, b, c) AS (...) compiled to a plain WITH, 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 WITH clause was recursive before popping it and passes that flag through to .with_(), so RECURSIVE is preserved. Non-recursive queries are unaffected.

Tests:

  • Added test_sql_recursive_cte in ibis/backends/duckdb/tests/test_client.py using the issue's repro query; it executes and returns the expected powers of 2.
  • Verified manually that non-recursive CTEs via .sql(), plain .sql() without CTEs, regular ibis expressions, and mixed recursive + non-recursive CTEs all still compile and execute correctly.
  • ruff check and ruff format --check pass on both changed files.

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.
@github-actions github-actions Bot added tests Issues or PRs related to tests duckdb The DuckDB backend sql Backends that generate SQL labels Sep 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duckdb The DuckDB backend sql Backends that generate SQL tests Issues or PRs related to tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Duplicate CTE Name in recursive .sql() query with duckdb backend

1 participant