Skip to content

Commit 44a4a52

Browse files
committed
Compare collations before merging UNION operations.
In the dim past we figured it was okay to ignore collations when combining UNION set-operation nodes into a single N-way UNION operation. I believe that was fine at the time, but it stopped being fine when we added nondeterministic collations: the semantics of distinct-ness are affected by those. v17 made it even less fine by allowing per-child sorting operations to be merged via MergeAppend, although I think we accidentally avoided any live bug from that. Add a check that collations match before deciding that two UNION nodes are equivalent. I also failed to resist the temptation to comment plan_union_children() a little better. Back-patch to all supported branches (v13 now), since they all have nondeterministic collations. Discussion: https://postgr.es/m/3605568.1731970579@sss.pgh.pa.us
1 parent 4f5b148 commit 44a4a52

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/backend/optimizer/prep/prepunion.c

+11-12
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,9 @@ generate_union_paths(SetOperationStmt *op, PlannerInfo *root,
570570

571571
/*
572572
* If any of my children are identical UNION nodes (same op, all-flag, and
573-
* colTypes) then they can be merged into this node so that we generate
574-
* only one Append and unique-ification for the lot. Recurse to find such
575-
* nodes and compute their children's paths.
573+
* colTypes/colCollations) then they can be merged into this node so that
574+
* we generate only one Append and unique-ification for the lot. Recurse
575+
* to find such nodes and compute their children's paths.
576576
*/
577577
rellist = plan_union_children(root, op, refnames_tlist, &tlist_list);
578578

@@ -861,17 +861,15 @@ generate_nonunion_paths(SetOperationStmt *op, PlannerInfo *root,
861861
}
862862

863863
/*
864-
* Pull up children of a UNION node that are identically-propertied UNIONs.
864+
* Pull up children of a UNION node that are identically-propertied UNIONs,
865+
* and perform planning of the queries underneath the N-way UNION.
866+
*
867+
* The result is a list of RelOptInfos containing Paths for sub-nodes, with
868+
* one entry for each descendant that is a leaf query or non-identical setop.
869+
* We also return a parallel list of the childrens' targetlists.
865870
*
866871
* NOTE: we can also pull a UNION ALL up into a UNION, since the distinct
867872
* output rows will be lost anyway.
868-
*
869-
* NOTE: currently, we ignore collations while determining if a child has
870-
* the same properties. This is semantically sound only so long as all
871-
* collations have the same notion of equality. It is valid from an
872-
* implementation standpoint because we don't care about the ordering of
873-
* a UNION child's result: UNION ALL results are always unordered, and
874-
* generate_union_paths will force a fresh sort if the top level is a UNION.
875873
*/
876874
static List *
877875
plan_union_children(PlannerInfo *root,
@@ -897,7 +895,8 @@ plan_union_children(PlannerInfo *root,
897895

898896
if (op->op == top_union->op &&
899897
(op->all == top_union->all || op->all) &&
900-
equal(op->colTypes, top_union->colTypes))
898+
equal(op->colTypes, top_union->colTypes) &&
899+
equal(op->colCollations, top_union->colCollations))
901900
{
902901
/* Same UNION, so fold children into parent */
903902
pending_rels = lcons(op->rarg, pending_rels);

0 commit comments

Comments
 (0)