agora inbox for pgsql-committers@postgresql.orghelp / color / mirror / Atom feed
pgsql: Fix qual pushdown past grouping through simple CASE 3+ messages / 1 participants [nested] [flat]
* pgsql: Fix qual pushdown past grouping through simple CASE @ 2026-09-02 06:35 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 3+ messages in thread From: Richard Guo @ 2026-09-02 06:35 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Fix qual pushdown past grouping through simple CASE Commit 44fb59fc6 taught the grouping-conflict walker to treat the arg of a simple CASE as a direct operand of each WHEN comparison, but it only checked the collation, on the assumption that the WHEN operator is always the type-default "=" and thus matches the grouping eqop. That assumption fails once the arg is relabeled to another type: the WHEN then compares under that type's "=", which need not agree with the grouping equality. For instance, with a DISTINCT over a citext column, a qual such as "CASE t::text WHEN 'A' THEN ..." was pushed below the Unique, although the equivalent "t::text = 'A'" is correctly kept above it. Instead of special-casing the arg, have the walker bind a Var arg while walking the WHEN conditions and resolve each CaseTestExpr to it, so that the arg is checked exactly as each WHEN uses it: with the opfamily and collation checks of a direct operand when the WHEN is a comparison, and as a non-operand reference otherwise. A non-Var arg is walked once as a non-operand, as before. The CaseTestExpr in an ArrayCoerceExpr's elemexpr and a JsonConstructorExpr's coercion stand for something else and are left alone. Reported-by: Tender Wang <tndrwang@gmail.com> Author: Richard Guo <guofenglinux@gmail.com> Reviewed-by: Tender Wang <tndrwang@gmail.com> Reviewed-by: Ewan Young <kdbase.hack@gmail.com> Discussion: https://postgr.es/m/CAHewXNkvGTOgijRLjmudpg=wz0d-J8ChY2o7ksh3Be+Q_Bxwog@mail.gmail.com Backpatch-through: 18 Branch ------ master Details ------- https://git.postgresql.org/pg/commitdiff/264ddc9a678c41fca3435f26d7c581fd2b034cf8 Modified Files -------------- src/backend/optimizer/util/clauses.c | 127 +++++++++++++++++-------- src/test/regress/expected/collate.icu.utf8.out | 35 +++++++ src/test/regress/expected/subselect.out | 27 ++++++ src/test/regress/sql/collate.icu.utf8.sql | 12 +++ src/test/regress/sql/subselect.sql | 11 +++ 5 files changed, 171 insertions(+), 41 deletions(-) ^ permalink raw reply [nested|flat] 3+ messages in thread
* pgsql: Fix qual pushdown past grouping through simple CASE @ 2026-09-02 06:35 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 3+ messages in thread From: Richard Guo @ 2026-09-02 06:35 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Fix qual pushdown past grouping through simple CASE Commit 44fb59fc6 taught the grouping-conflict walker to treat the arg of a simple CASE as a direct operand of each WHEN comparison, but it only checked the collation, on the assumption that the WHEN operator is always the type-default "=" and thus matches the grouping eqop. That assumption fails once the arg is relabeled to another type: the WHEN then compares under that type's "=", which need not agree with the grouping equality. For instance, with a DISTINCT over a citext column, a qual such as "CASE t::text WHEN 'A' THEN ..." was pushed below the Unique, although the equivalent "t::text = 'A'" is correctly kept above it. Instead of special-casing the arg, have the walker bind a Var arg while walking the WHEN conditions and resolve each CaseTestExpr to it, so that the arg is checked exactly as each WHEN uses it: with the opfamily and collation checks of a direct operand when the WHEN is a comparison, and as a non-operand reference otherwise. A non-Var arg is walked once as a non-operand, as before. The CaseTestExpr in an ArrayCoerceExpr's elemexpr and a JsonConstructorExpr's coercion stand for something else and are left alone. Reported-by: Tender Wang <tndrwang@gmail.com> Author: Richard Guo <guofenglinux@gmail.com> Reviewed-by: Tender Wang <tndrwang@gmail.com> Reviewed-by: Ewan Young <kdbase.hack@gmail.com> Discussion: https://postgr.es/m/CAHewXNkvGTOgijRLjmudpg=wz0d-J8ChY2o7ksh3Be+Q_Bxwog@mail.gmail.com Backpatch-through: 18 Branch ------ REL_19_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/a3bc7f2c0de8ad5c6ae746cb6c1c5c187a60c124 Modified Files -------------- src/backend/optimizer/util/clauses.c | 127 +++++++++++++++++-------- src/test/regress/expected/collate.icu.utf8.out | 35 +++++++ src/test/regress/expected/subselect.out | 27 ++++++ src/test/regress/sql/collate.icu.utf8.sql | 12 +++ src/test/regress/sql/subselect.sql | 11 +++ 5 files changed, 171 insertions(+), 41 deletions(-) ^ permalink raw reply [nested|flat] 3+ messages in thread
* pgsql: Fix qual pushdown past grouping through simple CASE @ 2026-09-02 06:35 Richard Guo <rguo@postgresql.org> 0 siblings, 0 replies; 3+ messages in thread From: Richard Guo @ 2026-09-02 06:35 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org Fix qual pushdown past grouping through simple CASE Commit 44fb59fc6 taught the grouping-conflict walker to treat the arg of a simple CASE as a direct operand of each WHEN comparison, but it only checked the collation, on the assumption that the WHEN operator is always the type-default "=" and thus matches the grouping eqop. That assumption fails once the arg is relabeled to another type: the WHEN then compares under that type's "=", which need not agree with the grouping equality. For instance, with a DISTINCT over a citext column, a qual such as "CASE t::text WHEN 'A' THEN ..." was pushed below the Unique, although the equivalent "t::text = 'A'" is correctly kept above it. Instead of special-casing the arg, have the walker bind a Var arg while walking the WHEN conditions and resolve each CaseTestExpr to it, so that the arg is checked exactly as each WHEN uses it: with the opfamily and collation checks of a direct operand when the WHEN is a comparison, and as a non-operand reference otherwise. A non-Var arg is walked once as a non-operand, as before. The CaseTestExpr in an ArrayCoerceExpr's elemexpr and a JsonConstructorExpr's coercion stand for something else and are left alone. Reported-by: Tender Wang <tndrwang@gmail.com> Author: Richard Guo <guofenglinux@gmail.com> Reviewed-by: Tender Wang <tndrwang@gmail.com> Reviewed-by: Ewan Young <kdbase.hack@gmail.com> Discussion: https://postgr.es/m/CAHewXNkvGTOgijRLjmudpg=wz0d-J8ChY2o7ksh3Be+Q_Bxwog@mail.gmail.com Backpatch-through: 18 Branch ------ REL_18_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/dd7c135bb97ec7a5dad09fa9e1e2dd9c044ceee8 Modified Files -------------- src/backend/optimizer/util/clauses.c | 127 +++++++++++++++++-------- src/test/regress/expected/collate.icu.utf8.out | 35 +++++++ src/test/regress/expected/subselect.out | 27 ++++++ src/test/regress/sql/collate.icu.utf8.sql | 12 +++ src/test/regress/sql/subselect.sql | 11 +++ 5 files changed, 171 insertions(+), 41 deletions(-) ^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2026-09-02 06:35 UTC | newest] Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2026-09-02 06:35 pgsql: Fix qual pushdown past grouping through simple CASE Richard Guo <rguo@postgresql.org> 2026-09-02 06:35 pgsql: Fix qual pushdown past grouping through simple CASE Richard Guo <rguo@postgresql.org> 2026-09-02 06:35 pgsql: Fix qual pushdown past grouping through simple CASE Richard Guo <rguo@postgresql.org>
This inbox is served by agora; see mirroring instructions for how to clone and mirror all data and code used for this inbox