Skip to content

Conversation

@nightscape
Copy link
Contributor

Fixes a panic when a UNION ALL query uses multiple CTEs where one CTE
references a column that exists in another CTE's source table but not
in its own.

Example that panicked:

WITH cte1 AS (SELECT * FROM t1),    -- t1 has (a, x)
     cte2 AS (SELECT *, x FROM t2)  -- t2 has (b), x doesn't exist
SELECT * FROM cte1 UNION ALL SELECT * FROM cte2;

The issue was that CTEs were added to outer_query_refs for table lookup
(allowing FROM cte1 inside another CTE), but this also made their columns
available for resolution. Now CTE columns are only accessible when the CTE
is explicitly in the FROM clause.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a panic that occurred when multiple CTEs were used in a UNION ALL query, where one CTE attempted to reference a column that exists in another CTE's source table but not in its own source table. The fix ensures that CTEs added to outer_query_refs are only used for table lookup (allowing FROM cte1 references) but not for implicit column resolution.

  • Modified column resolution logic to skip CTEs when searching outer_query_refs for unqualified column references
  • Added a test case that reproduces the panic scenario and validates the fix produces a proper "no such column" error

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
core/translate/expr.rs Added logic to skip Table::FromClauseSubquery entries in outer_query_refs during unqualified column resolution, preventing incorrect column binding
testing/select.test Added test case select-cte-union-all-column-resolution that verifies the fix by testing column resolution in CTEs with UNION ALL

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant