]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Fix HAVING-to-WHERE pushdown for simple-CASE form
authorRichard Guo <rguo@postgresql.org>
Fri, 8 May 2026 01:57:50 +0000 (10:57 +0900)
committerRichard Guo <rguo@postgresql.org>
Fri, 8 May 2026 01:59:13 +0000 (10:59 +0900)
commit1132af22cf7d31c224d39bcf2b55287f42b945da
tree8215546abc5a7e87799dee6fcdbb44145d7e5afb
parent936d8974c3bcf4fc7163fcd1b403eea2adffa73e
Fix HAVING-to-WHERE pushdown for simple-CASE form

Commit f76686ce7 added a walker that detects when a HAVING clause uses
a collation that conflicts with the GROUP BY's nondeterministic
collation, keeping such clauses in HAVING.  The walker uses
exprInputCollation() to identify each ancestor's comparison collation,
but missed the simple-CASE case: parse analysis builds each WHEN as
OpExpr(CaseTestExpr op val), where CaseTestExpr is a placeholder for
the arg, while the actual arg expression sits at cexpr->arg, outside
the OpExpr that carries the comparison's inputcollid.  A GROUP Var at
cexpr->arg was therefore visited with the WHEN's inputcollid absent
from the ancestor stack, the conflict went undetected, and the clause
was wrongly pushed to WHERE.

Fix by handling simple CASE explicitly: before walking cexpr->arg,
push every WHEN's inputcollid onto the ancestor stack so a GROUP Var
at the arg is checked against the same collations the WHEN comparisons
would apply.  Then walk the WHEN bodies and defresult under the
unchanged stack, where their own collation contexts are picked up by
the default path.

Back-patch to v18 only; this fix extends the walker added by commit
f76686ce7 and inherits its dependency on the v18 RTE_GROUP mechanism.

Author: SATYANARAYANA NARLAPURAM <satyanarlapuram@gmail.com>
Reviewed-by: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/CAHg+QDcqPdd=2V0PQ_oNYj50OUeqSqznqFaYtP3RdokLBDXBqw@mail.gmail.com
Backpatch-through: 18
src/backend/optimizer/plan/planner.c
src/test/regress/expected/collate.icu.utf8.out
src/test/regress/sql/collate.icu.utf8.sql