Skip to content

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

feat(pg): migrate query type classification from ANTLR to omni (#19655)

rebelicec8c3698d175f
diff --git a/backend/plugin/parser/pg/query_span_extractor.go b/backend/plugin/parser/pg/query_span_extractor.go
index b97d60c6d3..248bafcd80 100644
--- a/backend/plugin/parser/pg/query_span_extractor.go
+++ b/backend/plugin/parser/pg/query_span_extractor.go
@@ -147,17 +147,13 @@ func (q *querySpanExtractor) getQuerySpan(ctx context.Context, stmt string) (*ba
return nil, base.MixUserSystemTablesError
}
- // Get query type using ANTLR-based detection
- queryTypeListener := &queryTypeListener{
- result: base.QueryTypeUnknown,
- allSystems: allSystems,
+ // Get query type using omni AST.
+ // Fall back to Select if omni cannot parse the statement (e.g. non-standard
+ // syntax like 4-part column references that ANTLR accepts but libpg_query rejects).
+ queryType, isExplainAnalyze := base.Select, false
+ if omniStmts, parseErr := ParsePg(stmt); parseErr == nil && len(omniStmts) == 1 {
+ queryType, isExplainAnalyze = classifyQueryType(omniStmts[0].AST, allSystems)
}
- antlr.ParseTreeWalkerDefault.Walk(queryTypeListener, parseResult.Tree)
- if queryTypeListener.err != nil {
- return nil, errors.Wrapf(queryTypeListener.err, "failed to get query type from statement: %s", stmt)
- }
-
- queryType, isExplainAnalyze := queryTypeListener.result, queryTypeListener.isExplainAnalyze
// For non-SELECT queries, return early
if queryType != base.Select {
diff --git a/backend/plugin/parser/pg/query_type.go b/backend/plugin/parser/pg/query_type.go
index b5b5897c89..83e70a963d 100644
--- a/backend/plugin/parser/pg/query_type.go
+++ b/backend/plugin/parser/pg/query_type.go
@@ -3,313 +3,158 @@ package pg
import (
"strings"
- "github.com/pkg/errors"
-
- pgparser "github.com/bytebase/parser/postgresql"
+ "github.com/bytebase/omni/pg/ast"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-type queryTypeListener struct {
- *pgparser.BasePostgreSQLParserListener
- result base.QueryType
- allSystems bool
- isExplainAnalyze bool
- err error
-}
-
-func (q *queryTypeListener) EnterStmtblock(ctx *pgparser.StmtblockContext) {
- multi := ctx.Stmtmulti()
- if multi == nil || len(multi.AllStmt()) == 0 {
- q.result = base.QueryTypeUnknown
- return
- }
- if len(multi.AllStmt()) > 1 {
- q.err = errors.Errorf("only single statement is allowed, but got %d", len(multi.AllStmt()))
- return
- }
-
- stmt := multi.AllStmt()[0]
- q.result = q.getStatementType(stmt)
-}
-
-func (q *queryTypeListener) getStatementType(stmt pgparser.IStmtContext) base.QueryType {
- if stmt == nil {
- return base.QueryTypeUnknown
+// classifyQueryType classifies an omni AST node into a QueryType.
+// allSystems indicates whether all referenced tables are system/info_schema tables.
+func classifyQueryType(node ast.Node, allSystems bool) (queryType base.QueryType, isExplainAnalyze bool) {
+ if node == nil {
+ return base.QueryTypeUnknown, false
}
+ switch n := node.(type) {
// DML statements
- if stmt.Insertstmt() != nil {
- return base.DML
- }
- if stmt.Updatestmt() != nil {
- return base.DML
- }
- if stmt.Deletestmt() != nil {
- return base.DML
- }
- if stmt.Mergestmt() != nil {
- return base.DML
- }
- if stmt.Copystmt() != nil {
- return base.DML
- }
+ case *ast.InsertStmt, *ast.UpdateStmt, *ast.DeleteStmt, *ast.MergeStmt, *ast.CopyStmt:
+ return base.DML, false
- // Special case for SELECT with INTO clause (becomes DDL)
- if selectStmt := stmt.Selectstmt(); selectStmt != nil {
- // Check if it's a SELECT INTO statement
- if q.hasIntoClause(selectStmt) {
- return base.DDL
- }
- if q.allSystems {
- return base.SelectInfoSchema
+ // SELECT: check for INTO clause (becomes DDL)
+ case *ast.SelectStmt:
+ if hasOmniIntoClause(n) {
+ return base.DDL, false
}
- return base.Select
- }
-
- // Variable SET/SHOW statements
- if stmt.Variablesetstmt() != nil {
- // Treat SET as select (safe operation)
- return base.Select
- }
- if stmt.Variableshowstmt() != nil {
- return base.SelectInfoSchema
- }
- // Note: SHOW is handled via Variableshowstmt
-
- // EXPLAIN statement
- if explainStmt := stmt.Explainstmt(); explainStmt != nil {
- // Check if it has ANALYZE option
- if q.hasAnalyzeOption(explainStmt) {
- q.isExplainAnalyze = true
- // For EXPLAIN ANALYZE, return the type of the explained statement
- if explainedStmt := explainStmt.Explainablestmt(); explainedStmt != nil {
- // Check what kind of statement is being explained
- if explainedStmt.Selectstmt() != nil {
- // Check for SELECT INTO which is DDL
- if q.hasIntoClause(explainedStmt.Selectstmt()) {
- return base.DDL
- }
- return base.Select
- }
- if explainedStmt.Insertstmt() != nil {
- return base.DML
- }
- if explainedStmt.Updatestmt() != nil {
- return base.DML
- }
- if explainedStmt.Deletestmt() != nil {
- return base.DML
- }
- if explainedStmt.Declarecursorstmt() != nil {
- return base.Select
- }
- if explainedStmt.Createasstmt() != nil {
- return base.DDL
- }
- if explainedStmt.Refreshmatviewstmt() != nil {
- return base.DML
- }
- if explainedStmt.Executestmt() != nil {
- // EXECUTE statements are harder to classify without context
- return base.Select
- }
- }
- // Default to Select if we can't determine the explained statement type
- return base.Select
+ if allSystems {
+ return base.SelectInfoSchema, false
}
- return base.Explain
- }
-
- // DDL statements - Direct CREATE/DROP statements
- if stmt.Createstmt() != nil || stmt.Dropstmt() != nil {
- return base.DDL
- }
-
- // CREATE statements
- if stmt.Createasstmt() != nil || stmt.Createseqstmt() != nil ||
- stmt.Createschemastmt() != nil || stmt.Createdbstmt() != nil ||
- stmt.Createfunctionstmt() != nil || stmt.Createrolestmt() != nil ||
- stmt.Indexstmt() != nil || stmt.Createextensionstmt() != nil ||
- stmt.Createtrigstmt() != nil || stmt.Createeventtrigstmt() != nil ||
- stmt.Createdomainstmt() != nil || stmt.Createconversionstmt() != nil ||
- stmt.Createcaststmt() != nil || stmt.Createopclassstmt() != nil ||
- stmt.Createopfamilystmt() != nil || stmt.Createpolicystmt() != nil ||
- stmt.Createamstmt() != nil || stmt.Createtransformstmt() != nil ||
- stmt.Createstatsstmt() != nil || stmt.Createtablespacestmt() != nil ||
- stmt.Createfdwstmt() != nil || stmt.Createforeignserverstmt() != nil ||
- stmt.Createforeigntablestmt() != nil || stmt.Createplangstmt() != nil ||
- stmt.Createpublicationstmt() != nil || stmt.Createsubscriptionstmt() != nil ||
- stmt.Createusermappingstmt() != nil {
- return base.DDL
- }
+ return base.Select, false
- // VIEW statements (DDL)
- if stmt.Viewstmt() != nil {
- return base.DDL
- }
+ // SET is treated as safe (Select)
+ case *ast.VariableSetStmt:
+ return base.Select, false
- // ALTER statements
- if stmt.Altertablestmt() != nil || stmt.Alterseqstmt() != nil ||
- stmt.Alterdatabasestmt() != nil ||
- stmt.Alterdatabasesetstmt() != nil ||
- stmt.Alterfunctionstmt() != nil || stmt.Alterrolestmt() != nil ||
- stmt.Alterrolesetstmt() != nil || stmt.Altercollationstmt() != nil ||
- stmt.Alterdomainstmt() != nil || stmt.Alterextensionstmt() != nil ||
- stmt.Alterextensioncontentsstmt() != nil || stmt.Alterfdwstmt() != nil ||
- stmt.Alterforeignserverstmt() != nil || stmt.Alteropfamilystmt() != nil ||
- stmt.Alterpolicystmt() != nil || stmt.Altereventtrigstmt() != nil ||
- stmt.Alterobjectdependsstmt() != nil || stmt.Alterobjectschemastmt() != nil ||
- stmt.Alterownerstmt() != nil || stmt.Alteroperatorstmt() != nil ||
- stmt.Altertypestmt() != nil || stmt.Alterenumstmt() != nil ||
- stmt.Alterstatsstmt() != nil || stmt.Altertblspcstmt() != nil ||
- stmt.Altersystemstmt() != nil ||
- stmt.Alterpublicationstmt() != nil || stmt.Altersubscriptionstmt() != nil ||
- stmt.Alterusermappingstmt() != nil || stmt.Altercompositetypestmt() != nil ||
- stmt.Alterdefaultprivilegesstmt() != nil || stmt.Altergroupstmt() != nil ||
- stmt.Altertsconfigurationstmt() != nil || stmt.Altertsdictionarystmt() != nil {
- return base.DDL
- }
+ // SHOW → info schema
+ case *ast.VariableShowStmt:
+ return base.SelectInfoSchema, false
- // DROP statements (in addition to generic Dropstmt)
- if stmt.Dropdbstmt() != nil || stmt.Droprolestmt() != nil ||
- stmt.Dropownedstmt() != nil || stmt.Dropsubscriptionstmt() != nil ||
- stmt.Dropusermappingstmt() != nil {
- return base.DDL
- }
-
- // Other DDL statements
- if stmt.Truncatestmt() != nil || stmt.Commentstmt() != nil ||
- stmt.Grantstmt() != nil || stmt.Grantrolestmt() != nil ||
- stmt.Revokestmt() != nil || stmt.Revokerolestmt() != nil ||
- stmt.Clusterstmt() != nil || stmt.Vacuumstmt() != nil ||
- stmt.Analyzestmt() != nil || stmt.Lockstmt() != nil ||
- stmt.Reindexstmt() != nil || stmt.Rulestmt() != nil ||
- stmt.Renamestmt() != nil || stmt.Reassignownedstmt() != nil ||
- stmt.Loadstmt() != nil ||
- stmt.Importforeignschemastmt() != nil || stmt.Seclabelstmt() != nil ||
- stmt.Dostmt() != nil ||
- stmt.Creatematviewstmt() != nil || stmt.Discardstmt() != nil ||
- stmt.Fetchstmt() != nil || stmt.Constraintssetstmt() != nil ||
- stmt.Checkpointstmt() != nil || stmt.Createtablespacestmt() != nil {
- return base.DDL
- }
+ // EXPLAIN: check for ANALYZE option
+ case *ast.ExplainStmt:
+ if isExplainAnalyzeOmni(n) {
+ qt := classifyExplainedQuery(n.Query)
+ return qt, true
+ }
+ return base.Explain, false
// REFRESH MATERIALIZED VIEW is DML
- if stmt.Refreshmatviewstmt() != nil {
- return base.DML
- }
+ case *ast.RefreshMatViewStmt:
+ return base.DML, false
// CALL stored procedure is DML
- if stmt.Callstmt() != nil {
- return base.DML
+ case *ast.CallStmt:
+ return base.DML, false
+
+ // All DDL statements
+ case *ast.CreateStmt, *ast.DropStmt, *ast.AlterTableStmt,
+ *ast.CreateTableAsStmt, *ast.CreateSeqStmt, *ast.CreateSchemaStmt,
+ *ast.CreatedbStmt, *ast.CreateFunctionStmt, *ast.CreateRoleStmt,
+ *ast.IndexStmt, *ast.CreateExtensionStmt, *ast.CreateTrigStmt,
+ *ast.CreateEventTrigStmt, *ast.CreateDomainStmt, *ast.CreateConversionStmt,
+ *ast.CreateCastStmt, *ast.CreateOpClassStmt, *ast.CreateOpFamilyStmt,
+ *ast.CreatePolicyStmt, *ast.CreateAmStmt, *ast.CreateTransformStmt,
+ *ast.CreateStatsStmt, *ast.CreateTableSpaceStmt,
+ *ast.CreateFdwStmt, *ast.CreateForeignServerStmt,
+ *ast.CreateForeignTableStmt, *ast.CreatePLangStmt,
+ *ast.CreatePublicationStmt, *ast.CreateSubscriptionStmt,
+ *ast.CreateUserMappingStmt,
+ *ast.ViewStmt,
+ *ast.AlterSeqStmt, *ast.AlterDatabaseStmt, *ast.AlterDatabaseSetStmt,
+ *ast.AlterFunctionStmt, *ast.AlterRoleStmt, *ast.AlterRoleSetStmt,
+ *ast.AlterCollationStmt, *ast.AlterDomainStmt,
+ *ast.AlterExtensionStmt, *ast.AlterExtensionContentsStmt,
+ *ast.AlterFdwStmt, *ast.AlterForeignServerStmt,
+ *ast.AlterOpFamilyStmt, *ast.AlterPolicyStmt,
+ *ast.AlterEventTrigStmt, *ast.AlterObjectDependsStmt,
+ *ast.AlterObjectSchemaStmt, *ast.AlterOwnerStmt,
+ *ast.AlterOperatorStmt, *ast.AlterTypeStmt, *ast.AlterEnumStmt,
+ *ast.AlterStatsStmt, *ast.AlterTableSpaceOptionsStmt,
+ *ast.AlterSystemStmt, *ast.AlterPublicationStmt,
+ *ast.AlterSubscriptionStmt, *ast.AlterUserMappingStmt,
+ *ast.CompositeTypeStmt, *ast.AlterDefaultPrivilegesStmt,
+ *ast.AlterTSConfigurationStmt, *ast.AlterTSDictionaryStmt,
+ *ast.DropdbStmt, *ast.DropRoleStmt, *ast.DropOwnedStmt,
+ *ast.DropSubscriptionStmt, *ast.DropUserMappingStmt,
+ *ast.TruncateStmt, *ast.CommentStmt,
+ *ast.GrantStmt, *ast.GrantRoleStmt,
+ *ast.ClusterStmt, *ast.VacuumStmt, *ast.LockStmt,
+ *ast.ReindexStmt, *ast.RuleStmt,
+ *ast.RenameStmt, *ast.ReassignOwnedStmt,
+ *ast.SecLabelStmt,
+ *ast.DoStmt, *ast.DiscardStmt,
+ *ast.FetchStmt, *ast.ConstraintsSetStmt, *ast.CheckPointStmt,
+ *ast.CreateEnumStmt:
+ return base.DDL, false
+
+ default:
+ return base.QueryTypeUnknown, false
}
-
- // Default to unknown for any unhandled statement types
- return base.QueryTypeUnknown
}
-func (q *queryTypeListener) hasIntoClause(selectStmt pgparser.ISelectstmtContext) bool {
- // Check if SELECT has INTO clause by traversing the AST
- if selectStmt == nil {
+// hasOmniIntoClause checks if a SelectStmt has an INTO clause (SELECT INTO).
+func hasOmniIntoClause(n *ast.SelectStmt) bool {
+ if n == nil {
return false
}
-
- // Check select_no_parens path
- if selectNoParens := selectStmt.Select_no_parens(); selectNoParens != nil {
- if selectClause := selectNoParens.Select_clause(); selectClause != nil {
- // Traverse through select_clause -> simple_select_intersect -> simple_select_pramary
- for _, intersect := range selectClause.AllSimple_select_intersect() {
- if intersect != nil {
- for _, primary := range intersect.AllSimple_select_pramary() {
- if primary != nil {
- // Check if this simple_select_pramary has INTO clause
- if len(primary.AllInto_clause()) > 0 {
- return true
- }
- }
- }
- }
- }
- }
+ if n.IntoClause != nil {
+ return true
}
-
- // Check select_with_parens path
- if selectWithParens := selectStmt.Select_with_parens(); selectWithParens != nil {
- // Recursively check nested select statements
- if selectWithParens.Select_with_parens() != nil {
- // For nested parentheses, we need to check recursively
- // This is a simplified approach - a full implementation would need
- // to handle all nesting cases
- if selectWithParens.Select_no_parens() != nil {
- return q.hasIntoClauseInSelectNoParens(selectWithParens.Select_no_parens())
- }
- }
+ // Check set operations (UNION/INTERSECT/EXCEPT)
+ if n.Larg != nil && hasOmniIntoClause(n.Larg) {
+ return true
+ }
+ if n.Rarg != nil && hasOmniIntoClause(n.Rarg) {
+ return true
}
-
return false
}
-func (*queryTypeListener) hasIntoClauseInSelectNoParens(selectNoParens pgparser.ISelect_no_parensContext) bool {
- if selectNoParens == nil {
+// isExplainAnalyzeOmni checks if an ExplainStmt has the ANALYZE option.
+func isExplainAnalyzeOmni(n *ast.ExplainStmt) bool {
+ if n.Options == nil {
return false
}
-
- if selectClause := selectNoParens.Select_clause(); selectClause != nil {
- // Traverse through select_clause -> simple_select_intersect -> simple_select_pramary
- for _, intersect := range selectClause.AllSimple_select_intersect() {
- if intersect != nil {
- for _, primary := range intersect.AllSimple_select_pramary() {
- if primary != nil {
- // Check if this simple_select_pramary has INTO clause
- if len(primary.AllInto_clause()) > 0 {
- return true
- }
- }
- }
+ for _, item := range n.Options.Items {
+ if de, ok := item.(*ast.DefElem); ok {
+ if strings.EqualFold(de.Defname, "analyze") {
+ return true
}
}
}
-
return false
}
-func (*queryTypeListener) hasAnalyzeOption(explainStmt pgparser.IExplainstmtContext) bool {
- if explainStmt == nil {
- return false
- }
-
- // Check if EXPLAIN has ANALYZE keyword via AST
- // Method 1: Check if Analyze_keyword() exists
- if explainStmt.Analyze_keyword() != nil {
- return true
+// classifyExplainedQuery returns the QueryType for the query inside EXPLAIN ANALYZE.
+func classifyExplainedQuery(query ast.Node) base.QueryType {
+ if query == nil {
+ return base.Select
}
-
- // Method 2: Check in the explain option list
- if optionList := explainStmt.Explain_option_list(); optionList != nil {
- // Check each option element
- for _, optionElem := range optionList.AllExplain_option_elem() {
- if optionElem != nil {
- // Check if the option name is "analyze"
- if optionName := optionElem.Explain_option_name(); optionName != nil {
- // Check if it's the ANALYZE keyword
- if optionName.Analyze_keyword() != nil {
- return true
- }
- // Also check if it's specified as a nonreserved word
- // Note: We need to check the text here because ANALYZE can be specified
- // as a non-reserved word in PostgreSQL, and we need to verify it's actually
- // the ANALYZE option and not some other non-reserved word
- if optionName.Nonreservedword() != nil {
- optionText := optionName.Nonreservedword().GetText()
- if strings.EqualFold(optionText, "analyze") {
- return true
- }
- }
- }
- }
+ switch n := query.(type) {
+ case *ast.SelectStmt:
+ if hasOmniIntoClause(n) {
+ return base.DDL
}
+ return base.Select
+ case *ast.InsertStmt, *ast.UpdateStmt, *ast.DeleteStmt:
+ return base.DML
+ case *ast.DeclareCursorStmt:
+ return base.Select
+ case *ast.CreateTableAsStmt:
+ return base.DDL
+ case *ast.RefreshMatViewStmt:
+ return base.DML
+ case *ast.ExecuteStmt:
+ return base.Select
+ default:
+ return base.Select
}
-
- return false
}