Skip to content

Commit

Permalink
feat: fix pushing of values and filters to a value join
Browse files Browse the repository at this point in the history
Signed-off-by: Manan Gupta <manan@planetscale.com>
  • Loading branch information
GuptaManan100 committed Feb 21, 2025
1 parent df126a2 commit c558b3f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 6 deletions.
1 change: 1 addition & 0 deletions go/vt/vtgate/planbuilder/operators/phases.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func newValuesJoin(ctx *plancontext.PlanningContext, lhs, rhs Operator, joinType
v := &Values{
unaryOperator: newUnaryOp(rhs),
Name: bindVariableName,
TableID: TableID(lhs),
}
return &ValuesJoin{
binaryOperator: newBinaryOp(lhs, v),
Expand Down
5 changes: 2 additions & 3 deletions go/vt/vtgate/planbuilder/operators/query_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ func tryPushValues(ctx *plancontext.PlanningContext, in *Values) (Operator, *App
switch src := in.Source.(type) {
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 Expand Up @@ -724,7 +723,7 @@ func tryPushFilter(ctx *plancontext.PlanningContext, in *Filter) (Operator, *App
return src, Rewrote("push filter to outer query in subquery container")
case *ValuesJoin:
for _, pred := range in.Predicates {
src.AddJoinPredicate(ctx, pred)
src.AddPredicate(ctx, pred)
}
return src, Rewrote("pushed filter predicates through values join")
}
Expand Down
8 changes: 7 additions & 1 deletion go/vt/vtgate/planbuilder/operators/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import (
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
"vitess.io/vitess/go/vt/vtgate/semantics"
)

type Values struct {
unaryOperator

Name string
Name string
TableID semantics.TableSet
}

func (v *Values) Clone(inputs []Operator) Operator {
Expand Down Expand Up @@ -97,3 +99,7 @@ func (v *Values) ShortDescription() string {
func (v *Values) GetOrdering(ctx *plancontext.PlanningContext) []OrderBy {
return v.Source.GetOrdering(ctx)
}

func (v *Values) introducesTableID() semantics.TableSet {
return v.TableID
}
8 changes: 6 additions & 2 deletions go/vt/vtgate/planbuilder/operators/values_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ func (vj *ValuesJoin) MakeInner() {
// no-op for values-join
}

func (vj *ValuesJoin) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.Expr) Operator {
return AddPredicate(ctx, vj, expr, false, newFilterSinglePredicate)
}

func (vj *ValuesJoin) IsInner() bool {
return true
}
Expand Down Expand Up @@ -160,7 +164,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 +176,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
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 c558b3f

Please sign in to comment.