Skip to content

Commit

Permalink
refactor: minor cleanups in planner code (#14642)
Browse files Browse the repository at this point in the history
  • Loading branch information
systay authored Nov 30, 2023
1 parent 3176543 commit e818ca5
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (a *Aggregator) findColInternal(ctx *plancontext.PlanningContext, ae *sqlpa
}

if addToGroupBy {
panic(vterrors.VT13001("did not expect to add group by here"))
panic(vterrors.VT13001(fmt.Sprintf("did not expect to add group by here: %s", sqlparser.String(expr))))
}

return -1
Expand Down
15 changes: 10 additions & 5 deletions go/vt/vtgate/planbuilder/operators/phases.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,18 @@ type phaser struct {
}

func (p *phaser) next(ctx *plancontext.PlanningContext) Phase {
for phas := p.current; phas < DONE; phas++ {
if phas.shouldRun(ctx.SemTable.QuerySignature) {
p.current = p.current + 1
return phas
for {
curr := p.current
if curr == DONE {
return DONE
}

p.current++

if curr.shouldRun(ctx.SemTable.QuerySignature) {
return curr
}
}
return DONE
}

func removePerformanceDistinctAboveRoute(_ *plancontext.PlanningContext, op ops.Operator) (ops.Operator, error) {
Expand Down
11 changes: 3 additions & 8 deletions go/vt/vtgate/planbuilder/simplifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,14 @@ import (
"fmt"
"testing"

"vitess.io/vitess/go/test/vschemawrapper"

"vitess.io/vitess/go/vt/vterrors"

"github.com/stretchr/testify/assert"

"vitess.io/vitess/go/vt/vtgate/simplifier"

"github.com/stretchr/testify/require"

"vitess.io/vitess/go/test/vschemawrapper"
"vitess.io/vitess/go/vt/log"

"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/simplifier"
)

// TestSimplifyBuggyQuery should be used to whenever we get a planner bug reported
Expand Down
5 changes: 3 additions & 2 deletions go/vt/vtgate/simplifier/expression_simplifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"strconv"

"vitess.io/vitess/go/vt/log"

"vitess.io/vitess/go/vt/sqlparser"
)

Expand All @@ -44,10 +43,10 @@ func SimplifyExpr(in sqlparser.Expr, test CheckF) sqlparser.Expr {
cursor.Replace(expr)

valid := test(smallestKnown[0])
log.Errorf("test: %t: simplified %s to %s, full expr: %s", valid, sqlparser.String(node), sqlparser.String(expr), sqlparser.String(smallestKnown))
if valid {
break // we will still continue trying to simplify other expressions at this level
} else {
log.Errorf("failed attempt: tried changing {%s} to {%s} in {%s}", sqlparser.String(node), sqlparser.String(expr), sqlparser.String(in))
// undo the change
cursor.Replace(node)
}
Expand Down Expand Up @@ -105,6 +104,8 @@ func (s *shrinker) fillQueue() bool {
s.queue = append(s.queue, e.Left, e.Right)
case *sqlparser.BinaryExpr:
s.queue = append(s.queue, e.Left, e.Right)
case *sqlparser.BetweenExpr:
s.queue = append(s.queue, e.Left, e.From, e.To)
case *sqlparser.Literal:
switch e.Type {
case sqlparser.StrVal:
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/simplifier/simplifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func tryRemoveTable(tables []semantics.TableInfo, in sqlparser.SelectStatement,
simplified := removeTable(clone, searchedTS, currentDB, si)
name, _ := tbl.Name()
if simplified && test(clone) {
log.Errorf("removed table %s: %s -> %s", sqlparser.String(name), sqlparser.String(in), sqlparser.String(clone))
log.Errorf("removed table `%s`: \n%s\n%s", sqlparser.String(name), sqlparser.String(in), sqlparser.String(clone))
return clone
}
}
Expand Down
8 changes: 3 additions & 5 deletions go/vt/vtgate/simplifier/simplifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ import (
"fmt"
"testing"

"vitess.io/vitess/go/vt/log"

"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/vt/vtgate/evalengine"

"github.com/stretchr/testify/require"

"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vtgate/evalengine"
)

func TestFindAllExpressions(t *testing.T) {
Expand Down

0 comments on commit e818ca5

Please sign in to comment.