Fix assert failure when planning setop subqueries with CTEs
authorDavid Rowley <[email protected]>
Mon, 1 Apr 2024 23:15:45 +0000 (12:15 +1300)
committerDavid Rowley <[email protected]>
Mon, 1 Apr 2024 23:15:45 +0000 (12:15 +1300)
commitd5d2205c8ddc6670fa87474e172fdfab162b7a73
tree6445b3d946f634d0a683650619f0b7cb7694859f
parent3622c8084643cc05bc8f28d0b238615c845eae14
Fix assert failure when planning setop subqueries with CTEs

66c0185a3 adjusted the UNION planner to request that union child queries
produce Paths correctly ordered to implement the UNION by way of
MergeAppend followed by Unique.  The code there made a bad assumption
that if the root->parent_root->parse had setOperations set that the
query must be the child subquery of a set operation.  That's not true
when it comes to planning a non-inlined CTE which is parented by a set
operation.  This causes issues as the CTE's targetlist has no
requirement to match up to the SetOperationStmt's groupClauses

Fix this by adding a new parameter to both subquery_planner() and
grouping_planner() to explicitly pass the SetOperationStmt only when
planning set operation child subqueries.

Thank you to Tom Lane for helping to rationalize the decision on the
best function signature for subquery_planner().

Reported-by: Alexander Lakhin
Discussion: https://postgr.es/m/242fc7c6-a8aa-2daf-ac4c-0a231e2619c1@gmail.com
src/backend/optimizer/path/allpaths.c
src/backend/optimizer/plan/planner.c
src/backend/optimizer/plan/subselect.c
src/backend/optimizer/prep/prepunion.c
src/include/optimizer/planner.h
src/test/regress/expected/union.out
src/test/regress/sql/union.sql