Consider the SQL statement 'SELECT (('1' COLLATE "C") ||(B'1'));' . 
Intuitively, the result will be '11' and the result is '11' in pg 13.2 release 
as well.

The function stack is make_fn_arguments -> coerce_type, which means that the 
param "Node *node" of function coerce_type could be a CollateExpr Node.
Let's look at your patch:

```
// node is ('1' COLLATE "C")
// targetType is varbit and it is non-collatable
if (IsA(node, CollateExpr) && type_is_collatable(targetTypeId))
{

// we will not reach here.

CollateExpr *coll = (CollateExpr *) node;
CollateExpr *newcoll = makeNode(CollateExpr);

....

// An error will be generated. "failed to find conversion function"

}

```

So I suggest:

```
// node is ('1' COLLATE "C")

if (IsA(node, CollateExpr))
   {
      CollateExpr *coll = (CollateExpr *) node;
      CollateExpr *newcoll = makeNode(CollateExpr);


      //targetType is varbit and it is non-collatable

      if (!type_is_collatable(targetTypeId)) {

         // try to convert '1'(string) to varbit

         // We do not make a new CollateExpr here, but don't forget to coerce 
coll->arg.

         return coerce_type(pstate, (Node *) coll->arg,
                        inputTypeId, targetTypeId, targetTypeMod,
                        ccontext, cformat, location);
      }
     ...
   }

```






________________________________
寄件者: Tom Lane <t...@sss.pgh.pa.us>
寄件日期: 2021年4月19日 1:46
收件者: Yulin PEI <ype...@connect.ust.hk>
副本: pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>
主旨: Re: 回复: Core dump happens when execute sql CREATE VIEW v1(c1) AS (SELECT 
('4' COLLATE "C")::INT FROM generate_series(1, 10));

Yulin PEI <ype...@connect.ust.hk> writes:
>     After several tests, I found that this patch do not fix the bug well.

What do you think is wrong with it?

> So the attachment is my patch and it works well as far as I tested.

This seems equivalent to the already-committed patch [1] except that
it wastes a makeNode call in the coerce-to-uncollatable-type case.

                        regards, tom lane

[1] 
https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=c402b02b9fb53aee2a26876de90a8f95f9a9be92

Reply via email to