@@ -512,6 +512,7 @@ bool TryScanIndex(ART &art, const ColumnList &column_list, TableFunctionInitInpu
512512 if (indexed_columns[i] == input.column_ids [j]) {
513513 rewrite_index_exprs = i != j;
514514 index_column_to_input_pos.at (i) = j;
515+ break ;
515516 }
516517 }
517518 }
@@ -542,20 +543,24 @@ bool TryScanIndex(ART &art, const ColumnList &column_list, TableFunctionInitInpu
542543 return false ;
543544 }
544545
545- auto referenced_column = *referenced_columns.begin ();
546+ // Make sure the column reference can be looked up
547+ auto ref_col_idx = *referenced_columns.begin ();
548+ if (ref_col_idx >= index_column_to_input_pos.size () || ref_col_idx >= input.column_ids .size ()) {
549+ return false ;
550+ }
546551
547552 // The column for this position matches the indexed_column ID for this position directly
548- auto direct_match = referenced_column == indexed_columns[i];
553+ auto direct_match = input. column_ids [ref_col_idx] == indexed_columns[i];
549554
550555 // We should know if there is a different mapping for this reference.
551556 // If there is not, it won't match, so it is not worth trying.
552557 if (!direct_match && !rewrite_index_exprs) {
553558 return false ;
554559 }
555560
556- auto remapped_column_id = index_column_to_input_pos[referenced_column ];
557- auto remapped_match =
558- input. column_ids . size () > remapped_column_id && input.column_ids [remapped_column_id ] == indexed_columns[i];
561+ auto remapped_cid_position = index_column_to_input_pos[ref_col_idx ];
562+ auto remapped_match = remapped_cid_position < input. column_ids . size () &&
563+ input.column_ids [remapped_cid_position ] == indexed_columns[i];
559564
560565 if (!(direct_match || remapped_match)) {
561566 return false ;
0 commit comments