From a53624120ee5a8c5fae74f37b827c60b2d0c7343 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Taylor?= Date: Wed, 23 Oct 2024 17:17:43 +0200 Subject: [PATCH] bugfix: treat EXPLAIN like SELECT (#17054) Signed-off-by: Andres Taylor --- go/vt/vttablet/endtoend/queries_test.go | 14 +++++++++++ go/vt/vttablet/endtoend/transaction_test.go | 2 +- .../vttablet/tabletserver/planbuilder/plan.go | 4 +++- .../planbuilder/testdata/exec_cases.txt | 23 +++++++++++-------- 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/go/vt/vttablet/endtoend/queries_test.go b/go/vt/vttablet/endtoend/queries_test.go index 3dad415c1c1..b035013ecc3 100644 --- a/go/vt/vttablet/endtoend/queries_test.go +++ b/go/vt/vttablet/endtoend/queries_test.go @@ -121,6 +121,20 @@ var TestQueryCases = []framework.Testable{ }, RowsReturned: 1, }, + &framework.TestCase{ + Name: "explain with bindvars", + Query: "explain select :__vtudvp as `@p` from dual", + BindVars: map[string]*querypb.BindVariable{ + "__vtudvp": sqltypes.Int64BindVariable(1), + }, + Result: [][]string{ + {"1", "SIMPLE", "", "", "", "", "", "", "", "", "", "No tables used"}, + }, + Rewritten: []string{ + "explain select 1 as `@p` from dual", + }, + RowsReturned: 1, + }, &framework.TestCase{ Name: "limit", Query: "select /* limit */ eid, id from vitess_a limit :a", diff --git a/go/vt/vttablet/endtoend/transaction_test.go b/go/vt/vttablet/endtoend/transaction_test.go index 8f6546df5f1..8c86ecbebe1 100644 --- a/go/vt/vttablet/endtoend/transaction_test.go +++ b/go/vt/vttablet/endtoend/transaction_test.go @@ -256,7 +256,7 @@ func TestPrepareRollback(t *testing.T) { err = client.Prepare("aa") if err != nil { client.RollbackPrepared("aa", 0) - t.Fatalf(err.Error()) + t.Fatal(err.Error()) } err = client.RollbackPrepared("aa", 0) require.NoError(t, err) diff --git a/go/vt/vttablet/tabletserver/planbuilder/plan.go b/go/vt/vttablet/tabletserver/planbuilder/plan.go index 7b1e57c2f90..d37896a84f9 100644 --- a/go/vt/vttablet/tabletserver/planbuilder/plan.go +++ b/go/vt/vttablet/tabletserver/planbuilder/plan.go @@ -234,7 +234,9 @@ func Build(env *vtenv.Environment, statement sqlparser.Statement, tables map[str case *sqlparser.Show: plan, err = analyzeShow(stmt, dbName) case *sqlparser.Analyze, sqlparser.Explain: - plan, err = &Plan{PlanID: PlanOtherRead}, nil + // Analyze and Explain are treated as read-only queries. + // We send down a string, and get a table result back. + plan = &Plan{PlanID: PlanSelect, FullQuery: GenerateFullQuery(stmt)} case *sqlparser.OtherAdmin: plan, err = &Plan{PlanID: PlanOtherAdmin}, nil case *sqlparser.Savepoint: diff --git a/go/vt/vttablet/tabletserver/planbuilder/testdata/exec_cases.txt b/go/vt/vttablet/tabletserver/planbuilder/testdata/exec_cases.txt index 977b3822050..d921180f158 100644 --- a/go/vt/vttablet/tabletserver/planbuilder/testdata/exec_cases.txt +++ b/go/vt/vttablet/tabletserver/planbuilder/testdata/exec_cases.txt @@ -763,14 +763,15 @@ options:PassthroughDMLs # analyze "analyze table a" { - "PlanID": "OtherRead", + "PlanID": "Select", "TableName": "", "Permissions": [ - { - "TableName": "a", - "Role": 1 - } - ] + { + "TableName": "a", + "Role": 1 + } + ], + "FullQuery": "analyze table a" } # show @@ -783,15 +784,17 @@ options:PassthroughDMLs # describe "describe a" { - "PlanID": "OtherRead", - "TableName": "" + "PlanID": "Select", + "TableName": "", + "FullQuery": "explain a" } # explain "explain a" { - "PlanID": "OtherRead", - "TableName": "" + "PlanID": "Select", + "TableName": "", + "FullQuery": "explain a" } # repair