]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Teach expr_is_nonnullable() to handle more expression types
authorRichard Guo <rguo@postgresql.org>
Wed, 24 Dec 2025 09:00:44 +0000 (18:00 +0900)
committerRichard Guo <rguo@postgresql.org>
Wed, 24 Dec 2025 09:00:44 +0000 (18:00 +0900)
commitc8d2f68cc800e02f858f8f59e3e9ba46ce43e880
tree154903a99c884b139bbd5c34f1626787b059cd1d
parentcb7b7ec7a1c6d8933b7e731be2dd101080822cf8
Teach expr_is_nonnullable() to handle more expression types

Currently, the function expr_is_nonnullable() checks only Const and
Var expressions to determine if an expression is non-nullable.  This
patch extends the detection logic to handle more expression types.

This can enable several downstream optimizations, such as reducing
NullTest quals to constant truth values (e.g., "COALESCE(var, 1) IS
NULL" becomes FALSE) and converting "COUNT(expr)" to the more
efficient "COUNT(*)" when the expression is proven non-nullable.

This breaks a test case in test_predtest.sql, since we now simplify
"ARRAY[] IS NULL" to constant FALSE, preventing it from weakly
refuting a strict ScalarArrayOpExpr ("x = any(ARRAY[])").  To ensure
the refutation logic is still exercised as intended, wrap the array
argument in opaque_array().

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Reviewed-by: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
Reviewed-by: David Rowley <dgrowleyml@gmail.com>
Reviewed-by: Matheus Alcantara <matheusssilv97@gmail.com>
Discussion: https://postgr.es/m/CAMbWs49UhPBjm+NRpxerjaeuFKyUZJ_AjM3NBcSYK2JgZ6VTEQ@mail.gmail.com
src/backend/optimizer/util/clauses.c
src/test/modules/test_predtest/expected/test_predtest.out
src/test/modules/test_predtest/sql/test_predtest.sql
src/test/regress/expected/predicate.out
src/test/regress/sql/predicate.sql