Skip to content

Commit ba671b2

Browse files
committed
add method for pushing one operator under another
Signed-off-by: Andres Taylor <andres@planetscale.com>
1 parent 94bda95 commit ba671b2

File tree

7 files changed

+39
-22
lines changed

7 files changed

+39
-22
lines changed

go/vt/vtgate/planbuilder/operators/derived.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
)
3030

3131
type Derived struct {
32-
singleSource
32+
*singleSource
3333

3434
Query sqlparser.SelectStatement
3535
Alias string
@@ -49,7 +49,7 @@ func newDerived(
4949
columnAliases sqlparser.Columns,
5050
) ops.Operator {
5151
return &Derived{
52-
singleSource: singleSource{Source: src},
52+
singleSource: &singleSource{Source: src},
5353
Query: stmt,
5454
Alias: alias,
5555
ColumnAliases: columnAliases,
@@ -62,7 +62,7 @@ func (d *Derived) IPhysical() {}
6262
// Clone implements the Operator interface
6363
func (d *Derived) Clone(inputs []ops.Operator) ops.Operator {
6464
return &Derived{
65-
singleSource: singleSource{Source: inputs[0]},
65+
singleSource: &singleSource{Source: inputs[0]},
6666
Query: d.Query,
6767
Alias: d.Alias,
6868
ColumnAliases: sqlparser.CloneColumns(d.ColumnAliases),

go/vt/vtgate/planbuilder/operators/filter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
type Filter struct {
28-
singleSource
28+
*singleSource
2929

3030
Predicates []sqlparser.Expr
3131
}
@@ -34,7 +34,7 @@ var _ ops.PhysicalOperator = (*Filter)(nil)
3434

3535
func newFilter(op ops.Operator, expr sqlparser.Expr) ops.Operator {
3636
return &Filter{
37-
singleSource: singleSource{Source: op}, Predicates: []sqlparser.Expr{expr},
37+
singleSource: &singleSource{Source: op}, Predicates: []sqlparser.Expr{expr},
3838
}
3939
}
4040

@@ -46,7 +46,7 @@ func (f *Filter) Clone(inputs []ops.Operator) ops.Operator {
4646
predicatesClone := make([]sqlparser.Expr, len(f.Predicates))
4747
copy(predicatesClone, f.Predicates)
4848
return &Filter{
49-
singleSource: singleSource{Source: inputs[0]},
49+
singleSource: &singleSource{Source: inputs[0]},
5050
Predicates: predicatesClone,
5151
}
5252
}

go/vt/vtgate/planbuilder/operators/horizon.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
// Horizon is an operator we use until we decide how to handle the source to the horizon.
2626
// It contains information about the planning we have to do after deciding how we will send the query to the tablets.
2727
type Horizon struct {
28-
singleSource
28+
*singleSource
2929

3030
Select sqlparser.SelectStatement
3131

@@ -39,7 +39,7 @@ func (h *Horizon) IPhysical() {}
3939

4040
func newHorizon(src ops.Operator, stmt sqlparser.SelectStatement) ops.Operator {
4141
return &Horizon{
42-
singleSource: singleSource{src},
42+
singleSource: &singleSource{src},
4343
Select: stmt,
4444
}
4545
}
@@ -55,7 +55,7 @@ func (h *Horizon) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.
5555

5656
func (h *Horizon) Clone(inputs []ops.Operator) ops.Operator {
5757
return &Horizon{
58-
singleSource: singleSource{inputs[0]},
58+
singleSource: &singleSource{inputs[0]},
5959
Select: h.Select,
6060
}
6161
}

go/vt/vtgate/planbuilder/operators/horizon_planning.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,8 @@ func planHorizons(in ops.Operator) (ops.Operator, error) {
4242
func planHorizon(in *Horizon) (ops.Operator, error) {
4343
rb, isRoute := in.Source.(*Route)
4444
if isRoute && rb.IsSingleShard() && in.Select.GetLimit() == nil {
45-
return planSingleShardRoute(rb, in)
45+
return swap(in, rb), nil
4646
}
4747

4848
return in, nil
4949
}
50-
51-
func planSingleShardRoute(rb *Route, horizon *Horizon) (ops.Operator, error) {
52-
rb.Source, horizon.Source = horizon, rb.Source
53-
return rb, nil
54-
}

go/vt/vtgate/planbuilder/operators/operator.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ type (
5454
singleSource struct {
5555
Source ops.Operator
5656
}
57+
58+
singleSourceI interface {
59+
ops.Operator
60+
getSource() ops.Operator
61+
setSource(ops.Operator)
62+
}
5763
)
5864

5965
func PlanQuery(ctx *plancontext.PlanningContext, selStmt sqlparser.Statement) (ops.Operator, error) {
@@ -89,10 +95,18 @@ func (noInputs) Inputs() []ops.Operator {
8995
}
9096

9197
// Inputs implements the Operator interface
92-
func (s singleSource) Inputs() []ops.Operator {
98+
func (s *singleSource) Inputs() []ops.Operator {
9399
return []ops.Operator{s.Source}
94100
}
95101

102+
func (s *singleSource) getSource() ops.Operator {
103+
return s.Source
104+
}
105+
106+
func (s *singleSource) setSource(src ops.Operator) {
107+
s.Source = src
108+
}
109+
96110
// AddColumn implements the Operator interface
97111
func (noColumns) AddColumn(*plancontext.PlanningContext, sqlparser.Expr) (int, error) {
98112
return 0, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "this operator cannot accept columns")
@@ -102,3 +116,13 @@ func (noColumns) AddColumn(*plancontext.PlanningContext, sqlparser.Expr) (int, e
102116
func (noPredicates) AddPredicate(*plancontext.PlanningContext, sqlparser.Expr) (ops.Operator, error) {
103117
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "this operator cannot accept predicates")
104118
}
119+
120+
// swap takes two operators with a single source and swaps them. the assumption is that the first op provided has the
121+
// second operator as source. this method will the two operators with each other and return the new root.
122+
// this method is used to push one operator underneath the other
123+
func swap(op, src singleSourceI) ops.Operator {
124+
tmp := src.getSource()
125+
src.setSource(op)
126+
op.setSource(tmp)
127+
return src
128+
}

go/vt/vtgate/planbuilder/operators/route.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333

3434
type (
3535
Route struct {
36-
singleSource
36+
*singleSource
3737

3838
RouteOpCode engine.Opcode
3939
Keyspace *vindexes.Keyspace
@@ -99,7 +99,7 @@ func newRoute(
9999
sysTableTableName map[string]evalengine.Expr,
100100
) *Route {
101101
return &Route{
102-
singleSource: singleSource{src},
102+
singleSource: &singleSource{src},
103103
RouteOpCode: opCode,
104104
Keyspace: keyspace,
105105
VindexPreds: vindexPreds,
@@ -140,7 +140,7 @@ func (r *Route) Cost() int {
140140
// Clone implements the Operator interface
141141
func (r *Route) Clone(inputs []ops.Operator) ops.Operator {
142142
cloneRoute := *r
143-
cloneRoute.singleSource = singleSource{inputs[0]}
143+
cloneRoute.singleSource = &singleSource{inputs[0]}
144144
cloneRoute.VindexPreds = make([]*VindexPlusPredicates, len(r.VindexPreds))
145145
for i, pred := range r.VindexPreds {
146146
// we do this to create a copy of the struct

go/vt/vtgate/planbuilder/operators/route_planning.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@ func transformToPhysical(ctx *plancontext.PlanningContext, in ops.Operator) (ops
8989
func optimizeFilter(op *Filter) (ops.Operator, rewrite.TreeIdentity, error) {
9090
if route, ok := op.Source.(*Route); ok {
9191
// let's push the filter into the route
92-
op.Source = route.Source
93-
route.Source = op
94-
return route, rewrite.NewTree, nil
92+
return swap(op, route), rewrite.NewTree, nil
9593
}
9694

9795
return op, rewrite.SameTree, nil

0 commit comments

Comments
 (0)