From ca454e76b252ee1e0634b4286145208ebdf76240 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Thu, 3 Dec 2026 12:11:48 -0500 Subject: [PATCH] Box opclass ordering-return test, pending the point-to-box distance fix The "ORDER BY values to the target list" series lets a GiST or SP-GiST ordering scan hand the ORDER BY value it computed to the plan's target list. box_ops qualifies only once the point-to-box distance operator is exact, which a separate patch ("Compute the distance between a point and a box exactly") does; this series no longer carries that fix, so box_ops does not qualify here and these tests would fail. Keep the box tests in their own file, out of the regression schedule, until the distance fix lands. When it does, add box_ops back to gistcanreturnorderby() and spgcanreturnorderby(), fold these blocks into gist.sql beside the point_ops cases, and drop this file. Author: Greg Burd --- .../expected/gist_box_orderby.nocfbot.out | 89 +++++++++++++++++++ .../regress/sql/gist_box_orderby.nocfbot.sql | 57 ++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 src/test/regress/expected/gist_box_orderby.nocfbot.out create mode 100644 src/test/regress/sql/gist_box_orderby.nocfbot.sql diff --git a/src/test/regress/expected/gist_box_orderby.nocfbot.out b/src/test/regress/expected/gist_box_orderby.nocfbot.out new file mode 100644 index 00000000000..e7ede0ef786 --- /dev/null +++ b/src/test/regress/expected/gist_box_orderby.nocfbot.out @@ -0,0 +1,89 @@ +-- Box opclass ordering-return tests for the "ORDER BY values to the target +-- list" feature. These require the point-to-box distance fix (a separate +-- patch, "Compute the distance between a point and a box exactly") so that +-- box_ops' leaf distance equals the <-> operator's and gistcanreturnorderby / +-- spgcanreturnorderby qualify box_ops. Until that patch lands this file is +-- not wired into the schedule; once it does, fold these blocks back into +-- gist.sql beside the point_ops cases (and drop this file). +set enable_indexonlyscan = off; +create table gist_tbl (b box, p point, c circle); +insert into gist_tbl +select box(point(0.05 * i, 0.05 * i), point(0.05 * i, 0.05 * i)), + point(0.05 * i, 0.05 * i), + circle(point(0.05 * i, 0.05 * i), 1.0) +from generate_series(0, 10000) as i; +create index gist_tbl_box_index on gist_tbl using gist (b); +vacuum analyze gist_tbl; +-- The box opclass qualifies: its leaf distance is the operator's. +explain (verbose, costs off) +select b, b <-> point(5.2, 5.91) as dist from gist_tbl + where b <@ box(point(5,5), point(6,6)) order by b <-> point(5.2, 5.91); + QUERY PLAN +-------------------------------------------------------------- + Sort + Output: b, ((b <-> '(5.2,5.91)'::point)) + Sort Key: ((gist_tbl.b <-> '(5.2,5.91)'::point)) + -> Bitmap Heap Scan on public.gist_tbl + Output: b, (b <-> '(5.2,5.91)'::point) + Recheck Cond: (gist_tbl.b <@ '(6,6),(5,5)'::box) + -> Bitmap Index Scan on gist_tbl_box_index + Index Cond: (gist_tbl.b <@ '(6,6),(5,5)'::box) +(8 rows) + +select count(*) filter (where dist = b <-> point(5.2, 5.91)) as same, + count(*) as total + from (select b, b <-> point(5.2, 5.91) as dist from gist_tbl + order by b <-> point(5.2, 5.91) limit 300) ss; + same | total +------+------- + 300 | 300 +(1 row) + +-- Including for a point just off an edge's line, where the operator used to +-- be off in the last digits. +create temp table gist_box_eps (b box); +insert into gist_box_eps values ('((0,0),(1,1))'); +create index on gist_box_eps using gist (b); +select count(*) filter (where dist = b <-> point(0.9999996, 2)) as same, + count(*) as total + from (select b, b <-> point(0.9999996, 2) as dist from gist_box_eps + order by b <-> point(0.9999996, 2) limit 1) ss; + same | total +------+------- + 1 | 1 +(1 row) + +drop table gist_box_eps; +-- SP-GiST's box opclass qualifies as well. +create temp table spg_orderby (b box); +insert into spg_orderby + select box(point(i % 97 * 0.31, i / 97 * 0.29), + point(i % 97 * 0.31 + 0.2, i / 97 * 0.29 + 0.1)) + from generate_series(1, 5000) i; +create index spg_orderby_box on spg_orderby using spgist (b); +explain (verbose, costs off) +select b <-> point(3, 4) as dist from spg_orderby + order by b <-> point(3, 4) limit 3; + QUERY PLAN +--------------------------------------------------------------- + Limit + Output: ((b <-> '(3,4)'::point)) + -> Index Scan using spg_orderby_box on pg_temp.spg_orderby + Output: ((b <-> '(3,4)'::point)) + Order By: (spg_orderby.b <-> '(3,4)'::point) + Order By Values Used: 1 +(6 rows) + +select count(*) filter (where dist = b <-> point(3, 4)) as same, + count(*) as total + from (select b, b <-> point(3, 4) as dist from spg_orderby + order by b <-> point(3, 4) limit 300) ss; + same | total +------+------- + 300 | 300 +(1 row) + +drop table spg_orderby; +drop index gist_tbl_box_index; +drop table gist_tbl; +reset enable_indexonlyscan; diff --git a/src/test/regress/sql/gist_box_orderby.nocfbot.sql b/src/test/regress/sql/gist_box_orderby.nocfbot.sql new file mode 100644 index 00000000000..98c9f57b895 --- /dev/null +++ b/src/test/regress/sql/gist_box_orderby.nocfbot.sql @@ -0,0 +1,57 @@ +-- Box opclass ordering-return tests for the "ORDER BY values to the target +-- list" feature. These require the point-to-box distance fix (a separate +-- patch, "Compute the distance between a point and a box exactly") so that +-- box_ops' leaf distance equals the <-> operator's and gistcanreturnorderby / +-- spgcanreturnorderby qualify box_ops. Until that patch lands this file is +-- not wired into the schedule; once it does, fold these blocks back into +-- gist.sql beside the point_ops cases (and drop this file). +set enable_indexonlyscan = off; + +create table gist_tbl (b box, p point, c circle); +insert into gist_tbl +select box(point(0.05 * i, 0.05 * i), point(0.05 * i, 0.05 * i)), + point(0.05 * i, 0.05 * i), + circle(point(0.05 * i, 0.05 * i), 1.0) +from generate_series(0, 10000) as i; +create index gist_tbl_box_index on gist_tbl using gist (b); +vacuum analyze gist_tbl; + +-- The box opclass qualifies: its leaf distance is the operator's. +explain (verbose, costs off) +select b, b <-> point(5.2, 5.91) as dist from gist_tbl + where b <@ box(point(5,5), point(6,6)) order by b <-> point(5.2, 5.91); + +select count(*) filter (where dist = b <-> point(5.2, 5.91)) as same, + count(*) as total + from (select b, b <-> point(5.2, 5.91) as dist from gist_tbl + order by b <-> point(5.2, 5.91) limit 300) ss; +-- Including for a point just off an edge's line, where the operator used to +-- be off in the last digits. +create temp table gist_box_eps (b box); +insert into gist_box_eps values ('((0,0),(1,1))'); +create index on gist_box_eps using gist (b); +select count(*) filter (where dist = b <-> point(0.9999996, 2)) as same, + count(*) as total + from (select b, b <-> point(0.9999996, 2) as dist from gist_box_eps + order by b <-> point(0.9999996, 2) limit 1) ss; +drop table gist_box_eps; + +-- SP-GiST's box opclass qualifies as well. +create temp table spg_orderby (b box); +insert into spg_orderby + select box(point(i % 97 * 0.31, i / 97 * 0.29), + point(i % 97 * 0.31 + 0.2, i / 97 * 0.29 + 0.1)) + from generate_series(1, 5000) i; +create index spg_orderby_box on spg_orderby using spgist (b); +explain (verbose, costs off) +select b <-> point(3, 4) as dist from spg_orderby + order by b <-> point(3, 4) limit 3; +select count(*) filter (where dist = b <-> point(3, 4)) as same, + count(*) as total + from (select b, b <-> point(3, 4) as dist from spg_orderby + order by b <-> point(3, 4) limit 300) ss; +drop table spg_orderby; + +drop index gist_tbl_box_index; +drop table gist_tbl; +reset enable_indexonlyscan; -- 2.50.1