Skip to content

fix incorrect segment iteration in prepared geometry ops #617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/sgl/sgl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2369,7 +2369,9 @@ point_in_polygon_result prepared_geometry::contains(const vertex_xy &vert) const

// Now, we are at a leaf, so we need to check the segments
const auto beg_idx = entry * NODE_SIZE;
const auto end_idx = math::min(beg_idx + NODE_SIZE, index.items_count);
// We +1 to the node size here to get the end index, because the last segment spans across
// the end of the node. And our indexes are always sequential.
const auto end_idx = math::min(beg_idx + NODE_SIZE + 1, index.items_count);

// Loop over the segments
vertex_xy prev;
Expand Down Expand Up @@ -2436,7 +2438,9 @@ bool prepared_geometry::try_get_distance_recursive(uint32_t level, uint32_t entr
const auto vertex_width = get_vertex_width();

const auto beg_idx = entry * NODE_SIZE;
const auto end_idx = math::min(beg_idx + NODE_SIZE, index.items_count);
// We +1 to the node size here to get the end index, because the last segment spans across
// the end of the node. And our indexes are always sequential.
const auto end_idx = math::min(beg_idx + NODE_SIZE + 1, index.items_count);

if (beg_idx >= end_idx) {
return false; // No segments to check
Expand Down
25 changes: 25 additions & 0 deletions test/sql/geometry/st_distance.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require spatial

# This test case comes from a bug report, where the indexed point-in-polygon check would not consider that
# the second vertex of the last segment in each index node neccessarily have to be the first vertex of the next segment.
# Therefore when performing a raycast from a point to a polygon where the ray would intersect
# the segment formed by the last vertex in a index leaf node, and the first vertex in the next segment, the result
# would be incorrect.
# To test this, we create a polygon with 40 vertices, causing two index leaf nodes (0-31, 33-40) to be created when
# preparing the polygon, and we then query a point that is to the left of the polygon, but right between the segment
# spanning the two nodes (31-32).

query I
SELECT ST_Distance(
ST_Point(-5, 31),
-- 32 points
ST_GeomFromText('POLYGON((
0 0, 0 1, 0 2, 0 3, 0 4, 0 5, 0 6, 0 7,
0 8, 0 9, 0 10, 0 11, 0 12, 0 13, 0 14, 0 15,
0 16, 0 17, 0 18, 0 19, 0 20, 0 21, 0 22, 0 23,
0 24, 0 25, 0 26, 0 27, 0 28, 0 29, 0 30, 0 31,
0 32, 0 33, 0 34, 0 35, 0 36, 0 37, 0 38, 0 39,
0 0))'));
----
5

Loading