Skip to content

Commit 9dac7ac

Browse files
committed
Make compound ART index scan more robust for views
rewrite_index_exprs was overwritten (not OR'd) per column match, so if the last index column sat at position i==j the flag reset to false, silently falling back to a sequential scan. Accumulate the flag instead. Add regression test for partially reordered view projections.
1 parent 7947412 commit 9dac7ac

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/function/table/table_scan.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ bool TryScanIndex(ART &art, IndexEntry &entry, const ColumnList &column_list, Ta
583583
for (idx_t i = 0; i < indexed_columns.size(); ++i) {
584584
for (idx_t j = 0; j < input.column_ids.size(); ++j) {
585585
if (indexed_columns[i] == input.column_ids[j]) {
586-
rewrite_index_exprs = i != j;
586+
rewrite_index_exprs = rewrite_index_exprs || i != j;
587587
index_column_to_input_pos.at(i) = j;
588588
break;
589589
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# name: test/sql/index/art/issues/test_art_rewrite_index_exprs.test
2+
# description: Test index scan through views where the last index column has i==j position
3+
# group: [issues]
4+
5+
# Bug: rewrite_index_exprs was assigned (not OR'd) per match. If the last index
6+
# column sits at position i==j the flag resets to false, skipping column rebinding.
7+
8+
statement ok
9+
CREATE TABLE t (a INTEGER, b INTEGER, c INTEGER);
10+
11+
statement ok
12+
INSERT INTO t VALUES (1, 2, 3), (4, 5, 6), (7, 8, 9);
13+
14+
statement ok
15+
CREATE INDEX idx ON t(a, b, c);
16+
17+
# View swaps a and b but keeps c at position 2 (i==j for last index col)
18+
statement ok
19+
CREATE VIEW v AS SELECT b, a, c FROM t;
20+
21+
# Must use Index Scan, not Sequential Scan
22+
query II
23+
explain analyze select * from v where a = 1 AND b = 2 AND c = 3;
24+
----
25+
analyzed_plan <REGEX>:.*Index Scan.*
26+
27+
query III
28+
select b, a, c from v where a = 1 AND b = 2 AND c = 3;
29+
----
30+
2 1 3

0 commit comments

Comments
 (0)