Adjust collation determination rules as per discussion.

Remove crude hack that tried to propagate collation through a
function-returning-record, ie, from the function's arguments to individual
fields selected from its result record.  That is just plain inconsistent,
because the function result is composite and cannot have a collation;
and there's no hope of making this kind of action-at-a-distance work
consistently.  Adjust regression test cases that expected this to happen.

Meanwhile, the behavior of casting to a domain with a declared collation
stays the same as it was, since that seemed to be the consensus.
This commit is contained in:
Tom Lane 2011-04-09 14:40:09 -04:00
parent 7c76906b7e
commit a19002d4e5
7 changed files with 55 additions and 72 deletions

View File

@ -289,10 +289,11 @@ assign_collations_walker(Node *node, assign_collations_context *context)
case T_FieldSelect:
{
/*
* FieldSelect is a special case because the field may have
* a non-default collation, in which case we should use that.
* The field's collation was already looked up and saved
* in the node.
* For FieldSelect, the result has the field's declared
* collation, independently of what happened in the arguments.
* (The immediate argument must be composite and thus not
* collatable, anyhow.) The field's collation was already
* looked up and saved in the node.
*/
FieldSelect *expr = (FieldSelect *) node;
@ -304,24 +305,6 @@ assign_collations_walker(Node *node, assign_collations_context *context)
if (OidIsValid(expr->resultcollid))
{
/* Node's result type is collatable. */
if (expr->resultcollid == DEFAULT_COLLATION_OID)
{
/*
* The immediate input node necessarily yields a
* composite type, so it will have no exposed
* collation. However, if we are selecting a field
* from a function returning composite, see if we
* can bubble up a collation from the function's
* input. XXX this is a bit of a hack, rethink ...
*/
if (IsA(expr->arg, FuncExpr))
{
FuncExpr *fexpr = (FuncExpr *) expr->arg;
if (OidIsValid(fexpr->inputcollid))
expr->resultcollid = fexpr->inputcollid;
}
}
/* Pass up field's collation as an implicit choice. */
collation = expr->resultcollid;
strength = COLLATE_IMPLICIT;

View File

@ -1384,7 +1384,7 @@ ParseComplexProjection(ParseState *pstate, char *funcname, Node *first_arg,
fselect->fieldnum = i + 1;
fselect->resulttype = att->atttypid;
fselect->resulttypmod = att->atttypmod;
/* resultcollid may get overridden by parse_collate.c */
/* save attribute's collation for parse_collate.c */
fselect->resultcollid = att->attcollation;
return (Node *) fselect;
}

View File

@ -1290,7 +1290,7 @@ ExpandRowReference(ParseState *pstate, Node *expr,
fselect->fieldnum = i + 1;
fselect->resulttype = att->atttypid;
fselect->resulttypmod = att->atttypmod;
/* resultcollid may get overridden by parse_collate.c */
/* save attribute's collation for parse_collate.c */
fselect->resultcollid = att->attcollation;
if (targetlist)

View File

@ -771,33 +771,33 @@ SELECT * FROM unnest((SELECT array_agg(b ORDER BY b) FROM collate_test3)) ORDER
äbc
(4 rows)
CREATE FUNCTION dup (f1 anyelement, f2 out anyelement, f3 out anyarray)
AS 'select $1, array[$1,$1]' LANGUAGE sql;
SELECT a, (dup(b)).* FROM collate_test1 ORDER BY 2;
a | f2 | f3
---+-----+-----------
1 | abc | {abc,abc}
4 | ABC | {ABC,ABC}
2 | äbc | {äbc,äbc}
3 | bbc | {bbc,bbc}
CREATE FUNCTION dup (anyelement) RETURNS anyelement
AS 'select $1' LANGUAGE sql;
SELECT a, dup(b) FROM collate_test1 ORDER BY 2;
a | dup
---+-----
1 | abc
4 | ABC
2 | äbc
3 | bbc
(4 rows)
SELECT a, (dup(b)).* FROM collate_test2 ORDER BY 2;
a | f2 | f3
---+-----+-----------
1 | abc | {abc,abc}
4 | ABC | {ABC,ABC}
3 | bbc | {bbc,bbc}
2 | äbc | {äbc,äbc}
SELECT a, dup(b) FROM collate_test2 ORDER BY 2;
a | dup
---+-----
1 | abc
4 | ABC
3 | bbc
2 | äbc
(4 rows)
SELECT a, (dup(b)).* FROM collate_test3 ORDER BY 2;
a | f2 | f3
---+-----+-----------
4 | ABC | {ABC,ABC}
1 | abc | {abc,abc}
3 | bbc | {bbc,bbc}
2 | äbc | {äbc,äbc}
SELECT a, dup(b) FROM collate_test3 ORDER BY 2;
a | dup
---+-----
4 | ABC
1 | abc
3 | bbc
2 | äbc
(4 rows)
-- indexes

View File

@ -502,24 +502,24 @@ SELECT * FROM unnest((SELECT array_agg(b ORDER BY b) FROM collate_test2)) ORDER
bbc
(4 rows)
CREATE FUNCTION dup (f1 anyelement, f2 out anyelement, f3 out anyarray)
AS 'select $1, array[$1,$1]' LANGUAGE sql;
SELECT a, (dup(b)).* FROM collate_test1 ORDER BY 2;
a | f2 | f3
---+-----+-----------
4 | ABD | {ABD,ABD}
2 | Abc | {Abc,Abc}
1 | abc | {abc,abc}
3 | bbc | {bbc,bbc}
CREATE FUNCTION dup (anyelement) RETURNS anyelement
AS 'select $1' LANGUAGE sql;
SELECT a, dup(b) FROM collate_test1 ORDER BY 2;
a | dup
---+-----
4 | ABD
2 | Abc
1 | abc
3 | bbc
(4 rows)
SELECT a, (dup(b)).* FROM collate_test2 ORDER BY 2;
a | f2 | f3
---+-----+-----------
4 | ABD | {ABD,ABD}
2 | Abc | {Abc,Abc}
1 | abc | {abc,abc}
3 | bbc | {bbc,bbc}
SELECT a, dup(b) FROM collate_test2 ORDER BY 2;
a | dup
---+-----
4 | ABD
2 | Abc
1 | abc
3 | bbc
(4 rows)
-- indexes

View File

@ -242,12 +242,12 @@ SELECT * FROM unnest((SELECT array_agg(b ORDER BY b) FROM collate_test1)) ORDER
SELECT * FROM unnest((SELECT array_agg(b ORDER BY b) FROM collate_test2)) ORDER BY 1;
SELECT * FROM unnest((SELECT array_agg(b ORDER BY b) FROM collate_test3)) ORDER BY 1;
CREATE FUNCTION dup (f1 anyelement, f2 out anyelement, f3 out anyarray)
AS 'select $1, array[$1,$1]' LANGUAGE sql;
CREATE FUNCTION dup (anyelement) RETURNS anyelement
AS 'select $1' LANGUAGE sql;
SELECT a, (dup(b)).* FROM collate_test1 ORDER BY 2;
SELECT a, (dup(b)).* FROM collate_test2 ORDER BY 2;
SELECT a, (dup(b)).* FROM collate_test3 ORDER BY 2;
SELECT a, dup(b) FROM collate_test1 ORDER BY 2;
SELECT a, dup(b) FROM collate_test2 ORDER BY 2;
SELECT a, dup(b) FROM collate_test3 ORDER BY 2;
-- indexes

View File

@ -168,11 +168,11 @@ SELECT a, CAST(b AS varchar) FROM collate_test2 ORDER BY 2;
SELECT * FROM unnest((SELECT array_agg(b ORDER BY b) FROM collate_test1)) ORDER BY 1;
SELECT * FROM unnest((SELECT array_agg(b ORDER BY b) FROM collate_test2)) ORDER BY 1;
CREATE FUNCTION dup (f1 anyelement, f2 out anyelement, f3 out anyarray)
AS 'select $1, array[$1,$1]' LANGUAGE sql;
CREATE FUNCTION dup (anyelement) RETURNS anyelement
AS 'select $1' LANGUAGE sql;
SELECT a, (dup(b)).* FROM collate_test1 ORDER BY 2;
SELECT a, (dup(b)).* FROM collate_test2 ORDER BY 2;
SELECT a, dup(b) FROM collate_test1 ORDER BY 2;
SELECT a, dup(b) FROM collate_test2 ORDER BY 2;
-- indexes