3030#include " duckdb/planner/filter/conjunction_filter.hpp"
3131#include " duckdb/common/types/value_map.hpp"
3232#include " duckdb/main/settings.hpp"
33+ #include < limits>
3334#include < list>
35+ #include < utility>
3436
3537namespace duckdb {
3638
@@ -486,86 +488,158 @@ vector<unique_ptr<Expression>> ExtractFilterExpressions(const ColumnDefinition &
486488
487489bool TryScanIndex (ART &art, const ColumnList &column_list, TableFunctionInitInput &input, TableFilterSet &filter_set,
488490 idx_t max_count, set<row_t > &row_ids) {
489- // FIXME: No support for index scans on compound ARTs.
490- // See note above on multi-filter support.
491- if (art.unbound_expressions .size () > 1 ) {
492- return false ;
491+ vector<unique_ptr<Expression>> index_exprs;
492+ for (const auto &expr : art.unbound_expressions ) {
493+ index_exprs.push_back (expr->Copy ());
493494 }
494495
495- auto index_expr = art. unbound_expressions [ 0 ]-> Copy ();
496+ // If this is a view, the column IDs are (may be?) relative to the view projection
496497 auto &indexed_columns = art.GetColumnIds ();
497498
498- // NOTE: We do not push down multi-column filters, e.g., 42 = a + b.
499- if (indexed_columns.size () != 1 ) {
499+ // Allow composite ART scans
500+ if (indexed_columns.size () != index_exprs. size () ) {
500501 return false ;
501502 }
502503
503504 // Resolve bound column references in the index_expr against the current input projection
504- column_t updated_index_column;
505- bool found_index_column_in_input = false ;
505+ bool rewrite_index_exprs = false ;
506+ vector<column_t > index_column_to_input_pos;
507+ index_column_to_input_pos.resize (indexed_columns.size (), std::numeric_limits<idx_t >::max ());
508+
509+ // Associate indexed columns to input columns
510+ for (idx_t i = 0 ; i < indexed_columns.size (); ++i) {
511+ for (idx_t j = 0 ; j < input.column_ids .size (); ++j) {
512+ if (indexed_columns[i] == input.column_ids [j]) {
513+ rewrite_index_exprs = i != j;
514+ index_column_to_input_pos.at (i) = j;
515+ break ;
516+ }
517+ }
518+ }
506519
507- // Find the indexed column amongst the input columns
508- for (idx_t i = 0 ; i < input.column_ids .size (); ++i) {
509- if (input.column_ids [i] == indexed_columns[0 ]) {
510- updated_index_column = i;
511- found_index_column_in_input = true ;
512- break ;
520+ // Make sure that all indexed_columns were bound, or bail out
521+ for (auto col : index_column_to_input_pos) {
522+ if (col == std::numeric_limits<idx_t >::max ()) {
523+ return false ;
513524 }
514525 }
515526
516- // If found, update the bound column ref within index_expr
517- if (found_index_column_in_input) {
518- ExpressionIterator::EnumerateExpression (index_expr, [&](Expression &expr) {
519- if (expr.GetExpressionClass () != ExpressionClass::BOUND_COLUMN_REF ) {
520- return ;
527+ // Allow scan only if index expressions reference ONE column each, and that column
528+ // is associated with an indexed_column
529+ // NOTE: We do not push down multi-column filters, e.g., 42 = a + b.
530+ for (idx_t i = 0 ; i < index_exprs.size (); ++i) {
531+ unordered_set<column_t > referenced_columns;
532+ auto expr = &index_exprs[i];
533+
534+ // Walk the expr in case of nesting (e.g. function)
535+ ExpressionIterator::EnumerateExpression (*expr, [&](Expression &child_expr) {
536+ if (child_expr.GetExpressionClass () == ExpressionClass::BOUND_COLUMN_REF ) {
537+ auto &col_ref = child_expr.Cast <BoundColumnRefExpression>();
538+ referenced_columns.insert (col_ref.binding .column_index );
521539 }
540+ });
522541
523- auto &bound_column_ref_expr = expr.Cast <BoundColumnRefExpression>();
542+ if (referenced_columns.size () != 1 ) {
543+ return false ;
544+ }
524545
525- // If the bound column references the index column, use updated_index_column
526- if (bound_column_ref_expr.binding .column_index == indexed_columns[0 ]) {
527- bound_column_ref_expr.binding .column_index = updated_index_column;
528- }
529- });
530- }
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+ }
531551
532- // Get ART column.
533- auto &col = column_list. GetColumn ( LogicalIndex ( indexed_columns[0 ])) ;
552+ // The column for this position matches the indexed_column ID for this position directly
553+ auto direct_match = input. column_ids [ref_col_idx] == indexed_columns[i] ;
534554
535- // The indexes of the filters match input.column_indexes, which are: i -> column_index.
536- // Try to find a filter on the ART column.
537- optional_idx storage_index;
538- for (idx_t i = 0 ; i < input.column_indexes .size (); i++) {
539- if (input.column_indexes [i].ToLogical () == col.Logical ()) {
540- storage_index = i;
541- break ;
555+ // We should know if there is a different mapping for this reference.
556+ // If there is not, it won't match, so it is not worth trying.
557+ if (!direct_match && !rewrite_index_exprs) {
558+ return false ;
559+ }
560+
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];
564+
565+ if (!(direct_match || remapped_match)) {
566+ return false ;
542567 }
543568 }
544569
545- // No filter matches the ART column.
546- if (!storage_index.IsValid ()) {
547- return false ;
570+ // If the position of the indexed_columns differs from the order of the input, remap the index expressions
571+ if (rewrite_index_exprs) {
572+ for (auto &index_expr : index_exprs) {
573+ ExpressionIterator::EnumerateExpression (index_expr, [&](Expression &expr) {
574+ if (expr.GetExpressionClass () != ExpressionClass::BOUND_COLUMN_REF ) {
575+ return ;
576+ }
577+
578+ auto &bound_column_ref_expr = expr.Cast <BoundColumnRefExpression>();
579+
580+ // If the bound column references an indexed column, update it
581+ for (idx_t i = 0 ; i < indexed_columns.size (); ++i) {
582+ auto remapped_index = index_column_to_input_pos[bound_column_ref_expr.binding .column_index ];
583+ if (input.column_ids [remapped_index] == indexed_columns[i]) {
584+ bound_column_ref_expr.binding .column_index = index_column_to_input_pos[i];
585+ break ;
586+ }
587+ }
588+ });
589+ }
590+ }
591+
592+ // The indexes of the filters match input.column_indexes, which are: i -> column_index.
593+ // Reuse the index <-> projection mappings from index expr rebinding (which are canonical even if not rewriting)
594+ vector<vector<unique_ptr<Expression>>> index_filters;
595+
596+ for (idx_t i = 0 ; i < index_column_to_input_pos.size (); ++i) {
597+ auto column_def = &column_list.GetColumn (LogicalIndex (indexed_columns[i]));
598+ auto maybe_filter = filter_set.filters .find (index_column_to_input_pos[i]);
599+ if (maybe_filter != filter_set.filters .end ()) {
600+ auto filter = &maybe_filter->second ;
601+ auto filter_expressions = ExtractFilterExpressions (*column_def, *filter, index_column_to_input_pos[i]);
602+
603+ index_filters.push_back (std::move (filter_expressions));
604+ }
548605 }
549606
550- // Try to find a matching filter for the column.
551- auto filter = filter_set.filters .find (storage_index.GetIndex ());
552- if (filter == filter_set.filters .end ()) {
607+ // Index filters must:
608+ // - Match ART column count 1:1
609+ // - Match filter expression set 1:1 (there may be filters on non-indexed columns, bail out if so)
610+ if (index_filters.size () != indexed_columns.size () || filter_set.filters .size () != index_filters.size () ||
611+ index_filters.empty ()) {
553612 return false ;
554613 }
555614
556- auto expressions = ExtractFilterExpressions (col, filter-> second , storage_index. GetIndex ());
557- for ( const auto &filter_expr : expressions ) {
558- auto scan_state = art.TryInitializeScan (*index_expr, *filter_expr );
615+ // Do a compound scan if we have filter exprs bound for several columns
616+ if (index_filters. size () > 1 ) {
617+ auto scan_state = art.TryInitializeCompoundKeyScan (index_exprs, index_filters );
559618 if (!scan_state) {
560619 return false ;
561620 }
562621
563- // Check if we can use an index scan, and already retrieve the matching row ids.
564- if (!art.Scan (*scan_state, max_count, row_ids)) {
622+ if (!art.CompoundKeyScan (*scan_state, max_count, row_ids)) {
565623 row_ids.clear ();
566624 return false ;
567625 }
568626 }
627+ // Original single column index scan
628+ else {
629+ for (const auto &filter_expr : index_filters[0 ]) {
630+ auto scan_state = art.TryInitializeScan (*index_exprs[0 ], *filter_expr);
631+ if (!scan_state) {
632+ return false ;
633+ }
634+
635+ // Check if we can use an index scan, and already retrieve the matching row ids.
636+ if (!art.Scan (*scan_state, max_count, row_ids)) {
637+ row_ids.clear ();
638+ return false ;
639+ }
640+ }
641+ }
642+
569643 return true ;
570644}
571645
@@ -588,9 +662,9 @@ unique_ptr<GlobalTableFunctionState> TableScanInitGlobal(ClientContext &context,
588662 // 1.2. Find + scan one ART for b = 24.
589663 // 1.3. Return the intersecting row IDs.
590664 // 2. (Reorder and) scan a single ART with a compound key of (a, b).
591- if (filter_set.filters .size () != 1 ) {
592- return DuckTableScanInitGlobal (context, input, storage, bind_data);
593- }
665+ // if (filter_set.filters.size() != 1) {
666+ // return DuckTableScanInitGlobal(context, input, storage, bind_data);
667+ // }
594668
595669 // The checkpoint lock ensures that we do not checkpoint while scanning this table.
596670 auto &transaction = DuckTransaction::Get (context, storage.db );
0 commit comments