refactor: extract FlatRTree to shared header + add KNN search primitive#796
Open
pierre-warnier wants to merge 2 commits intoduckdb:v1.5-variegatafrom
Open
refactor: extract FlatRTree to shared header + add KNN search primitive#796pierre-warnier wants to merge 2 commits intoduckdb:v1.5-variegatafrom
pierre-warnier wants to merge 2 commits intoduckdb:v1.5-variegatafrom
Conversation
The Copy() method returns a unique_ptr<BindData> which must be converted to unique_ptr<FunctionData>. Under GCC 11+ this conversion requires explicit std::move() — newer compilers reject the implicit conversion as a copy-initialization. Fixes: error: could not convert 'copy' from 'unique_ptr<ST_Distance_Sphere::BindData>' to 'unique_ptr<FunctionData>'
This was referenced Apr 15, 2026
There was a problem hiding this comment.
Pull request overview
Refactors the spatial join implementation by extracting the internal FlatRTree implementation into a reusable header, and adds foundational primitives needed for a future KNN spatial join operator (plus a small GCC11-related unique_ptr return fix).
Changes:
- Moved
FlatRTree,typed_view, and scan state types out ofspatial_join_physical.cppinto a new shared headerflat_rtree.hpp. - Added a best-first (Hjaltason–Samet)
FlatRTree::KNNSearch()primitive with a correspondingFlatRTreeKNNState. - Added
Box2D::MinDistanceSquaredhelpers and applied the GCC 11+unique_ptrreturn fix inST_Distance_Sphere.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| src/spatial/operators/spatial_join_physical.cpp | Replaces the inlined FlatRTree implementation with an include of the extracted shared header. |
| src/spatial/operators/flat_rtree.hpp | New shared header containing FlatRTree and new KNN traversal primitive. |
| src/spatial/geometry/bbox.hpp | Adds min squared distance helpers for point→box and box→box, used by KNN priority ordering. |
| src/spatial/modules/main/spatial_functions_scalar.cpp | Fixes GCC11+ compilation by explicitly std::move-returning a derived unique_ptr. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Move FlatRTree from anonymous namespace in spatial_join_physical.cpp to a shared header (flat_rtree.hpp) so it can be reused by the upcoming KNN join operator. Changes: - Extract FlatRTree class to src/spatial/operators/flat_rtree.hpp - Add KNNSearch method using Hjaltason-Samet priority queue traversal - Add FlatRTreeKNNState for KNN search state management - Add Box2D::MinDistanceSquared for KNN distance lower bound - Use Allocator instead of BufferManager (in-memory only) - No functional changes to existing SPATIAL_JOIN behavior
324000b to
707ea8e
Compare
Author
|
Addressed the 5 review items in the amended commit (
All 133 existing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Preparation for the upcoming KNN spatial join operator. This PR is pure plumbing — no new user-visible functionality, no behavior change to the existing
SPATIAL_JOIN.Depends on: #795 (GCC 11+ build fix). Merging PR #795 first is recommended so this branch can rebase cleanly onto
v1.5-variegata.Changes
1. Extract
FlatRTreefrom anonymous namespaceFlatRTree(and helperstyped_view,FlatRTreeScanState) were defined inline inspatial_join_physical.cppinside an anonymous namespace. They move to a new public headersrc/spatial/operators/flat_rtree.hppso the upcoming KNN operator can reuse them.spatial_join_physical.cpploses 361 lines, gains 1#includeflat_rtree.hppis new (418 lines)2. Add KNN search primitive
FlatRTreegains aKNNSearch()method implementing Hjaltason-Samet best-first traversal (priority queue on squared bbox distance). The newFlatRTreeKNNStateholds the scan state (heap, result vector).This is not wired into any user-facing function yet — it's the building block for the next PR in the stack.
3. Add
Box2D::MinDistanceSquaredhelpersTwo overloads on
Box2D<T>insrc/spatial/geometry/bbox.hpp:MinDistanceSquared(const V &point)— point to boxMinDistanceSquared(const Box &other)— box to boxBoth return 0 for inside/overlapping. Used by the KNN priority queue's lower-bound distance. Zero existing callers, so no risk of affecting
SPATIAL_JOIN.Verification
SPATIAL_JOINtests unchanged and green:test/sql/join/*— 133 assertions passgit diff --stat:What this PR does NOT do
Why split it out
Reviewing a 1600+ line KNN operator PR is painful. This PR lets reviewers focus on the code movement and the new primitive in isolation. The operator PR then only adds the integration.