Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: treat EXPLAIN ANALYZE as the inner statement type (#14395)
rebelice7601861e0129
diff --git a/backend/plugin/parser/base/span.go b/backend/plugin/parser/base/span.goindex abf9012d2a..5e90756496 100644--- a/backend/plugin/parser/base/span.go+++ b/backend/plugin/parser/base/span.go@@ -28,9 +28,6 @@ const (DDL// The DML query that changes table data.DML- // The explain analyze query is different from explain and select query.- // Explain analyze queries exactly run but return nothing data.- ExplainAnalyze)var (diff --git a/backend/plugin/parser/mysql/query_span_extractor.go b/backend/plugin/parser/mysql/query_span_extractor.goindex fc46e93549..e4d403a27c 100644--- a/backend/plugin/parser/mysql/query_span_extractor.go+++ b/backend/plugin/parser/mysql/query_span_extractor.go@@ -91,6 +91,15 @@ func (q *querySpanExtractor) getQuerySpan(ctx context.Context, stmt string) (*ba}, nil}+ // For Explain Analyze SELECT, we determine the query type and return the access tables.+ if queryTypeListener.isExplainAnalyze {+ return &base.QuerySpan{+ Type: queryTypeListener.result,+ SourceColumns: accessTables,+ Results: []base.QuerySpanResult{},+ }, nil+ }+q.ctx = ctx// We assumes the caller had handled the statement type case,// so we only need to handle the determined statement type here.diff --git a/backend/plugin/parser/mysql/query_type.go b/backend/plugin/parser/mysql/query_type.goindex 7eae0c372e..2084a29a01 100644--- a/backend/plugin/parser/mysql/query_type.go+++ b/backend/plugin/parser/mysql/query_type.go@@ -11,6 +11,8 @@ type queryTypeListener struct {allSystems boolresult base.QueryType+ // This is for skipping the query span.+ isExplainAnalyze bool}func (l *queryTypeListener) EnterSimpleStatement(ctx *mysql.SimpleStatementContext) {@@ -51,7 +53,16 @@ func (l *queryTypeListener) EnterSimpleStatement(ctx *mysql.SimpleStatementConte}if ctx.UtilityStatement().ExplainStatement() != nil {if ctx.UtilityStatement().ExplainStatement().ANALYZE_SYMBOL() != nil {- l.result = base.ExplainAnalyze+ l.isExplainAnalyze = true+ explainableStatement := ctx.UtilityStatement().ExplainStatement().ExplainableStatement()+ switch {+ case explainableStatement.SelectStatement() != nil:+ l.result = base.Select+ case explainableStatement.DeleteStatement() != nil, explainableStatement.InsertStatement() != nil, explainableStatement.ReplaceStatement() != nil, explainableStatement.UpdateStatement() != nil:+ l.result = base.DML+ default:+ l.result = base.Explain+ }} else {l.result = base.Explain}diff --git a/backend/plugin/parser/mysql/test-data/query-span/query_type.yaml b/backend/plugin/parser/mysql/test-data/query-span/query_type.yamlindex dd11599d6e..cf5ef403bc 100644--- a/backend/plugin/parser/mysql/test-data/query-span/query_type.yaml+++ b/backend/plugin/parser/mysql/test-data/query-span/query_type.yaml@@ -1,3 +1,19 @@+- description: explain analyze query+ statement: explain analyze select * from t+ defaultDatabase: db+ metadata: |-+ {+ "name": "db"+ }+ querySpan:+ type: 1+ results: []+ sourcecolumns:+ - server: ""+ database: db+ schema: ""+ table: t+ column: ""- description: Treat set statement as selectstatement: SET CHARSET DEFAULT;defaultDatabase: db@@ -53,11 +69,11 @@type: 1results: []sourcecolumns:- - server: ""- database: db- schema: ""- table: t- column: ""+ - server: ""+ database: db+ schema: ""+ table: t+ column: ""- description: ddlstatement: create table t(a int);defaultDatabase: dbdiff --git a/backend/plugin/parser/pg/query_span_extractor.go b/backend/plugin/parser/pg/query_span_extractor.goindex d3d12cb105..d75f0e6d11 100644--- a/backend/plugin/parser/pg/query_span_extractor.go+++ b/backend/plugin/parser/pg/query_span_extractor.go@@ -128,7 +128,7 @@ func (q *querySpanExtractor) getQuerySpan(ctx context.Context, stmt string) (*ba}ast := res.Stmts[0]- queryType := getQueryType(ast.Stmt, allSystems)+ queryType, isExplainAnalyze := getQueryType(ast.Stmt, allSystems)if queryType != base.Select {return &base.QuerySpan{Type: queryType,@@ -137,6 +137,15 @@ func (q *querySpanExtractor) getQuerySpan(ctx context.Context, stmt string) (*ba}, nil}+ // For EXPLAIN ANALYZE SELECT, we determine the query type and access tables based on the inner query.+ if isExplainAnalyze {+ return &base.QuerySpan{+ Type: queryType,+ SourceColumns: accessTables,+ Results: []base.QuerySpanResult{},+ }, nil+ }+tableSource, err := q.extractTableSourceFromNode(ast.Stmt)if err != nil {var resourceNotFound *parsererror.ResourceNotFoundErrordiff --git a/backend/plugin/parser/pg/query_type.go b/backend/plugin/parser/pg/query_type.goindex 961a63a76d..a61dd872c4 100644--- a/backend/plugin/parser/pg/query_type.go+++ b/backend/plugin/parser/pg/query_type.go@@ -7,7 +7,7 @@ import ()// no session, so we don't need to check prepare and execute.-func getQueryType(node *pgquery.Node, allSystems bool) base.QueryType {+func getQueryType(node *pgquery.Node, allSystems bool) (base.QueryType, bool) {switch n := node.Node.(type) {case *pgquery.Node_InsertStmt,*pgquery.Node_UpdateStmt,@@ -15,21 +15,21 @@ func getQueryType(node *pgquery.Node, allSystems bool) base.QueryType {*pgquery.Node_CopyStmt,*pgquery.Node_RefreshMatViewStmt,*pgquery.Node_DeleteStmt:- return base.DML+ return base.DML, falsecase *pgquery.Node_SelectStmt:// SELECT INTO — define a new table from the results of a queryif n.SelectStmt.IntoClause != nil {- return base.DDL+ return base.DDL, false}if allSystems {- return base.SelectInfoSchema+ return base.SelectInfoSchema, false}- return base.Select+ return base.Select, falsecase *pgquery.Node_VariableSetStmt:// Treat SAFE SET as select statement.- return base.Select+ return base.Select, falsecase *pgquery.Node_AlterTableStmt,*pgquery.Node_AlterCollationStmt,*pgquery.Node_AlterDomainStmt,@@ -107,18 +107,19 @@ func getQueryType(node *pgquery.Node, allSystems bool) base.QueryType {*pgquery.Node_AlterSubscriptionStmt,*pgquery.Node_DropSubscriptionStmt,*pgquery.Node_CreateSchemaStmt:- return base.DDL+ return base.DDL, falsecase *pgquery.Node_VariableShowStmt:- return base.SelectInfoSchema+ return base.SelectInfoSchema, falsecase *pgquery.Node_ExplainStmt:for _, option := range n.ExplainStmt.Options {if defElem, ok := option.Node.(*pgquery.Node_DefElem); ok && defElem.DefElem.Defname == "analyze" {- return base.ExplainAnalyze+ t, _ := getQueryType(n.ExplainStmt.Query, allSystems)+ return t, true}}- return base.Explain+ return base.Explain, false}- return base.QueryTypeUnknown+ return base.QueryTypeUnknown, false}diff --git a/backend/plugin/parser/pg/test-data/query_type.yaml b/backend/plugin/parser/pg/test-data/query_type.yamlindex 0bad8ae995..b2df959bf3 100644--- a/backend/plugin/parser/pg/test-data/query_type.yaml+++ b/backend/plugin/parser/pg/test-data/query_type.yaml@@ -214,6 +214,11 @@]}querySpan:- type: 6+ type: 1results: []- sourcecolumns: []+ sourcecolumns:+ - server: ""+ database: db+ schema: public+ table: t1+ column: ""diff --git a/backend/plugin/parser/tidb/query_span_extractor.go b/backend/plugin/parser/tidb/query_span_extractor.goindex b046d28d34..f85872859b 100644--- a/backend/plugin/parser/tidb/query_span_extractor.go+++ b/backend/plugin/parser/tidb/query_span_extractor.go@@ -56,7 +56,7 @@ func (q *querySpanExtractor) getQuerySpan(ctx context.Context, statement string)return nil, base.MixUserSystemTablesError}- queryType := getQueryType(node, allSystems)+ queryType, isExplainAnalyze := getQueryType(node, allSystems)if skipQuerySpan(node, queryType) {return &base.QuerySpan{Type: queryType,@@ -65,14 +65,26 @@ func (q *querySpanExtractor) getQuerySpan(ctx context.Context, statement string)}, nil}- if explain, ok := node.(*tidbast.ExplainStmt); ok && queryType == base.Select && explain.Analyze {- node = explain.Stmt+ // For EXPLAIN ANALYZE SELECT, we determine the query type and access tables from the inner SELECT statement.+ if isExplainAnalyze {+ return &base.QuerySpan{+ Type: queryType,+ Results: []base.QuerySpanResult{},+ SourceColumns: accessTables,+ }, nil}tableSource, err := q.extractTableSourceFromNode(node)if err != nil {return nil, err}+ if tableSource == nil {+ return &base.QuerySpan{+ Type: queryType,+ Results: []base.QuerySpanResult{},+ SourceColumns: accessTables,+ }, nil+ }return &base.QuerySpan{Type: queryType,diff --git a/backend/plugin/parser/tidb/query_span_test.go b/backend/plugin/parser/tidb/query_span_test.goindex b25001f68a..292a4b7950 100644--- a/backend/plugin/parser/tidb/query_span_test.go+++ b/backend/plugin/parser/tidb/query_span_test.go@@ -32,7 +32,10 @@ func TestGetQuerySpan(t *testing.T) {)var (- testDataPaths = []string{"test-data/query_span.yaml"}+ testDataPaths = []string{+ "test-data/query_span.yaml",+ "test-data/query_type.yaml",+ })a := require.New(t)diff --git a/backend/plugin/parser/tidb/query_type.go b/backend/plugin/parser/tidb/query_type.goindex 7beea32e66..029245ca1a 100644--- a/backend/plugin/parser/tidb/query_type.go+++ b/backend/plugin/parser/tidb/query_type.go@@ -6,30 +6,31 @@ import ("github.com/bytebase/bytebase/backend/plugin/parser/base")-func getQueryType(node tidbast.Node, allSystem bool) base.QueryType {+func getQueryType(node tidbast.Node, allSystem bool) (base.QueryType, bool) {switch n := node.(type) {case *tidbast.SelectStmt, *tidbast.SetOprStmt:if allSystem {- return base.SelectInfoSchema+ return base.SelectInfoSchema, false}- return base.Select+ return base.Select, falsecase *tidbast.ExplainStmt:if n.Analyze {- return base.ExplainAnalyze+ t, _ := getQueryType(n.Stmt, allSystem)+ return t, true}- return base.Explain+ return base.Explain, falsecase *tidbast.ShowStmt:- return base.SelectInfoSchema+ return base.SelectInfoSchema, falsecase tidbast.DMLNode:// The order between DMLNode and SelectStmt/SetOprStmt is important.// Here means all DMLNodes except SelectStmt and SetOprStmt.- return base.DML+ return base.DML, falsecase tidbast.DDLNode:- return base.DDL+ return base.DDL, falsecase *tidbast.SetStmt, *tidbast.SetConfigStmt, *tidbast.SetResourceGroupStmt, *tidbast.SetRoleStmt, *tidbast.SetBindingStmt:// Treat SAFE SET as select statement.- return base.Select+ return base.Select, false}- return base.QueryTypeUnknown+ return base.QueryTypeUnknown, false}diff --git a/backend/plugin/parser/tidb/test-data/query_type.yaml b/backend/plugin/parser/tidb/test-data/query_type.yamlindex fceb6c7b53..d32243bdfd 100644--- a/backend/plugin/parser/tidb/test-data/query_type.yaml+++ b/backend/plugin/parser/tidb/test-data/query_type.yaml@@ -84,7 +84,7 @@]}querySpan:- type: 6+ type: 1results: []sourcecolumns: []- description: Test Explain