Skip to content

Commit 4daad0a

Browse files
committed
Fix bug: accidentally skipped index selection for other tables except first found
1 parent 457bded commit 4daad0a

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

core/translate/optimizer.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,12 @@ fn use_indexes(
301301

302302
// Try to use indexes for WHERE conditions
303303
for (table_index, table_reference) in table_references.iter_mut().enumerate() {
304-
if let Operation::Scan {
305-
iter_dir, index, ..
306-
} = &table_reference.op
307-
{
304+
if matches!(table_reference.op, Operation::Scan { .. }) {
305+
let index = if let Operation::Scan { index, .. } = &table_reference.op {
306+
Option::clone(index)
307+
} else {
308+
None
309+
};
308310
match index {
309311
// If we decided to eliminate ORDER BY using an index, let's constrain our search to only that index
310312
Some(index) => {
@@ -319,7 +321,6 @@ fn use_indexes(
319321
table_index,
320322
table_reference,
321323
&available_indexes,
322-
*iter_dir,
323324
)? {
324325
table_reference.op = Operation::Search(search);
325326
}
@@ -328,13 +329,18 @@ fn use_indexes(
328329
let table_name = table_reference.table.get_name();
329330

330331
// If we can utilize the rowid alias of the table, let's preferentially always use it for now.
331-
for (i, term) in where_clause.iter_mut().enumerate() {
332-
if let Some(search) =
333-
try_extract_rowid_search_expression(term, table_index, *iter_dir)?
334-
{
332+
let mut i = 0;
333+
while i < where_clause.len() {
334+
if let Some(search) = try_extract_rowid_search_expression(
335+
&mut where_clause[i],
336+
table_index,
337+
table_reference,
338+
)? {
335339
where_clause.remove(i);
336340
table_reference.op = Operation::Search(search);
337-
return Ok(());
341+
continue;
342+
} else {
343+
i += 1;
338344
}
339345
}
340346
if let Some(indexes) = available_indexes.get(table_name) {
@@ -343,7 +349,6 @@ fn use_indexes(
343349
table_index,
344350
table_reference,
345351
indexes,
346-
*iter_dir,
347352
)? {
348353
table_reference.op = Operation::Search(search);
349354
}
@@ -624,7 +629,6 @@ pub fn try_extract_index_search_from_where_clause(
624629
table_index: usize,
625630
table_reference: &TableReference,
626631
table_indexes: &[Arc<Index>],
627-
iter_dir: IterationDirection,
628632
) -> Result<Option<Search>> {
629633
// If there are no WHERE terms, we can't extract a search
630634
if where_clause.is_empty() {
@@ -635,6 +639,12 @@ pub fn try_extract_index_search_from_where_clause(
635639
return Ok(None);
636640
}
637641

642+
let iter_dir = if let Operation::Scan { iter_dir, .. } = &table_reference.op {
643+
*iter_dir
644+
} else {
645+
return Ok(None);
646+
};
647+
638648
// Find all potential index constraints
639649
// For WHERE terms to be used to constrain an index scan, they must:
640650
// 1. refer to columns in the table that the index is on
@@ -1096,8 +1106,13 @@ fn build_seek_def(
10961106
pub fn try_extract_rowid_search_expression(
10971107
cond: &mut WhereTerm,
10981108
table_index: usize,
1099-
iter_dir: IterationDirection,
1109+
table_reference: &TableReference,
11001110
) -> Result<Option<Search>> {
1111+
let iter_dir = if let Operation::Scan { iter_dir, .. } = &table_reference.op {
1112+
*iter_dir
1113+
} else {
1114+
return Ok(None);
1115+
};
11011116
if !cond.should_eval_at_loop(table_index) {
11021117
return Ok(None);
11031118
}

0 commit comments

Comments
 (0)