Skip to content

Commit fb2cca8

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 5656f2c commit fb2cca8

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

src/backend/parser/parse_coerce.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype,
9595
* *must* know that to avoid possibly calling hide_coercion_node on
9696
* something that wasn't generated by coerce_type. Note that if there are
9797
* multiple stacked CollateExprs, we just discard all but the topmost.
98+
* Also, if the target type isn't collatable, we discard the CollateExpr.
9899
*/
99100
origexpr = expr;
100101
while (expr && IsA(expr, CollateExpr))
@@ -114,7 +115,7 @@ coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype,
114115
ccontext, cformat, location,
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;
@@ -382,20 +383,26 @@ coerce_type(ParseState *pstate, Node *node,
382383
{
383384
/*
384385
* If we have a COLLATE clause, we have to push the coercion
385-
* underneath the COLLATE. This is really ugly, but there is little
386-
* choice because the above hacks on Consts and Params wouldn't happen
386+
* underneath the COLLATE; or discard the COLLATE if the target type
387+
* isn't collatable. This is really ugly, but there is little choice
388+
* because the above hacks on Consts and Params wouldn't happen
387389
* otherwise. This kluge has consequences in coerce_to_target_type.
388390
*/
389391
CollateExpr *coll = (CollateExpr *) node;
390-
CollateExpr *newcoll = makeNode(CollateExpr);
391392

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

src/test/regress/expected/collate.out

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,11 +661,24 @@ SELECT collation for ((SELECT b FROM collate_test1 LIMIT 1));
661661
"C"
662662
(1 row)
663663

664+
-- old bug with not dropping COLLATE when coercing to non-collatable type
665+
CREATE VIEW collate_on_int AS
666+
SELECT c1+1 AS c1p FROM
667+
(SELECT ('4' COLLATE "C")::INT AS c1) ss;
668+
\d+ collate_on_int
669+
View "collate_tests.collate_on_int"
670+
Column | Type | Collation | Nullable | Default | Storage | Description
671+
--------+---------+-----------+----------+---------+---------+-------------
672+
c1p | integer | | | | plain |
673+
View definition:
674+
SELECT ss.c1 + 1 AS c1p
675+
FROM ( SELECT 4 AS c1) ss;
676+
664677
--
665678
-- Clean up. Many of these table names will be re-used if the user is
666679
-- trying to run any platform-specific collation tests later, so we
667680
-- must get rid of them.
668681
--
669682
\set VERBOSITY terse
670683
DROP SCHEMA collate_tests CASCADE;
671-
NOTICE: drop cascades to 17 other objects
684+
NOTICE: drop cascades to 18 other objects

src/test/regress/sql/collate.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,12 @@ SELECT collation for ('foo'::text);
253253
SELECT collation for ((SELECT a FROM collate_test1 LIMIT 1)); -- non-collatable type - error
254254
SELECT collation for ((SELECT b FROM collate_test1 LIMIT 1));
255255

256+
-- old bug with not dropping COLLATE when coercing to non-collatable type
257+
CREATE VIEW collate_on_int AS
258+
SELECT c1+1 AS c1p FROM
259+
(SELECT ('4' COLLATE "C")::INT AS c1) ss;
260+
\d+ collate_on_int
261+
256262

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

0 commit comments

Comments
 (0)