Skip to content

Commit f5635c2

Browse files
author
Richard Guo
committed
Fix expr_is_nonnullable() for IS DISTINCT FROM
Commit c8d2f68 taught expr_is_nonnullable() to treat a DistinctExpr as never returning NULL. That is wrong: when neither input is NULL, the result is NULL if the underlying equality operator returns NULL, which nothing forbids. This could lead to wrong query results. To fix, remove the DistinctExpr case, along with its test case. While at it, test the relkind before calling has_subclass() in var_is_nonnullable(), to avoid a useless syscache lookup for partitioned tables. Reported-by: Robert Haas <robertmhaas@gmail.com> Author: Richard Guo <guofenglinux@gmail.com> Discussion: https://postgr.es/m/CA+TgmoaLcsmc9weJr1yo1X1SwZJX1t4XXHj0SJzH8XztmN9Hdg@mail.gmail.com Backpatch-through: 19
1 parent 91f7521 commit f5635c2

3 files changed

Lines changed: 2 additions & 24 deletions

File tree

‎src/backend/optimizer/util/clauses.c‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4950,8 +4950,8 @@ var_is_nonnullable(PlannerInfo *root, Var *var, NotNullSource source)
49504950
* Note that we need to check if the relation actually has any
49514951
* children, as we might not have done that yet.
49524952
*/
4953-
if (rte->inh && has_subclass(rte->relid) &&
4954-
rte->relkind != RELKIND_PARTITIONED_TABLE)
4953+
if (rte->relkind != RELKIND_PARTITIONED_TABLE &&
4954+
rte->inh && has_subclass(rte->relid))
49554955
return false;
49564956

49574957
/* We need not lock the relation since it was already locked */
@@ -5100,14 +5100,6 @@ expr_is_nonnullable(PlannerInfo *root, Expr *expr, NotNullSource source)
51005100
*/
51015101
return true;
51025102
}
5103-
case T_DistinctExpr:
5104-
{
5105-
/*
5106-
* IS DISTINCT FROM never returns NULL, effectively acting as
5107-
* though NULL were a normal data value.
5108-
*/
5109-
return true;
5110-
}
51115103
case T_RelabelType:
51125104
{
51135105
/*

‎src/test/regress/expected/predicate.out‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -611,16 +611,6 @@ SELECT * FROM pred_tab WHERE ((a > 1) IS TRUE) IS NULL;
611611
One-Time Filter: false
612612
(3 rows)
613613

614-
-- DistinctExpr
615-
EXPLAIN (COSTS OFF)
616-
SELECT * FROM pred_tab WHERE (a IS DISTINCT FROM b) IS NULL;
617-
QUERY PLAN
618-
------------------------------
619-
Result
620-
Replaces: Scan on pred_tab
621-
One-Time Filter: false
622-
(3 rows)
623-
624614
-- RelabelType
625615
EXPLAIN (COSTS OFF)
626616
SELECT * FROM pred_tab WHERE (a::oid) IS NULL;

‎src/test/regress/sql/predicate.sql‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,6 @@ SELECT * FROM pred_tab WHERE (b IS NULL) IS NULL;
299299
EXPLAIN (COSTS OFF)
300300
SELECT * FROM pred_tab WHERE ((a > 1) IS TRUE) IS NULL;
301301

302-
-- DistinctExpr
303-
EXPLAIN (COSTS OFF)
304-
SELECT * FROM pred_tab WHERE (a IS DISTINCT FROM b) IS NULL;
305-
306302
-- RelabelType
307303
EXPLAIN (COSTS OFF)
308304
SELECT * FROM pred_tab WHERE (a::oid) IS NULL;

0 commit comments

Comments
 (0)