Skip to content

Commit

Permalink
sql: support EXPLAIN ANALYZE EXECUTE
Browse files Browse the repository at this point in the history
We currently cannot EXPLAIN an EXECUTE statement (the syntax doesn't
even allow it). EXECUTE is handled with special code at the top-level,
so it's tricky to make it work with EXPLAIN. However, EXPLAIN ANALYZE
Is also handled at the top level, before the EXECUTE handling, so that
should just work.

This change allows the parsing of such statements so we can use
EXPLAIN ANALYZE EXECUTE. If EXPLAIN EXECUTE is attempted, we get a
"not supported" error (this is less cryptic than the parsing error
anyway).

Release note: None
  • Loading branch information
RaduBerinde committed Apr 20, 2021
1 parent defd143 commit 22e4d64
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 29 deletions.
12 changes: 6 additions & 6 deletions docs/generated/sql/bnf/explain_analyze_stmt.bnf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
explain_stmt ::=
'EXPLAIN' preparable_stmt
| 'EXPLAIN' '(' ( 'PLAN' | 'DISTSQL' | 'DEBUG' ) ( ( ',' ( 'PLAN' | 'DISTSQL' | 'DEBUG' ) ) )* ')' preparable_stmt
| 'EXPLAIN' 'ANALYZE' preparable_stmt
| 'EXPLAIN' 'ANALYSE' preparable_stmt
| 'EXPLAIN' 'ANALYZE' '(' ( 'PLAN' | 'DISTSQL' | 'DEBUG' ) ( ( ',' ( 'PLAN' | 'DISTSQL' | 'DEBUG' ) ) )* ')' preparable_stmt
| 'EXPLAIN' 'ANALYSE' '(' ( 'PLAN' | 'DISTSQL' | 'DEBUG' ) ( ( ',' ( 'PLAN' | 'DISTSQL' | 'DEBUG' ) ) )* ')' preparable_stmt
'EXPLAIN' explainable_stmt
| 'EXPLAIN' '(' ( 'PLAN' | 'DISTSQL' | 'DEBUG' ) ( ( ',' ( 'PLAN' | 'DISTSQL' | 'DEBUG' ) ) )* ')' explainable_stmt
| 'EXPLAIN' 'ANALYZE' explainable_stmt
| 'EXPLAIN' 'ANALYSE' explainable_stmt
| 'EXPLAIN' 'ANALYZE' '(' ( 'PLAN' | 'DISTSQL' | 'DEBUG' ) ( ( ',' ( 'PLAN' | 'DISTSQL' | 'DEBUG' ) ) )* ')' explainable_stmt
| 'EXPLAIN' 'ANALYSE' '(' ( 'PLAN' | 'DISTSQL' | 'DEBUG' ) ( ( ',' ( 'PLAN' | 'DISTSQL' | 'DEBUG' ) ) )* ')' explainable_stmt
4 changes: 2 additions & 2 deletions docs/generated/sql/bnf/explain_stmt.bnf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
explain_stmt ::=
'EXPLAIN' preparable_stmt
| 'EXPLAIN' '(' ( 'VERBOSE' | 'TYPES' | 'OPT' | 'DISTSQL' | 'VEC' ) ( ( ',' ( 'VERBOSE' | 'TYPES' | 'OPT' | 'DISTSQL' | 'VEC' ) ) )* ')' preparable_stmt
'EXPLAIN' explainable_stmt
| 'EXPLAIN' '(' ( 'VERBOSE' | 'TYPES' | 'OPT' | 'DISTSQL' | 'VEC' ) ( ( ',' ( 'VERBOSE' | 'TYPES' | 'OPT' | 'DISTSQL' | 'VEC' ) ) )* ')' explainable_stmt
16 changes: 10 additions & 6 deletions docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ drop_stmt ::=
| drop_schedule_stmt

explain_stmt ::=
'EXPLAIN' preparable_stmt
| 'EXPLAIN' '(' explain_option_list ')' preparable_stmt
| 'EXPLAIN' 'ANALYZE' preparable_stmt
| 'EXPLAIN' 'ANALYSE' preparable_stmt
| 'EXPLAIN' 'ANALYZE' '(' explain_option_list ')' preparable_stmt
| 'EXPLAIN' 'ANALYSE' '(' explain_option_list ')' preparable_stmt
'EXPLAIN' explainable_stmt
| 'EXPLAIN' '(' explain_option_list ')' explainable_stmt
| 'EXPLAIN' 'ANALYZE' explainable_stmt
| 'EXPLAIN' 'ANALYSE' explainable_stmt
| 'EXPLAIN' 'ANALYZE' '(' explain_option_list ')' explainable_stmt
| 'EXPLAIN' 'ANALYSE' '(' explain_option_list ')' explainable_stmt

import_stmt ::=
'IMPORT' import_format string_or_placeholder opt_with_options
Expand Down Expand Up @@ -492,6 +492,10 @@ drop_schedule_stmt ::=
'DROP' 'SCHEDULE' a_expr
| 'DROP' 'SCHEDULES' select_stmt

explainable_stmt ::=
preparable_stmt
| execute_stmt

