Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(mysql,tidb,oceanbase): allow EXPLAIN ANALYZE over a read (#21734)

Danny Xu099e2d6a56b4
diff --git a/backend/plugin/parser/mysql/query.go b/backend/plugin/parser/mysql/query.go
index 29f965520d..bf2e6ae47c 100644
--- a/backend/plugin/parser/mysql/query.go
+++ b/backend/plugin/parser/mysql/query.go
@@ -14,7 +14,7 @@ func init() {
// validateQuery validates the SQL statement for SQL editor.
// Only SELECT, EXPLAIN, SHOW, SET, and DESCRIBE are allowed in read-only mode.
-// EXPLAIN ANALYZE is treated as non-read-only since it actually executes the query.
+// EXPLAIN ANALYZE executes what it wraps, so it is allowed only over a read.
func validateQuery(statement string) (bool, bool, error) {
stmts, err := ParseMySQL(statement)
if err != nil {
@@ -37,7 +37,7 @@ func validateQuery(statement string) (bool, bool, error) {
hasExecute = true
}
case *ast.ExplainStmt:
- if stmt.Analyze {
+ if !explainReads(stmt) {
readOnly = false
}
case *ast.ShowStmt:
@@ -77,3 +77,39 @@ func selectIntoTargets(n *ast.SelectStmt) (writesFile, assignsVariables bool) {
})
return writesFile, assignsVariables
}
+
+// explainReads reports whether an EXPLAIN is a read.
+//
+// A plain EXPLAIN only plans, so it reads whatever it wraps, DML included — the
+// editor has always allowed that. An export is the exception: INTO OUTFILE
+// under EXPLAIN is still a request to write a file on the server, and the
+// editor must not label as a read what it refuses when asked directly.
+// EXPLAIN ANALYZE runs what it wraps, so there the wrapped statement has to be
+// a read in full.
+func explainReads(explain *ast.ExplainStmt) bool {
+ if sel, ok := explain.Stmt.(*ast.SelectStmt); ok {
+ if writesFile, _ := selectIntoTargets(sel); writesFile {
+ return false
+ }
+ }
+ if explain.Analyze {
+ return explainAnalyzeReads(explain.Stmt)
+ }
+ return true
+}
+
+// explainAnalyzeReads reports whether EXPLAIN ANALYZE over stmt only reads.
+//
+// EXPLAIN ANALYZE runs the statement to report actual row counts and timings,
+// so it is a read exactly when what it wraps is one: a SELECT that writes no
+// file and assigns no variable. Postgres gates its own EXPLAIN ANALYZE the
+// same way. Anything else is refused, which matters most for DML — EXPLAIN
+// ANALYZE DELETE really deletes, and a plain EXPLAIN does not.
+func explainAnalyzeReads(stmt ast.StmtNode) bool {
+ sel, ok := stmt.(*ast.SelectStmt)
+ if !ok {
+ return false
+ }
+ writesFile, assignsVariables := selectIntoTargets(sel)
+ return !writesFile && !assignsVariables
+}
diff --git a/backend/plugin/parser/mysql/query_test.go b/backend/plugin/parser/mysql/query_test.go
index d8ffc40e47..2615cf106a 100644
--- a/backend/plugin/parser/mysql/query_test.go
+++ b/backend/plugin/parser/mysql/query_test.go
@@ -62,7 +62,7 @@ func TestValidateSQLForEditor(t *testing.T) {
},
{
statement: `explain analyze select * from t`,
- valid: false,
+ valid: true,
gotAllQuery: true,
},
{
@@ -140,3 +140,64 @@ func TestValidateSQLForEditor(t *testing.T) {
})
}
}
+
+// EXPLAIN ANALYZE executes what it wraps, unlike a plain EXPLAIN, so it is
+// allowed over a read and refused over everything else. Before this it was
+// refused outright, which cost the legitimate case: profiling a SELECT.
+func TestValidateQueryExplainAnalyze(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ statement string
+ wantValid bool
+ }{
+ {"a plain EXPLAIN plans without running", "EXPLAIN SELECT count(*) FROM t WHERE a = 1", true},
+ {"EXPLAIN ANALYZE over a read", "EXPLAIN ANALYZE SELECT count(*) FROM t WHERE a = 1", true},
+ {"EXPLAIN ANALYZE over a set operation", "EXPLAIN ANALYZE SELECT a FROM t UNION SELECT a FROM s", true},
+ {"EXPLAIN ANALYZE really deletes", "EXPLAIN ANALYZE DELETE FROM t WHERE a = 1", false},
+ {"EXPLAIN ANALYZE really inserts", "EXPLAIN ANALYZE INSERT INTO t SELECT a FROM s", false},
+ {"EXPLAIN ANALYZE really updates", "EXPLAIN ANALYZE UPDATE t SET a = 1", false},
+ {"a plain EXPLAIN of a DML still only plans", "EXPLAIN DELETE FROM t WHERE a = 1", true},
+ {"EXPLAIN ANALYZE over a SELECT that writes a file", "EXPLAIN ANALYZE SELECT a FROM t INTO OUTFILE '/tmp/x'", false},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ gotValid, _, err := validateQuery(tc.statement)
+ require.NoError(t, err)
+ require.Equal(t, tc.wantValid, gotValid, tc.statement)
+ })
+ }
+}
+
+// An export under EXPLAIN is still an export request: the editor must not label
+// as a read what it refuses when asked directly. The reads beneath keep the
+// gate from being satisfied by refusing every EXPLAIN.
+func TestValidateQueryExportGate(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ statement string
+ wantValid bool
+ }{
+ {"a bare export", "SELECT a FROM t INTO OUTFILE '/tmp/x'", false},
+ {"a bare dump", "SELECT a FROM t INTO DUMPFILE '/tmp/x'", false},
+ {"an export on the second arm", "SELECT 1 UNION SELECT a FROM t INTO OUTFILE '/tmp/x'", false},
+ {"an export in a braced arm", "(SELECT a FROM t INTO OUTFILE '/tmp/x') UNION (SELECT 1)", false},
+ {"an export inside a subquery", "SELECT * FROM (SELECT a FROM t INTO OUTFILE '/tmp/x') y", false},
+ {"an export under a plain EXPLAIN", "EXPLAIN SELECT a FROM t INTO OUTFILE '/tmp/x'", false},
+ {"a dump under a plain EXPLAIN", "EXPLAIN SELECT a FROM t INTO DUMPFILE '/tmp/x'", false},
+ {"an export on an arm under a plain EXPLAIN", "EXPLAIN SELECT 1 UNION SELECT a FROM t INTO OUTFILE '/tmp/x'", false},
+ {"an export under EXPLAIN ANALYZE", "EXPLAIN ANALYZE SELECT a FROM t INTO OUTFILE '/tmp/x'", false},
+
+ {"a set operation that exports nothing", "SELECT 1 UNION SELECT 2", true},
+ {"EXPLAIN ANALYZE over a set operation", "EXPLAIN ANALYZE SELECT 1 UNION SELECT 2", true},
+ {"a variable assignment writes no file", "SELECT a FROM t INTO @v", true},
+ {"a variable assignment under a plain EXPLAIN only plans", "EXPLAIN SELECT a FROM t INTO @v", true},
+ {"a variable assignment under EXPLAIN ANALYZE really assigns", "EXPLAIN ANALYZE SELECT a FROM t INTO @v", false},
+ {"a variable assignment on an arm under EXPLAIN ANALYZE", "EXPLAIN ANALYZE SELECT 1 UNION SELECT a FROM t INTO @v", false},
+ {"the clause named in a string literal", "SELECT 'INTO OUTFILE' FROM t", true},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ gotValid, _, err := validateQuery(tc.statement)
+ require.NoError(t, err)
+ require.Equal(t, tc.wantValid, gotValid, tc.statement)
+ })
+ }
+}
diff --git a/backend/plugin/parser/oceanbase/query.go b/backend/plugin/parser/oceanbase/query.go
index cd272c7d76..99250c0f87 100644
--- a/backend/plugin/parser/oceanbase/query.go
+++ b/backend/plugin/parser/oceanbase/query.go
@@ -13,7 +13,7 @@ func init() {
// validateQuery validates the SQL statement for SQL editor.
// Only SELECT, EXPLAIN, SHOW, SET, and DESCRIBE are allowed in read-only mode.
-// EXPLAIN ANALYZE is treated as non-read-only since it actually executes the query.
+// EXPLAIN ANALYZE executes what it wraps, so it is allowed only over a read.
func validateQuery(statement string) (bool, bool, error) {
stmts, err := ParseOceanBase(statement)
if err != nil {
@@ -36,7 +36,7 @@ func validateQuery(statement string) (bool, bool, error) {
hasExecute = true
}
case *ast.ExplainStmt:
- if stmt.Analyze {
+ if !explainReads(stmt) {
readOnly = false
}
case *ast.ShowStmt:
@@ -76,3 +76,39 @@ func selectIntoTargets(n *ast.SelectStmt) (writesFile, assignsVariables bool) {
})
return writesFile, assignsVariables
}
+
+// explainReads reports whether an EXPLAIN is a read.
+//
+// A plain EXPLAIN only plans, so it reads whatever it wraps, DML included — the
+// editor has always allowed that. An export is the exception: INTO OUTFILE
+// under EXPLAIN is still a request to write a file on the server, and the
+// editor must not label as a read what it refuses when asked directly.
+// EXPLAIN ANALYZE runs what it wraps, so there the wrapped statement has to be
+// a read in full.
+func explainReads(explain *ast.ExplainStmt) bool {
+ if sel, ok := explain.Stmt.(*ast.SelectStmt); ok {
+ if writesFile, _ := selectIntoTargets(sel); writesFile {
+ return false
+ }
+ }
+ if explain.Analyze {
+ return explainAnalyzeReads(explain.Stmt)
+ }
+ return true
+}
+
+// explainAnalyzeReads reports whether EXPLAIN ANALYZE over stmt only reads.
+//
+// EXPLAIN ANALYZE runs the statement to report actual row counts and timings,
+// so it is a read exactly when what it wraps is one: a SELECT that writes no
+// file and assigns no variable. Postgres gates its own EXPLAIN ANALYZE the
+// same way. Anything else is refused, which matters most for DML — EXPLAIN
+// ANALYZE DELETE really deletes, and a plain EXPLAIN does not.
+func explainAnalyzeReads(stmt ast.StmtNode) bool {
+ sel, ok := stmt.(*ast.SelectStmt)
+ if !ok {
+ return false
+ }
+ writesFile, assignsVariables := selectIntoTargets(sel)
+ return !writesFile && !assignsVariables
+}
diff --git a/backend/plugin/parser/oceanbase/query_test.go b/backend/plugin/parser/oceanbase/query_test.go
index 2b3c2f504c..e6b56a9617 100644
--- a/backend/plugin/parser/oceanbase/query_test.go
+++ b/backend/plugin/parser/oceanbase/query_test.go
@@ -62,7 +62,7 @@ func TestValidateSQLForEditor(t *testing.T) {
},
{
statement: `explain analyze select * from t`,
- valid: false,
+ valid: true,
gotAllQuery: true,
},
{
@@ -140,3 +140,64 @@ func TestValidateSQLForEditor(t *testing.T) {
})
}
}
+
+// EXPLAIN ANALYZE executes what it wraps, unlike a plain EXPLAIN, so it is
+// allowed over a read and refused over everything else. Before this it was
+// refused outright, which cost the legitimate case: profiling a SELECT.
+func TestValidateQueryExplainAnalyze(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ statement string
+ wantValid bool
+ }{
+ {"a plain EXPLAIN plans without running", "EXPLAIN SELECT count(*) FROM t WHERE a = 1", true},
+ {"EXPLAIN ANALYZE over a read", "EXPLAIN ANALYZE SELECT count(*) FROM t WHERE a = 1", true},
+ {"EXPLAIN ANALYZE over a set operation", "EXPLAIN ANALYZE SELECT a FROM t UNION SELECT a FROM s", true},
+ {"EXPLAIN ANALYZE really deletes", "EXPLAIN ANALYZE DELETE FROM t WHERE a = 1", false},
+ {"EXPLAIN ANALYZE really inserts", "EXPLAIN ANALYZE INSERT INTO t SELECT a FROM s", false},
+ {"EXPLAIN ANALYZE really updates", "EXPLAIN ANALYZE UPDATE t SET a = 1", false},
+ {"a plain EXPLAIN of a DML still only plans", "EXPLAIN DELETE FROM t WHERE a = 1", true},
+ {"EXPLAIN ANALYZE over a SELECT that writes a file", "EXPLAIN ANALYZE SELECT a FROM t INTO OUTFILE '/tmp/x'", false},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ gotValid, _, err := validateQuery(tc.statement)
+ require.NoError(t, err)
+ require.Equal(t, tc.wantValid, gotValid, tc.statement)
+ })
+ }
+}
+
+// An export under EXPLAIN is still an export request: the editor must not label
+// as a read what it refuses when asked directly. The reads beneath keep the
+// gate from being satisfied by refusing every EXPLAIN.
+func TestValidateQueryExportGate(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ statement string
+ wantValid bool
+ }{
+ {"a bare export", "SELECT a FROM t INTO OUTFILE '/tmp/x'", false},
+ {"a bare dump", "SELECT a FROM t INTO DUMPFILE '/tmp/x'", false},
+ {"an export on the second arm", "SELECT 1 UNION SELECT a FROM t INTO OUTFILE '/tmp/x'", false},
+ {"an export in a braced arm", "(SELECT a FROM t INTO OUTFILE '/tmp/x') UNION (SELECT 1)", false},
+ {"an export inside a subquery", "SELECT * FROM (SELECT a FROM t INTO OUTFILE '/tmp/x') y", false},
+ {"an export under a plain EXPLAIN", "EXPLAIN SELECT a FROM t INTO OUTFILE '/tmp/x'", false},
+ {"a dump under a plain EXPLAIN", "EXPLAIN SELECT a FROM t INTO DUMPFILE '/tmp/x'", false},
+ {"an export on an arm under a plain EXPLAIN", "EXPLAIN SELECT 1 UNION SELECT a FROM t INTO OUTFILE '/tmp/x'", false},
+ {"an export under EXPLAIN ANALYZE", "EXPLAIN ANALYZE SELECT a FROM t INTO OUTFILE '/tmp/x'", false},
+
+ {"a set operation that exports nothing", "SELECT 1 UNION SELECT 2", true},
+ {"EXPLAIN ANALYZE over a set operation", "EXPLAIN ANALYZE SELECT 1 UNION SELECT 2", true},
+ {"a variable assignment writes no file", "SELECT a FROM t INTO @v", true},
+ {"a variable assignment under a plain EXPLAIN only plans", "EXPLAIN SELECT a FROM t INTO @v", true},
+ {"a variable assignment under EXPLAIN ANALYZE really assigns", "EXPLAIN ANALYZE SELECT a FROM t INTO @v", false},
+ {"a variable assignment on an arm under EXPLAIN ANALYZE", "EXPLAIN ANALYZE SELECT 1 UNION SELECT a FROM t INTO @v", false},
+ {"the clause named in a string literal", "SELECT 'INTO OUTFILE' FROM t", true},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ gotValid, _, err := validateQuery(tc.statement)
+ require.NoError(t, err)
+ require.Equal(t, tc.wantValid, gotValid, tc.statement)
+ })
+ }
+}
diff --git a/backend/plugin/parser/tidb/query.go b/backend/plugin/parser/tidb/query.go
index 1e6b63029a..7a98f10159 100644
--- a/backend/plugin/parser/tidb/query.go
+++ b/backend/plugin/parser/tidb/query.go
@@ -12,10 +12,8 @@ func init() {
}
// validateQuery validates the SQL statement for SQL editor.
-// We validate the statement by following steps:
-// 1. Remove all quoted text(quoted identifier, string literal) and comments from the statement.
-// 2. Use regexp to check if the statement is a normal SELECT statement and EXPLAIN statement.
-// 3. For CTE, use regexp to check if the statement has UPDATE, DELETE and INSERT statements.
+// Only SELECT, EXPLAIN, SHOW, SET and DESCRIBE are allowed in read-only mode.
+// EXPLAIN ANALYZE executes what it wraps, so it is allowed only over a read.
func validateQuery(statement string) (bool, bool, error) {
stmtList, err := ParseTiDB(statement, "", "")
if err != nil {
@@ -25,25 +23,18 @@ func validateQuery(statement string) (bool, bool, error) {
readOnly := true
for _, stmt := range stmtList {
switch stmt := stmt.(type) {
- case *ast.SelectStmt:
+ case *ast.SelectStmt, *ast.SetOprStmt:
// INTO OUTFILE writes a file on the database server, so the
- // statement is not a read whatever the SELECT itself does — pg
+ // statement is not a read whatever the query itself does — pg
// refuses its own SELECT ... INTO on the same grounds.
- //
- // Every INTO form is refused here, not just OUTFILE, and TiDB's
- // parser is why there is nothing more to distinguish: it rejects
- // DUMPFILE, INTO <variable>, and INTO on a set operation outright,
- // so OUTFILE is the only form that reaches this line today and any
- // form a later parser accepts lands on the same refusal.
- if stmt.SelectIntoOpt != nil {
+ if hasSelectInto(stmt) {
return false, false, nil
}
- case *ast.SetOprStmt:
case *ast.SetStmt:
hasExecute = true
case *ast.ShowStmt:
case *ast.ExplainStmt:
- if stmt.Analyze {
+ if !explainReads(stmt) {
readOnly = false
}
default:
@@ -245,3 +236,72 @@ func unfoldSelectList(list *ast.SetOprSelectList, unfoldList *ast.SetOprSelectLi
}
}
}
+
+// explainReads reports whether an EXPLAIN is a read. The two EXPLAIN forms are
+// decided here together so the export gate below is the only one either goes
+// through: a second gate on the ANALYZE path could only drift from this one.
+//
+// The parser gives every EXPLAIN form this handles a statement to wrap, DESCRIBE
+// included; statement-less `EXPLAIN FOR CONNECTION` parses to ExplainForStmt,
+// which the validator refuses by default. So the nil guard is for safety here,
+// not a policy.
+func explainReads(explain *ast.ExplainStmt) bool {
+ if explain.Stmt == nil {
+ return false
+ }
+ // An export is refused under either form. A plain EXPLAIN does not run it,
+ // but the editor must not label as a read what it refuses when asked
+ // directly.
+ if hasSelectInto(explain.Stmt) {
+ return false
+ }
+ if !explain.Analyze {
+ // A plain EXPLAIN only plans, so it reads whatever it wraps, DML
+ // included — the editor has always allowed that.
+ return true
+ }
+ // EXPLAIN ANALYZE runs the statement to report actual row counts and
+ // timings, so only the SELECT family is a read. Postgres gates its own
+ // EXPLAIN ANALYZE the same way. The refusal matters most for DML: EXPLAIN
+ // ANALYZE DELETE really deletes, and a plain EXPLAIN does not.
+ switch explain.Stmt.(type) {
+ case *ast.SelectStmt, *ast.SetOprStmt:
+ return true
+ }
+ return false
+}
+
+// hasSelectInto reports whether any SELECT in node carries an INTO clause.
+//
+// The whole tree is searched rather than the node the caller holds, because the
+// parser attaches INTO to an arm of a set operation and not to the set
+// operation itself: `SELECT 1 UNION SELECT a FROM t INTO OUTFILE '/x'` is a
+// SetOprStmt whose second arm carries the export. The parser also accepts INTO
+// inside a subquery or a CTE, so the search reaches statements whose root is an
+// ordinary SELECT; refusing those is the conservative answer for a clause that
+// writes a file on the server, and it is what the MySQL validator already does.
+//
+// Every INTO form counts, not just OUTFILE: the parser rejects DUMPFILE and
+// INTO <variable> outright, so OUTFILE is the only form that reaches here
+// today and any form a later parser accepts is refused by default.
+func hasSelectInto(node ast.Node) bool {
+ v := &selectIntoVisitor{}
+ node.Accept(v)
+ return v.found
+}
+
+// selectIntoVisitor finds a SELECT carrying an INTO clause.
+type selectIntoVisitor struct {
+ found bool
+}
+
+func (v *selectIntoVisitor) Enter(node ast.Node) (ast.Node, bool) {
+ if sel, ok := node.(*ast.SelectStmt); ok && sel.SelectIntoOpt != nil {
+ v.found = true
+ }
+ return node, v.found
+}
+
+func (*selectIntoVisitor) Leave(node ast.Node) (ast.Node, bool) {
+ return node, true
+}
diff --git a/backend/plugin/parser/tidb/query_test.go b/backend/plugin/parser/tidb/query_test.go
index 8fd1201a3a..710423df5a 100644
--- a/backend/plugin/parser/tidb/query_test.go
+++ b/backend/plugin/parser/tidb/query_test.go
@@ -65,7 +65,7 @@ func TestValidateSQLForEditor(t *testing.T) {
},
{
statement: `explain analyze select * from t`,
- valid: false,
+ valid: true,
gotAllQuery: true,
},
{
@@ -137,3 +137,65 @@ func TestValidateSQLForEditor(t *testing.T) {
}
}
}
+
+// EXPLAIN ANALYZE executes what it wraps, unlike a plain EXPLAIN, so it is
+// allowed over a read and refused over everything else. Before this it was
+// refused outright, which cost the legitimate case: profiling a SELECT.
+func TestValidateQueryExplainAnalyze(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ statement string
+ wantValid bool
+ }{
+ {"a plain EXPLAIN plans without running", "EXPLAIN SELECT count(*) FROM t WHERE a = 1", true},
+ {"EXPLAIN ANALYZE over a read", "EXPLAIN ANALYZE SELECT count(*) FROM t WHERE a = 1", true},
+ {"EXPLAIN ANALYZE over a set operation", "EXPLAIN ANALYZE SELECT a FROM t UNION SELECT a FROM s", true},
+ {"EXPLAIN ANALYZE really deletes", "EXPLAIN ANALYZE DELETE FROM t WHERE a = 1", false},
+ {"EXPLAIN ANALYZE really inserts", "EXPLAIN ANALYZE INSERT INTO t SELECT a FROM s", false},
+ {"EXPLAIN ANALYZE really updates", "EXPLAIN ANALYZE UPDATE t SET a = 1", false},
+ {"a plain EXPLAIN of a DML still only plans", "EXPLAIN DELETE FROM t WHERE a = 1", true},
+ {"EXPLAIN ANALYZE over a SELECT that writes a file", "EXPLAIN ANALYZE SELECT a FROM t INTO OUTFILE '/tmp/x'", false},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ gotValid, _, err := validateQuery(tc.statement)
+ require.NoError(t, err)
+ require.Equal(t, tc.wantValid, gotValid, tc.statement)
+ })
+ }
+}
+
+// A set operation carries INTO on an arm, not on the node the validator holds,
+// so classifying by the top-level node alone let a union launder an export past
+// the gate. Every shape below must be refused, and the reads beneath them must
+// stay reads so the gate cannot be satisfied by refusing everything.
+func TestValidateQueryExportGate(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ statement string
+ wantValid bool
+ }{
+ {"a bare export", "SELECT a FROM t INTO OUTFILE '/tmp/x'", false},
+ {"an export on the second arm", "SELECT 1 UNION SELECT a FROM t INTO OUTFILE '/tmp/x'", false},
+ {"an export on the first arm", "SELECT a FROM t INTO OUTFILE '/tmp/x' UNION SELECT 1", false},
+ {"an export in a braced arm", "(SELECT a FROM t INTO OUTFILE '/tmp/x') UNION (SELECT 1)", false},
+ {"an export in a nested set operation", "SELECT 1 UNION (SELECT 2 UNION SELECT a FROM t INTO OUTFILE '/tmp/x')", false},
+ {"an export on the third arm", "SELECT 1 UNION SELECT 2 UNION SELECT a FROM t INTO OUTFILE '/tmp/x'", false},
+ {"an export reached through a CTE", "WITH c AS (SELECT 1) SELECT 1 UNION SELECT a FROM c INTO OUTFILE '/tmp/x'", false},
+ {"an export inside a subquery", "SELECT * FROM (SELECT a FROM t INTO OUTFILE '/tmp/x') y", false},
+ {"an export under a plain EXPLAIN", "EXPLAIN SELECT a FROM t INTO OUTFILE '/tmp/x'", false},
+ {"an export on an arm under a plain EXPLAIN", "EXPLAIN SELECT 1 UNION SELECT a FROM t INTO OUTFILE '/tmp/x'", false},
+ {"an export on an arm under EXPLAIN ANALYZE", "EXPLAIN ANALYZE SELECT 1 UNION SELECT a FROM t INTO OUTFILE '/tmp/x'", false},
+
+ {"a set operation that exports nothing", "SELECT 1 UNION SELECT 2", true},
+ {"a derived table", "SELECT * FROM (SELECT a FROM t) x", true},
+ {"a CTE", "WITH c AS (SELECT a FROM t) SELECT * FROM c", true},
+ {"EXPLAIN ANALYZE over a set operation", "EXPLAIN ANALYZE SELECT 1 UNION SELECT 2", true},
+ {"the clause named in a string literal", "SELECT 'INTO OUTFILE' FROM t", true},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ gotValid, _, err := validateQuery(tc.statement)
+ require.NoError(t, err)
+ require.Equal(t, tc.wantValid, gotValid, tc.statement)
+ })
+ }
+}