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

planner: support ScalarSubQuery to display them in EXPLAIN (#45252) #45521

Closed
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: 2 additions & 0 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1743,12 +1743,14 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) {
sc.OriginalSQL = s.Text()
if explainStmt, ok := s.(*ast.ExplainStmt); ok {
sc.InExplainStmt = true
sc.InExplainAnalyzeStmt = explainStmt.Analyze
sc.IgnoreExplainIDSuffix = (strings.ToLower(explainStmt.Format) == types.ExplainFormatBrief)
sc.InVerboseExplain = strings.ToLower(explainStmt.Format) == types.ExplainFormatVerbose
s = explainStmt.Stmt
}
if explainForStmt, ok := s.(*ast.ExplainForStmt); ok {
sc.InExplainStmt = true
sc.InExplainAnalyzeStmt = true
sc.InVerboseExplain = strings.ToLower(explainForStmt.Format) == types.ExplainFormatVerbose
}
// TODO: Many same bool variables here.
Expand Down
1 change: 1 addition & 0 deletions expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const (
columnFlag byte = 1
scalarFunctionFlag byte = 3
parameterFlag byte = 4
ScalarSubQFlag byte = 5
)

// EvalAstExpr evaluates ast expression directly.
Expand Down
23 changes: 23 additions & 0 deletions planner/core/common_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,10 @@ func (e *Explain) RenderResult() error {
if err != nil {
return err
}
err = e.explainPlanInRowFormatScalarSubquery()
if err != nil {
return err
}
}
case types.ExplainFormatDOT:
if physicalPlan, ok := e.TargetPlan.(PhysicalPlan); ok {
Expand Down Expand Up @@ -1338,6 +1342,25 @@ func (e *Explain) explainPlanInRowFormatCTE() (err error) {
return
}

func (e *Explain) explainPlanInRowFormatScalarSubquery() (err error) {
if e.ctx == nil {
return nil
}
for _, subQ := range e.ctx.GetSessionVars().MapScalarSubQ {
scalarQ, ok := subQ.(*ScalarSubqueryEvalCtx)
if !ok {
continue
}
e.prepareOperatorInfo(scalarQ, "root", "", "", true)
childIndent := texttree.Indent4Child("", true)
err = e.explainPlanInRowFormat(scalarQ.scalarSubQuery, "root", "", childIndent, true)
if err != nil {
return
}
}
return
}

// explainPlanInRowFormat generates explain information for root-tasks.
func (e *Explain) explainPlanInRowFormat(p Plan, taskType, driverSide, indent string, isLastChild bool) (err error) {
e.prepareOperatorInfo(p, taskType, driverSide, indent, isLastChild)
Expand Down
61 changes: 61 additions & 0 deletions planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,33 @@ func (er *expressionRewriter) handleExistSubquery(ctx context.Context, v *ast.Ex
er.err = err
return v, true
}
if er.b.ctx.GetSessionVars().StmtCtx.InExplainStmt && !er.b.ctx.GetSessionVars().StmtCtx.InExplainAnalyzeStmt && er.b.ctx.GetSessionVars().ExplainNonEvaledSubQuery {
newColID := er.b.ctx.GetSessionVars().AllocPlanColumnID()
subqueryCtx := ScalarSubqueryEvalCtx{
scalarSubQuery: physicalPlan,
ctx: ctx,
is: er.b.is,
outputColIDs: []int64{newColID},
}.Init(er.b.ctx, np.SelectBlockOffset())
scalarSubQ := &ScalarSubQueryExpr{
scalarSubqueryColID: newColID,
evalCtx: subqueryCtx,
}
scalarSubQ.RetType = np.Schema().Columns[0].GetType()
scalarSubQ.SetCoercibility(np.Schema().Columns[0].Coercibility())
er.b.ctx.GetSessionVars().RegisterScalarSubQ(subqueryCtx)
if v.Not {
notWrapped, err := expression.NewFunction(er.b.ctx, ast.UnaryNot, types.NewFieldType(mysql.TypeTiny), scalarSubQ)
if err != nil {
er.err = err
return v, true
}
er.ctxStackAppend(notWrapped, types.EmptyName)
return v, true
}
er.ctxStackAppend(scalarSubQ, types.EmptyName)
return v, true
}
row, err := EvalSubqueryFirstRow(ctx, physicalPlan, er.b.is, er.b.ctx)
if err != nil {
er.err = err
Expand Down Expand Up @@ -1018,6 +1045,40 @@ func (er *expressionRewriter) handleScalarSubquery(ctx context.Context, v *ast.S
er.err = err
return v, true
}
if er.b.ctx.GetSessionVars().StmtCtx.InExplainStmt && !er.b.ctx.GetSessionVars().StmtCtx.InExplainAnalyzeStmt && er.b.ctx.GetSessionVars().ExplainNonEvaledSubQuery {
subqueryCtx := ScalarSubqueryEvalCtx{
scalarSubQuery: physicalPlan,
ctx: ctx,
is: er.b.is,
}.Init(er.b.ctx, np.SelectBlockOffset())
newColIDs := make([]int64, 0, np.Schema().Len())
newScalarSubQueryExprs := make([]expression.Expression, 0, np.Schema().Len())
for _, col := range np.Schema().Columns {
newColID := er.b.ctx.GetSessionVars().AllocPlanColumnID()
scalarSubQ := &ScalarSubQueryExpr{
scalarSubqueryColID: newColID,
evalCtx: subqueryCtx,
}
scalarSubQ.RetType = col.RetType
scalarSubQ.SetCoercibility(col.Coercibility())
newColIDs = append(newColIDs, newColID)
newScalarSubQueryExprs = append(newScalarSubQueryExprs, scalarSubQ)
}
subqueryCtx.outputColIDs = newColIDs

er.b.ctx.GetSessionVars().RegisterScalarSubQ(subqueryCtx)
if len(newScalarSubQueryExprs) == 1 {
er.ctxStackAppend(newScalarSubQueryExprs[0], types.EmptyName)
} else {
rowFunc, err := er.newFunction(ast.RowFunc, newScalarSubQueryExprs[0].GetType(), newScalarSubQueryExprs...)
if err != nil {
er.err = err
return v, true
}
er.ctxStack = append(er.ctxStack, rowFunc)
}
return v, true
}
row, err := EvalSubqueryFirstRow(ctx, physicalPlan, er.b.is, er.b.ctx)
if err != nil {
er.err = err
Expand Down
6 changes: 6 additions & 0 deletions planner/core/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,9 @@ func (p PhysicalCTETable) Init(ctx sessionctx.Context, stats *property.StatsInfo
p.stats = stats
return &p
}

// Init initializes ScalarSubqueryEvalCtx
func (p ScalarSubqueryEvalCtx) Init(ctx sessionctx.Context, offset int) *ScalarSubqueryEvalCtx {
p.basePlan = newBasePlan(ctx, plancodec.TypeScalarSubQuery, offset)
return &p
}
Loading