Skip to content

Commit 37d6d07

Browse files
committed
Throw error for indeterminate collation of an ORDER/GROUP/DISTINCT target.
This restores a parse error that was thrown (though only in the ORDER BY case) by the original collation patch. I had removed it in my recent revisions because it was thrown at a place where collations now haven't been computed yet; but I thought of another way to handle it. Throwing the error at parse time, rather than leaving it to be done at runtime, is good because a syntax error pointer is helpful for localizing the problem. We can reasonably assume that the comparison function for a collatable datatype will complain if it doesn't have a collation to use. Now the planner might choose to implement GROUP or DISTINCT via hashing, in which case no runtime error would actually occur, but it seems better to throw error consistently rather than let the error depend on what the planner chooses to do. Another possible objection is that the user might specify a nondefault sort operator that doesn't care about collation ... but that's surely an uncommon usage, and it wouldn't hurt him to throw in a COLLATE clause anyway. This change also makes the ORDER BY/GROUP BY/DISTINCT case more consistent with the UNION/INTERSECT/EXCEPT case, which was already coded to throw this error even though the same objections could be raised there.
1 parent 1192ba8 commit 37d6d07

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

src/backend/parser/parse_collate.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,30 @@ assign_collations_walker(Node *node, assign_collations_context *context)
533533
collation = loccontext.collation;
534534
strength = loccontext.strength;
535535
location = loccontext.location;
536+
537+
/*
538+
* Throw error if the collation is indeterminate for a TargetEntry
539+
* that is a sort/group target. We prefer to do this now, instead
540+
* of leaving the comparison functions to fail at runtime, because
541+
* we can give a syntax error pointer to help locate the problem.
542+
* There are some cases where there might not be a failure, for
543+
* example if the planner chooses to use hash aggregation instead
544+
* of sorting for grouping; but it seems better to predictably
545+
* throw an error. (Compare transformSetOperationTree, which will
546+
* throw error for indeterminate collation of set-op columns,
547+
* even though the planner might be able to implement the set-op
548+
* without sorting.)
549+
*/
550+
if (strength == COLLATE_CONFLICT &&
551+
((TargetEntry *) node)->ressortgroupref != 0)
552+
ereport(ERROR,
553+
(errcode(ERRCODE_COLLATION_MISMATCH),
554+
errmsg("collation mismatch between implicit collations \"%s\" and \"%s\"",
555+
get_collation_name(loccontext.collation),
556+
get_collation_name(loccontext.collation2)),
557+
errhint("You can choose the collation by applying the COLLATE clause to one or both expressions."),
558+
parser_errposition(context->pstate,
559+
loccontext.location2)));
536560
break;
537561
case T_RangeTblRef:
538562
case T_JoinExpr:

src/test/regress/expected/collate.linux.utf8.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,11 @@ select x || y from collate_test10; -- ok, because || is not collation aware
637637
HIJHIJ
638638
(2 rows)
639639

640+
select x, y from collate_test10 order by x || y; -- not so ok
641+
ERROR: collation mismatch between implicit collations "en_US.utf8" and "tr_TR.utf8"
642+
LINE 1: select x, y from collate_test10 order by x || y;
643+
^
644+
HINT: You can choose the collation by applying the COLLATE clause to one or both expressions.
640645
-- collation mismatch between recursive and non-recursive term
641646
WITH RECURSIVE foo(x) AS
642647
(SELECT x FROM (VALUES('a' COLLATE "en_US"),('b')) t(x)

src/test/regress/expected/collate.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,11 @@ select x || y from collate_test10; -- ok, because || is not collation aware
443443
HIJHIJ
444444
(2 rows)
445445

446+
select x, y from collate_test10 order by x || y; -- not so ok
447+
ERROR: collation mismatch between implicit collations "C" and "POSIX"
448+
LINE 1: select x, y from collate_test10 order by x || y;
449+
^
450+
HINT: You can choose the collation by applying the COLLATE clause to one or both expressions.
446451
-- collation mismatch between recursive and non-recursive term
447452
WITH RECURSIVE foo(x) AS
448453
(SELECT x FROM (VALUES('a' COLLATE "C"),('b')) t(x)

src/test/regress/sql/collate.linux.utf8.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ CREATE TABLE test_u AS SELECT a, b FROM collate_test1 UNION ALL SELECT a, b FROM
193193
-- ideally this would be a parse-time error, but for now it must be run-time:
194194
select x < y from collate_test10; -- fail
195195
select x || y from collate_test10; -- ok, because || is not collation aware
196+
select x, y from collate_test10 order by x || y; -- not so ok
196197

197198
-- collation mismatch between recursive and non-recursive term
198199
WITH RECURSIVE foo(x) AS

src/test/regress/sql/collate.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ CREATE TABLE test_u AS SELECT a, b FROM collate_test1 UNION ALL SELECT a, b FROM
145145
-- ideally this would be a parse-time error, but for now it must be run-time:
146146
select x < y from collate_test10; -- fail
147147
select x || y from collate_test10; -- ok, because || is not collation aware
148+
select x, y from collate_test10 order by x || y; -- not so ok
148149

149150
-- collation mismatch between recursive and non-recursive term
150151
WITH RECURSIVE foo(x) AS

0 commit comments

Comments
 (0)