Skip to content

Commit

Permalink
boost: draft for boost executor
Browse files Browse the repository at this point in the history
  • Loading branch information
benetis committed Mar 26, 2024
1 parent 5d8fa9a commit 625ea7d
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 11 deletions.
12 changes: 7 additions & 5 deletions go/vt/sqlparser/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ const (
StmtCallProc
StmtRevert
StmtShowMigrationLogs

StmtBoost
)

//ASTToStatementType returns a StatementType from an AST stmt
// ASTToStatementType returns a StatementType from an AST stmt
func ASTToStatementType(stmt Statement) StatementType {
switch stmt.(type) {
case *Select, *Union:
Expand Down Expand Up @@ -121,7 +123,7 @@ func ASTToStatementType(stmt Statement) StatementType {
}
}

//CanNormalize takes Statement and returns if the statement can be normalized.
// CanNormalize takes Statement and returns if the statement can be normalized.
func CanNormalize(stmt Statement) bool {
switch stmt.(type) {
case *Select, *Union, *Insert, *Update, *Delete, *Set, *CallProc, *Stream: // TODO: we could merge this logic into ASTrewriter
Expand All @@ -140,7 +142,7 @@ func CachePlan(stmt Statement) bool {
return false
}

//MustRewriteAST takes Statement and returns true if RewriteAST must run on it for correct execution irrespective of user flags.
// MustRewriteAST takes Statement and returns true if RewriteAST must run on it for correct execution irrespective of user flags.
func MustRewriteAST(stmt Statement) bool {
switch node := stmt.(type) {
case *Set:
Expand Down Expand Up @@ -301,7 +303,7 @@ func IsDML(sql string) bool {
return false
}

//IsDMLStatement returns true if the query is an INSERT, UPDATE or DELETE statement.
// IsDMLStatement returns true if the query is an INSERT, UPDATE or DELETE statement.
func IsDMLStatement(stmt Statement) bool {
switch stmt.(type) {
case *Insert, *Update, *Delete:
Expand Down Expand Up @@ -490,7 +492,7 @@ func NewPlanValue(node Expr) (sqltypes.PlanValue, error) {
return sqltypes.PlanValue{}, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "expression is too complex '%v'", String(node))
}

//IsLockingFunc returns true for all functions that are used to work with mysql advisory locks
// IsLockingFunc returns true for all functions that are used to work with mysql advisory locks
func IsLockingFunc(node Expr) bool {
switch p := node.(type) {
case *FuncExpr:
Expand Down
26 changes: 21 additions & 5 deletions go/vt/sqlparser/normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,20 @@ func Normalize(stmt Statement, reserved *ReservedVars, bindVars map[string]*quer
}

type normalizer struct {
bindVars map[string]*querypb.BindVariable
reserved *ReservedVars
vals map[string]string
err error
bindVars map[string]*querypb.BindVariable
reserved *ReservedVars
vals map[string]string
columns map[string]string
currentColumn string
err error
}

func newNormalizer(reserved *ReservedVars, bindVars map[string]*querypb.BindVariable) *normalizer {
return &normalizer{
bindVars: bindVars,
reserved: reserved,
vals: make(map[string]string),
columns: make(map[string]string),
}
}

Expand Down Expand Up @@ -86,7 +89,10 @@ func (nz *normalizer) WalkSelect(cursor *Cursor) bool {
nz.convertLiteralDedup(node, cursor)
case *ComparisonExpr:
nz.convertComparison(node)
case *ColName, TableName:
case *ColName:
nz.currentColumn = node.Name.String()
return false
case *TableName:
// Common node types that never contain Literals or ListArgs but create a lot of object
// allocations.
return false
Expand Down Expand Up @@ -134,6 +140,11 @@ func (nz *normalizer) convertLiteralDedup(node *Literal, cursor *Cursor) {
nz.bindVars[bvname] = bval
}

// <BOOST> Store the column to bind var mapping.
if nz.currentColumn != "" && !ok {
nz.columns[nz.currentColumn] = bvname
}

// Modify the AST node to a bindvar.
cursor.Replace(NewArgument(bvname))
}
Expand All @@ -148,6 +159,11 @@ func (nz *normalizer) convertLiteral(node *Literal, cursor *Cursor) {
bvname := nz.reserved.nextUnusedVar()
nz.bindVars[bvname] = bval

// <BOOST> Store the column to bind var mapping.
if nz.currentColumn != "" {
nz.columns[nz.currentColumn] = bvname
}

cursor.Replace(NewArgument(bvname))
}

Expand Down
51 changes: 51 additions & 0 deletions go/vt/vtgate/engine/empty_primitive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package engine

import (
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/proto/query"
)

// EmptyPrimitive represents a no-operation primitive,
// fulfilling the Primitive interface without performing any actions.
type EmptyPrimitive struct {
noInputs
noTxNeeded
}

func (ep *EmptyPrimitive) Execute(vcursor VCursor, bindVars map[string]*query.BindVariable, wantfields bool) (*sqltypes.Result, error) {
return &sqltypes.Result{}, nil
}

func (ep *EmptyPrimitive) StreamExecute(vcursor VCursor, bindVars map[string]*query.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error {
return callback(&sqltypes.Result{})
}

func (ep *EmptyPrimitive) GetFields(vcursor VCursor, bindVars map[string]*query.BindVariable) (*sqltypes.Result, error) {
return &sqltypes.Result{}, nil
}

func (ep *EmptyPrimitive) RouteType() string {
return "Empty"
}

func (ep *EmptyPrimitive) GetKeyspaceName() string {
return ""
}

func (ep *EmptyPrimitive) GetTableName() string {
return ""
}

func (ep *EmptyPrimitive) Inputs() []Primitive {
return nil
}

func (ep *EmptyPrimitive) NeedsTransaction() bool {
return false
}

func (ep *EmptyPrimitive) description() PrimitiveDescription {
return PrimitiveDescription{
OperatorType: "Empty",
}
}
29 changes: 28 additions & 1 deletion go/vt/vtgate/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func (e *Executor) handleCommit(ctx context.Context, safeSession *SafeSession, l
return &sqltypes.Result{}, err
}

//Commit commits the existing transactions
// Commit commits the existing transactions
func (e *Executor) Commit(ctx context.Context, safeSession *SafeSession) error {
return e.txConn.Commit(ctx, safeSession)
}
Expand Down Expand Up @@ -1046,6 +1046,8 @@ func (e *Executor) StreamExecute(ctx context.Context, method string, safeSession
switch plan.Type {
case sqlparser.StmtBegin, sqlparser.StmtCommit, sqlparser.StmtRollback:
return vterrors.Errorf(vtrpcpb.Code_UNIMPLEMENTED, "OLAP does not supported statement type: %s", plan.Type)
case sqlparser.StmtBoost:
return e.handleBoost(ctx, safeSession, plan, bindVars, callback)
}

err = e.addNeededBindVars(plan.BindVarNeeds, bindVars, safeSession)
Expand Down Expand Up @@ -1224,6 +1226,23 @@ func (e *Executor) getPlan(vcursor *vcursorImpl, sql string, comments sqlparser.
return plan.(*engine.Plan), nil
}

isBoosted := true

if isBoosted {
plan, err := planbuilder.BuildBoost(query, statement)
if err != nil {
return nil, err
}

plan.Warnings = vcursor.warnings
vcursor.warnings = nil

if !skipQueryPlanCache && !sqlparser.SkipQueryPlanCacheDirective(statement) && sqlparser.CachePlan(statement) {
e.plans.Set(planKey, plan)
}
return plan, nil
}

plan, err := planbuilder.BuildFromStmt(query, statement, reservedVars, vcursor, bindVarNeeds, *enableOnlineDDL, *enableDirectDDL)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1511,3 +1530,11 @@ func (e *Executor) startVStream(ctx context.Context, rss []*srvtopo.ResolvedShar
vs.stream(ctx)
return nil
}

// Instead of mysql query, query Redis with custom-built key
func (e *Executor) handleBoost(ctx context.Context, session *SafeSession, plan *engine.Plan, vars map[string]*querypb.BindVariable, callback func(*sqltypes.Result) error) error {

panic("not implemented")
return nil

Check failure on line 1538 in go/vt/vtgate/executor.go

View workflow job for this annotation

GitHub Actions / Lint using golangci-lint

unreachable: unreachable code (govet)

Check failure on line 1538 in go/vt/vtgate/executor.go

View workflow job for this annotation

GitHub Actions / Lint using golangci-lint

unreachable: unreachable code (govet)

}
8 changes: 8 additions & 0 deletions go/vt/vtgate/planbuilder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ func BuildFromStmt(query string, stmt sqlparser.Statement, reservedVars *sqlpars
return plan, nil
}

func BuildBoost(query string, stmt sqlparser.Statement) (*engine.Plan, error) {
return &engine.Plan{
Type: sqlparser.StmtBoost,
Instructions: &engine.EmptyPrimitive{},
BindVarNeeds: &sqlparser.BindVarNeeds{},
}, nil
}

func getConfiguredPlanner(vschema ContextVSchema) (selectPlanner, error) {
switch vschema.Planner() {
case Gen4, Gen4Left2Right, Gen4GreedyOnly:
Expand Down

0 comments on commit 625ea7d

Please sign in to comment.