Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: implement query type for PG (#14338)
rebelicee8736250238e
diff --git a/backend/plugin/parser/pg/query_span_extractor.go b/backend/plugin/parser/pg/query_span_extractor.goindex facd080b96..80a95ce140 100644--- a/backend/plugin/parser/pg/query_span_extractor.go+++ b/backend/plugin/parser/pg/query_span_extractor.go@@ -118,12 +118,6 @@ func (q *querySpanExtractor) getQuerySpan(ctx context.Context, stmt string) (*baif mixed {return nil, base.MixUserSystemTablesError}- if allSystems {- return &base.QuerySpan{- SourceColumns: base.SourceColumnSet{},- Results: []base.QuerySpanResult{},- }, nil- }res, err := pgquery.Parse(stmt)if err != nil {@@ -134,22 +128,13 @@ func (q *querySpanExtractor) getQuerySpan(ctx context.Context, stmt string) (*ba}ast := res.Stmts[0]- switch ast.Stmt.Node.(type) {- case *pgquery.Node_SelectStmt:- case *pgquery.Node_ExplainStmt:- // Skip the EXPLAIN statement.+ queryType := getQueryType(ast.Stmt, allSystems)+ if queryType != base.Select {return &base.QuerySpan{+ Type: queryType,SourceColumns: base.SourceColumnSet{},Results: []base.QuerySpanResult{},}, nil- case *pgquery.Node_VariableSetStmt, *pgquery.Node_VariableShowStmt:- // Skip the SET statement and VARIABLE SHOW statement.- return &base.QuerySpan{- SourceColumns: base.SourceColumnSet{},- Results: []base.QuerySpanResult{},- }, nil- default:- return nil, errors.Errorf("expect a query statement but found %T", ast.Stmt.Node)}tableSource, err := q.extractTableSourceFromNode(ast.Stmt)@@ -163,6 +148,7 @@ func (q *querySpanExtractor) getQuerySpan(ctx context.Context, stmt string) (*ba}] = true}return &base.QuerySpan{+ Type: base.Select,SourceColumns: accessTables,Results: []base.QuerySpanResult{},NotFoundError: resourceNotFound,@@ -178,6 +164,7 @@ func (q *querySpanExtractor) getQuerySpan(ctx context.Context, stmt string) (*ba}return &base.QuerySpan{+ Type: base.Select,SourceColumns: accessTables,Results: tableSource.GetQuerySpanResult(),}, nil@@ -191,6 +178,8 @@ func (q *querySpanExtractor) extractTableSourceFromNode(node *pgquery.Node) (bas}switch node := node.Node.(type) {+ case *pgquery.Node_ExplainStmt:+ return q.extractTableSourceFromNode(node.ExplainStmt.Query)case *pgquery.Node_SelectStmt:return q.extractTableSourceFromSelect(node)case *pgquery.Node_RangeVar:diff --git a/backend/plugin/parser/pg/query_type.go b/backend/plugin/parser/pg/query_type.gonew file mode 100644index 0000000000..670d60d62c--- /dev/null+++ b/backend/plugin/parser/pg/query_type.go@@ -0,0 +1,121 @@+package pg++import (+ pgquery "github.com/pganalyze/pg_query_go/v5"++ "github.com/bytebase/bytebase/backend/plugin/parser/base"+)++// no session, so we don't need to check prepare and execute.+func getQueryType(node *pgquery.Node, allSystems bool) base.QueryType {+ switch n := node.Node.(type) {+ case *pgquery.Node_InsertStmt,+ *pgquery.Node_UpdateStmt,+ *pgquery.Node_MergeStmt,+ *pgquery.Node_CopyStmt,+ *pgquery.Node_RefreshMatViewStmt,+ *pgquery.Node_DeleteStmt:+ return base.DML+ case *pgquery.Node_SelectStmt:+ // SELECT INTO — define a new table from the results of a query+ if n.SelectStmt.IntoClause != nil {+ return base.DDL+ }++ if allSystems {+ return base.SelectInfoSchema+ }++ return base.Select+ case *pgquery.Node_AlterTableStmt,+ *pgquery.Node_AlterCollationStmt,+ *pgquery.Node_AlterDomainStmt,+ *pgquery.Node_CreateStmt,+ *pgquery.Node_CreateTableSpaceStmt,+ *pgquery.Node_AlterTableSpaceOptionsStmt,+ *pgquery.Node_AlterTableMoveAllStmt,+ *pgquery.Node_CreateExtensionStmt,+ *pgquery.Node_AlterExtensionStmt,+ *pgquery.Node_AlterExtensionContentsStmt,+ *pgquery.Node_CreateFdwStmt,+ *pgquery.Node_AlterFdwStmt,+ *pgquery.Node_CreateForeignServerStmt,+ *pgquery.Node_AlterForeignServerStmt,+ *pgquery.Node_CreateForeignTableStmt,+ *pgquery.Node_CreateUserMappingStmt,+ *pgquery.Node_AlterUserMappingStmt,+ *pgquery.Node_DropUserMappingStmt,+ *pgquery.Node_ImportForeignSchemaStmt,+ *pgquery.Node_CreatePolicyStmt,+ *pgquery.Node_AlterPolicyStmt,+ *pgquery.Node_CreateAmStmt,+ *pgquery.Node_CreateTrigStmt,+ *pgquery.Node_CreateEventTrigStmt,+ *pgquery.Node_AlterEventTrigStmt,+ *pgquery.Node_CreatePlangStmt,+ *pgquery.Node_CreateRoleStmt,+ *pgquery.Node_AlterRoleStmt,+ *pgquery.Node_AlterRoleSetStmt,+ *pgquery.Node_DropRoleStmt,+ *pgquery.Node_CreateSeqStmt,+ *pgquery.Node_AlterSeqStmt,+ *pgquery.Node_CreateDomainStmt,+ *pgquery.Node_CreateOpClassStmt,+ *pgquery.Node_CreateOpFamilyStmt,+ *pgquery.Node_AlterOpFamilyStmt,+ *pgquery.Node_DropStmt,+ *pgquery.Node_TruncateStmt,+ *pgquery.Node_IndexStmt,+ *pgquery.Node_CommentStmt,+ *pgquery.Node_CreateStatsStmt,+ *pgquery.Node_AlterStatsStmt,+ *pgquery.Node_CreateFunctionStmt,+ *pgquery.Node_AlterFunctionStmt,+ *pgquery.Node_RenameStmt,+ *pgquery.Node_AlterObjectDependsStmt,+ *pgquery.Node_AlterObjectSchemaStmt,+ *pgquery.Node_AlterOwnerStmt,+ *pgquery.Node_AlterOperatorStmt,+ *pgquery.Node_AlterTypeStmt,+ *pgquery.Node_RuleStmt,+ *pgquery.Node_CompositeTypeStmt,+ *pgquery.Node_CreateEnumStmt,+ *pgquery.Node_CreateRangeStmt,+ *pgquery.Node_AlterEnumStmt,+ *pgquery.Node_ViewStmt,+ *pgquery.Node_LoadStmt,+ *pgquery.Node_CreatedbStmt,+ *pgquery.Node_AlterDatabaseStmt,+ *pgquery.Node_AlterDatabaseRefreshCollStmt,+ *pgquery.Node_AlterDatabaseSetStmt,+ *pgquery.Node_DropdbStmt,+ *pgquery.Node_AlterSystemStmt,+ *pgquery.Node_ClusterStmt,+ *pgquery.Node_VacuumStmt,+ *pgquery.Node_CreateTableAsStmt,+ *pgquery.Node_CreateConversionStmt,+ *pgquery.Node_CreateCastStmt,+ *pgquery.Node_CreateTransformStmt,+ *pgquery.Node_DropOwnedStmt,+ *pgquery.Node_ReassignOwnedStmt,+ *pgquery.Node_CreatePublicationStmt,+ *pgquery.Node_AlterPublicationStmt,+ *pgquery.Node_CreateSubscriptionStmt,+ *pgquery.Node_AlterSubscriptionStmt,+ *pgquery.Node_DropSubscriptionStmt,+ *pgquery.Node_CreateSchemaStmt:+ return base.DDL+ case *pgquery.Node_VariableShowStmt:+ return base.SelectInfoSchema+ case *pgquery.Node_ExplainStmt:+ for _, option := range n.ExplainStmt.Options {+ if defElem, ok := option.Node.(*pgquery.Node_DefElem); ok && defElem.DefElem.Defname == "analyze" {+ return getQueryType(n.ExplainStmt.Query, allSystems)+ }+ }++ return base.Explain+ }++ return base.QueryTypeUnknown+}diff --git a/backend/plugin/parser/pg/test-data/query_span.yaml b/backend/plugin/parser/pg/test-data/query_span.yamlindex b49263406c..eec5c64f5d 100644--- a/backend/plugin/parser/pg/test-data/query_span.yaml+++ b/backend/plugin/parser/pg/test-data/query_span.yaml@@ -1,3 +1,237 @@+- description: DML+ statement: INSERT INTO t (a, b, c, d) VALUES (1, 2, 3, 4);+ defaultDatabase: db+ metadata: |-+ {+ "name": "db",+ "schemas": [+ {+ "name": "public",+ "tables": [+ {+ "name": "t1",+ "columns": [+ {+ "name": "a"+ },+ {+ "name": "b"+ },+ {+ "name": "c"+ }+ ]+ },+ {+ "name": "t2",+ "columns": [+ {+ "name": "c"+ },+ {+ "name": "d"+ },+ {+ "name": "e"+ }+ ]+ }+ ],+ "functions": [+ {+ "name": "if_query",+ "definition": "CREATE OR REPLACE FUNCTION public.IF_QUERY() RETURNS TABLE(x int, xx int, xxx int) LANGUAGE plpgsql AS $function$ declare data_count int; BEGIN data_count := (select count(*) from public.t where a = 1); if (count(data_count) > 0) then RETURN QUERY SELECT a, b, c FROM t1; else RETURN QUERY SELECT c, d, e FROM t2; end if; END; $function$;"+ }+ ]+ }+ ]+ }+ querySpan:+ type: 5+ results: []+ sourcecolumns: []+- description: DDL+ statement: create table t (a int, b int, c int, d int);+ defaultDatabase: db+ metadata: |-+ {+ "name": "db",+ "schemas": [+ {+ "name": "public",+ "tables": [+ {+ "name": "t1",+ "columns": [+ {+ "name": "a"+ },+ {+ "name": "b"+ },+ {+ "name": "c"+ }+ ]+ },+ {+ "name": "t2",+ "columns": [+ {+ "name": "c"+ },+ {+ "name": "d"+ },+ {+ "name": "e"+ }+ ]+ }+ ],+ "functions": [+ {+ "name": "if_query",+ "definition": "CREATE OR REPLACE FUNCTION public.IF_QUERY() RETURNS TABLE(x int, xx int, xxx int) LANGUAGE plpgsql AS $function$ declare data_count int; BEGIN data_count := (select count(*) from public.t where a = 1); if (count(data_count) > 0) then RETURN QUERY SELECT a, b, c FROM t1; else RETURN QUERY SELECT c, d, e FROM t2; end if; END; $function$;"+ }+ ]+ }+ ]+ }+ querySpan:+ type: 4+ results: []+ sourcecolumns: []+- description: Explain+ statement: EXPLAIN SELECT * FROM t;+ defaultDatabase: db+ metadata: |-+ {+ "name": "db",+ "schemas": [+ {+ "name": "public",+ "tables": [+ {+ "name": "t1",+ "columns": [+ {+ "name": "a"+ },+ {+ "name": "b"+ },+ {+ "name": "c"+ }+ ]+ },+ {+ "name": "t2",+ "columns": [+ {+ "name": "c"+ },+ {+ "name": "d"+ },+ {+ "name": "e"+ }+ ]+ }+ ],+ "functions": [+ {+ "name": "if_query",+ "definition": "CREATE OR REPLACE FUNCTION public.IF_QUERY() RETURNS TABLE(x int, xx int, xxx int) LANGUAGE plpgsql AS $function$ declare data_count int; BEGIN data_count := (select count(*) from public.t where a = 1); if (count(data_count) > 0) then RETURN QUERY SELECT a, b, c FROM t1; else RETURN QUERY SELECT c, d, e FROM t2; end if; END; $function$;"+ }+ ]+ }+ ]+ }+ querySpan:+ type: 2+ results: []+ sourcecolumns: []+- description: Explain Analyze+ statement: EXPLAIN ANALYZE SELECT * FROM t1;+ defaultDatabase: db+ metadata: |-+ {+ "name": "db",+ "schemas": [+ {+ "name": "public",+ "tables": [+ {+ "name": "t1",+ "columns": [+ {+ "name": "a"+ },+ {+ "name": "b"+ },+ {+ "name": "c"+ }+ ]+ },+ {+ "name": "t2",+ "columns": [+ {+ "name": "c"+ },+ {+ "name": "d"+ },+ {+ "name": "e"+ }+ ]+ }+ ],+ "functions": [+ {+ "name": "if_query",+ "definition": "CREATE OR REPLACE FUNCTION public.IF_QUERY() RETURNS TABLE(x int, xx int, xxx int) LANGUAGE plpgsql AS $function$ declare data_count int; BEGIN data_count := (select count(*) from public.t where a = 1); if (count(data_count) > 0) then RETURN QUERY SELECT a, b, c FROM t1; else RETURN QUERY SELECT c, d, e FROM t2; end if; END; $function$;"+ }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: a+ sourcecolumns:+ - server: ""+ database: db+ schema: public+ table: t1+ column: a+ - name: b+ sourcecolumns:+ - server: ""+ database: db+ schema: public+ table: t1+ column: b+ - name: c+ sourcecolumns:+ - server: ""+ database: db+ schema: public+ table: t1+ column: c+ sourcecolumns:+ - server: ""+ database: db+ schema: public+ table: t1+ column: ""- description: Function with if-elsestatement: SELECT * from IF_QUERY();defaultDatabase: db@@ -47,7 +281,7 @@]}querySpan:- type: 0+ type: 1results:- name: xsourcecolumns:@@ -139,7 +373,7 @@]}querySpan:- type: 0+ type: 1results:- name: plpgsql_tsourcecolumns:@@ -211,7 +445,7 @@]}querySpan:- type: 0+ type: 1results:- name: plpgsql_tsourcecolumns:@@ -642,7 +876,7 @@]}querySpan:- type: 0+ type: 1results:- name: monthsourcecolumns:@@ -735,7 +969,7 @@]}querySpan:- type: 0+ type: 1results:- name: plpgsql_tsourcecolumns:@@ -778,7 +1012,7 @@]}querySpan:- type: 0+ type: 1results:- name: ?column?sourcecolumns:@@ -816,7 +1050,7 @@]}querySpan:- type: 0+ type: 1results:- name: ?column?sourcecolumns:@@ -854,7 +1088,7 @@]}querySpan:- type: 0+ type: 1results:- name: jsonb_eachsourcecolumns:@@ -892,7 +1126,7 @@]}querySpan:- type: 0+ type: 1results:- name: asourcecolumns:@@ -944,7 +1178,7 @@]}querySpan:- type: 0+ type: 1results:- name: to_jsonbsourcecolumns:@@ -982,7 +1216,7 @@]}querySpan:- type: 0+ type: 1results:- name: row_to_jsonsourcecolumns:@@ -1020,7 +1254,7 @@]}querySpan:- type: 0+ type: 1results:- name: unnestsourcecolumns: []@@ -1050,7 +1284,7 @@]}querySpan:- type: 0+ type: 1results:- name: unnestsourcecolumns: []@@ -1078,7 +1312,7 @@]}querySpan:- type: 0+ type: 1results:- name: generate_seriessourcecolumns: []@@ -1106,7 +1340,7 @@]}querySpan:- type: 0+ type: 1results:- name: generate_subscriptssourcecolumns: []@@ -1134,7 +1368,7 @@]}querySpan:- type: 0+ type: 1results:- name: asourcecolumns:@@ -1193,7 +1427,7 @@]}querySpan:- type: 0+ type: 1results:- name: asourcecolumns:@@ -1247,7 +1481,7 @@]}querySpan:- type: 0+ type: 1results:- name: asourcecolumns:@@ -1295,7 +1529,7 @@]}querySpan:- type: 0+ type: 1results:- name: asourcecolumns:@@ -1339,7 +1573,7 @@]}querySpan:- type: 0+ type: 1results:- name: asourcecolumns:@@ -1377,7 +1611,7 @@]}querySpan:- type: 0+ type: 2results: []sourcecolumns: []- description: Test for simple select const@@ -1403,7 +1637,7 @@]}querySpan:- type: 0+ type: 1results:- name: ?column?sourcecolumns: []@@ -1440,7 +1674,7 @@]}querySpan:- type: 0+ type: 1results:- name: asourcecolumns:@@ -1515,7 +1749,7 @@]}querySpan:- type: 0+ type: 1results:- name: asourcecolumns:@@ -1583,7 +1817,7 @@]}querySpan:- type: 0+ type: 1results:- name: asourcecolumns:@@ -1651,7 +1885,7 @@]}querySpan:- type: 0+ type: 1results:- name: col_1sourcecolumns: []@@ -1712,7 +1946,7 @@]}querySpan:- type: 0+ type: 1results:- name: ?column?sourcecolumns: []@@ -1765,7 +1999,7 @@]}querySpan:- type: 0+ type: 1results:- name: citysourcecolumns:@@ -1819,7 +2053,7 @@]}querySpan:- type: 0+ type: 1results:- name: maxsourcecolumns:@@ -1929,7 +2163,7 @@]}querySpan:- type: 0+ type: 1results:- name: asourcecolumns:@@ -1997,7 +2231,7 @@]}querySpan:- type: 0+ type: 1results:- name: asourcecolumns:@@ -2086,7 +2320,7 @@]}querySpan:- type: 0+ type: 1results:- name: asourcecolumns:@@ -2154,7 +2388,7 @@]}querySpan:- type: 0+ type: 1results:- name: maxsourcecolumns:@@ -2201,7 +2435,7 @@]}querySpan:- type: 0+ type: 1results:- name: dsourcecolumns:@@ -2269,7 +2503,7 @@]}querySpan:- type: 0+ type: 1results:- name: cc1sourcecolumns:@@ -2352,7 +2586,7 @@]}querySpan:- type: 0+ type: 1results:- name: c1sourcecolumns:@@ -2411,6 +2645,6 @@]}querySpan:- type: 0+ type: 3results: []sourcecolumns: []