Skip to content

Make the pointInPolygon preprocessed polygon cache shared, bounded, and configurable - #107247

Merged
nihalzp merged 26 commits into
ClickHouse:masterfrom
nihalzp:pointinpolygon-shared-bounded-cache
Jun 25, 2026
Merged

nihalzp merged 26 commits into
ClickHouse:masterfrom
nihalzp:pointinpolygon-shared-bounded-cache

Conversation

@nihalzp

@nihalzp nihalzp commented Jun 11, 2026 •

Copy link
Copy Markdown
Member

pointInPolygon preprocesses constant polygons into a grid / R-tree and keeps them in a process-wide pool. The pool kept up to max_threads copies of the same preprocessed polygon per thread, grew without bound across distinct polygons, and never released anything until server restart, while the tracked memory counted toward the server memory limit and could fail unrelated queries with MEMORY_LIMIT_EXCEEDED.

Now all queries and threads share a single immutable preprocessed instance per polygon, kept in a bounded LRU cache. The bound is the new server setting point_in_polygon_cache_size (default 256 MiB, changeable at runtime, 0 disables the cache), and the current usage is visible in system.metrics as PointInPolygonCacheBytes and PointInPolygonCacheCells.

Related: #100759

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

The cache of preprocessed constant polygons for the function pointInPolygon is now shared between queries and threads and bounded by the new server setting point_in_polygon_cache_size (default 256 MiB, 0 disables the cache, changeable at runtime; previously the cache kept up to max_threads copies of each preprocessed polygon, grew without bound across distinct polygons, and was never released until server restart). It can be cleared at runtime with SYSTEM DROP POINT IN POLYGON CACHE and current usage is reported in system.metrics as PointInPolygonCacheBytes, PointInPolygonCacheCells, and PointInPolygonCacheSizeLimit. Closes #106393.

Version info

  • Merged into: 26.7.1.82

@clickhouse-gh

clickhouse-gh Bot commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1a98b24]

Summary: ✅


AI Review

Summary

This PR replaces the process-lifetime pointInPolygon preprocessing pools with one shared bounded cache, adds runtime sizing and SYSTEM DROP POINT IN POLYGON CACHE, and exposes cache usage via system.metrics. I reviewed the current PR diff, prior review threads, linked issue context, and the changed implementation paths; I did not find a new actionable correctness or safety issue that warrants an inline comment.

Final Verdict

✅ No blockers or majors found in this pass.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jun 11, 2026
Comment thread src/Functions/pointInPolygon.cpp Resolved
Comment thread src/Core/ServerSettings.cpp Resolved
@nihalzp
nihalzp requested a review from Algunenano June 11, 2026 17:17
@Algunenano Algunenano self-assigned this Jun 12, 2026

@Algunenano Algunenano left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • We need a way to drop the cache as we have SYSTEM DROP PARQUET METADATA CACHE, SYSTEM CLEAR|DROP TEXT INDEX CACHES, etc.
  • The cache should be using the arena for caches (JemallocCacheArena / JemallocCacheAllocator).

Comment thread src/Functions/pointInPolygon.cpp Resolved
Comment thread src/Functions/pointInPolygon.cpp Resolved
Comment thread programs/local/LocalServer.cpp Resolved
Comment thread src/Functions/pointInPolygon.cpp Resolved
Comment thread src/Functions/pointInPolygon.cpp Resolved
Comment thread src/Functions/pointInPolygon.cpp Resolved
Comment thread src/Functions/pointInPolygon.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.30% 85.40% +0.10%
Functions 92.60% 92.60% +0.00%
Branches 77.50% 77.60% +0.10%

