Skip to content

Commit

Permalink
add new mutable bitset and use it
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed Jan 17, 2025
1 parent b16f792 commit b75398d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
56 changes: 56 additions & 0 deletions go/vt/vtgate/semantics/bitset/mutable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2024 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package bitset

// Mutable is a growable, in-place version that we can OR bits into
// and eventually turn into a stable (immutable) TableSet.
type Mutable struct {
data []byte
}

// NewMutableTableSet creates an initially empty MutableTableSet.
func NewMutableTableSet() *Mutable {
return &Mutable{}
}

// Or merges another TableSet into this Mutable, resizing if needed.
func (m *Mutable) Or(ts Bitset) {
// If ts is longer than our current data, grow to accommodate it.
if len(ts) > len(m.data) {
oldData := m.data
m.data = make([]byte, len(ts))
copy(m.data, oldData)
}
// Merge in-place.
for i := 0; i < len(ts); i++ {
m.data[i] |= ts[i]
}
}

// AsImmutable finalizes the Mutable into a TableSet, trimming trailing zeros.
func (m *Mutable) AsImmutable() Bitset {
trim := len(m.data)
for trim > 0 && m.data[trim-1] == 0 {
trim--
}
if trim == 0 {
// Means all bits are zero -> empty
return ""
}

return toBitset(m.data[:trim])
}
8 changes: 5 additions & 3 deletions go/vt/vtgate/semantics/semantic_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package semantics
import (
"fmt"

"vitess.io/vitess/go/vt/vtgate/semantics/bitset"

"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/key"
Expand Down Expand Up @@ -704,7 +706,7 @@ func (d ExprDependencies) dependencies(expr sqlparser.Expr) (deps TableSet) {
}()
}

var tables []TableSet
depsCalc := bitset.NewMutableTableSet()
// During the original semantic analysis, all ColNames were found and bound to the corresponding tables
// Here, we'll walk the expression tree and look to see if we can find any sub-expressions
// that have already set dependencies.
Expand All @@ -718,14 +720,14 @@ func (d ExprDependencies) dependencies(expr sqlparser.Expr) (deps TableSet) {

set, found := d[expr]
if found {
tables = append(tables, set)
depsCalc.Or(bitset.Bitset(set))
}

// if we found a cached value, there is no need to continue down to visit children
return !found, nil
}, expr)

deps = MergeTableSets(tables...)
deps = TableSet(depsCalc.AsImmutable())

return
}
Expand Down

0 comments on commit b75398d

Please sign in to comment.