tomsanbear opened a new pull request, #23098:
URL: https://github.com/apache/datafusion/pull/23098

   ## Which issue does this PR close?
   
   - Closes #23097.
   
   ## Rationale for this change
   
   `WITH RECURSIVE t(n) AS (...)` failed to plan because the CTE's declared 
column-list names (the `t(n)` part) were never applied to the recursive working 
relation. They were applied (via `apply_table_alias`) only after the whole CTE 
plan was built, but the working table is derived from the static term's schema 
*before* that — so the self-reference couldn't resolve the declared names and 
planning failed with `Schema error: No field named n. Valid fields are 
t."Int64(1)".`. PostgreSQL and DuckDB accept the query; aliasing inside the 
static `SELECT` (`SELECT 1 AS n`) was the only workaround.
   
   ## What changes are included in this PR?
   
   Apply the column-list aliases to the static term inside `recursive_cte()`, 
before the work table is created, so the working relation and the 
self-reference carry the declared names. The caller now applies only the 
relation-name alias on the recursive path (the columns are already applied), 
avoiding a redundant projection on top of the `RecursiveQuery` node. The 
non-UNION fallback applies the aliases directly; non-recursive CTEs are 
unchanged. A column/alias-count mismatch is now reported at the static term — a 
clearer error than the previous "No field named …".
   
   ## Are these changes tested?
   
   Yes, added `cte.slt` cases for single- and multi-column column-list 
recursive CTEs (asserting the recursion produces the expected rows), `UNION 
(DISTINCT)`, the arity-mismatch error, and an `EXPLAIN` locking the plan shape 
(no extra projection over `RecursiveQuery`).
   
   ## Are there any user-facing changes?
   
   `WITH RECURSIVE t(n) AS (...)` and multi-column column lists now plan and 
execute correctly, matching PostgreSQL/DuckDB. No API changes.
   
   *Note: this pull request was created together with AI tools (claude code), 
the full diff was reviewed by myself in full prior to submission*


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to