Changed lines: Changed C/C++ lines covered by tests: 146/155 (94.19%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 3 line(s) · Uncovered code

Full report · Diff report

@Algunenano Algunenano left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@nihalzp
nihalzp added this pull request to the merge queue Jun 25, 2026
Merged via the queue into ClickHouse:master with commit 75ea03e Jun 25, 2026
328 of 330 checks passed
@nihalzp
nihalzp deleted the pointinpolygon-shared-bounded-cache branch June 25, 2026 18:41
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 25, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Jun 25, 2026
For a constant polygon, executeImpl parsed and hashed the polygon on every
input block before the preprocessed-polygon cache lookup, because the cache key
was a hash of the parsed geometry. Key the cache on the raw constant arguments
instead and move the parse into the cache-miss load function, so repeated
invocations skip the parse entirely. This addresses the per-block parse cost
behind the ARM regression in ClickHouse#106596, which the shared bounded cache added in
ClickHouse#107247 did not remove.

The key folds in the validate_polygons setting and the argument types: parsing
(and its validity check) runs only on a miss, so an entry built under one
validate mode or coordinate type must not satisfy a lookup under another.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Jun 26, 2026
For a constant polygon, executeImpl parsed and hashed the polygon on every
input block before the preprocessed-polygon cache lookup, because the cache key
was a hash of the parsed geometry. Key the cache on the raw constant arguments
instead and move the parse into the cache-miss load function, so repeated
invocations skip the parse entirely. This addresses the per-block parse cost
behind the ARM regression in ClickHouse#106596, which the shared bounded cache added in
ClickHouse#107247 did not remove.

The raw-argument key is computed with XXH3 over the constant columns' contiguous
buffers (array offsets, tuple elements, numeric leaves), the same hash the
parsed-geometry key used. Hashing the constant polygon is on the per-block hot
path, so an element-wise SipHash walk over the raw values regressed the
point_in_polygon_const_hashing perf test; the bulk XXH3 walk is faster than the
previous parse-then-hash (no parse on the hot path) and removes that regression.

The key folds in the validate_polygons setting and the argument types: parsing
(and its validity check) runs only on a miss, so an entry built under one
validate mode or coordinate type must not satisfy a lookup under another.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Jun 26, 2026
For a constant polygon, executeImpl parsed and hashed the polygon on every
input block before the preprocessed-polygon cache lookup, because the cache key
was a hash of the parsed geometry. Key the cache on the raw constant arguments
instead and move the parse into the cache-miss load function, so repeated
invocations skip the parse entirely. This addresses the per-block parse cost
behind the ARM regression in ClickHouse#106596, which the shared bounded cache added in
ClickHouse#107247 did not remove.

The raw-argument key is computed with XXH3 over the constant columns' contiguous
buffers (array offsets, tuple elements, numeric leaves), the same hash the
parsed-geometry key used. Hashing the constant polygon is on the per-block hot
path, so an element-wise SipHash walk over the raw values regressed the
point_in_polygon_const_hashing perf test; the bulk XXH3 walk is faster than the
previous parse-then-hash (no parse on the hot path) and removes that regression.

The key folds in the validate_polygons setting and the argument types: parsing
(and its validity check) runs only on a miss, so an entry built under one
validate mode or coordinate type must not satisfy a lookup under another.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Jun 26, 2026
For a constant polygon, executeImpl parsed and hashed the polygon on every
input block before the preprocessed-polygon cache lookup, because the cache key
was a hash of the parsed geometry. Key the cache on the raw constant arguments
instead and move the parse into the cache-miss load function, so repeated
invocations skip the parse entirely. This addresses the per-block parse cost
behind the ARM regression in ClickHouse#106596, which the shared bounded cache added in
ClickHouse#107247 did not remove.

The raw-argument key is computed with XXH3 over the constant columns' contiguous
buffers (array offsets, tuple elements, numeric leaves), the same hash the
parsed-geometry key used. Hashing the constant polygon is on the per-block hot
path, so an element-wise SipHash walk over the raw values regressed the
point_in_polygon_const_hashing perf test; the bulk XXH3 walk is faster than the
previous parse-then-hash (no parse on the hot path) and removes that regression.

The key folds in the validate_polygons setting and the argument types: parsing
(and its validity check) runs only on a miss, so an entry built under one
validate mode or coordinate type must not satisfy a lookup under another.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ed-bounded-cache

Make the `pointInPolygon` preprocessed polygon cache shared, bounded, and configurable
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
For a constant polygon, executeImpl parsed and hashed the polygon on every
input block before the preprocessed-polygon cache lookup, because the cache key
was a hash of the parsed geometry. Key the cache on the raw constant arguments
instead and move the parse into the cache-miss load function, so repeated
invocations skip the parse entirely. This addresses the per-block parse cost
behind the ARM regression in ClickHouse#106596, which the shared bounded cache added in
ClickHouse#107247 did not remove.

The raw-argument key is computed with XXH3 over the constant columns' contiguous
buffers (array offsets, tuple elements, numeric leaves), the same hash the
parsed-geometry key used. Hashing the constant polygon is on the per-block hot
path, so an element-wise SipHash walk over the raw values regressed the
point_in_polygon_const_hashing perf test; the bulk XXH3 walk is faster than the
previous parse-then-hash (no parse on the hot path) and removes that regression.

The key folds in the validate_polygons setting and the argument types: parsing
(and its validity check) runs only on a miss, so an entry built under one
validate mode or coordinate type must not satisfy a lookup under another.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pointInPolygon: preprocessed const-polygon cache is unbounded, duplicated per thread, and never released

3 participants