Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: minor cleanups in planner code #14642

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -202,7 +202,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 {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the old code for this was wrong, and we were hitting some phases twice. didn't really make a noticeable difference in plan outputs, but it took longer than needed to run.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

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
Loading