vitess.io/vitess@v0.16.2/go/vt/vtgate/semantics/table_set.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package semantics
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"vitess.io/vitess/go/vt/vtgate/semantics/bitset"
    23  )
    24  
    25  // TableSet is how a set of tables is expressed.
    26  // Tables get unique bits assigned in the order that they are encountered during semantic analysis.
    27  type TableSet bitset.Bitset
    28  
    29  // Format formats the TableSet.
    30  func (ts TableSet) Format(f fmt.State, _ rune) {
    31  	first := true
    32  	fmt.Fprintf(f, "TableSet{")
    33  	bitset.Bitset(ts).ForEach(func(tid int) {
    34  		if first {
    35  			fmt.Fprintf(f, "%d", tid)
    36  			first = false
    37  		} else {
    38  			fmt.Fprintf(f, ",%d", tid)
    39  		}
    40  	})
    41  	fmt.Fprintf(f, "}")
    42  }
    43  
    44  // IsOverlapping returns true if at least one table exists in both sets
    45  func (ts TableSet) IsOverlapping(other TableSet) bool {
    46  	return bitset.Bitset(ts).Overlaps(bitset.Bitset(other))
    47  }
    48  
    49  // IsSolvedBy returns true if all of `ts` is contained in `other`
    50  func (ts TableSet) IsSolvedBy(other TableSet) bool {
    51  	return bitset.Bitset(ts).IsContainedBy(bitset.Bitset(other))
    52  }
    53  
    54  // NumberOfTables returns the number of bits set
    55  func (ts TableSet) NumberOfTables() int {
    56  	return bitset.Bitset(ts).Popcount()
    57  }
    58  
    59  // NonEmpty returns true if there are tables in the tableset
    60  func (ts TableSet) NonEmpty() bool {
    61  	return !ts.IsEmpty()
    62  }
    63  
    64  // IsEmpty returns true if there are no tables in the tableset
    65  func (ts TableSet) IsEmpty() bool {
    66  	return len(ts) == 0
    67  }
    68  
    69  // TableOffset returns the offset in the Tables array from TableSet
    70  func (ts TableSet) TableOffset() int {
    71  	return bitset.Bitset(ts).SingleBit()
    72  }
    73  
    74  // ForEachTable calls the given callback with the indices for all tables in this TableSet
    75  func (ts TableSet) ForEachTable(callback func(int)) {
    76  	bitset.Bitset(ts).ForEach(callback)
    77  }
    78  
    79  // Constituents returns a slice with the indices for all tables in this TableSet
    80  func (ts TableSet) Constituents() (result []TableSet) {
    81  	ts.ForEachTable(func(t int) {
    82  		result = append(result, SingleTableSet(t))
    83  	})
    84  	return
    85  }
    86  
    87  // Merge creates a TableSet that contains both inputs
    88  func (ts TableSet) Merge(other TableSet) TableSet {
    89  	return TableSet(bitset.Bitset(ts).Or(bitset.Bitset(other)))
    90  }
    91  
    92  // Remove returns a new TableSet with all the tables in `other` removed
    93  func (ts TableSet) Remove(other TableSet) TableSet {
    94  	return TableSet(bitset.Bitset(ts).AndNot(bitset.Bitset(other)))
    95  }
    96  
    97  // KeepOnly removes all the tables not in `other` from this TableSet
    98  func (ts TableSet) KeepOnly(other TableSet) TableSet {
    99  	return TableSet(bitset.Bitset(ts).And(bitset.Bitset(other)))
   100  }
   101  
   102  // WithTable returns a new TableSet that contains this table too
   103  func (ts TableSet) WithTable(tableidx int) TableSet {
   104  	return TableSet(bitset.Bitset(ts).Set(tableidx))
   105  }
   106  
   107  // SingleTableSet creates a TableSet that contains only the given table
   108  func SingleTableSet(tableidx int) TableSet {
   109  	return TableSet(bitset.Single(tableidx))
   110  }
   111  
   112  // EmptyTableSet creates an empty TableSet
   113  func EmptyTableSet() TableSet {
   114  	return ""
   115  }
   116  
   117  // MergeTableSets merges all the given TableSet into a single one
   118  func MergeTableSets(tss ...TableSet) TableSet {
   119  	var result bitset.Bitset
   120  	for _, t := range tss {
   121  		result = result.Or(bitset.Bitset(t))
   122  	}
   123  	return TableSet(result)
   124  }
   125  
   126  // TableSetFromIds returns TableSet for all the id passed in argument.
   127  func TableSetFromIds(tids ...int) (ts TableSet) {
   128  	return TableSet(bitset.Build(tids...))
   129  }