Re: Unsafe qual pushdown through DISTINCT with simple CASE expressions - Mailing list pgsql-hackers

From pgsql-hackers
Subject Re: Unsafe qual pushdown through DISTINCT with simple CASE expressions
Date
Msg-id 53911081-A871-4701-97B2-0906719D53E6@alibaba-inc.com
Whole thread
Responses Re: Fix \crosstabview to honor \pset display_true/display_false
List pgsql-hackers
> On Tue, Aug 18, 2026 at 8:54 AM Tender Wang <tndrwang@gmail.com> wrote:
> The attached patch fixes this by checking the CASE argument against
> the equality operator and input collation of each WHEN comparison
> using the existing grouping_check_operand() logic.

I confirmed that this is a real wrong-result issue, and this approach
looks correct to me.

One small issue with the patch:
+               Assert(IsA(cw->expr, OpExpr));
+               opexpr = (OpExpr *) cw->expr;

cw->expr is not guaranteed to be an OpExpr.  transformCaseExpr() calls
coerce_to_boolean() after constructing the "=" expression.  With a
user-defined "=" operator returning a non-boolean type and an assignment
cast from that type to boolean, cw->expr becomes a FuncExpr wrapping the
OpExpr.

Therefore, the assertion can fail in an assert build, while the cast is
unsafe in a non-assert build.  I think the non-OpExpr case should be
handled conservatively as a grouping conflict.

Best Regards,
Yuhang Qiu




Attachment

pgsql-hackers by date:

Previous
From: Alexander Lakhin
Date:
Subject: Re: datachecksums: handle invalid and dropped databases during enable
Next
From: "Hayato Kuroda (Fujitsu)"
Date:
Subject: RE: Proposal: Conflict log history table for Logical Replication