Skip to content

Commit a6158a4

Browse files
committed
Fix old bug with coercing the result of a COLLATE expression.
There are hacks in parse_coerce.c to push down a requested coercion to below any CollateExpr that may appear. However, we did that even if the requested data type is non-collatable, leading to an invalid expression tree in which CollateExpr is applied to a non-collatable type. The fix is just to drop the CollateExpr altogether, reasoning that it's useless. This bug is ten years old, dating to the original addition of COLLATE support. The lack of field complaints suggests that there aren't a lot of user-visible consequences. We noticed the problem because it would trigger an assertion in DefineVirtualRelation if the invalid structure appears as an output column of a view; however, in a non-assert build, you don't see a crash just a (subtly incorrect) complaint about applying collation to a non-collatable type. I found that by putting the incorrect structure further down in a view, I could make a view definition that would fail dump/reload, per the added regression test case. But CollateExpr doesn't do anything at run-time, so this likely doesn't lead to any really exciting consequences. Per report from Yulin Pei. Back-patch to all supported branches. Discussion: https://postgr.es/m/HK0PR01MB22744393C474D503E16C8509F4709@HK0PR01MB2274.apcprd01.prod.exchangelabs.com
1 parent 6540322 commit a6158a4

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

src/backend/parser/parse_coerce.c

+18-11
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype,
9494
* *must* know that to avoid possibly calling hide_coercion_node on
9595
* something that wasn't generated by coerce_type. Note that if there are
9696
* multiple stacked CollateExprs, we just discard all but the topmost.
97+
* Also, if the target type isn't collatable, we discard the CollateExpr.
9798
*/
9899
origexpr = expr;
99100
while (expr && IsA(expr, CollateExpr))
@@ -114,7 +115,7 @@ coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype,
114115
(cformat != COERCE_IMPLICIT_CAST),
115116
(result != expr && !IsA(result, Const)));
116117

117-
if (expr != origexpr)
118+
if (expr != origexpr && type_is_collatable(targettype))
118119
{
119120
/* Reinstall top CollateExpr */
120121
CollateExpr *coll = (CollateExpr *) origexpr;
@@ -381,20 +382,26 @@ coerce_type(ParseState *pstate, Node *node,
381382
{
382383
/*
383384
* If we have a COLLATE clause, we have to push the coercion
384-
* underneath the COLLATE. This is really ugly, but there is little
385-
* choice because the above hacks on Consts and Params wouldn't happen
385+
* underneath the COLLATE; or discard the COLLATE if the target type
386+
* isn't collatable. This is really ugly, but there is little choice
387+
* because the above hacks on Consts and Params wouldn't happen
386388
* otherwise. This kluge has consequences in coerce_to_target_type.
387389
*/
388390
CollateExpr *coll = (CollateExpr *) node;
389-
CollateExpr *newcoll = makeNode(CollateExpr);
390391

391-
newcoll->arg = (Expr *)
392-
coerce_type(pstate, (Node *) coll->arg,
393-
inputTypeId, targetTypeId, targetTypeMod,
394-
ccontext, cformat, location);
395-
newcoll->collOid = coll->collOid;
396-
newcoll->location = coll->location;
397-
return (Node *) newcoll;
392+
result = coerce_type(pstate, (Node *) coll->arg,
393+
inputTypeId, targetTypeId, targetTypeMod,
394+
ccontext, cformat, location);
395+
if (type_is_collatable(targetTypeId))
396+
{
397+
CollateExpr *newcoll = makeNode(CollateExpr);
398+
399+
newcoll->arg = (Expr *) result;
400+
newcoll->collOid = coll->collOid;
401+
newcoll->location = coll->location;
402+
result = (Node *) newcoll;
403+
}
404+
return result;
398405
}
399406
pathtype = find_coercion_pathway(targetTypeId, inputTypeId, ccontext,
400407
&funcId);

src/test/regress/expected/collate.out

+15-1
Original file line numberDiff line numberDiff line change
@@ -645,13 +645,26 @@ SELECT collation for ((SELECT b FROM collate_test1 LIMIT 1));
645645
"C"
646646
(1 row)
647647

648+
-- old bug with not dropping COLLATE when coercing to non-collatable type
649+
CREATE VIEW collate_on_int AS
650+
SELECT c1+1 AS c1p FROM
651+
(SELECT ('4' COLLATE "C")::INT AS c1) ss;
652+
\d+ collate_on_int
653+
View "collate_tests.collate_on_int"
654+
Column | Type | Modifiers | Storage | Description
655+
--------+---------+-----------+---------+-------------
656+
c1p | integer | | plain |
657+
View definition:
658+
SELECT ss.c1 + 1 AS c1p
659+
FROM ( SELECT 4 AS c1) ss;
660+
648661
--
649662
-- Clean up. Many of these table names will be re-used if the user is
650663
-- trying to run any platform-specific collation tests later, so we
651664
-- must get rid of them.
652665
--
653666
DROP SCHEMA collate_tests CASCADE;
654-
NOTICE: drop cascades to 15 other objects
667+
NOTICE: drop cascades to 16 other objects
655668
DETAIL: drop cascades to table collate_test1
656669
drop cascades to table collate_test_like
657670
drop cascades to table collate_test2
@@ -667,3 +680,4 @@ drop cascades to function dup(anyelement)
667680
drop cascades to table collate_test20
668681
drop cascades to table collate_test21
669682
drop cascades to table collate_test22
683+
drop cascades to view collate_on_int

src/test/regress/sql/collate.sql

+6
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,12 @@ SELECT collation for ('foo'::text);
240240
SELECT collation for ((SELECT a FROM collate_test1 LIMIT 1)); -- non-collatable type - error
241241
SELECT collation for ((SELECT b FROM collate_test1 LIMIT 1));
242242

243+
-- old bug with not dropping COLLATE when coercing to non-collatable type
244+
CREATE VIEW collate_on_int AS
245+
SELECT c1+1 AS c1p FROM
246+
(SELECT ('4' COLLATE "C")::INT AS c1) ss;
247+
\d+ collate_on_int
248+
243249

244250
--
245251
-- Clean up. Many of these table names will be re-used if the user is

0 commit comments

Comments
 (0)