Skip to content

fix(spatial): keep joins on term ids within the query memory (#811) - #821

Merged
MathiasVDA merged 3 commits into
mainfrom
b5/side-memory
Dec 3, 2026
Merged

MathiasVDA merged 3 commits into
mainfrom
b5/side-memory

Conversation

@MathiasVDA

@MathiasVDA MathiasVDA commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Requested by Mathias · project thread

Closes #811

What and why

A spatial join on term ids held both sides' rows and boxes, and its table, whole, whatever --query-memory said. A join of two large sides could therefore take many times the limit.

Now, with a query memory set:

  • The side estimated smaller is held. It is read in parts of a quarter of the limit and may take at most half of it.
  • The other side is streamed in parts of a quarter of the limit (Snapshot::join_ids_each_part, the per-chunk form of perf(spatial): osm_track_landuse times out on OSM Europe at 1B triples #340's join_ids_in_parts). Each part is swept against the held parts, and its pairs are appended to one table.
  • When held + part + table would pass the limit, the rewrite records RewriteStats::over_memory. The query then fails with QueryEvaluationError::MemoryLimit, which is 503 on the server, the same as a hash join of the evaluator.
  • Part sizes use one shared per-geometry size, tripletrail_spatial::GEOMETRY_BYTES, so the reader's estimate matches the join's accounting. The floor of 64 key values per part is now 1.

Documented margin: while a part is read, the values of its key (8 bytes each), plus the parsed geometries of the candidates being tested.

With no limit (memory_limit() is None), the sides are read whole as before.

How it was tested

  • New joins_on_ids_stay_within_the_query_memory in cost_model.rs, on 2,500 spots and 2,500 zones:
    • at 64 MiB the join is swept once;
    • at 1 MiB it is swept in 4 parts, with the same 2,500 rows on ids;
    • at 256 KiB the query fails with the memory error.
  • a_join_side_larger_than_the_query_memory_is_read_in_parts (perf(spatial): osm_track_landuse times out on OSM Europe at 1B triples #340) still completes at 8 KiB, now in more than one sweep.
  • cargo test -p tripletrail -p tripletrail-spatial -p tripletrail-store, clippy -D warnings and fmt are clean.

Documentation

  • docs/server.md (Query memory): the held, part and table bounds, and the margin.
  • docs/spatial-index.md
  • Guide: docs/guide/13-production.md
  • CHANGELOG (Unreleased, Fixed)

Generated by Claude Code

The side estimated smaller is held in at most half of the query memory
and the other is read in parts of a quarter of it, each part swept
against the held side and its pairs added to the table. When the held
side, a part and the table would pass the limit, the query fails with
the memory error, as a hash join of the evaluator does. Without a limit
nothing changes.
@MathiasVDA MathiasVDA self-assigned this Dec 3, 2026
@MathiasVDA MathiasVDA added the review:changes Reviewer asks for changes label Dec 3, 2026 — with Claude

Copy link
Copy Markdown
Contributor Author

Changes requested at 50f25cd. The design (hold the smaller side in half the limit, stream the other in quarters, fail past the limit) fits #811 and the accounting reads right. Two must-fix items, both small.

1. With no query memory set, a side past the 1 GiB guard is no longer read in parts (regression of #340).
In side_ids_each (crates/tripletrail/src/lib.rs), bytes == usize::MAX makes whole true. When join_ids then gives up (the side takes more than max_ids(), which without a limit is still the 128 M id guard), the code goes on with values = usize::MAX, so join_ids_each_part puts every key value in one chunk and its self.join(...) gives up for the same reason. The side comes back as not read and the join runs row by row again. Before this PR that case went to join_ids_in_parts, whose parts were max_ids() / (4 * width) values. A server always has a limit, so this hits the library, Python and CLI use on a large store. Please keep the old part size when bytes == usize::MAX, and correct the PR text and docs ("read whole as before" was "whole, or in parts past the guard").

2. The memory error is read from the shared spatial.last, so it can land on another query.
The four call sites store stats in self.spatial.last and then check_spatial_memory() locks it again and reads over_memory. Two requests on the same engine race: query B stores its stats, query A stores stats with over_memory, B then reads As and answers 503 although it has no join over the limit; A, if B overwrote it first, misses its own error and runs the join row by row. Please decide from the local statsbefore (or while) storing it, for examplelet over = stats.over_memory; *last = stats; ...; if let Some(limit) = over { return Err(MemoryLimit(limit)) }`.

Small, with the same push: the doc comment of check_cancelled now sits on top of check_spatial_memory (the new function was put between the comment and its function).

Not blocking, for the next scale run as the issue says: at the default 1 GiB a join whose smaller side passes 512 MiB now answers 503 where it answered before; from the 1B diagnostic run the osm_* sides (1.4 M tracks, about 180 MB) stay under it.


Generated by Claude Code

…the right query (#811)

Without a query memory, join_ids_each_part takes usize::MAX values as parts
within the join memory again, as join_ids_in_parts did, rather than one part
of the whole side. The memory error is decided from the query's own rewrite
statistics, not the engine's last ones, which a concurrent query may replace.
@MathiasVDA MathiasVDA removed the review:changes Reviewer asks for changes label Dec 3, 2026
@MathiasVDA MathiasVDA added the review:approved Reviewer approved label Dec 3, 2026 — with Claude

Copy link
Copy Markdown
Contributor Author

Approved at 2158f89. Both items are fixed: with no query memory the parts are #340's again (and join_in_parts.rs checks it), and the memory error comes from the query's own statistics. Locally join_in_parts and the 19 cost_model tests pass and clippy is clean. I merge on green CI.


Generated by Claude Code

@MathiasVDA
MathiasVDA merged commit cab0f26 into main Dec 3, 2026
15 checks passed
@MathiasVDA
MathiasVDA deleted the b5/side-memory branch December 3, 2026 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review:approved Reviewer approved

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(spatial): a spatial join side's rows and boxes are not bounded by the query memory

1 participant