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,134 @@ 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 ();
496496 auto &indexed_columns = art.GetColumnIds ();
497497
498- // NOTE: We do not push down multi-column filters, e.g., 42 = a + b.
499- if (indexed_columns.size () != 1 ) {
498+ // Allow composite ART scans
499+ if (indexed_columns.size () != index_exprs. size () ) {
500500 return false ;
501501 }
502502
503+ // ...only if each expression has a single column reference, and
504+ // that single column reference positionally matches that of the index (this is guaranteed? paranoid?)
505+ // NOTE: We do not push down multi-column filters, e.g., 42 = a + b.
506+ for (idx_t i = 0 ; i < index_exprs.size (); ++i) {
507+ unordered_set<column_t > referenced_columns;
508+ auto expr = &index_exprs[i];
509+
510+ // Walk the expr in case of nesting (e.g. function)
511+ ExpressionIterator::EnumerateExpression (*expr, [&](Expression &child_expr) {
512+ if (child_expr.GetExpressionClass () == ExpressionClass::BOUND_COLUMN_REF ) {
513+ auto &col_ref = child_expr.Cast <BoundColumnRefExpression>();
514+ referenced_columns.insert (col_ref.binding .column_index );
515+ }
516+ });
517+
518+ if (referenced_columns.size () != 1 || *referenced_columns.begin () != indexed_columns[i]) {
519+ return false ;
520+ }
521+ }
522+
503523 // Resolve bound column references in the index_expr against the current input projection
504- column_t updated_index_column;
524+ vector<column_t > index_column_to_proj_pos;
525+ index_column_to_proj_pos.resize (indexed_columns.size (), std::numeric_limits<idx_t >::max ());
526+
505527 bool found_index_column_in_input = false ;
506528
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 ;
529+ // Associate indexed columns to input columns
530+ for (idx_t i = 0 ; i < indexed_columns.size (); ++i) {
531+ for (idx_t j = 0 ; j < input.column_ids .size (); ++j) {
532+ if (indexed_columns[i] == input.column_ids [j]) {
533+ index_column_to_proj_pos.at (i) = j;
534+ found_index_column_in_input = true ;
535+ }
513536 }
514537 }
515538
516- // If found, update the bound column ref within index_expr
517- if (found_index_column_in_input) {
539+ if (!found_index_column_in_input) {
540+ return false ;
541+ }
542+
543+ for (auto col : index_column_to_proj_pos) {
544+ if (col == std::numeric_limits<idx_t >::max ()) {
545+ return false ;
546+ }
547+ }
548+
549+ // Update the bound column refs within all index_exprs
550+ for (auto &index_expr : index_exprs) {
518551 ExpressionIterator::EnumerateExpression (index_expr, [&](Expression &expr) {
519552 if (expr.GetExpressionClass () != ExpressionClass::BOUND_COLUMN_REF ) {
520553 return ;
521554 }
522555
523556 auto &bound_column_ref_expr = expr.Cast <BoundColumnRefExpression>();
524557
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;
558+ // If the bound column references an indexed column, update it
559+ for (idx_t i = 0 ; i < indexed_columns.size (); ++i) {
560+ if (bound_column_ref_expr.binding .column_index == indexed_columns[i]) {
561+ bound_column_ref_expr.binding .column_index = index_column_to_proj_pos[i];
562+ break ;
563+ }
528564 }
529565 });
530566 }
531567
532- // Get ART column.
533- auto &col = column_list.GetColumn (LogicalIndex (indexed_columns[0 ]));
534-
535568 // 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 ;
542- }
543- }
569+ // Reuse the index <-> projection mappings from index expr rebinding
570+ vector<vector<unique_ptr<Expression>>> index_filters;
544571
545- // No filter matches the ART column.
546- if (!storage_index.IsValid ()) {
547- return false ;
572+ for (idx_t i = 0 ; i < index_column_to_proj_pos.size (); ++i) {
573+ auto column_def = &column_list.GetColumn (LogicalIndex (indexed_columns[i]));
574+ auto maybe_filter = filter_set.filters .find (index_column_to_proj_pos[i]);
575+ if (maybe_filter != filter_set.filters .end ()) {
576+ auto filter = &maybe_filter->second ;
577+ auto filter_expressions = ExtractFilterExpressions (*column_def, *filter, index_column_to_proj_pos[i]);
578+
579+ index_filters.push_back (std::move (filter_expressions));
580+ }
548581 }
549582
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 ()) {
583+ // Index filters must:
584+ // - Match ART column count 1:1
585+ // - Match filter expression set 1:1 (there may be filters on non-indexed columns, bail out if so)
586+ if (index_filters.size () != indexed_columns.size () || filter_set.filters .size () != index_filters.size () ||
587+ index_filters.empty ()) {
553588 return false ;
554589 }
555590
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 );
591+ // Do a compound scan if we have filter exprs bound for several columns
592+ if (index_filters. size () > 1 ) {
593+ auto scan_state = art.TryInitializeCompoundKeyScan (index_exprs, index_filters );
559594 if (!scan_state) {
560595 return false ;
561596 }
562597
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)) {
598+ if (!art.CompoundKeyScan (*scan_state, max_count, row_ids)) {
565599 row_ids.clear ();
566600 return false ;
567601 }
568602 }
603+ // Original single column index scan
604+ else {
605+ for (const auto &filter_expr : index_filters[0 ]) {
606+ auto scan_state = art.TryInitializeScan (*index_exprs[0 ], *filter_expr);
607+ if (!scan_state) {
608+ return false ;
609+ }
610+
611+ // Check if we can use an index scan, and already retrieve the matching row ids.
612+ if (!art.Scan (*scan_state, max_count, row_ids)) {
613+ row_ids.clear ();
614+ return false ;
615+ }
616+ }
617+ }
618+
569619 return true ;
570620}
571621
@@ -588,9 +638,9 @@ unique_ptr<GlobalTableFunctionState> TableScanInitGlobal(ClientContext &context,
588638 // 1.2. Find + scan one ART for b = 24.
589639 // 1.3. Return the intersecting row IDs.
590640 // 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- }
641+ // if (filter_set.filters.size() != 1) {
642+ // return DuckTableScanInitGlobal(context, input, storage, bind_data);
643+ // }
594644
595645 // The checkpoint lock ensures that we do not checkpoint while scanning this table.
596646 auto &transaction = DuckTransaction::Get (context, storage.db );
0 commit comments