coerce_type discard unnecessary CollateExprs

Started by jian he27 days ago3 messageshackers
Jump to latest
#1jian he
jian.universality@gmail.com

Hi.

In coerce_to_target_type, we have comments like
/*
* Note that if there are multiple stacked CollateExprs, we just
discard all but the topmost.
*/
origexpr = expr;
while (expr && IsA(expr, CollateExpr))
expr = (Node *) ((CollateExpr *) expr)->arg;

Only the topmost CollateExpr will be used, all others will be dropped.
We can apply this to coerce_type also.
See the attached minor patch.

--
jian
https://www.enterprisedb.com/

Attachments:

v1-0001-coerce_type.patchapplication/x-patch; name=v1-0001-coerce_type.patchDownload+4-2
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: jian he (#1)
Re: coerce_type discard unnecessary CollateExprs

jian he <jian.universality@gmail.com> writes:

Only the topmost CollateExpr will be used, all others will be dropped.
We can apply this to coerce_type also.
See the attached minor patch.

It's not apparent to me that this does anything (will there ever
be a CollateExpr at this stage?), or that it's correct if it does.
Please provide an example. Not to mention a comment.

regards, tom lane

#3jian he
jian.universality@gmail.com
In reply to: Tom Lane (#2)
Re: coerce_type discard unnecessary CollateExprs

On Mon, Jun 29, 2026 at 9:38 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:

jian he <jian.universality@gmail.com> writes:

Only the topmost CollateExpr will be used, all others will be dropped.
We can apply this to coerce_type also.
See the attached minor patch.

It's not apparent to me that this does anything (will there ever
be a CollateExpr at this stage?), or that it's correct if it does.
Please provide an example. Not to mention a comment.

will there ever be a CollateExpr at this stage?)
Yes. We use coerce_to_target_type->coerce_type, but some places we
just use coerce_type directly.
For example ParseFuncOrColumn->make_fn_arguments->coerce_type

select to_char(1, 'x' collate "C" collate "POSIX");

Without the patch: After make_fn_arguments->coerce_type the second fargs is:
{COLLATEEXPR
:arg
{COLLATEEXPR
:arg
{CONST
:consttype 25
:consttypmod -1
:constcollid 100
:constlen -1
:constbyval false
:constisnull false
:location 18
:constvalue 5 [ 20 0 0 0 120 ]
}
:collOid 950
:location 22
}
:collOid 951
:location 34
}

With the patch:

{COLLATEEXPR
:arg
{CONST
:consttype 25
:consttypmod -1
:constcollid 100
:constlen -1
:constbyval false
:constisnull false
:location 18
:constvalue 5 [ 20 0 0 0 120 ]
}
:collOid 951
:location 34
}