Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
systay committed Feb 20, 2025
1 parent df126a2 commit 671b022
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 6 deletions.
7 changes: 6 additions & 1 deletion go/vt/vtgate/planbuilder/operators/op_to_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func buildValues(op *Values, qb *queryBuilder) {
apa := semantics.EmptyTableSet()
for _, ae := range qb.ctx.ValuesJoinColumns[op.Name] {
apa = apa.Merge(qb.ctx.SemTable.RecursiveDeps(ae.Expr))
fmt.Printf("%v\n", apa)
}

tableName := getTableName(qb.ctx, apa)
Expand All @@ -165,18 +166,22 @@ func getTableName(ctx *plancontext.PlanningContext, id semantics.TableSet) strin
var names []string
for _, ts := range id.Constituents() {
ti, err := ctx.SemTable.TableInfoFor(ts)
fmt.Printf("1: %v\n", ti)
if err != nil {
names = append(names, "X")
continue
}
name, err := ti.Name()
fmt.Printf("2: %v\n", name)
if err != nil {
names = append(names, "X")
continue
}
names = append(names, name.Name.String())
}
return strings.Join(names, "_")
x := strings.Join(names, "_")
fmt.Println("3: ", x)
return x
}

func buildDelete(op *Delete, qb *queryBuilder) {
Expand Down
9 changes: 7 additions & 2 deletions go/vt/vtgate/planbuilder/operators/query_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,15 @@ func runPushDownRewriters(ctx *plancontext.PlanningContext, root Operator) Opera

func tryPushValues(ctx *plancontext.PlanningContext, in *Values) (Operator, *ApplyResult) {
switch src := in.Source.(type) {
case *Values:
// we can merge two values operators into one
cols := ctx.ValuesJoinColumns[in.Name]
ctx.ValuesJoinColumns[src.Name] = append(ctx.ValuesJoinColumns[src.Name], cols...)
ctx.ValueJoins[in.Name] = ctx.ValueJoins[src.Name]
return src, Rewrote("merged two values operators")
case *ValuesJoin:
src.LHS = in.Clone([]Operator{src.LHS})
src.RHS = in.Clone([]Operator{src.RHS})
return src, Rewrote("pushed values under value join")
return src, Rewrote("pushed values to the LHS of values join")
case *Filter:
return Swap(in, src, "pushed values under filter")
case *Route:
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/operators/values_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (vj *ValuesJoin) GetOrdering(ctx *plancontext.PlanningContext) []OrderBy {
}

func (vj *ValuesJoin) planOffsets(ctx *plancontext.PlanningContext) Operator {
exprs := ctx.ValuesJoinColumns[vj.ValuesDestination]
exprs := ctx.GetColumns(vj.ValuesDestination)
for _, jc := range vj.JoinColumns {
newExprs := vj.planOffsetsForLHSExprs(ctx, jc.LHS)
exprs = append(exprs, newExprs...)
Expand All @@ -172,7 +172,7 @@ func (vj *ValuesJoin) planOffsets(ctx *plancontext.PlanningContext) Operator {
newExprs := vj.planOffsetsForLHSExprs(ctx, jc.LHS)
exprs = append(exprs, newExprs...)
}
ctx.ValuesJoinColumns[vj.ValuesDestination] = exprs
ctx.SetColumns(vj.ValuesDestination, exprs)
return vj
}

Expand Down
9 changes: 9 additions & 0 deletions go/vt/vtgate/planbuilder/plancontext/planning_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,15 @@ func (ctx *PlanningContext) ActiveCTE() *ContextCTE {
return ctx.CurrentCTE[len(ctx.CurrentCTE)-1]
}

func (ctx *PlanningContext) GetColumns(joinName string) []*sqlparser.AliasedExpr {
valuesName := ctx.ValueJoins[joinName]
return ctx.ValuesJoinColumns[valuesName]
}
func (ctx *PlanningContext) SetColumns(joinName string, cols []*sqlparser.AliasedExpr) {
valuesName := ctx.ValueJoins[joinName]
ctx.ValuesJoinColumns[valuesName] = cols
}

func (ctx *PlanningContext) UseMirror() *PlanningContext {
if ctx.isMirrored {
panic(vterrors.VT13001("cannot mirror already mirrored planning context"))
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/testdata/onecase.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"comment": "Add your test case here for debugging and run go test -run=One.",
"query": "",
"query": "select /*vt+ ALLOW_VALUES_JOIN */ user.foo, user_extra.user_id, user_metadata.name from user, user_extra, user_metadata where user.id = user_extra.toto and user_extra.age = user_metadata.age",
"plan": {
}
}
Expand Down
17 changes: 17 additions & 0 deletions go/vt/vtgate/semantics/table_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package semantics

import (
"fmt"
"strings"

"vitess.io/vitess/go/vt/vtgate/semantics/bitset"
)
Expand All @@ -41,6 +42,22 @@ func (ts TableSet) Format(f fmt.State, verb rune) {
fmt.Fprintf(f, "}")
}

func (ts TableSet) DebugString() string {
var f strings.Builder
first := true
f.WriteString("TableSet{")
bitset.Bitset(ts).ForEach(func(tid int) {
if first {
f.WriteString(fmt.Sprintf("%d", tid))
first = false
} else {
f.WriteString(fmt.Sprintf(",%d", tid))
}
})
f.WriteString("}")
return f.String()
}

// IsOverlapping returns true if at least one table exists in both sets
func (ts TableSet) IsOverlapping(other TableSet) bool {
return bitset.Bitset(ts).Overlaps(bitset.Bitset(other))
Expand Down

0 comments on commit 671b022

Please sign in to comment.