Repository navigation
Fix wrong results of pointInPolygon for a polygon with huge coordinates - #123740
alexey-milovidov merged 7 commits into
Conversation
…ygon The grid of a constant polygon clips the polygon to each cell with boost::geometry::intersection. boost's Float64 overlay multiplies a coordinate by a coordinate difference, so once a cell extent times a cell coordinate exceeds the Float64 range the clip comes back empty or equal to the cell, and the whole cell is classified by its center. For the AST fuzzer polygon [(0,0), (1.19e-7, DBL_MAX), (0.9999, FLT_MAX), (1.0001, 0)] this made the x = 0 sliver outside the polygon "inside" and the interior points at x = 0.9 and 1.0 "outside". The primary key analysis of the pointInPolygon atom is exact for those boxes, so a table ordered by (x, y) also returned fewer rows than a full scan (812 vs 900), which is the TLP mismatch the fuzzer reported. The measured failure threshold for that shape is a cell height of sqrt(DBL_MAX) ~ 1.3e154. Coordinates up to 1e150 keep every such product below 1e301 and give correct results, so reject larger coordinates with BAD_ARGUMENTS next to the existing "bounding box is unbounded" check. Every ring is checked (holes and every polygon of a multipolygon). The function runs on a 0-row block while the query is planned, so the error is raised before index analysis even when every granule would be pruned. 01700 claimed a 1e200-wide box "stays usable"; it returns 0 for the interior point (1e197, 1.99), so the bound there moves to 1e150. Provenance: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=782be4dcec24970dad336d1af4f321c7ff53da40&name_0=MasterCI&name_1=AST%20fuzzer%20%28amd_release%2C%20oracle%29 Co-Authored-By: Claude Opus 5.5 <[email protected]>
… algorithms Review round 1 found three gaps in the previous commit. Constant Geometry, Variant and Dynamic polygons never reach the grid: the Variant/Dynamic adaptors pass them as full columns, so they take the ray casting path, like every non-constant polygon. Ray casting computes (x2 - x1) * (y - y1) before dividing, which overflows for a polygon huge in both axes: [(0,0), (1e200,0), (0,1e200)] answered 1 for the outside point (9e199, 5e199). isInsideRing now checks the ring it evaluates with one branch-free pass before casting; it is the only ray casting site. The grid check ran after the empty-bound return, so a degenerate polygon with a 1e200 vertex still answered. It is now the first statement of calcGridAttributes. boost computes each clip intersection point as a 2x2 determinant times a coordinate difference, a 3-factor product, so a polygon huge in both axes breaks the grid much earlier than 1e150. A 41x41 lattice sweep on master (holes-as-arguments form, which the parseRing Int64 guard does not cover) found no scale-induced error up to 1e101 and 65 wrong points at 1e103 (E^3 reaches DBL_MAX near 5.6e102). The bound is now 1e100, shared by both algorithms through max_abs_polygon_coordinate and throwPolygonCoordinateIsTooLarge. Tests: 05320 gains the degenerate polygon, a 2-D polygon at 1e103, the Geometry/Variant/Dynamic/materialize forms with a unit-scale control, and the positive control moves to 1e100; 01700's "stays usable" lines move to 1e100. Provenance: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=782be4dcec24970dad336d1af4f321c7ff53da40&name_0=MasterCI&name_1=AST%20fuzzer%20%28amd_release%2C%20oracle%29 Co-Authored-By: Claude Opus 5.5 <[email protected]>
… the ray The previous commit checked every ring with a separate pass before ray casting. That pass is a loop-carried std::max chain over all vertices, for every row, and cost about 23% on the non-constant 180-vertex polygon workload of tests/performance/point_in_polygon.xml (Debug: 0.053 s -> 0.065 s on 500k polygons, median of 30). Ray casting does arithmetic only for an edge that straddles the point's ray, where the point's y lies between the edge's y1 and y2. Bounding the four coordinates of that edge by 1e100 keeps (x2 - x1) * (y - y1) below 4e200 and the quotient within |x2 - x1|, so the check now runs only there, right before the crossing is computed. A coordinate on an edge that is never computed cannot make the answer wrong. The same workload now measures 0.053 s, equal to the base. The predicate is the grid's, so NaN keeps its behaviour. The function documentation now states the 1e100 limit. The positive control of 05320 runs in both primary key analysis modes. Provenance: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=782be4dcec24970dad336d1af4f321c7ff53da40&name_0=MasterCI&name_1=AST%20fuzzer%20%28amd_release%2C%20oracle%29 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ols and document the coordinate limit The two positive-control lines of 05320 run the same predicate back to back, once per primary key analysis mode. The query condition cache keeps the ranges excluded by the first line under a key that does not include use_lightweight_primary_key_index_analysis, so the second line could pass max_rows_to_read without doing its own pruning. Both lines now set use_query_condition_cache = 0. A ray-casting line with a vertex at exactly 1e100 pins the bound as inclusive, so a ">=" in the edge check fails the test. The function's documentation page now states the 1e100 limit, as the function's own documentation already does. Provenance: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=782be4dcec24970dad336d1af4f321c7ff53da40&name_0=MasterCI&name_1=AST%20fuzzer%20%28amd_release%2C%20oracle%29 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…he documented exception Ray casting checks only the coordinates of edges that cross the point's ray, so a polygon with an off-ray huge vertex is answered without an error. The documentation now says the function throws when it would compute with a larger coordinate, which matches that behaviour. The test gains four materialized polygons, each of which trips exactly one of the four compares in the isInsideRing guard (x1, x2, y1, y2), so deleting any single compare fails exactly one line. 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 cold review of the resulting code. Round 1 (head
Round 2 (head
Round 3 (head
Round 4 (head
Round 5 (head
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261003-174503 |
|
Workflow [PR], commit [58ccc12] Summary: ✅
AI ReviewSummaryThis PR adds a Findings❌ Blockers
Final Verdict❌ Changes requested — make the single-column constant path honor the same LLVM Coverage ReportMeasured on commit 58ccc12.
Changed lines: Changed C/C++ lines covered: 26/26 (100.00%) · Uncovered code |
| { | ||
| return std::abs(point.x()) > max_abs_polygon_coordinate || std::abs(point.y()) > max_abs_polygon_coordinate; | ||
| }; | ||
| if (std::ranges::any_of(polygon.outer(), is_too_large) |
There was a problem hiding this comment.
This guard still runs after the single-column constant path has already gone through parseRing in src/Functions/pointInPolygon.cpp:716. That helper still does static_cast<Int64>(x_data[i]) / static_cast<Int64>(y_data[i]) before any bound check, so inputs above 1e100 are already hitting out-of-range FP→Int conversions before we reach the new rejection path here.
That matters for cases like [(0., 0.), (1e200, 0.), (0., 1.)]: the old mulOverflow probe does not necessarily fire there, so today the query still depends on implementation-defined / undefined conversion behavior before eventually throwing later. Please move the max_abs_polygon_coordinate check into parseRing (or before calling it) so huge constant-array polygons are rejected before any integer casts or preprocessing.
Build profile diff (arm_release)Commit See the job log for details. |
CI finish ledger - 9862cd9Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
None of these touches the Session id: cron:our-pr-ci-monitor:20261003-223443 |
|
Ledger follow-up for |
The test asserts JoinBuildPostProcessingMicroseconds > 0 on the initiator's query_log row. Under parallel replicas the join-order randomization can put t_post_build_left, the coordinated read, on the build side. Its marks form one coordinator segment owned by a remote replica, so when the local replica's first read request arrives after that replica announces, the local replica gets 0 marks and builds an empty hash table. The whole build and its post-build phase then run in the remote replicas' secondary queries, whose ProfileEvents are forwarded to the client only and are not added to the initiator's query_log row, so the check reads 0. Seen in "Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel)" on PRs ClickHouse#117005 and ClickHouse#123740 with the output "0 0" instead of "1 0". Pin enable_parallel_replicas = 0 on the measured join only, as other join tests asserting join ProfileEvents already do. The test's subject, SpillingHashJoin running the post-build phase of the wrapped join, does not depend on how the read is split between replicas. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Merge master. |
…n-huge-coordinates
|
Merged master in e4f70e7, which brings in #123767. Re-measured on top of it: the fuzzer's polygon no longer loses rows on master, but huge coordinates still return wrong results there. For In a sweep of valid polygons scaled by powers of two, both algorithms on master now match the unscaled result up to |
|
Raise. |
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]>
|
Raised to One leftover I did not touch: the 2021 |
Related: #115035
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results of
pointInPolygonfor polygons with huge coordinates. With a constant array polygon, a filter on a table whose primary key covers the point columns could also return fewer rows than a full scan.pointInPolygonnow throwsBAD_ARGUMENTSwhere it would compute with a polygon coordinate larger than1e150in absolute value.Description
On 26.8.10.6,
pointInPolygon((0.9, 5.), [(0., 0.), (1.1920928955078125e-7, 1.7976931348623157e308), (0.9999, 3.4028234663852886e38), (1.0001, 0.)])returns0for an inside point, and the same filter on a tableORDER BY (x, y)returns 812 rows where a full scan returns 900 (also on 26.3, 26.9). Found by the AST fuzzer: report.Root cause. Both algorithms overflow
Float64for huge coordinates. After #123767 the constant-polygon grid and ray casting match the unscaled result for valid polygons scaled up to about2^512(1.3e154), but not above: for[(0., 0.), (1e200, 0.), (0., 1e200)]and 10000 points on a1e198grid, master reports 5713 points inside the constant polygon (576 of them withx + y > 1e200) and all 10000 withmaterialize. Ray casting also serves constantGeometry,VariantandDynamicpolygons, which the Variant adaptor passes as full columns.Change. Both algorithms now reject a coordinate above
1e150in absolute value: the grid checks every ring first (degenerate polygons included); ray casting checks the vertices of each edge that crosses the point's ray, right before computing the crossing, so it adds no pass over the polygon. The planning-time execution raises it before index analysis.Validation. The new test and
01700fail on master (every rejected line returns a value, wrappers andmaterializeincluded) and pass with the fix; the pruning control runs in both primary key analysis modes, and the bound1e100fails exactly the eight lines at1e150. For 40 valid polygons scaled by2^kup to2^498(just under1e150), the grid, ray casting and the primary key pruning return the same rows as the unscaled polygon. 50 randomized runs and thepointInPolygontests pass.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/123740]
Version info
26.10.1.2165-master(included in26.10and later)