回覆: 回复: Core dump happens when execute sql CREATE VIEW v1(c1) AS (SELECT ('4' COLLATE "C")::INT FROM generate_series(1, 10)); - Mailing list pgsql-hackers

From Yulin PEI
Subject 回覆: 回复: Core dump happens when execute sql CREATE VIEW v1(c1) AS (SELECT ('4' COLLATE "C")::INT FROM generate_series(1, 10));
Date
Msg-id HK0PR01MB227473FB7164AECC8C948F73F4499@HK0PR01MB2274.apcprd01.prod.exchangelabs.com
Whole thread Raw
In response to Re: 回复: Core dump happens when execute sql CREATE VIEW v1(c1) AS (SELECT ('4' COLLATE "C")::INT FROM generate_series(1, 10));  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: 回覆: 回复: Core dump happens when execute sql CREATE VIEW v1(c1) AS (SELECT ('4' COLLATE "C")::INT FROM generate_series(1, 10));  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
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 <tgl@sss.pgh.pa.us>
寄件日期: 2021年4月19日 1:46
收件者: Yulin PEI <ypeiae@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 <ypeiae@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

pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: Commit 86dc90056 - Rework planning and execution of UPDATE and DELETE
Next
From: Robert Haas
Date:
Subject: Re: [HACKERS] [PATCH] Caching for stable expressions with constant arguments v3