jonahgao commented on PR #10876: URL: https://github.com/apache/datafusion/pull/10876#issuecomment-2168277849
Hi @leoyvens , for the following query, the table reference in the subquery on the right side of the UNION will still be shadowed by the cte. ```sql (with t as (select 1) select * from t) union (select * from t); ``` The situation becomes more complicated when working with nested CTEs and recursive CTEs. A recursive CTE can reference itself internally, which is different from a non-recursive CTE. I think there might be three possible solutions. 1. The initial one. 2. Make this function not support CTE, that is, report an error when a CTE is detected. 3. Achieve full-fledged CTE support. This might require a structure similar to [PlannerContext](https://github.com/apache/datafusion/blob/cc60278f50eac33f9c8ea3509c171b4f2b008b27/datafusion/sql/src/planner.rs#L143) to store the visible CTEs within the current query scope. Each query has its own PlannerContext, which subqueries can inherit or override. We might also need to handle recursive CTEs specially. For the latter two solutions, I think it need to be implemented as a standalone function, meaning SeesionState should no longer use it. This is due to the lack of CTE support or the presence of redundant work. -- 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: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org