From 7749aa23cd5573a35d7edb9fbc883e6c2a967c13 Mon Sep 17 00:00:00 2001 From: Shihao Date: Mon, 21 Sep 2026 19:06:37 -0400 Subject: [PATCH v1 2/2] Add tests for NaN handling in bound_box() and BRIN box_inclusion_ops Check that a box with a NaN coordinate does not hide the other rows of its page range from a BRIN index scan, and that bound_box() returns an infinite bound for a NaN coordinate. Discussion: https://postgr.es/m/19705-548fda77321e062d@postgresql.org --- src/test/regress/expected/brin.out | 37 ++++++++++++++++++++++++++ src/test/regress/expected/geometry.out | 7 +++++ src/test/regress/sql/brin.sql | 14 ++++++++++ src/test/regress/sql/geometry.sql | 3 +++ 4 files changed, 61 insertions(+) diff --git a/src/test/regress/expected/brin.out b/src/test/regress/expected/brin.out index e1db2280cf9..5ebf9e74faf 100644 --- a/src/test/regress/expected/brin.out +++ b/src/test/regress/expected/brin.out @@ -589,3 +589,40 @@ CREATE INDEX brin_insert_optimization_idx ON brin_insert_optimization USING brin UPDATE brin_insert_optimization SET a = a; REINDEX INDEX CONCURRENTLY brin_insert_optimization_idx; DROP TABLE brin_insert_optimization; +-- A box with a NaN coordinate must not hide the other rows of its page range +CREATE TABLE brin_box_nan (b box); +INSERT INTO brin_box_nan SELECT box '(0,0),(1,1)' FROM generate_series(1, 100); +INSERT INTO brin_box_nan VALUES (box '(NaN,NaN),(0,0)'); +CREATE INDEX ON brin_box_nan USING brin (b); +SET enable_seqscan = off; +EXPLAIN (COSTS OFF) +SELECT count(*) FROM brin_box_nan WHERE b && box '(-2,-2),(2,2)'; + QUERY PLAN +------------------------------------------------------- + Aggregate + -> Bitmap Heap Scan on brin_box_nan + Recheck Cond: (b && '(2,2),(-2,-2)'::box) + -> Bitmap Index Scan on brin_box_nan_b_idx + Index Cond: (b && '(2,2),(-2,-2)'::box) +(5 rows) + +SELECT count(*) FROM brin_box_nan WHERE b && box '(-2,-2),(2,2)'; + count +------- + 100 +(1 row) + +SELECT count(*) FROM brin_box_nan WHERE b @> point '(0.5,0.5)'; + count +------- + 100 +(1 row) + +SELECT count(*) FROM brin_box_nan WHERE b ~= box '(0,0),(1,1)'; + count +------- + 100 +(1 row) + +RESET enable_seqscan; +DROP TABLE brin_box_nan; diff --git a/src/test/regress/expected/geometry.out b/src/test/regress/expected/geometry.out index 1d168b21cbc..fa9c746fedd 100644 --- a/src/test/regress/expected/geometry.out +++ b/src/test/regress/expected/geometry.out @@ -5321,3 +5321,10 @@ SELECT * FROM pg_input_error_info('(1,2),-1', 'circle'); invalid input syntax for type circle: "(1,2),-1" | | | 22P02 (1 row) +-- A NaN coordinate makes the bound infinite on that side +SELECT bound_box(box '(0,0),(1,1)', box '(NaN,3),(0,2)'); + bound_box +-------------------- + (Infinity,3),(0,0) +(1 row) + diff --git a/src/test/regress/sql/brin.sql b/src/test/regress/sql/brin.sql index 7ea97f47c8d..11c93177cda 100644 --- a/src/test/regress/sql/brin.sql +++ b/src/test/regress/sql/brin.sql @@ -534,3 +534,17 @@ CREATE INDEX brin_insert_optimization_idx ON brin_insert_optimization USING brin UPDATE brin_insert_optimization SET a = a; REINDEX INDEX CONCURRENTLY brin_insert_optimization_idx; DROP TABLE brin_insert_optimization; + +-- A box with a NaN coordinate must not hide the other rows of its page range +CREATE TABLE brin_box_nan (b box); +INSERT INTO brin_box_nan SELECT box '(0,0),(1,1)' FROM generate_series(1, 100); +INSERT INTO brin_box_nan VALUES (box '(NaN,NaN),(0,0)'); +CREATE INDEX ON brin_box_nan USING brin (b); +SET enable_seqscan = off; +EXPLAIN (COSTS OFF) +SELECT count(*) FROM brin_box_nan WHERE b && box '(-2,-2),(2,2)'; +SELECT count(*) FROM brin_box_nan WHERE b && box '(-2,-2),(2,2)'; +SELECT count(*) FROM brin_box_nan WHERE b @> point '(0.5,0.5)'; +SELECT count(*) FROM brin_box_nan WHERE b ~= box '(0,0),(1,1)'; +RESET enable_seqscan; +DROP TABLE brin_box_nan; diff --git a/src/test/regress/sql/geometry.sql b/src/test/regress/sql/geometry.sql index c3ea368da5e..5d8a1749c35 100644 --- a/src/test/regress/sql/geometry.sql +++ b/src/test/regress/sql/geometry.sql @@ -529,3 +529,6 @@ SELECT pg_input_is_valid('(1', 'circle'); SELECT * FROM pg_input_error_info('1,', 'circle'); SELECT pg_input_is_valid('(1,2),-1', 'circle'); SELECT * FROM pg_input_error_info('(1,2),-1', 'circle'); + +-- A NaN coordinate makes the bound infinite on that side +SELECT bound_box(box '(0,0),(1,1)', box '(NaN,3),(0,2)'); -- 2.37.1 (Apple Git-137.1)