From 166f5bc36808ae680f730077316d112cfb5043f6 Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Sat, 28 Nov 2026 12:29:55 -0500 Subject: [PATCH 2/2] Add row estimate tests for IS [NOT] DISTINCT FROM Cover joins, semijoins and restriction clauses with NULLs on both sides. Co-authored-by: Rahul Yadav Discussion: https://postgr.es/m/17545-a0ca4de888953169@postgresql.org --- src/test/regress/expected/planner_est.out | 87 +++++++++++++++++++++++ src/test/regress/sql/planner_est.sql | 53 ++++++++++++++ 2 files changed, 140 insertions(+) diff --git a/src/test/regress/expected/planner_est.out b/src/test/regress/expected/planner_est.out index 6a8d223831d..a3b279e8de8 100644 --- a/src/test/regress/expected/planner_est.out +++ b/src/test/regress/expected/planner_est.out @@ -224,4 +224,91 @@ EXPLAIN (COSTS OFF) SELECT * FROM char_table_1 WHERE c < 'Q'; Filter: (c < 'Q'::"char") (2 rows) +-- +-- Test IS [NOT] DISTINCT FROM join estimates with NULLs on both sides. Only +-- the top plan line is shown, since the rest is not stable across platforms. +-- +CREATE TEMP TABLE distinct_t1 AS + SELECT CASE WHEN i > 30 THEN i END AS a FROM generate_series(1, 100) i; +CREATE TEMP TABLE distinct_t2 AS + SELECT CASE WHEN i > 100 THEN i - 100 END AS a FROM generate_series(1, 200) i; +ANALYZE distinct_t1, distinct_t2; +-- Ensure pairs of NULLs are counted +SELECT * FROM explain_mask_costs($$ +SELECT * FROM distinct_t1 t1 JOIN distinct_t2 t2 ON t1.a IS NOT DISTINCT FROM t2.a;$$, +true, true, false, true) LIMIT 1; + explain_mask_costs +-------------------------------------------------------------------------- + Nested Loop (cost=N..N rows=3070 width=N) (actual rows=3070.00 loops=1) +(1 row) + +-- The equivalent OR clause should get about the same estimate +SELECT * FROM explain_mask_costs($$ +SELECT * FROM distinct_t1 t1 JOIN distinct_t2 t2 ON t1.a = t2.a OR (t1.a IS NULL AND t2.a IS NULL);$$, +true, true, false, true) LIMIT 1; + explain_mask_costs +-------------------------------------------------------------------------- + Nested Loop (cost=N..N rows=3060 width=N) (actual rows=3070.00 loops=1) +(1 row) + +-- Ensure pairs of NULLs are not counted +SELECT * FROM explain_mask_costs($$ +SELECT * FROM distinct_t1 t1 JOIN distinct_t2 t2 ON t1.a IS DISTINCT FROM t2.a;$$, +true, true, false, true) LIMIT 1; + explain_mask_costs +---------------------------------------------------------------------------- + Nested Loop (cost=N..N rows=16930 width=N) (actual rows=16930.00 loops=1) +(1 row) + +-- Ensure NULLs are matched in semijoins and antijoins +SELECT * FROM explain_mask_costs($$ +SELECT * FROM distinct_t2 t2 WHERE EXISTS + (SELECT 1 FROM distinct_t1 t1 WHERE t1.a IS NOT DISTINCT FROM t2.a);$$, +true, true, false, true) LIMIT 1; + explain_mask_costs +---------------------------------------------------------------------------------- + Nested Loop Semi Join (cost=N..N rows=170 width=N) (actual rows=170.00 loops=1) +(1 row) + +SELECT * FROM explain_mask_costs($$ +SELECT * FROM distinct_t2 t2 WHERE NOT EXISTS + (SELECT 1 FROM distinct_t1 t1 WHERE t1.a IS NOT DISTINCT FROM t2.a);$$, +true, true, false, true) LIMIT 1; + explain_mask_costs +-------------------------------------------------------------------------------- + Nested Loop Anti Join (cost=N..N rows=30 width=N) (actual rows=30.00 loops=1) +(1 row) + +-- Columns that are all NULL, have no NULLs, and are half NULL +CREATE TEMP TABLE distinct_t3 AS + SELECT NULL::int AS n, i AS v, CASE WHEN i % 2 = 0 THEN i END AS a + FROM generate_series(1, 100) i; +ANALYZE distinct_t3; +SELECT * FROM explain_mask_costs($$ +SELECT * FROM distinct_t1 t1 JOIN distinct_t3 t3 ON t1.a IS NOT DISTINCT FROM t3.n;$$, +true, true, false, true) LIMIT 1; + explain_mask_costs +-------------------------------------------------------------------------- + Nested Loop (cost=N..N rows=3000 width=N) (actual rows=3000.00 loops=1) +(1 row) + +SELECT * FROM explain_mask_costs($$ +SELECT * FROM distinct_t1 t1 JOIN distinct_t3 t3 ON t1.a IS NOT DISTINCT FROM t3.v;$$, +true, true, false, true) LIMIT 1; + explain_mask_costs +---------------------------------------------------------------------- + Nested Loop (cost=N..N rows=70 width=N) (actual rows=70.00 loops=1) +(1 row) + +-- Ensure pairs of NULLs are counted in restriction clauses too +SELECT * FROM explain_mask_costs($$ +SELECT * FROM distinct_t3 WHERE a IS NOT DISTINCT FROM n;$$, +true, true, false, true); + explain_mask_costs +---------------------------------------------------------------------------------- + Seq Scan on distinct_t3 (cost=N..N rows=50 width=N) (actual rows=50.00 loops=1) + Filter: (NOT (a IS DISTINCT FROM n)) + Rows Removed by Filter: 50 +(3 rows) + DROP FUNCTION explain_mask_costs(text, bool, bool, bool, bool); diff --git a/src/test/regress/sql/planner_est.sql b/src/test/regress/sql/planner_est.sql index 1da208eccce..54094e2e751 100644 --- a/src/test/regress/sql/planner_est.sql +++ b/src/test/regress/sql/planner_est.sql @@ -156,4 +156,57 @@ CREATE TEMP TABLE char_table_1 AS ANALYZE char_table_1; EXPLAIN (COSTS OFF) SELECT * FROM char_table_1 WHERE c < 'Q'; +-- +-- Test IS [NOT] DISTINCT FROM join estimates with NULLs on both sides. Only +-- the top plan line is shown, since the rest is not stable across platforms. +-- +CREATE TEMP TABLE distinct_t1 AS + SELECT CASE WHEN i > 30 THEN i END AS a FROM generate_series(1, 100) i; +CREATE TEMP TABLE distinct_t2 AS + SELECT CASE WHEN i > 100 THEN i - 100 END AS a FROM generate_series(1, 200) i; +ANALYZE distinct_t1, distinct_t2; + +-- Ensure pairs of NULLs are counted +SELECT * FROM explain_mask_costs($$ +SELECT * FROM distinct_t1 t1 JOIN distinct_t2 t2 ON t1.a IS NOT DISTINCT FROM t2.a;$$, +true, true, false, true) LIMIT 1; + +-- The equivalent OR clause should get about the same estimate +SELECT * FROM explain_mask_costs($$ +SELECT * FROM distinct_t1 t1 JOIN distinct_t2 t2 ON t1.a = t2.a OR (t1.a IS NULL AND t2.a IS NULL);$$, +true, true, false, true) LIMIT 1; + +-- Ensure pairs of NULLs are not counted +SELECT * FROM explain_mask_costs($$ +SELECT * FROM distinct_t1 t1 JOIN distinct_t2 t2 ON t1.a IS DISTINCT FROM t2.a;$$, +true, true, false, true) LIMIT 1; + +-- Ensure NULLs are matched in semijoins and antijoins +SELECT * FROM explain_mask_costs($$ +SELECT * FROM distinct_t2 t2 WHERE EXISTS + (SELECT 1 FROM distinct_t1 t1 WHERE t1.a IS NOT DISTINCT FROM t2.a);$$, +true, true, false, true) LIMIT 1; + +SELECT * FROM explain_mask_costs($$ +SELECT * FROM distinct_t2 t2 WHERE NOT EXISTS + (SELECT 1 FROM distinct_t1 t1 WHERE t1.a IS NOT DISTINCT FROM t2.a);$$, +true, true, false, true) LIMIT 1; + +-- Columns that are all NULL, have no NULLs, and are half NULL +CREATE TEMP TABLE distinct_t3 AS + SELECT NULL::int AS n, i AS v, CASE WHEN i % 2 = 0 THEN i END AS a + FROM generate_series(1, 100) i; +ANALYZE distinct_t3; +SELECT * FROM explain_mask_costs($$ +SELECT * FROM distinct_t1 t1 JOIN distinct_t3 t3 ON t1.a IS NOT DISTINCT FROM t3.n;$$, +true, true, false, true) LIMIT 1; +SELECT * FROM explain_mask_costs($$ +SELECT * FROM distinct_t1 t1 JOIN distinct_t3 t3 ON t1.a IS NOT DISTINCT FROM t3.v;$$, +true, true, false, true) LIMIT 1; + +-- Ensure pairs of NULLs are counted in restriction clauses too +SELECT * FROM explain_mask_costs($$ +SELECT * FROM distinct_t3 WHERE a IS NOT DISTINCT FROM n;$$, +true, true, false, true); + DROP FUNCTION explain_mask_costs(text, bool, bool, bool, bool); -- 2.37.1 (Apple Git-137.1)