Repository navigation
Fix wrong results of pointInPolygon for ordinary constant polygons - #123767
Conversation
`pointInPolygon` with a constant polygon builds an 8x8 grid over the bounding box and classified each cell from `boost::geometry::intersection(polygon, cell)`. That overlay is not robust at degenerate positions (a vertex on a cell corner, a vertex within a few ulps of a grid line): it returns the whole cell, the whole polygon or a clip with a missing edge, and the cell is then classified as fully inside, fully outside or with the wrong half-planes. Since contrib/boost moved from 1.83 to 1.90 this hits ordinary polygons, e.g. the unit diamond returns 0 for (0.76, 0.01). Independently, a hole inside a single cell was ignored: `addComplexPolygonCell` was not followed by a `return`, so the cell was then overwritten by the centre test. Each cell is now classified from the polygon edges that enter the open cell (Liang-Barsky): none -> the centre test; one -> the half-plane of that edge; two -> the AND or OR of their half-planes, decided by the turn at the shared vertex, or for two separate chords by the side of the second chord; three or more -> a complex cell whose polygon is every ring clipped by the slightly expanded cell with Sutherland-Hodgman, which keeps the winding number of every point inside the cell. The grid no longer uses boost overlay and builds about 10x faster; query time is unchanged. Two existing references change: 00500_point_in_polygon_empty_bound (the point lies on the polygon boundary, where the result is unspecified) and 04897_point_in_polygon_primary_key_invalid_polygon (counts for invalid polygons under `validate_polygons = 0`, documented as possibly incorrect; the primary key and full scan counts still agree). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tive to the edge An edge with an endpoint strictly inside a grid cell now always counts as entering that cell, so both edges at a vertex inside the cell are seen and the cell is classified by the turn at that vertex rather than by one edge's extended line. A half-plane is stored as the edge origin and direction and evaluated as `dx * (y - y0) - dy * (x - x0)`, so its sign does not depend on how far the polygon is from the origin. The test covers a polygon vertex at a quarter of the bounding box and a small triangle near 1e9. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex) plus my own review; 5 findings over 2 rounds.
Severity: ❌ blocker / Session id: cron:clickhouse-review-self-slot-58:20261003-212600 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261003-204100 |
|
Workflow [PR], commit [5748f68] Summary: ✅
AI ReviewSummaryThis PR replaces the constant-polygon grid classification in Final Verdict
LLVM Coverage ReportMeasured on commit 5748f68.
Changed lines: Changed C/C++ lines covered: 145/146 (99.32%) · Uncovered code |
Build profile diff (arm_release)Comparing Binary sizes
The official master build is compiled with Object file sizes
|
| Object file | Master | PR | Δ |
|---|---|---|---|
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/pointInPolygon.cpp.o |
5.98 MiB | 4.18 MiB | -1.81 MiB (-30.23%) |
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.
Compile time of recompiled translation units
7 translation units recompiled, 20 s compile time in total, 7 of them have a recent master baseline.
CI finish ledger - 6540824Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
None of these touches Session id: cron:our-pr-ci-monitor:20261004-013156 |
nihalzp
left a comment
There was a problem hiding this comment.
There are few edge cases that produce wrong result:
1
The polygon's lowest vertex (0.24999999999999994, 0) lies on the bottom border of the bounding box, two doubles left of the grid line x = 0.25. The point is the centre of grid cell [0.125, 0.25] × [0, 0.125], outside the polygon. Both edges at that vertex cross this cell over a piece only a few ulps long, and rounding in the midpoint test of crossesBox counts only one of them. The cell becomes singleLine, and that one edge's half-plane marks the whole cell as inside.
SELECT pointInPolygon((0.1875, 0.0625), [(0.24999999999999994, 0.0), (1.0, 0.3), (1.0, 1.0), (0.0, 1.0), (0.0, 0.95), (0.5, 0.9)]);- PR: 1
- master: 0
- expected: 0
2
A star polygon on a 1/32 lattice with coordinates nudged by up to three doubles, which turns two of them into subnormals (1.5e-323, -5e-324). The vertex (0.25000000000000006, -5e-324) lies on the bottom border, one double left of the grid line x = 0.2500000000000001. The point is the centre of the grid cell to the left of that vertex, outside the polygon. Same cause as case 1: crossesBox counts only one of the vertex's two edges, and the singleLine cell is marked inside.
SELECT pointInPolygon((0.18750000000000008, 0.0625), [(0.21874999999999997, 0.49999999999999994), (1.5e-323, 0.40625), (0.4062499999999999, 0.43749999999999994), (0.3125, 0.15624999999999997), (0.25000000000000006, -5e-324), (0.43749999999999983, 0.031249999999999993), (0.4687499999999999, 0.1875), (0.6874999999999999, 0.28125), (0.7812500000000002, 0.28125000000000017), (0.6562500000000003, 0.46875000000000017), (0.7187499999999998, 0.5000000000000003), (0.71875, 0.5937499999999999), (1.0000000000000004, 0.7187499999999998), (0.84375, 0.6562500000000001), (0.8124999999999999, 0.8749999999999998), (0.59375, 0.6562499999999999), (0.5312500000000002, 0.5624999999999998), (0.5312499999999999, 0.6249999999999997), (0.5, 0.9687500000000002), (0.40625000000000017, 1.0), (0.34375000000000017, 0.9062499999999999), (0.2812500000000001, 0.8124999999999998), (0.21874999999999994, 0.8124999999999997), (0.18749999999999992, 0.6874999999999998)]);- PR: 1
- master: 0
- expected: 0
3
A pentagon with vertices on a 1/16 lattice, nudged by up to four doubles. Its lowest vertex (0.24999999999999994, 0) sits exactly as in case 1, and the point is again the centre of cell [0.125, 0.25] × [0, 0.125], outside the polygon. Same cause as case 1: of the two edges at that vertex, crossesBox counts only the one going to (1, 0.5625), and its half-plane marks the whole cell as inside.
SELECT pointInPolygon((0.1875, 0.0625), [(0.4375000000000002, 0.4999999999999999), (0.24999999999999994, 0.0), (1.0, 0.5625), (0.5624999999999997, 1.0), (0.0, 0.5625000000000002)]);- PR: 1
- master: 0
- expected: 0
4
A hexagon with vertices on a 1/16 lattice, nudged by up to four doubles. Its leftmost vertex (0, 0.7500000000000001) lies on the left border, one double above the grid line y = 0.75. The point is the centre of cell [0, 0.125] × [0.75, 0.875], whose bottom-left corner touches that vertex; it is outside the polygon. Same cause as case 1 with x and y swapped: crossesBox counts only one of the vertex's two edges, so the cell becomes singleLine and is marked inside.
SELECT pointInPolygon((0.0625, 0.8125), [(0.3124999999999998, 0.0), (0.49999999999999994, 0.31249999999999994), (1.0, 0.7499999999999999), (0.6250000000000004, 1.0), (0.5, 0.6874999999999997), (0.0, 0.7500000000000001)]);- PR: 1
- master: 0
- expected: 0
5
A square about 1e-6 across at (0.5, -0.25) with two small holes. The point lies about 1e-8 below the bottom edge of the triangular hole, an edge only about 1.6e-8 long, so the point is inside the polygon. The hole makes this a complex cell, answered by boost::geometry::within. Boost's collinearity tolerance places the point on the hole's edge, even on the whole, unclipped polygon, and within returns 0 for boundary points. master's complex cells use the same call: it answers this point correctly, but answers 13 of 400 test points in this cell wrongly.
SELECT pointInPolygon((0.5000007707394368, -0.24999994741116524), [[(0.5, -0.25), (0.500001, -0.25), (0.500001, -0.249999), (0.5, -0.249999)], [(0.500000125, -0.24999975), (0.50000025, -0.24999975), (0.50000025, -0.249999625), (0.500000125, -0.249999625)], [(0.500000765625, -0.2499999375), (0.50000078125, -0.2499999375), (0.5000007734375, -0.249999875)]]);- PR: 0
- master: 1
- expected: 1
6
A 23-vertex polygon about 1e-6 across at (0.5, -0.25), and a point inside it. Its grid cell is a complex cell. boost::geometry::within gives the correct answer on the whole polygon, but on the polygon clipped to the cell, which contains a spike, boost places the point on the boundary and within returns 0. master gets this point wrong too.
SELECT pointInPolygon((0.5000003894894368, -0.24999925366116524), [(0.5, -0.2499995625), (0.500000375, -0.2499995625), (0.500000265625, -0.249999703125), (0.5000004375, -0.249999828125), (0.5000005, -0.2499995625), (0.500000546875, -0.25), (0.500000609375, -0.24999971875), (0.500000671875, -0.24999971875), (0.500001, -0.24999965625), (0.500000796875, -0.2499995625), (0.500000703125, -0.24999953125), (0.5000008125, -0.24999953125), (0.50000075, -0.24999925), (0.500000671875, -0.249999078125), (0.500000515625, -0.24999921875), (0.5000005, -0.249999375), (0.50000046875, -0.24999934375), (0.50000028125, -0.249999), (0.500000296875, -0.249999109375), (0.50000040625, -0.24999934375), (0.5000003125, -0.249999203125), (0.50000009375, -0.249999234375), (0.500000046875, -0.249999421875)]);- PR: 0
- master: 0
- expected: 1
7
A square about 1e-6 across at (0.5, -0.25) with two small holes. The point lies about 4e-9 to the right of the triangular hole, inside the polygon. It falls in a complex cell, and boost's within places it on the hole's edge, even on the whole polygon, so it returns 0. master gets this point wrong too.
SELECT pointInPolygon((0.5000000832394368, -0.24999940366116524), [[(0.5, -0.25), (0.500001, -0.25), (0.500001, -0.249999), (0.5, -0.249999)], [(0.5000000526357758, -0.24999944017885178), (0.5000000945093698, -0.24999944017885178), (0.5000000735725728, -0.24999939154485615)], [(0.5000001495231287, -0.24999934829611353), (0.5000002028428762, -0.24999934829611353), (0.5000002028428762, -0.24999930877053325), (0.5000001495231287, -0.24999930877053325)]]);- PR: 0
- master: 0
- expected: 1
8
A comb-shaped polygon whose boundary along y = 0.1875 has two extra collinear vertices only 1e-12 apart, which form an edge 1e-12 long. The point is 1e-6 directly below that short edge, inside the polygon. It falls in a complex cell, and boost's within places it on the short edge, even on the whole polygon, so it returns 0. master gets this point wrong too.
SELECT pointInPolygon((0.654267174011323, 0.187499), [(0.0, 0.0), (1.0, 0.0), (1.0, 0.875), (0.9375, 0.875), (0.9375, 0.5625), (0.6875, 0.5625), (0.6875, 0.1875), (0.6542671740120364, 0.1875), (0.6542671740110364, 0.1875), (0.5, 0.1875), (0.5, 0.6875), (0.125, 0.6875), (0.125, 1.0), (0.0625, 1.0), (0.0625, 0.875), (0.0, 0.875)]);- PR: 0
- master: 0
- expected: 1
9
A unit square with a slanted slot, about 9e-16 wide, cut from the top edge down to y = 0.3. The point is 0.001 directly below the slot's bottom edge, which is also only about 9e-16 long, so it is inside the polygon. It falls in a complex cell, and boost's within places it on that tiny edge, even on the whole polygon, so it returns 0. master answers this point correctly, but gave similar wrong answers for wider slots in the same tests.
SELECT pointInPolygon((0.60916694932799, 0.299), [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.32605962407053535, 1.0), (0.6091669493279903, 0.3), (0.6091669493279894, 0.3), (0.32605962407053446, 1.0), (0.0, 1.0)]);- PR: 0
- master: 1
- expected: 1
…on grid Review of ClickHouse#123767 found constant polygons whose grid cells were still classified wrongly: - A vertex on the cell border a few doubles from a grid line: both of its edges enter the cell over an ulp-sized piece, and the Liang-Barsky midpoint test counted only one of them, so the cell became `singleLine` with the wrong half-plane. The census of edges entering a cell is now exact: a separating-axis test with the exact orientation predicate of boost (`side_robust`, Shewchuk's adaptive arithmetic). The turn and the strip side for two crossing edges use the same predicate. The predicate is exact only without overflow and underflow of products, so polygons with nonzero coordinates outside [2^-300, 2^300] fall back to complex cells for every cell an edge's bounding box overlaps. - Complex cells and the centre test used `boost::geometry::within`, whose collinearity tolerance puts points far from a tiny edge (1e-8 from a 1.6e-8 hole edge, 0.001 from a 9e-16 slot edge) on the boundary. They now use the crossing number test of the non-constant path. Grid build is faster again and complex cells answer 2-7x faster (out-of-tree -O2 port: geo10k 1375 -> 247 ms per 2M points). Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks, all nine reproduced. Fixed in 5748f68:
The nine queries are in |
After ClickHouse#123767 both point-in-polygon algorithms and the primary key analysis agree with the unscaled result for valid polygons scaled by powers of two up to 2^512 (about 1.3e154), so 1e100 was more conservative than needed. 1e150 keeps a margin below that. The 05320 polygon huge in both axes moves from the rejected lines to the evaluated ones, at 1e150. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`clipRing` copies the ring into `points` and runs four clip passes, each ending with `points.swap(clipped)`. After the fourth swap `points` owns the original copy again, so every complex grid cell of a cached `PointInPolygonWithGrid` kept a buffer with the capacity of the whole ring. A 100k-vertex polygon took 46.4 MB of the polygon cache instead of 3.3 MB, and a polygon over ~550k vertices no longer fit the default 256 MiB cache, so it was rebuilt for every block. Shrink the returned ring. The cache entry is back to the size of 26.8/26.9 (3,252,640 bytes for the 100k-vertex circle). The regression came from ClickHouse#123767 and is not in a release. Closes ClickHouse#124567 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #123740
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
pointInPolygonwith a constant polygon returning wrong results for some points of ordinary polygons, for example0for the point(0.76, 0.01)inside the polygon[(1, 0), (0, 1), (-1, 0), (0, -1)], or1for a point inside a small hole. The results now match those for a non-constant polygon.Description
On 26.8.10.6 (also 26.3 and master),
SELECT pointInPolygon((0.76, 0.01), [(1., 0.), (0., 1.), (-1., 0.), (0., -1.)])returns0for a point inside the polygon; withmaterializeit returns1. With the hole[(3.1, 3.1), (3.3, 3.1), (3.3, 3.3), (3.1, 3.3)]in the square0..8,(3.2, 3.2)is reported inside. 25.3 answers the first query correctly.Root cause. For a constant polygon,
PointInPolygonWithGridprecomputes an 8x8 grid and classified each cell fromboost::geometry::intersection(polygon, cell). That overlay is wrong at degenerate positions (a vertex on a cell corner, or within a few ulps of a grid line); since the boost 1.90 update this also happens for ordinary polygons. A hole inside one cell was ignored (noreturnafteraddComplexPolygonCell), and complex cells usedboost::geometry::within, whose collinearity tolerance misplaces points near very short edges.Change. Each cell is now classified from the polygon edges that enter it, counted with an exact orientation predicate (
side_robust): none means inside or outside by the cell centre, one gives that edge's half-plane, two give the AND or OR of their half-planes (by the turn at their shared vertex, or by which side of one edge the other lies), three or more store every ring clipped to the cell. Complex cells and cell centres use the crossing number test of the non-constant path. Polygons with coordinates outside [2^-300, 2^300], where the predicate could overflow or underflow, use complex cells. The grid builds over 10x faster and complex cells answer 2-7x faster.Validation. The new test compares constant and
materializepolygons at such positions and fails on master. A differential check on 6711 degenerate polygons (735K points, exact rational reference) finds no wrong point farther than1e-15from an edge; master has 2962. Two references change: a boundary point, and invalid polygons undervalidate_polygons = 0.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/123767]
Version info
26.10.1.1797-master(included in26.10and later)