Skip to content

Commit 2d59643

Browse files
committed
Account for collation when coercing the output of a SQL function.
Commit 913bbd8 overlooked that the result of coerce_to_target_type might need collation fixups. Per report from Andreas Joseph Krogh. Discussion: https://postgr.es/m/VisenaEmail.72.37d08ec2b8cb8fb5.17179940cd3@tc7-visena
1 parent 0516f94 commit 2d59643

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/backend/executor/functions.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "nodes/makefuncs.h"
2525
#include "nodes/nodeFuncs.h"
2626
#include "parser/parse_coerce.h"
27+
#include "parser/parse_collate.h"
2728
#include "parser/parse_func.h"
2829
#include "storage/proc.h"
2930
#include "tcop/utility.h"
@@ -1989,6 +1990,7 @@ coerce_fn_result_column(TargetEntry *src_tle,
19891990
-1);
19901991
if (cast_result == NULL)
19911992
return false;
1993+
assign_expr_collations(NULL, cast_result);
19921994
src_tle->expr = (Expr *) cast_result;
19931995
/* Make a Var referencing the possibly-modified TLE */
19941996
new_tle_expr = (Expr *) makeVarFromTargetEntry(1, src_tle);
@@ -2007,6 +2009,7 @@ coerce_fn_result_column(TargetEntry *src_tle,
20072009
-1);
20082010
if (cast_result == NULL)
20092011
return false;
2012+
assign_expr_collations(NULL, cast_result);
20102013
/* Did the coercion actually do anything? */
20112014
if (cast_result != (Node *) var)
20122015
*upper_tlist_nontrivial = true;

src/test/regress/expected/collate.out

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,18 @@ SELECT a, CAST(b AS varchar) FROM collate_test2 ORDER BY 2;
536536
3 | bbc
537537
(4 rows)
538538

539+
-- result of a SQL function
540+
CREATE FUNCTION vc (text) RETURNS text LANGUAGE sql
541+
AS 'select $1::varchar';
542+
SELECT a, b FROM collate_test1 ORDER BY a, vc(b);
543+
a | b
544+
---+-----
545+
1 | abc
546+
2 | Abc
547+
3 | bbc
548+
4 | ABD
549+
(4 rows)
550+
539551
-- polymorphism
540552
SELECT * FROM unnest((SELECT array_agg(b ORDER BY b) FROM collate_test1)) ORDER BY 1;
541553
unnest
@@ -682,7 +694,7 @@ SELECT collation for ((SELECT b FROM collate_test1 LIMIT 1));
682694
-- must get rid of them.
683695
--
684696
DROP SCHEMA collate_tests CASCADE;
685-
NOTICE: drop cascades to 17 other objects
697+
NOTICE: drop cascades to 18 other objects
686698
DETAIL: drop cascades to table collate_test1
687699
drop cascades to table collate_test_like
688700
drop cascades to table collate_test2
@@ -694,6 +706,7 @@ drop cascades to view collview1
694706
drop cascades to view collview2
695707
drop cascades to view collview3
696708
drop cascades to type testdomain
709+
drop cascades to function vc(text)
697710
drop cascades to function dup(anyelement)
698711
drop cascades to table collate_test20
699712
drop cascades to table collate_test21

src/test/regress/sql/collate.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ SELECT a, CAST(b AS varchar) FROM collate_test1 ORDER BY 2;
177177
SELECT a, CAST(b AS varchar) FROM collate_test2 ORDER BY 2;
178178

179179

180+
-- result of a SQL function
181+
182+
CREATE FUNCTION vc (text) RETURNS text LANGUAGE sql
183+
AS 'select $1::varchar';
184+
185+
SELECT a, b FROM collate_test1 ORDER BY a, vc(b);
186+
187+
180188
-- polymorphism
181189

182190
SELECT * FROM unnest((SELECT array_agg(b ORDER BY b) FROM collate_test1)) ORDER BY 1;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy