Skip to content

Commit 83e4f10

Browse files
committed
opt/sql: fix explain analyze missing option
ConstructExplain previously ignored the ANALYZE option so any EXPLAIN ANALYZE statement would result in execution as an EXPLAIN (DISTSQL) statement. The ANALYZE option is now observed in ConstructExplain. Additionally, the stmtType field from the explainDistSQLNode has been removed because it was not necessary and it was unclear how to pass this from the `execFactory`. Release note: None
1 parent 33c6fac commit 83e4f10

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

pkg/sql/explain.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ func (p *planner) Explain(ctx context.Context, n *tree.Explain) (planNode, error
4242
return nil, err
4343
}
4444
return &explainDistSQLNode{
45-
plan: plan,
46-
analyze: opts.Flags.Contains(tree.ExplainFlagAnalyze),
47-
stmtType: n.Statement.StatementType(),
45+
plan: plan,
46+
analyze: opts.Flags.Contains(tree.ExplainFlagAnalyze),
4847
}, nil
4948

5049
case tree.ExplainPlan:

pkg/sql/explain_distsql.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ type explainDistSQLNode struct {
3434
// pointing to a visual query plan with statistics will be in the row
3535
// returned by the node.
3636
analyze bool
37-
// stmtType is the StatementType of the plan. It is needed by the
38-
// distSQLWrapper when analyzing a statement.
39-
stmtType tree.StatementType
4037

4138
run explainDistSQLRun
4239
}
@@ -89,10 +86,14 @@ func (n *explainDistSQLNode) startExec(params runParams) error {
8986
return nil
9087
})
9188
execCfg := params.p.ExecCfg()
89+
// tree.RowsAffected is used as the statement type passed in to the distsql
90+
// receiver because it isn't necessary to process any result rows from the
91+
// wrapped plan.
92+
const stmtType = tree.RowsAffected
9293
recv := makeDistSQLReceiver(
9394
params.ctx,
9495
rw,
95-
n.stmtType,
96+
stmtType,
9697
execCfg.RangeDescriptorCache,
9798
execCfg.LeaseHolderCache,
9899
params.p.txn,

pkg/sql/opt_exec_factory.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,14 +514,22 @@ func (ef *execFactory) ConstructExplain(
514514
) (exec.Node, error) {
515515
p := plan.(*planTop)
516516

517+
analyzeSet := options.Flags.Contains(tree.ExplainFlagAnalyze)
518+
517519
switch options.Mode {
518520
case tree.ExplainDistSQL:
519521
if len(p.subqueryPlans) > 0 {
520522
return nil, fmt.Errorf("subqueries not supported yet")
521523
}
522-
return &explainDistSQLNode{plan: p.plan}, nil
524+
return &explainDistSQLNode{
525+
plan: p.plan,
526+
analyze: analyzeSet,
527+
}, nil
523528

524529
case tree.ExplainPlan:
530+
if analyzeSet {
531+
return nil, errors.New("EXPLAIN ANALYZE only supported with (DISTSQL) option")
532+
}
525533
// NOEXPAND and NOOPTIMIZE must always be set when using the optimizer to
526534
// prevent the plans from being modified.
527535
opts := *options

0 commit comments

Comments
 (0)