diff --git a/src/spatial/index/rtree/rtree_index_plan_scan.cpp b/src/spatial/index/rtree/rtree_index_plan_scan.cpp index 38fa3e57..6079be18 100644 --- a/src/spatial/index/rtree/rtree_index_plan_scan.cpp +++ b/src/spatial/index/rtree/rtree_index_plan_scan.cpp @@ -200,6 +200,14 @@ class RTreeIndexScanOptimizer : public OptimizerExtension { if (get.table_filters.filters.empty()) { return true; } + + // Before we clear projection ids, replace projection map in the filter + if (!get.projection_ids.empty()) { + for (auto &id : filter.projection_map) { + id = get.projection_ids[id]; + } + } + get.projection_ids.clear(); get.types.clear(); diff --git a/test/sql/index/rtree_filter_pullup_2.test b/test/sql/index/rtree_filter_pullup_2.test new file mode 100644 index 00000000..46cacf16 --- /dev/null +++ b/test/sql/index/rtree_filter_pullup_2.test @@ -0,0 +1,17 @@ +require spatial + +# This test only really works on release builds, where the column lifetime optimizers +# doesnt pollute the plan (but still applies) + +statement ok +CREATE TABLE t1 AS +SELECT st_point(i, j) as pt, row_number() over () as i, row_number () over () as j +FROM range(0, 3) as r(i), range(0, 3) as rr(j); + +statement ok +CREATE INDEX my_idx ON t1 USING RTREE (pt); + +query I rowsort +SELECT i FROM t1 WHERE j = 5 AND ST_Contains(ST_GeomFromText('POLYGON((0 0, 0 50, 50 50, 50 0, 0 0))'), pt); +---- +5 \ No newline at end of file