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 <[email protected]> Reviewed-by: Richard Guo <[email protected]> Discussion: https://postgr.es/m/CAHg+QDcqPdd=2v0pq_onyj50oueqsqznqfaytp3rdoklbdx...@mail.gmail.com Backpatch-through: 18 Branch ------ REL_18_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/1132af22cf7d31c224d39bcf2b55287f42b945da Modified Files -------------- src/backend/optimizer/plan/planner.c | 62 ++++++++++++++++++++++++-- src/test/regress/expected/collate.icu.utf8.out | 51 +++++++++++++++++++++ src/test/regress/sql/collate.icu.utf8.sql | 15 +++++++ 3 files changed, 124 insertions(+), 4 deletions(-)
