Skip to content

Commit

Permalink
updated test to use new planning methods
Browse files Browse the repository at this point in the history
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
  • Loading branch information
harshit-gangal committed Feb 25, 2025
1 parent 0df2fc5 commit 81cfe14
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 174 deletions.
108 changes: 0 additions & 108 deletions go/vt/vtgate/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1201,114 +1201,6 @@ func (e *Executor) getCachedOrBuildPlan(
return plan, false, stmt, err
}

// getPlan computes the plan for the given query. If one is in
// the cache, it reuses it.
func (e *Executor) getPlan(
ctx context.Context,
vcursor *econtext.VCursorImpl,
query string,
stmt sqlparser.Statement,
comments sqlparser.MarginComments,
bindVars map[string]*querypb.BindVariable,
reservedVars *sqlparser.ReservedVars,
allowParameterization bool,
logStats *logstats.LogStats,
) (plan *engine.Plan, err error) {
if e.VSchema() == nil {
return nil, vterrors.VT13001("vschema not initialized")
}
var setVarComment string
if e.vConfig.SetVarEnabled {
setVarComment = vcursor.PrepareSetVarComment()
}

plan, err = e.getCachedOrBuild(ctx, vcursor, query, stmt, reservedVars, bindVars, allowParameterization, comments, logStats, setVarComment)
if err != nil {
return nil, err
}

qh := plan.QueryHints
vcursor.SetIgnoreMaxMemoryRows(qh.IgnoreMaxMemoryRows)
vcursor.SetConsolidator(qh.Consolidator)
vcursor.SetWorkloadName(qh.Workload)
vcursor.SetPriority(qh.Priority)
vcursor.SetExecQueryTimeout(qh.Timeout)

if setVarComment != "" {
switch stmt.(type) {
// If the statement is a transaction statement or a `SET`, no reserved connection / SET_VAR is needed
case *sqlparser.Begin, *sqlparser.Commit, *sqlparser.Rollback, *sqlparser.Savepoint,
*sqlparser.SRollback, *sqlparser.Release, *sqlparser.Set, *sqlparser.Show, sqlparser.SupportOptimizerHint:
default:
vcursor.NeedsReservedConn()
}
}

return plan, nil
}

func (e *Executor) getCachedOrBuild(
ctx context.Context,
vcursor *econtext.VCursorImpl,
query string,
stmt sqlparser.Statement,
reservedVars *sqlparser.ReservedVars,
bindVars map[string]*querypb.BindVariable,
allowParameterization bool,
comments sqlparser.MarginComments,
logStats *logstats.LogStats,
setVarComment string,
) (*engine.Plan, error) {

qh, err := sqlparser.BuildQueryHints(stmt)
if err != nil {
return nil, err
}

if qh.ForeignKeyChecks == nil {
qh.ForeignKeyChecks = vcursor.SafeSession.ForeignKeyChecks()
}
vcursor.SetForeignKeyCheckState(qh.ForeignKeyChecks)

rewriteASTResult, err := sqlparser.Normalize(
stmt,
reservedVars,
bindVars,
allowParameterization,
vcursor.GetKeyspace(),
vcursor.SafeSession.GetSelectLimit(),
setVarComment,
vcursor.GetSystemVariablesCopy(),
qh.ForeignKeyChecks,
vcursor,
)
if err != nil {
return nil, err
}
stmt = rewriteASTResult.AST
bindVarNeeds := rewriteASTResult.BindVarNeeds
if rewriteASTResult.UpdateQueryFromAST {
query = sqlparser.String(stmt)
}

logStats.SQL = comments.Leading + query + comments.Trailing
logStats.BindVariables = sqltypes.CopyBindVariables(bindVars)

planCachable := sqlparser.CachePlan(stmt) && vcursor.CachePlan()
if planCachable {
// build Plan key
planKey := buildPlanKey(ctx, vcursor, query, setVarComment)

var plan *engine.Plan
var err error
plan, logStats.CachedPlan, err = e.plans.GetOrLoad(planKey.Hash(), e.epoch.Load(), func() (*engine.Plan, error) {
return e.buildStatement(ctx, vcursor, query, stmt, reservedVars, bindVarNeeds, qh, -1)
})
return plan, err
}
return e.buildStatement(ctx, vcursor, query, stmt, reservedVars, bindVarNeeds, qh, -1)
}

func buildPlanKey(ctx context.Context, vcursor *econtext.VCursorImpl, query string, setVarComment string) engine.PlanKey {
allDest := getDestinations(ctx, vcursor)

Expand Down
Loading

0 comments on commit 81cfe14

Please sign in to comment.