@@ -90,9 +90,9 @@ func TryFilterInvertedIndex(
9090) {
9191 // Attempt to constrain the prefix columns, if there are any. If they cannot
9292 // be constrained to single values, the index cannot be used.
93- columns , notNullCols := prefixCols (tabID , index )
93+ columns , notNullCols := idxconstraint . IndexPrefixCols (tabID , index )
9494 if len (columns ) > 0 {
95- constraint , filters , ok = constrainNonInvertedCols (
95+ constraint , filters , ok = idxconstraint . ConstrainIndexPrefixCols (
9696 ctx , evalCtx , factory , columns , notNullCols , filters ,
9797 optionalFilters , tabID , index , checkCancellation ,
9898 )
@@ -299,7 +299,7 @@ func TryFilterInvertedIndexBySimilarity(
299299 // If the index is a multi-column index, then we need to constrain the
300300 // prefix columns.
301301 var prefixConstraint * constraint.Constraint
302- prefixConstraint , remainingFilters , ok = constrainNonInvertedCols (
302+ prefixConstraint , remainingFilters , ok = idxconstraint . ConstrainIndexPrefixCols (
303303 ctx , evalCtx , f , cols , notNullCols , filters ,
304304 optionalFilters , tabID , index , checkCancellation ,
305305 )
@@ -634,91 +634,6 @@ func evalInvertedExpr(
634634 }
635635}
636636
637- // prefixCols returns a slice of ordering columns for each of the non-inverted
638- // prefix of the index. It also returns a set of those columns that are NOT
639- // NULL. If the index is a single-column inverted index, the function returns
640- // nil ordering columns.
641- func prefixCols (
642- tabID opt.TableID , index cat.Index ,
643- ) (_ []opt.OrderingColumn , notNullCols opt.ColSet ) {
644- prefixColumnCount := index .PrefixColumnCount ()
645-
646- // If this is a single-column inverted index, there are no prefix columns.
647- // constrain.
648- if prefixColumnCount == 0 {
649- return nil , opt.ColSet {}
650- }
651-
652- prefixColumns := make ([]opt.OrderingColumn , prefixColumnCount )
653- for i := range prefixColumns {
654- col := index .Column (i )
655- colID := tabID .ColumnID (col .Ordinal ())
656- prefixColumns [i ] = opt .MakeOrderingColumn (colID , col .Descending )
657- if ! col .IsNullable () {
658- notNullCols .Add (colID )
659- }
660- }
661- return prefixColumns , notNullCols
662- }
663-
664- // constrainNonInvertedCols attempts to build a constraint for the non-inverted
665- // prefix columns of the given index. If a constraint is successfully built, it
666- // is returned along with remaining filters and ok=true. The function is only
667- // successful if it can generate a constraint where all spans have the same
668- // start and end keys for all non-inverted prefix columns. This is required for
669- // building spans for scanning multi-column inverted indexes (see
670- // span.Builder.SpansFromInvertedSpans).
671- func constrainNonInvertedCols (
672- ctx context.Context ,
673- evalCtx * eval.Context ,
674- factory * norm.Factory ,
675- columns []opt.OrderingColumn ,
676- notNullCols opt.ColSet ,
677- filters memo.FiltersExpr ,
678- optionalFilters memo.FiltersExpr ,
679- tabID opt.TableID ,
680- index cat.Index ,
681- checkCancellation func (),
682- ) (_ * constraint.Constraint , remainingFilters memo.FiltersExpr , ok bool ) {
683- tabMeta := factory .Metadata ().TableMeta (tabID )
684- prefixColumnCount := index .PrefixColumnCount ()
685- ps := tabMeta .IndexPartitionLocality (index .Ordinal ())
686-
687- // Consolidation of a constraint converts contiguous spans into a single
688- // span. By definition, the consolidated span would have different start and
689- // end keys and could not be used for multi-column inverted index scans.
690- // Therefore, we only generate and check the unconsolidated constraint,
691- // allowing the optimizer to plan multi-column inverted index scans in more
692- // cases.
693- //
694- // For example, the consolidated constraint for (x IN (1, 2, 3)) is:
695- //
696- // /x: [/1 - /3]
697- // Prefix: 0
698- //
699- // The unconsolidated constraint for the same expression is:
700- //
701- // /x: [/1 - /1] [/2 - /2] [/3 - /3]
702- // Prefix: 1
703- //
704- var ic idxconstraint.Instance
705- ic .Init (
706- ctx , filters , optionalFilters ,
707- columns , notNullCols , tabMeta .ComputedCols ,
708- tabMeta .ColsInComputedColsExpressions ,
709- false , /* consolidate */
710- evalCtx , factory , ps , checkCancellation ,
711- )
712- var c constraint.Constraint
713- ic .UnconsolidatedConstraint (& c )
714- if c .Prefix (ctx , evalCtx ) != prefixColumnCount {
715- // The prefix columns must be constrained to single values.
716- return nil , nil , false
717- }
718-
719- return & c , ic .RemainingFilters (), true
720- }
721-
722637type invertedFilterPlanner interface {
723638 // extractInvertedFilterConditionFromLeaf extracts an inverted filter
724639 // condition from the given expression, which represents a leaf of an
0 commit comments