Re: COALESCE with single argument looks like identity function

From: Maksim Milyutin <maksim(dot)milyutin(at)tantorlabs(dot)ru>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: COALESCE with single argument looks like identity function
Date: 2025-04-14 14:25:53
Message-ID: 44cc359d-dda2-4159-96ae-38bbc017150e@tantorlabs.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 4/11/25 17:00, Tom Lane wrote:
> Maksim Milyutin<maksim(dot)milyutin(at)tantorlabs(dot)ru> writes:
>> I've noticed that COALESCE function doesn't converge to argument
>> expression if it is alone in argument list of COALESCE as part
>> simplification routine for expressions in planner. This might suppress
>> further useful transformations when non-strict ops are required from
>> some expression like converging OUTER JOIN to INNER one with WHERE qual
>> containing COALESCE over single column from inner side.
> Seems like a reasonable idea --- it's probably a rare case, but the
> check is cheap enough. I'd add some comments though.

Thanks for your comments.

> Please add this to the open commitfest so we don't lose track of it.

Done. In regression tests I've replaced all COALESCEs with single
argument to ones with dummy second argument to preserve coalesce calls
as AFAICS their usages are intentional for wrapping attributes to
generate PHVs above.

Also I've noticed the issue in query (in join.sql test suite):

SELECT 1 FROM group_tbl t1
    LEFT JOIN (SELECT a c1, *COALESCE(a)* c2 FROM group_tbl t2) s ON TRUE
GROUP BY s.c1, s.c2

repeatable t2.a in GROUP BY clauses are not converged to single appearance:

                 QUERY PLAN
--------------------------------------------
 Group
   Group Key: t2.a, *t2.a*
   ->  Sort
         Sort Key: t2.a, *t2.a*
         ->  Nested Loop Left Join
               ->  Seq Scan on group_tbl t1
               ->  Seq Scan on group_tbl t2

IMO the cause is in PHV surrounding s.c2 that differentiates its
internal expression with the same first grouping key.

--
Best regard,
Maksim Milyutin

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2025-04-14 14:26:42 Re: rounding_up
Previous Message Ranier Vilela 2025-04-14 14:21:38 Re: Add pg_get_injection_points() for information of injection points