Re: Unsafe qual pushdown through DISTINCT with simple CASE expressions

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Ewan Young <kdbase(dot)hack(at)gmail(dot)com>
Cc: Tender Wang <tndrwang(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Unsafe qual pushdown through DISTINCT with simple CASE expressions
Date: 2026-09-02 06:52:17
Message-ID: CAMbWs4-Rh24Ay8b8pjLyaXWVzY0ZY7jQ7Q3=19h2=2LP3A7JEg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, Aug 28, 2026 at 4:15 PM Ewan Young <kdbase(dot)hack(at)gmail(dot)com> wrote:
> CREATE COLLATION ci (provider=icu, locale='und-u-ks-level2',
> deterministic=false);
> CREATE TABLE cit (t text COLLATE ci);
> INSERT INTO cit VALUES ('a'),('A'),('b');
>
> -- pushed on master, but v2 keeps the Filter above the grouping
> EXPLAIN (COSTS OFF)
> SELECT * FROM (SELECT DISTINCT t FROM cit) d
> WHERE (CASE t WHEN JSON_OBJECT('a': 'b' RETURNING text)
> THEN 1 ELSE 0 END) = 1;

Nice catch. I did not realize CaseTestExpr can be used in a JSON
constructor's coercion expression. I've fixed that and then pushed
the patch.

(Interestingly, eval_const_expressions also mishandles CaseTestExpr in
a JSON constructor's coercion expression, and thus has the same bug.
See [1].)

[1] https://postgr.es/m/CAMbWs48A=VCFbteTkuCoknO1_0-Cu0aMBT0M07dm7vj1QyixDg@mail.gmail.com

- Richard

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2026-09-02 07:25:29 Re: [PATCH] Test coverage for pg_clear_attribute_stats() null arguments
Previous Message Kiran Kaki 2026-09-02 06:25:24 Re: WAIT FOR NO_THROW option could use some documentation