explain_option_list ::=
( explain_option_name ) ( ( ',' explain_option_name ) )*

Expand Down
24 changes: 24 additions & 0 deletions pkg/sql/opt/exec/execbuilder/testdata/explain
Original file line number Diff line number Diff line change
Expand Up @@ -1567,3 +1567,27 @@ vectorized: true
• insert fast path
into: t2(x)
auto commit

statement ok
PREPARE prep AS SELECT $1 + 1

statement error EXPLAIN EXECUTE is not supported
EXPLAIN EXECUTE prep(1)

statement error EXPLAIN EXECUTE is not supported
SELECT 1 FROM [ EXPLAIN EXECUTE prep(1) ]

query T
EXPLAIN ANALYZE EXECUTE prep(1)
----
planning time: 10µs
execution time: 100µs
distribution: <hidden>
vectorized: <hidden>
maximum memory usage: <hidden>
network usage: <hidden>
·
• values
cluster nodes: <hidden>
actual row count: 1
size: 1 column, 1 row
8 changes: 8 additions & 0 deletions pkg/sql/opt/optbuilder/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ import (
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
"github.com/cockroachdb/errors"
)

func (b *Builder) buildExplain(explain *tree.Explain, inScope *scope) (outScope *scope) {
if _, ok := explain.Statement.(*tree.Execute); ok {
panic(pgerror.New(
pgcode.FeatureNotSupported, "EXPLAIN EXECUTE is not supported; use EXPLAIN ANALYZE",
))
}

stmtScope := b.buildStmtAtRoot(explain.Statement, nil /* desiredTypes */)

outScope = inScope.push()
Expand Down
17 changes: 11 additions & 6 deletions pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,7 @@ func (u *sqlSymUnion) objectNamePrefixList() tree.ObjectNamePrefixList {
%type <tree.Statement> explain_stmt
%type <tree.Statement> prepare_stmt
%type <tree.Statement> preparable_stmt
%type <tree.Statement> explainable_stmt
%type <tree.Statement> row_source_extension_stmt
%type <tree.Statement> export_stmt
%type <tree.Statement> execute_stmt
Expand Down Expand Up @@ -3801,7 +3802,7 @@ analyze_target:
//
// %SeeAlso: WEBDOCS/explain.html
explain_stmt:
EXPLAIN preparable_stmt
EXPLAIN explainable_stmt
{
var err error
$$.val, err = tree.MakeExplain(nil /* options */, $2.stmt())
Expand All @@ -3810,39 +3811,39 @@ explain_stmt:
}
}
| EXPLAIN error // SHOW HELP: EXPLAIN
| EXPLAIN '(' explain_option_list ')' preparable_stmt
| EXPLAIN '(' explain_option_list ')' explainable_stmt
{
var err error
$$.val, err = tree.MakeExplain($3.strs(), $5.stmt())
if err != nil {
return setErr(sqllex, err)
}
}
| EXPLAIN ANALYZE preparable_stmt
| EXPLAIN ANALYZE explainable_stmt
{
var err error
$$.val, err = tree.MakeExplain([]string{"ANALYZE"}, $3.stmt())
if err != nil {
return setErr(sqllex, err)
}
}
| EXPLAIN ANALYSE preparable_stmt
| EXPLAIN ANALYSE explainable_stmt
{
var err error
$$.val, err = tree.MakeExplain([]string{"ANALYZE"}, $3.stmt())
if err != nil {
return setErr(sqllex, err)
}
}
| EXPLAIN ANALYZE '(' explain_option_list ')' preparable_stmt
| EXPLAIN ANALYZE '(' explain_option_list ')' explainable_stmt
{
var err error
$$.val, err = tree.MakeExplain(append($4.strs(), "ANALYZE"), $6.stmt())
if err != nil {
return setErr(sqllex, err)
}
}
| EXPLAIN ANALYSE '(' explain_option_list ')' preparable_stmt
| EXPLAIN ANALYSE '(' explain_option_list ')' explainable_stmt
{
var err error
$$.val, err = tree.MakeExplain(append($4.strs(), "ANALYZE"), $6.stmt())
Expand All @@ -3856,6 +3857,10 @@ explain_stmt:
// the context of EXPLAIN.
| EXPLAIN '(' error // SHOW HELP: EXPLAIN

explainable_stmt:
preparable_stmt
| execute_stmt

preparable_stmt:
alter_stmt // help texts in sub-rule
| backup_stmt // EXTEND WITH HELP: BACKUP
Expand Down
9 changes: 0 additions & 9 deletions pkg/sql/parser/testdata/errors
Original file line number Diff line number Diff line change
Expand Up @@ -515,15 +515,6 @@ DETAIL: source SQL:
e'\xad'::string
^

error
EXPLAIN EXECUTE a
----
at or near "execute": syntax error
DETAIL: source SQL:
EXPLAIN EXECUTE a
^
HINT: try \h EXPLAIN

error
EXPLAIN (ANALYZE, PLAN) SELECT 1
----
Expand Down

0 comments on commit 22e4d64

Please sign in to comment.