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(parser/doris): switch to omni hand-written parser (#20395)
h3n4l91cb65600e8a
diff --git a/backend/plugin/parser/doris/diagnose.go b/backend/plugin/parser/doris/diagnose.goindex 0eba756c75..32b8179677 100644--- a/backend/plugin/parser/doris/diagnose.go+++ b/backend/plugin/parser/doris/diagnose.go@@ -2,55 +2,65 @@ package dorisimport ("context"+ "strings"+ "unicode/utf8"- "github.com/antlr4-go/antlr/v4"- parser "github.com/bytebase/parser/doris"+ "github.com/bytebase/omni/doris/parser"- "github.com/bytebase/bytebase/backend/generated-go/store"+ storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/parser/base")func init() {- base.RegisterDiagnoseFunc(store.Engine_DORIS, Diagnose)- base.RegisterDiagnoseFunc(store.Engine_STARROCKS, Diagnose)+ base.RegisterDiagnoseFunc(storepb.Engine_DORIS, Diagnose)+ base.RegisterDiagnoseFunc(storepb.Engine_STARROCKS, Diagnose)}+// Diagnose returns syntax diagnostics for the given Doris statement.+// Surfaces genuine lex/parse errors from the omni parser, filtering out+// the "not yet supported" stub messages from statement-dispatch fallthroughs.func Diagnose(_ context.Context, _ base.DiagnoseContext, statement string) ([]base.Diagnostic, error) {- diagnostics := make([]base.Diagnostic, 0)- syntaxError := parseDorisStatement(statement)- if syntaxError != nil {- diagnostics = append(diagnostics, base.ConvertSyntaxErrorToDiagnostic(syntaxError, statement))+ diags := parser.Diagnose(statement)+ out := make([]base.Diagnostic, 0, len(diags))+ for _, d := range diags {+ if strings.HasSuffix(d.Msg, "statement parsing is not yet supported") {+ continue+ }+ // Convert byte offset to line/col position.+ line, col := byteOffsetToLineCol(statement, d.Loc.Start)+ syntaxErr := &base.SyntaxError{+ Position: &storepb.Position{+ Line: int32(line),+ Column: int32(col),+ },+ Message: d.Msg,+ }+ out = append(out, base.ConvertSyntaxErrorToDiagnostic(syntaxErr, statement))}-- return diagnostics, nil+ return out, nil}-func parseDorisStatement(statement string) *base.SyntaxError {- lexer := parser.NewDorisLexer(antlr.NewInputStream(statement))- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)- p := parser.NewDorisParser(stream)- lexerErrorListener := &base.ParseErrorListener{- Statement: statement,+// byteOffsetToLineCol returns the 1-based line and 1-based column for a byte+// offset within the statement. Counts \n as line breaks. The 1-based column+// matches the storepb.Position convention that ConvertSyntaxErrorToDiagnostic+// expects (it subtracts 1 internally to land on the 0-based LSP offset).+func byteOffsetToLineCol(s string, offset int) (int, int) {+ if offset < 0 {+ return 1, 1}- lexer.RemoveErrorListeners()- lexer.AddErrorListener(lexerErrorListener)-- parserErrorListener := &base.ParseErrorListener{- Statement: statement,+ if offset > len(s) {+ offset = len(s)}- p.RemoveErrorListeners()- p.AddErrorListener(parserErrorListener)-- p.BuildParseTrees = false-- _ = p.MultiStatements()- if lexerErrorListener.Err != nil {- return lexerErrorListener.Err+ line, col := 1, 1+ for i := 0; i < offset; {+ r, size := utf8.DecodeRuneInString(s[i:])+ if r == '\n' {+ line+++ col = 1+ } else {+ col+++ }+ i += size}-- if parserErrorListener.Err != nil {- return parserErrorListener.Err- }-- return nil+ return line, col}diff --git a/backend/plugin/parser/doris/doris.go b/backend/plugin/parser/doris/doris.goindex a199cab823..69de1b51d8 100644--- a/backend/plugin/parser/doris/doris.go+++ b/backend/plugin/parser/doris/doris.go@@ -1,8 +1,11 @@package dorisimport (- "github.com/antlr4-go/antlr/v4"- parser "github.com/bytebase/parser/doris"+ "strings"+ "unicode/utf8"++ "github.com/bytebase/omni/doris/ast"+ "github.com/bytebase/omni/doris/parser"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/parser/base"@@ -10,33 +13,62 @@ import (func init() {base.RegisterParseStatementsFunc(storepb.Engine_DORIS, parseDorisStatements)+ base.RegisterParseStatementsFunc(storepb.Engine_STARROCKS, parseDorisStatements)+}++// omniAST wraps an omni AST node to implement the base.AST interface.+type omniAST struct {+ node ast.Node+ startPos *storepb.Position+}++// ASTStartPosition implements base.AST.+func (a *omniAST) ASTStartPosition() *storepb.Position {+ return a.startPos+}++// Node returns the underlying omni AST node. May be nil if the segment+// failed to parse cleanly but errors were tolerated by the caller.+func (a *omniAST) Node() ast.Node {+ return a.node}// parseDorisStatements is the ParseStatementsFunc for Doris.// Returns []ParsedStatement with both text and AST populated.+//+// For non-empty segments, the AST is an *omniAST wrapping the omni AST node.+// The first parse error encountered (per-segment) is returned to the caller,+// matching the prior behaviour of bailing out on the first syntax error.func parseDorisStatements(statement string) ([]base.ParsedStatement, error) {- // First split to get Statement with text and positionsstmts, err := SplitSQL(statement)if err != nil {return nil, err}- // Then parse to get ASTs- antlrASTs, err := ParseDorisSQL(statement)- if err != nil {- return nil, err- }-- // Combine: Statement provides text/positions, ANTLRAST provides AST- var result []base.ParsedStatement- astIndex := 0+ result := make([]base.ParsedStatement, 0, len(stmts))for _, stmt := range stmts {ps := base.ParsedStatement{Statement: stmt,}- if !stmt.Empty && astIndex < len(antlrASTs) {- ps.AST = antlrASTs[astIndex]- astIndex+++ if stmt.Empty || strings.TrimSpace(stmt.Text) == "" {+ result = append(result, ps)+ continue+ }+ // Parse the segment text on its own so byte offsets in any ParseError+ // align with stmt.Text; convertParseError then shifts them into the+ // coordinates of the original multi-statement script.+ file, errs := parser.Parse(stmt.Text)+ if len(errs) > 0 {+ pe := errs[0]+ return nil, convertParseError(stmt.Text, &pe, stmt.Start)+ }+ var node ast.Node+ if file != nil && len(file.Stmts) > 0 {+ node = file.Stmts[0]+ }+ ps.AST = &omniAST{+ node: node,+ startPos: stmt.Start,}result = append(result, ps)}@@ -44,64 +76,84 @@ func parseDorisStatements(statement string) ([]base.ParsedStatement, error) {return result, nil}-// ParseDorisSQL parses the given SQL statement by using antlr4. Returns a list of AST and token stream if no error.-func ParseDorisSQL(statement string) ([]*base.ANTLRAST, error) {+// parseDorisSQL parses the given SQL statement using the omni Doris parser.+// Returns one *omniAST per non-empty segment (empty / comment-only segments+// are skipped).+//+// This retains the historical signature shape used by other doris package+// files; on the first parse error it returns a *base.SyntaxError with the+// position translated into the coordinates of the original input.+func parseDorisSQL(statement string) ([]*omniAST, error) {stmts, err := SplitSQL(statement)if err != nil {return nil, err}- var result []*base.ANTLRAST+ var result []*omniASTfor _, stmt := range stmts {- if stmt.Empty {+ if stmt.Empty || strings.TrimSpace(stmt.Text) == "" {continue}-- antlrAST, err := parseSingleDorisSQL(stmt.Text, stmt.BaseLine())- if err != nil {- return nil, err+ file, errs := parser.Parse(stmt.Text)+ if len(errs) > 0 {+ pe := errs[0]+ return nil, convertParseError(stmt.Text, &pe, stmt.Start)+ }+ var node ast.Node+ if file != nil && len(file.Stmts) > 0 {+ node = file.Stmts[0]}- result = append(result, antlrAST)+ result = append(result, &omniAST{+ node: node,+ startPos: stmt.Start,+ })}-return result, nil}-func parseSingleDorisSQL(statement string, baseLine int) (*base.ANTLRAST, error) {- lexer := parser.NewDorisLexer(antlr.NewInputStream(statement))- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)- p := parser.NewDorisParser(stream)- startPosition := &storepb.Position{Line: int32(baseLine) + 1}- lexerErrorListener := &base.ParseErrorListener{- Statement: statement,- StartPosition: startPosition,- }- lexer.RemoveErrorListeners()- lexer.AddErrorListener(lexerErrorListener)-- parserErrorListener := &base.ParseErrorListener{- Statement: statement,- StartPosition: startPosition,- }- p.RemoveErrorListeners()- p.AddErrorListener(parserErrorListener)-- p.BuildParseTrees = true-- tree := p.MultiStatements()- if lexerErrorListener.Err != nil {- return nil, lexerErrorListener.Err+// convertParseError converts an omni *parser.ParseError to a+// *base.SyntaxError that sql_service.go recognises for structured+// error diagnostics. It converts the byte offset in ParseError.Loc+// to 1-based line and 1-based column (rune-based) matching the+// storepb.Position convention used across other omni parser adapters.+//+// If basePos is non-nil, the computed position is offset by it so that+// errors from parsing an isolated statement segment are reported in the+// coordinates of the original multi-statement script.+func convertParseError(statement string, pe *parser.ParseError, basePos *storepb.Position) *base.SyntaxError {+ line, col := byteOffsetToPosition(statement, pe.Loc.Start)+ if basePos != nil {+ // The first line of the segment shares a line with basePos, so+ // column offsets only apply when the error is on that line.+ if line == 1 {+ col = int(basePos.Column) + col - 1+ }+ line = int(basePos.Line) + line - 1}-- if parserErrorListener.Err != nil {- return nil, parserErrorListener.Err+ return &base.SyntaxError{+ Position: &storepb.Position{+ Line: int32(line),+ Column: int32(col),+ },+ Message: pe.Error(),+ RawMessage: pe.Msg,}+}- result := &base.ANTLRAST{- StartPosition: startPosition,- Tree: tree,- Tokens: stream,+// byteOffsetToPosition converts a 0-based byte offset to a 1-based line and+// 1-based column (in runes).+func byteOffsetToPosition(s string, offset int) (line, col int) {+ line = 1+ col = 1+ for i := 0; i < offset && i < len(s); {+ r, size := utf8.DecodeRuneInString(s[i:])+ if r == '\n' {+ line+++ col = 1+ } else {+ col+++ }+ i += size}-- return result, nil+ return line, col}diff --git a/backend/plugin/parser/doris/doris_test.go b/backend/plugin/parser/doris/doris_test.goindex 4bb75acc07..20f616e720 100644--- a/backend/plugin/parser/doris/doris_test.go+++ b/backend/plugin/parser/doris/doris_test.go@@ -3,7 +3,6 @@ package dorisimport ("testing"- parser "github.com/bytebase/parser/doris""github.com/stretchr/testify/require")@@ -19,21 +18,21 @@ func TestDorisSQLParser(t *testing.T) {statement: "SELECT * FROM schema1.t1 JOIN schema2.t2 ON t1.c1 = t2.c1",},{+ // Truncated SELECT — must be reported as a syntax error.statement: "SELECT a > (select max(a) from t1) FROM",- errorMessage: "Syntax error at line 1:40 \nrelated text: SELECT a > (select max(a) from t1) FROM",+ errorMessage: "syntax error at end of input",},}for _, test := range tests {- res, err := ParseDorisSQL(test.statement)- if len(res) > 0 {- _, ok := res[0].Tree.(*parser.MultiStatementsContext)- require.True(t, ok)- }+ res, err := parseDorisSQL(test.statement)if test.errorMessage == "" {require.NoError(t, err)+ require.NotEmpty(t, res)+ require.NotNil(t, res[0].Node())} else {- require.EqualError(t, err, test.errorMessage)+ require.Error(t, err)+ require.Contains(t, err.Error(), test.errorMessage)}}}diff --git a/backend/plugin/parser/doris/normalize.go b/backend/plugin/parser/doris/normalize.godeleted file mode 100644index d1fd016a89..0000000000--- a/backend/plugin/parser/doris/normalize.go+++ /dev/null@@ -1,42 +0,0 @@-package doris--import (- "strings"-- parser "github.com/bytebase/parser/doris"-)--// NormalizeMultipartIdentifier extracts parts from a MultipartIdentifierContext.-func NormalizeMultipartIdentifier(ctx parser.IMultipartIdentifierContext) []string {- if ctx == nil {- return nil- }- var result []string- for _, part := range ctx.AllErrorCapturingIdentifier() {- if part == nil {- continue- }- id := part.Identifier()- if id == nil {- continue- }- result = append(result, NormalizeIdentifier(id))- }- return result-}--// NormalizeIdentifier extracts the identifier text from an IdentifierContext.-// It removes backticks from quoted identifiers.-func NormalizeIdentifier(ctx parser.IIdentifierContext) string {- if ctx == nil {- return ""- }-- text := ctx.GetText()- // Check if it's a backtick-quoted identifier and remove the backticks- if strings.HasPrefix(text, "`") && strings.HasSuffix(text, "`") && len(text) >= 2 {- return text[1 : len(text)-1]- }-- return text-}diff --git a/backend/plugin/parser/doris/query.go b/backend/plugin/parser/doris/query.goindex 8536322563..baed67f616 100644--- a/backend/plugin/parser/doris/query.go+++ b/backend/plugin/parser/doris/query.go@@ -1,8 +1,7 @@package dorisimport (- "github.com/antlr4-go/antlr/v4"- parser "github.com/bytebase/parser/doris"+ "github.com/bytebase/omni/doris/ast"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/parser/base"@@ -13,263 +12,82 @@ func init() {base.RegisterQueryValidator(storepb.Engine_DORIS, validateQuery)}+// validateQuery reports whether the given statement is a read-only query+// suitable for the SQL editor / data-query path.+//+// Decision is AST-based: each parsed top-level statement must be a SELECT,+// SHOW, DESCRIBE, EXPLAIN, or HELP. Relying on leading-keyword classification+// alone would mis-accept CTE-prefixed DML such as `WITH x AS (...) UPDATE ...`,+// which Classify would tag as SELECT because `WITH` is its first token.+//+// Syntax errors are surfaced up; that lets validateQueryRequest reject+// malformed read-only SQL before execution.+//+// The (bool, bool, error) return shape matches the bytebase QueryValidator+// contract: (isReadOnly, isExplicitReadOnly, syntaxError).func validateQuery(statement string) (bool, bool, error) {- // TODO: support other readonly statements like SHOW TABLES, SHOW CREATE TABLE, etc.- results, err := ParseDorisSQL(statement)+ parsed, err := parseDorisSQL(statement)if err != nil {return false, false, err}- for _, result := range results {- l := &queryValidateListener{- valid: true,- }- antlr.ParseTreeWalkerDefault.Walk(l, result.Tree)- if !l.valid {+ for _, p := range parsed {+ if !isReadOnlyAST(p.Node()) {return false, false, nil}}return true, true, nil}-type queryValidateListener struct {- *parser.BaseDorisParserListener-- valid bool-}--// EnterStatementDefault is called when entering the statementDefault production (SELECT queries).-// SELECT statements are valid queries.-func (l *queryValidateListener) EnterStatementDefault(ctx *parser.StatementDefaultContext) {- if !l.valid {- return- }- // SELECT queries are allowed- if ctx != nil && ctx.Query() != nil {- return+// isReadOnlyAST returns true when the given top-level AST node represents a+// read-only Doris statement (SELECT family, SHOW, DESCRIBE, EXPLAIN, HELP).+//+// Omni's parser currently has stub-shaped acceptance for some shapes — bare+// `SHOW`, `DESCRIBE`, `EXPLAIN` all produce a corresponding AST node with+// empty content rather than a parse error. We reject those here so the+// previous ANTLR helper's behaviour (rejecting incomplete forms) is+// preserved; the database would only reject them at execution time+// otherwise.+//+// Nil nodes are conservatively rejected — they indicate a parse path that+// produced no concrete statement, which shouldn't happen for valid read-only+// SQL after parseDorisSQL succeeds.+func isReadOnlyAST(node ast.Node) bool {+ switch n := node.(type) {+ case *ast.SelectStmt, *ast.SetOpStmt:+ return true+ case *ast.ShowStmt:+ // Reject bare `SHOW` (Type is empty when the parser took the stub path+ // without seeing a recognised variant keyword).+ return n.Type != ""+ case *ast.ShowRoutineLoadStmt, *ast.ShowRoutineLoadTaskStmt,+ *ast.ShowJobStmt, *ast.ShowJobTaskStmt,+ *ast.ShowConstraintsStmt, *ast.ShowAnalyzeStmt, *ast.ShowStatsStmt:+ return true+ case *ast.DescribeStmt:+ // Reject bare `DESCRIBE` / `DESC` (Target nil means no table named).+ return n.Target != nil+ case *ast.ExplainStmt:+ // EXPLAIN requires an inner query; the inner query must itself be a+ // shape EXPLAIN is meaningful for (SELECT/DML/Show). This blocks+ // inputs like `EXPLAIN`, `EXPLAIN DROP TABLE t`, etc.+ return n.Query != nil && isExplainableInner(n.Query)+ case *ast.HelpStmt:+ return true+ default:+ return false}}-// EnterSupportedShowStatementAlias is called for all SHOW statements.-// SHOW statements are valid read-only queries.-func (*queryValidateListener) EnterSupportedShowStatementAlias(_ *parser.SupportedShowStatementAliasContext) {- // SHOW statements are allowed-}--// EnterSupportedDmlStatementAlias is called for DML statements (INSERT, UPDATE, DELETE).-// DML statements are NOT valid read-only queries, unless they have an EXPLAIN prefix.-func (l *queryValidateListener) EnterSupportedDmlStatementAlias(ctx *parser.SupportedDmlStatementAliasContext) {- if ctx == nil {- return- }-- // Check if this DML statement has an EXPLAIN prefix (read-only)- if dml := ctx.SupportedDmlStatement(); dml != nil {- hasExplain := false- switch stmt := dml.(type) {- case *parser.InsertTableContext:- hasExplain = stmt.Explain() != nil- case *parser.UpdateContext:- hasExplain = stmt.Explain() != nil- case *parser.DeleteContext:- hasExplain = stmt.Explain() != nil- case *parser.MergeIntoContext:- hasExplain = stmt.Explain() != nil- default:- }-- if hasExplain {- // EXPLAIN on DML is read-only, so it's valid- return- }+// isExplainableInner reports whether `node` is a shape that EXPLAIN can+// legitimately wrap (SELECT family or DML). DDL inside EXPLAIN is rejected:+// Doris only supports EXPLAIN on query and DML statements.+func isExplainableInner(node ast.Node) bool {+ switch node.(type) {+ case *ast.SelectStmt, *ast.SetOpStmt:+ return true+ case *ast.InsertStmt, *ast.UpdateStmt, *ast.DeleteStmt,+ *ast.MergeStmt, *ast.TruncateTableStmt:+ return true}-- l.valid = false-}--// EnterSupportedCreateStatementAlias is called for CREATE statements.-func (l *queryValidateListener) EnterSupportedCreateStatementAlias(_ *parser.SupportedCreateStatementAliasContext) {- l.valid = false-}--// EnterSupportedAlterStatementAlias is called for ALTER statements.-func (l *queryValidateListener) EnterSupportedAlterStatementAlias(_ *parser.SupportedAlterStatementAliasContext) {- l.valid = false-}--// EnterSupportedDropStatementAlias is called for DROP statements.-func (l *queryValidateListener) EnterSupportedDropStatementAlias(_ *parser.SupportedDropStatementAliasContext) {- l.valid = false-}--// EnterMaterializedViewStatementAlias is called for materialized view statements.-func (l *queryValidateListener) EnterMaterializedViewStatementAlias(_ *parser.MaterializedViewStatementAliasContext) {- l.valid = false-}--// EnterConstraintStatementAlias is called for constraint statements.-func (l *queryValidateListener) EnterConstraintStatementAlias(_ *parser.ConstraintStatementAliasContext) {- l.valid = false-}--// EnterSupportedLoadStatementAlias is called for LOAD statements.-func (l *queryValidateListener) EnterSupportedLoadStatementAlias(_ *parser.SupportedLoadStatementAliasContext) {- l.valid = false-}--// EnterSupportedGrantRevokeStatementAlias is called for GRANT/REVOKE statements.-func (l *queryValidateListener) EnterSupportedGrantRevokeStatementAlias(_ *parser.SupportedGrantRevokeStatementAliasContext) {- l.valid = false-}--// EnterSupportedAdminStatementAlias is called for ADMIN statements.-func (l *queryValidateListener) EnterSupportedAdminStatementAlias(_ *parser.SupportedAdminStatementAliasContext) {- l.valid = false-}--// EnterSupportedTransactionStatementAlias is called for transaction statements.-func (l *queryValidateListener) EnterSupportedTransactionStatementAlias(_ *parser.SupportedTransactionStatementAliasContext) {- l.valid = false-}--// EnterSupportedKillStatementAlias is called for KILL statements.-func (l *queryValidateListener) EnterSupportedKillStatementAlias(_ *parser.SupportedKillStatementAliasContext) {- l.valid = false-}--// EnterSupportedJobStatementAlias is called for JOB statements.-func (l *queryValidateListener) EnterSupportedJobStatementAlias(_ *parser.SupportedJobStatementAliasContext) {- l.valid = false-}--// EnterSupportedSetStatementAlias is called for SET statements.-func (l *queryValidateListener) EnterSupportedSetStatementAlias(_ *parser.SupportedSetStatementAliasContext) {- l.valid = false-}--// EnterSupportedUnsetStatementAlias is called for UNSET statements.-func (l *queryValidateListener) EnterSupportedUnsetStatementAlias(_ *parser.SupportedUnsetStatementAliasContext) {- l.valid = false-}--// EnterSupportedRefreshStatementAlias is called for REFRESH statements.-func (l *queryValidateListener) EnterSupportedRefreshStatementAlias(_ *parser.SupportedRefreshStatementAliasContext) {- l.valid = false-}--// EnterSupportedCancelStatementAlias is called for CANCEL statements.-func (l *queryValidateListener) EnterSupportedCancelStatementAlias(_ *parser.SupportedCancelStatementAliasContext) {- l.valid = false-}--// EnterSupportedRecoverStatementAlias is called for RECOVER statements.-func (l *queryValidateListener) EnterSupportedRecoverStatementAlias(_ *parser.SupportedRecoverStatementAliasContext) {- l.valid = false-}--// EnterSupportedCleanStatementAlias is called for CLEAN statements.-func (l *queryValidateListener) EnterSupportedCleanStatementAlias(_ *parser.SupportedCleanStatementAliasContext) {- l.valid = false-}--// EnterSupportedOtherStatementAlias is called for other unsupported statements.-func (l *queryValidateListener) EnterSupportedOtherStatementAlias(_ *parser.SupportedOtherStatementAliasContext) {- l.valid = false-}--// EnterSupportedStatsStatementAlias is called for stats statements.-func (l *queryValidateListener) EnterSupportedStatsStatementAlias(_ *parser.SupportedStatsStatementAliasContext) {- l.valid = false-}--// EnterSupportedDescribeStatementAlias is called for DESCRIBE statements (read-only).-func (*queryValidateListener) EnterSupportedDescribeStatementAlias(_ *parser.SupportedDescribeStatementAliasContext) {- // DESCRIBE statements are allowed-}--// The following are SHOW statements nested within other statement categories.-// They override the parent category's invalid setting.--// SHOW statements in supportedLoadStatement-func (l *queryValidateListener) EnterShowCreateLoad(_ *parser.ShowCreateLoadContext) {- l.valid = true-}--func (l *queryValidateListener) EnterShowCreateRoutineLoad(_ *parser.ShowCreateRoutineLoadContext) {- l.valid = true-}--func (l *queryValidateListener) EnterShowRoutineLoad(_ *parser.ShowRoutineLoadContext) {- l.valid = true-}--func (l *queryValidateListener) EnterShowRoutineLoadTask(_ *parser.ShowRoutineLoadTaskContext) {- l.valid = true-}--func (l *queryValidateListener) EnterShowIndexAnalyzer(_ *parser.ShowIndexAnalyzerContext) {- l.valid = true-}--func (l *queryValidateListener) EnterShowIndexTokenizer(_ *parser.ShowIndexTokenizerContext) {- l.valid = true-}--func (l *queryValidateListener) EnterShowIndexTokenFilter(_ *parser.ShowIndexTokenFilterContext) {- l.valid = true-}--func (l *queryValidateListener) EnterShowIndexCharFilter(_ *parser.ShowIndexCharFilterContext) {- l.valid = true-}--// SHOW statements in supportedAdminStatement-func (l *queryValidateListener) EnterAdminShowReplicaDistribution(_ *parser.AdminShowReplicaDistributionContext) {- l.valid = true-}--func (l *queryValidateListener) EnterAdminShowReplicaStatus(_ *parser.AdminShowReplicaStatusContext) {- l.valid = true-}--func (l *queryValidateListener) EnterAdminShowTabletStorageFormat(_ *parser.AdminShowTabletStorageFormatContext) {- l.valid = true-}--// SHOW statements in supportedStatsStatement-func (l *queryValidateListener) EnterShowAnalyze(_ *parser.ShowAnalyzeContext) {- l.valid = true-}--func (l *queryValidateListener) EnterShowQueuedAnalyzeJobs(_ *parser.ShowQueuedAnalyzeJobsContext) {- l.valid = true-}--func (l *queryValidateListener) EnterShowColumnHistogramStats(_ *parser.ShowColumnHistogramStatsContext) {- l.valid = true-}--func (l *queryValidateListener) EnterShowColumnStats(_ *parser.ShowColumnStatsContext) {- l.valid = true-}--func (l *queryValidateListener) EnterShowAnalyzeTask(_ *parser.ShowAnalyzeTaskContext) {- l.valid = true-}--func (l *queryValidateListener) EnterShowIndexStats(_ *parser.ShowIndexStatsContext) {- l.valid = true-}--func (l *queryValidateListener) EnterShowTableStats(_ *parser.ShowTableStatsContext) {- l.valid = true-}--// SHOW statements in materializedViewStatement-func (l *queryValidateListener) EnterShowCreateMTMV(_ *parser.ShowCreateMTMVContext) {- l.valid = true-}--// SHOW statements in constraintStatement-func (l *queryValidateListener) EnterShowConstraint(_ *parser.ShowConstraintContext) {- l.valid = true+ return false}diff --git a/backend/plugin/parser/doris/query_span_extractor.go b/backend/plugin/parser/doris/query_span_extractor.goindex 192f9a92e9..036617cb5b 100644--- a/backend/plugin/parser/doris/query_span_extractor.go+++ b/backend/plugin/parser/doris/query_span_extractor.go@@ -4,18 +4,32 @@ import ("context""strings"- "github.com/antlr4-go/antlr/v4"- parser "github.com/bytebase/parser/doris"+ "github.com/bytebase/omni/doris/analysis"+ "github.com/bytebase/omni/doris/ast"+ "github.com/bytebase/omni/doris/parser""github.com/pkg/errors""github.com/bytebase/bytebase/backend/plugin/parser/base")+// querySpanExtractor analyses a single Doris statement and produces a+// base.QuerySpan: the set of physical tables it reads and its classification+// as a Select / SelectInfoSchema / DML / DDL statement.+//+// The extractor delegates the heavy lifting to omni's analysis.GetQuerySpan,+// then applies bytebase-specific post-processing:+// - default-database fill-in for bare table references,+// - system-vs-user mixed-query rejection (MixUserSystemTablesError),+// - QueryType promotion to SelectInfoSchema when every accessed table is a+// system table (matching the legacy ANTLR listener behaviour).type querySpanExtractor struct {ctx context.ContextdefaultDatabase stringgCtx base.GetQuerySpanContext- // ctes tracks Common Table Expressions in the current scope+ // ctes tracks Common Table Expressions in the current scope. omni's+ // GetQuerySpan already filters CTE references from AccessTables, but we+ // keep the field to preserve the construction signature used by callers+ // (and any future logic that needs it).ctes map[string]boolignoreCaseSensitive bool}@@ -31,147 +45,107 @@ func newQuerySpanExtractor(database string, gCtx base.GetQuerySpanContext, ignorfunc (q *querySpanExtractor) getQuerySpan(ctx context.Context, statement string) (*base.QuerySpan, error) {q.ctx = ctx- antlrASTs, err := ParseDorisSQL(statement)++ // Split into top-level statements so we can reject inputs that contain+ // multiple statements (matching the legacy behaviour) before doing any+ // per-statement analysis.+ stmts, err := SplitSQL(statement)if err != nil {return nil, err}-- if len(antlrASTs) == 0 {+ nonEmpty := 0+ var single string+ for _, s := range stmts {+ if s.Empty {+ continue+ }+ nonEmpty+++ single = s.Text+ }+ if nonEmpty == 0 {return &base.QuerySpan{SourceColumns: base.SourceColumnSet{},Results: []base.QuerySpanResult{},}, nil}- if len(antlrASTs) != 1 {- return nil, errors.Errorf("expecting only one statement to get query span, but got %d", len(antlrASTs))+ if nonEmpty > 1 {+ return nil, errors.Errorf("expecting only one statement to get query span, but got %d", nonEmpty)+ }++ // omni's span walker only descends into SELECT/SetOp at the top level,+ // so EXPLAIN <query> would return zero AccessTables. Unwrap an EXPLAIN+ // to the inner statement's text before delegating; that way table-level+ // ACL checks still see what the underlying query reads.+ spanInput := unwrapExplainForSpan(single)++ // Delegate to omni for table-access extraction + classification.+ omniSpan, err := analysis.GetQuerySpan(spanInput)+ if err != nil {+ return nil, err}- antlrAST := antlrASTs[0]- accessTables := getAccessTables(q.defaultDatabase, antlrAST, q.ctes, q.gCtx, q.ignoreCaseSensitive)+ sources := q.toSourceColumnSet(omniSpan)++ // Track CTE names omni discovered for callers that want to inspect them.+ for _, name := range omniSpan.CTEs {+ q.ctes[strings.ToLower(name)] = true+ }- // We do not support simultaneous access to the system table and the user table- // because we do not synchronize the schema of the system table.- // This causes an error (NOT_FOUND) when using querySpanExtractor.findTableSchema.- // As a result, we exclude getting query span results for accessing only the system table.- allSystems, mixed := isMixedQuery(accessTables, q.ignoreCaseSensitive)+ allSystems, mixed := isMixedQuery(sources, q.ignoreCaseSensitive)if mixed {return nil, base.MixUserSystemTablesError}- queryTypeListener := &queryTypeListener{- allSystems: allSystems,- result: base.QueryTypeUnknown,- }- antlr.ParseTreeWalkerDefault.Walk(queryTypeListener, antlrAST.Tree)+ queryType := getQueryType(single, allSystems)return &base.QuerySpan{- Type: queryTypeListener.result,- SourceColumns: accessTables,+ Type: queryType,+ SourceColumns: sources,Results: []base.QuerySpanResult{},}, nil}-func getAccessTables(database string, antlrAST *base.ANTLRAST, ctes map[string]bool, gCtx base.GetQuerySpanContext, ignoreCaseSensitive bool) base.SourceColumnSet {- // First, extract CTEs from the query- cteListener := newCTEListener()- antlr.ParseTreeWalkerDefault.Walk(cteListener, antlrAST.Tree)-- // Merge extracted CTEs with any existing ones- for cte := range cteListener.ctes {- ctes[cte] = true- }-- accessTableListener := newAccessTableListener(database, ctes, gCtx, ignoreCaseSensitive)- antlr.ParseTreeWalkerDefault.Walk(accessTableListener, antlrAST.Tree)-- return accessTableListener.sourceColumnSet-}--type accessTableListener struct {- *parser.BaseDorisParserListener-- defaultDatabase string- sourceColumnSet base.SourceColumnSet- ctes map[string]bool- gCtx base.GetQuerySpanContext- ignoreCaseSensitive bool-}--func newAccessTableListener(database string, ctes map[string]bool, gCtx base.GetQuerySpanContext, ignoreCaseSensitive bool) *accessTableListener {- return &accessTableListener{- defaultDatabase: database,- sourceColumnSet: base.SourceColumnSet{},- ctes: ctes,- gCtx: gCtx,- ignoreCaseSensitive: ignoreCaseSensitive,- }+// unwrapExplainForSpan returns the inner statement's text when `statement`+// parses to a top-level EXPLAIN; otherwise it returns the original string.+//+// omni's span walker only descends into SELECT/SetOp at the top level, so+// without this unwrap an `EXPLAIN SELECT ... FROM t` would yield zero+// AccessTables — table-level ACL checks need to see `t`.+func unwrapExplainForSpan(statement string) string {+ file, errs := parser.Parse(statement)+ if len(errs) > 0 || file == nil || len(file.Stmts) == 0 {+ return statement+ }+ explain, ok := file.Stmts[0].(*ast.ExplainStmt)+ if !ok || explain.Query == nil {+ return statement+ }+ inner := ast.NodeLoc(explain.Query)+ if inner.Start < 0 || inner.End > len(statement) || inner.Start >= inner.End {+ return statement+ }+ return statement[inner.Start:inner.End]}-// EnterTableName is called when entering a tableName production.-func (l *accessTableListener) EnterTableName(ctx *parser.TableNameContext) {- if ctx == nil {- return- }-- multipart := ctx.MultipartIdentifier()- if multipart == nil {- return- }-- list := NormalizeMultipartIdentifier(multipart)- switch len(list) {- case 1:- // Check if this is a CTE reference- if l.ctes[list[0]] {- // Skip CTE references - they don't need permission checks- return+// toSourceColumnSet converts an omni QuerySpan's AccessTables into the+// base.SourceColumnSet shape bytebase expects. Tables with no database+// qualifier fall back to the extractor's default database.+func (q *querySpanExtractor) toSourceColumnSet(span *analysis.QuerySpan) base.SourceColumnSet {+ out := base.SourceColumnSet{}+ if span == nil {+ return out+ }+ for _, t := range span.AccessTables {+ db := t.Database+ if db == "" {+ db = q.defaultDatabase}- l.sourceColumnSet[base.ColumnResource{- Database: l.defaultDatabase,- Table: list[0],- }] = true- case 2:- // For qualified names (db.table), CTEs cannot have schema qualifiers- l.sourceColumnSet[base.ColumnResource{- Database: list[0],- Table: list[1],+ out[base.ColumnResource{+ Database: db,+ Table: t.Table,}] = true- default:- // Ignore qualified names with more than 2 parts- }-}--// cteListener extracts CTE names from WITH clauses-type cteListener struct {- *parser.BaseDorisParserListener-- ctes map[string]bool-}--func newCTEListener() *cteListener {- return &cteListener{- ctes: make(map[string]bool),- }-}--// EnterCte is called when entering a CTE production.-func (l *cteListener) EnterCte(ctx *parser.CteContext) {- if ctx == nil {- return- }-- // Extract all CTEs from the WITH clause- for _, aliasQuery := range ctx.AllAliasQuery() {- if aliasQuery == nil {- continue- }- id := aliasQuery.Identifier()- if id == nil {- continue- }- cteName := NormalizeIdentifier(id)- l.ctes[cteName] = true}+ return out}// isMixedQuery checks whether the query accesses the user table and system table at the same time.diff --git a/backend/plugin/parser/doris/query_span_extractor_explain_test.go b/backend/plugin/parser/doris/query_span_extractor_explain_test.gonew file mode 100644index 0000000000..87def7e967--- /dev/null+++ b/backend/plugin/parser/doris/query_span_extractor_explain_test.go@@ -0,0 +1,59 @@+package doris++import (+ "context"+ "testing"++ "github.com/stretchr/testify/require"++ "github.com/bytebase/bytebase/backend/plugin/parser/base"+)++// TestQuerySpanExtractor_ExplainUnwrap pins the behaviour that EXPLAIN+// statements still surface the tables the underlying query reads — omni's+// span walker only descends into top-level SELECT/SetOp, so without the+// EXPLAIN unwrap in the extractor an `EXPLAIN SELECT ... FROM t` would+// produce zero AccessTables and table-level ACL checks would not see `t`.+func TestQuerySpanExtractor_ExplainUnwrap(t *testing.T) {+ tests := []struct {+ name string+ sql string+ expected []base.ColumnResource+ }{+ {+ name: "EXPLAIN SELECT exposes the SELECT's tables",+ sql: "EXPLAIN SELECT * FROM users",+ expected: []base.ColumnResource{{Database: "test", Table: "users"}},+ },+ {+ name: "EXPLAIN VERBOSE SELECT exposes tables",+ sql: "EXPLAIN VERBOSE SELECT id FROM t1 JOIN t2 ON t1.id = t2.id",+ expected: []base.ColumnResource{{Database: "test", Table: "t1"}, {Database: "test", Table: "t2"}},+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ extractor := newQuerySpanExtractor("test", base.GetQuerySpanContext{}, false)+ span, err := extractor.getQuerySpan(context.Background(), tt.sql)+ require.NoError(t, err)++ var got []base.ColumnResource+ for table := range span.SourceColumns {+ got = append(got, table)+ }+ require.Equal(t, len(tt.expected), len(got),+ "expected %d tables, got %d (%v)", len(tt.expected), len(got), got)+ for _, want := range tt.expected {+ found := false+ for _, g := range got {+ if g.Database == want.Database && g.Table == want.Table {+ found = true+ break+ }+ }+ require.True(t, found, "missing table %s.%s in %v", want.Database, want.Table, got)+ }+ })+ }+}diff --git a/backend/plugin/parser/doris/query_test.go b/backend/plugin/parser/doris/query_test.goindex 64c3496d88..77067f3798 100644--- a/backend/plugin/parser/doris/query_test.go+++ b/backend/plugin/parser/doris/query_test.go@@ -102,6 +102,11 @@ func TestValidateQuery(t *testing.T) {valid: true,description: "EXPLAIN DELETE should be valid (read-only)",},+ {+ statement: "WITH c AS (SELECT 1) SELECT * FROM c",+ valid: true,+ description: "CTE-prefixed SELECT is read-only",+ },}for _, tc := range tests {@@ -112,4 +117,56 @@ func TestValidateQuery(t *testing.T) {a.Equal(tc.valid, valid, "statement: %s", tc.statement)})}++ // Cases that must fail validation — either because they're not read-only+ // or because they don't parse. Both shapes flow through the same code+ // path and must be rejected (either via valid=false or err!=nil).+ rejectCases := []struct {+ statement string+ description string+ }{+ {+ // CTE-prefixed DML must NOT be accepted as read-only — the+ // keyword-based Classify would have tagged it as SELECT because+ // `WITH` is the leading keyword. AST-based validation catches it+ // (or, currently, the omni parser rejects it as a syntax error;+ // either rejection is acceptable).+ statement: "WITH c AS (SELECT 1) UPDATE t SET x = 1",+ description: "CTE-prefixed UPDATE must be rejected",+ },+ {+ statement: "WITH c AS (SELECT 1) DELETE FROM t",+ description: "CTE-prefixed DELETE must be rejected",+ },+ {+ statement: "SELECT a > (select max(a) from t1) FROM",+ description: "Truncated SELECT must be rejected as syntax error",+ },+ {+ // omni's stub parser accepts bare SHOW as *ast.ShowStmt with+ // Type="". AST-content validation rejects it.+ statement: "SHOW",+ description: "Bare SHOW must be rejected",+ },+ {+ statement: "DESCRIBE",+ description: "Bare DESCRIBE must be rejected",+ },+ {+ statement: "EXPLAIN",+ description: "Bare EXPLAIN must be rejected",+ },+ {+ // EXPLAIN over DDL is not a real read-only operation.+ statement: "EXPLAIN DROP TABLE t",+ description: "EXPLAIN over DDL must be rejected",+ },+ }+ for _, tc := range rejectCases {+ t.Run(tc.description, func(t *testing.T) {+ a := require.New(t)+ valid, _, err := validateQuery(tc.statement)+ a.False(valid && err == nil, "expected rejection, statement: %s", tc.statement)+ })+ }}diff --git a/backend/plugin/parser/doris/query_type.go b/backend/plugin/parser/doris/query_type.goindex bebba7414d..d7af361bae 100644--- a/backend/plugin/parser/doris/query_type.go+++ b/backend/plugin/parser/doris/query_type.go@@ -1,268 +1,103 @@package dorisimport (- parser "github.com/bytebase/parser/doris"+ "github.com/bytebase/omni/doris/analysis"+ "github.com/bytebase/omni/doris/ast"+ "github.com/bytebase/omni/doris/parser""github.com/bytebase/bytebase/backend/plugin/parser/base")-type queryTypeListener struct {- *parser.BaseDorisParserListener-- allSystems bool- result base.QueryType-}--// EnterStatementDefault is called when entering the statementDefault production (SELECT queries).-func (l *queryTypeListener) EnterStatementDefault(ctx *parser.StatementDefaultContext) {- if ctx == nil {- return- }-- if ctx.Query() != nil {- // If all tables are system tables, we should return SelectInfoSchema.- if l.allSystems {- l.result = base.SelectInfoSchema- } else {- l.result = base.Select+// getQueryType classifies a single statement.+//+// Uses AST inspection where possible — keyword-based Classify alone would+// mislabel CTE-prefixed DML (`WITH ... UPDATE ...`) as Select because `WITH`+// is its first token, which would propagate into ACL checks. AST inspection+// surfaces the real operation. If parsing fails or produces no statements+// (e.g. comment-only input), we fall back to the keyword-based classifier so+// callers still receive a best-effort classification.+//+// allSystems is the flag computed by the query-span extractor that indicates+// whether every accessed table belongs to a system database. When true and+// the resolved type is a user SELECT, the result is promoted to+// SelectInfoSchema to match the legacy ANTLR listener behaviour.+//+// EXPLAIN-prefixed statements defer to the inner statement's type — an+// `EXPLAIN DROP TABLE t` classifies as DDL, not Select, so type-based ACL+// checks see the inner operation rather than a downgrade-to-read-only.+func getQueryType(statement string, allSystems bool) base.QueryType {+ qt := classifyByAST(statement)++ switch qt {+ case analysis.QueryTypeSelect:+ if allSystems {+ return base.SelectInfoSchema}+ return base.Select+ case analysis.QueryTypeSelectInfoSchema:+ return base.SelectInfoSchema+ case analysis.QueryTypeDML:+ return base.DML+ case analysis.QueryTypeDDL:+ return base.DDL+ default:+ return base.QueryTypeUnknown}}-// EnterSupportedDmlStatementAlias is called for DML statements (INSERT, UPDATE, DELETE).-// If the statement has an EXPLAIN prefix, it's treated as a read-only query.-func (l *queryTypeListener) EnterSupportedDmlStatementAlias(ctx *parser.SupportedDmlStatementAliasContext) {- if ctx == nil {- return+// classifyByAST parses the statement and inspects the first top-level AST+// node. On parse failure or empty input it falls back to the keyword-based+// Classify.+func classifyByAST(statement string) analysis.QueryType {+ file, errs := parser.Parse(statement)+ if len(errs) > 0 || file == nil || len(file.Stmts) == 0 {+ return analysis.Classify(statement)}-- // Check if this DML statement has an EXPLAIN prefix- if dml := ctx.SupportedDmlStatement(); dml != nil {- hasExplain := false- switch stmt := dml.(type) {- case *parser.InsertTableContext:- hasExplain = stmt.Explain() != nil- case *parser.UpdateContext:- hasExplain = stmt.Explain() != nil- case *parser.DeleteContext:- hasExplain = stmt.Explain() != nil- case *parser.MergeIntoContext:- hasExplain = stmt.Explain() != nil- default:- }-- if hasExplain {- // EXPLAIN on DML is read-only- l.result = base.Select- return+ if qt, ok := astQueryType(file.Stmts[0]); ok {+ return qt+ }+ return analysis.Classify(statement)+}++// astQueryType maps a top-level AST node to its QueryType. The bool return+// is false only when the node was nil-shaped (e.g. EXPLAIN with no inner+// Query); callers should fall back to Classify in that case.+//+// EXPLAIN recurses into its inner Query so the classification reflects the+// real operation (e.g. `EXPLAIN DROP TABLE t` → DDL).+//+// Anything we don't otherwise enumerate is treated as DDL — that's a safe+// upper bound for ACL purposes (it won't accidentally label a write as+// read-only) and matches the legacy listener's "everything else is DDL"+// behaviour.+func astQueryType(node ast.Node) (analysis.QueryType, bool) {+ if node == nil {+ return 0, false+ }+ switch n := node.(type) {+ case *ast.SelectStmt, *ast.SetOpStmt:+ return analysis.QueryTypeSelect, true+ case *ast.ShowStmt,+ *ast.ShowRoutineLoadStmt, *ast.ShowRoutineLoadTaskStmt,+ *ast.ShowJobStmt, *ast.ShowJobTaskStmt,+ *ast.ShowConstraintsStmt, *ast.ShowAnalyzeStmt, *ast.ShowStatsStmt:+ return analysis.QueryTypeSelectInfoSchema, true+ case *ast.DescribeStmt, *ast.HelpStmt:+ return analysis.QueryTypeSelectInfoSchema, true+ case *ast.ExplainStmt:+ if n.Query == nil {+ return 0, false}+ return astQueryType(n.Query)+ case *ast.InsertStmt, *ast.UpdateStmt, *ast.DeleteStmt,+ *ast.MergeStmt, *ast.TruncateTableStmt:+ return analysis.QueryTypeDML, true+ case *ast.UseStmt:+ // USE was rejected as Unknown by the legacy listener — its ACL flow+ // treats Unknown as a hard deny while DDL is permitted via+ // bb.sql.ddl. Keep the classification at Unknown so users with DDL+ // rights cannot run USE.+ return analysis.QueryTypeUnknown, true}-- l.result = base.DML-}--// EnterSupportedShowStatementAlias is called for all SHOW statements.-func (l *queryTypeListener) EnterSupportedShowStatementAlias(_ *parser.SupportedShowStatementAliasContext) {- l.result = base.SelectInfoSchema-}--// EnterSupportedCreateStatementAlias is called for all CREATE statements.-func (l *queryTypeListener) EnterSupportedCreateStatementAlias(_ *parser.SupportedCreateStatementAliasContext) {- l.result = base.DDL-}--// EnterSupportedAlterStatementAlias is called for all ALTER statements.-func (l *queryTypeListener) EnterSupportedAlterStatementAlias(_ *parser.SupportedAlterStatementAliasContext) {- l.result = base.DDL-}--// EnterSupportedDropStatementAlias is called for all DROP statements.-func (l *queryTypeListener) EnterSupportedDropStatementAlias(_ *parser.SupportedDropStatementAliasContext) {- l.result = base.DDL-}--// EnterMaterializedViewStatementAlias is called for materialized view statements.-func (l *queryTypeListener) EnterMaterializedViewStatementAlias(_ *parser.MaterializedViewStatementAliasContext) {- l.result = base.DDL-}--// EnterConstraintStatementAlias is called for constraint statements.-func (l *queryTypeListener) EnterConstraintStatementAlias(_ *parser.ConstraintStatementAliasContext) {- l.result = base.DDL-}--// EnterSupportedDescribeStatementAlias is called for DESCRIBE statements (read-only).-func (l *queryTypeListener) EnterSupportedDescribeStatementAlias(_ *parser.SupportedDescribeStatementAliasContext) {- l.result = base.SelectInfoSchema-}--// EnterSupportedUseStatementAlias is called for USE statements.-// USE statements are treated as unknown so they can be forbidden.-func (l *queryTypeListener) EnterSupportedUseStatementAlias(_ *parser.SupportedUseStatementAliasContext) {- l.result = base.QueryTypeUnknown-}--// The following statement types default to DDL as they can modify data or schema.--// EnterSupportedLoadStatementAlias is called for LOAD statements.-func (l *queryTypeListener) EnterSupportedLoadStatementAlias(_ *parser.SupportedLoadStatementAliasContext) {- l.result = base.DDL-}--// EnterSupportedGrantRevokeStatementAlias is called for GRANT/REVOKE statements.-func (l *queryTypeListener) EnterSupportedGrantRevokeStatementAlias(_ *parser.SupportedGrantRevokeStatementAliasContext) {- l.result = base.DDL-}--// EnterSupportedAdminStatementAlias is called for ADMIN statements.-func (l *queryTypeListener) EnterSupportedAdminStatementAlias(_ *parser.SupportedAdminStatementAliasContext) {- l.result = base.DDL-}--// EnterSupportedTransactionStatementAlias is called for transaction statements.-func (l *queryTypeListener) EnterSupportedTransactionStatementAlias(_ *parser.SupportedTransactionStatementAliasContext) {- l.result = base.DDL-}--// EnterSupportedKillStatementAlias is called for KILL statements.-func (l *queryTypeListener) EnterSupportedKillStatementAlias(_ *parser.SupportedKillStatementAliasContext) {- l.result = base.DDL-}--// EnterSupportedJobStatementAlias is called for JOB statements.-func (l *queryTypeListener) EnterSupportedJobStatementAlias(_ *parser.SupportedJobStatementAliasContext) {- l.result = base.DDL-}--// EnterSupportedSetStatementAlias is called for SET statements.-func (l *queryTypeListener) EnterSupportedSetStatementAlias(_ *parser.SupportedSetStatementAliasContext) {- l.result = base.DDL-}--// EnterSupportedUnsetStatementAlias is called for UNSET statements.-func (l *queryTypeListener) EnterSupportedUnsetStatementAlias(_ *parser.SupportedUnsetStatementAliasContext) {- l.result = base.DDL-}--// EnterSupportedRefreshStatementAlias is called for REFRESH statements.-func (l *queryTypeListener) EnterSupportedRefreshStatementAlias(_ *parser.SupportedRefreshStatementAliasContext) {- l.result = base.DDL-}--// EnterSupportedCancelStatementAlias is called for CANCEL statements.-func (l *queryTypeListener) EnterSupportedCancelStatementAlias(_ *parser.SupportedCancelStatementAliasContext) {- l.result = base.DDL-}--// EnterSupportedRecoverStatementAlias is called for RECOVER statements.-func (l *queryTypeListener) EnterSupportedRecoverStatementAlias(_ *parser.SupportedRecoverStatementAliasContext) {- l.result = base.DDL-}--// EnterSupportedCleanStatementAlias is called for CLEAN statements.-func (l *queryTypeListener) EnterSupportedCleanStatementAlias(_ *parser.SupportedCleanStatementAliasContext) {- l.result = base.DDL-}--// EnterSupportedOtherStatementAlias is called for other statements not covered above.-func (l *queryTypeListener) EnterSupportedOtherStatementAlias(_ *parser.SupportedOtherStatementAliasContext) {- l.result = base.DDL-}--// EnterCallProcedure is called for CALL stored procedure statements.-func (l *queryTypeListener) EnterCallProcedure(_ *parser.CallProcedureContext) {- l.result = base.DML-}--// EnterSupportedStatsStatementAlias is called for stats statements.-func (l *queryTypeListener) EnterSupportedStatsStatementAlias(_ *parser.SupportedStatsStatementAliasContext) {- l.result = base.DDL-}--// The following are SHOW statements nested within other statement categories.-// They override the parent category's DDL setting with SelectInfoSchema.--// SHOW statements in supportedLoadStatement-func (l *queryTypeListener) EnterShowCreateLoad(_ *parser.ShowCreateLoadContext) {- l.result = base.SelectInfoSchema-}--func (l *queryTypeListener) EnterShowCreateRoutineLoad(_ *parser.ShowCreateRoutineLoadContext) {- l.result = base.SelectInfoSchema-}--func (l *queryTypeListener) EnterShowRoutineLoad(_ *parser.ShowRoutineLoadContext) {- l.result = base.SelectInfoSchema-}--func (l *queryTypeListener) EnterShowRoutineLoadTask(_ *parser.ShowRoutineLoadTaskContext) {- l.result = base.SelectInfoSchema-}--func (l *queryTypeListener) EnterShowIndexAnalyzer(_ *parser.ShowIndexAnalyzerContext) {- l.result = base.SelectInfoSchema-}--func (l *queryTypeListener) EnterShowIndexTokenizer(_ *parser.ShowIndexTokenizerContext) {- l.result = base.SelectInfoSchema-}--func (l *queryTypeListener) EnterShowIndexTokenFilter(_ *parser.ShowIndexTokenFilterContext) {- l.result = base.SelectInfoSchema-}--func (l *queryTypeListener) EnterShowIndexCharFilter(_ *parser.ShowIndexCharFilterContext) {- l.result = base.SelectInfoSchema-}--// SHOW statements in supportedAdminStatement-func (l *queryTypeListener) EnterAdminShowReplicaDistribution(_ *parser.AdminShowReplicaDistributionContext) {- l.result = base.SelectInfoSchema-}--func (l *queryTypeListener) EnterAdminShowReplicaStatus(_ *parser.AdminShowReplicaStatusContext) {- l.result = base.SelectInfoSchema-}--func (l *queryTypeListener) EnterAdminShowTabletStorageFormat(_ *parser.AdminShowTabletStorageFormatContext) {- l.result = base.SelectInfoSchema-}--// SHOW statements in supportedStatsStatement-func (l *queryTypeListener) EnterShowAnalyze(_ *parser.ShowAnalyzeContext) {- l.result = base.SelectInfoSchema-}--func (l *queryTypeListener) EnterShowQueuedAnalyzeJobs(_ *parser.ShowQueuedAnalyzeJobsContext) {- l.result = base.SelectInfoSchema-}--func (l *queryTypeListener) EnterShowColumnHistogramStats(_ *parser.ShowColumnHistogramStatsContext) {- l.result = base.SelectInfoSchema-}--func (l *queryTypeListener) EnterShowColumnStats(_ *parser.ShowColumnStatsContext) {- l.result = base.SelectInfoSchema-}--func (l *queryTypeListener) EnterShowAnalyzeTask(_ *parser.ShowAnalyzeTaskContext) {- l.result = base.SelectInfoSchema-}--func (l *queryTypeListener) EnterShowIndexStats(_ *parser.ShowIndexStatsContext) {- l.result = base.SelectInfoSchema-}--func (l *queryTypeListener) EnterShowTableStats(_ *parser.ShowTableStatsContext) {- l.result = base.SelectInfoSchema-}--// SHOW statements in materializedViewStatement-func (l *queryTypeListener) EnterShowCreateMTMV(_ *parser.ShowCreateMTMVContext) {- l.result = base.SelectInfoSchema-}--// SHOW statements in constraintStatement-func (l *queryTypeListener) EnterShowConstraint(_ *parser.ShowConstraintContext) {- l.result = base.SelectInfoSchema+ return analysis.QueryTypeDDL, true}diff --git a/backend/plugin/parser/doris/query_type_test.go b/backend/plugin/parser/doris/query_type_test.goindex 191683242f..8cd0396e16 100644--- a/backend/plugin/parser/doris/query_type_test.go+++ b/backend/plugin/parser/doris/query_type_test.go@@ -62,6 +62,28 @@ func TestGetQueryType(t *testing.T) {statement: "SHOW CREATE TABLE users",want: base.SelectInfoSchema,},+ {+ // EXPLAIN over a query is read-only.+ statement: "EXPLAIN SELECT * FROM users",+ want: base.Select,+ },+ {+ // EXPLAIN defers to the inner statement's type — EXPLAIN over+ // DML is classified as DML for ACL purposes, not Select.+ statement: "EXPLAIN INSERT INTO users (id) VALUES (1)",+ want: base.DML,+ },+ {+ // EXPLAIN over DDL is DDL, not a read-only downgrade.+ statement: "EXPLAIN DROP TABLE users",+ want: base.DDL,+ },+ {+ // USE is intentionally Unknown so ACL rejects it as a hard+ // deny rather than authorising it under bb.sql.ddl.+ statement: "USE db1",+ want: base.QueryTypeUnknown,+ },}for _, tc := range tests {diff --git a/backend/plugin/parser/doris/split.go b/backend/plugin/parser/doris/split.goindex 7ee49fc059..56c89e7085 100644--- a/backend/plugin/parser/doris/split.go+++ b/backend/plugin/parser/doris/split.go@@ -1,8 +1,9 @@package dorisimport (- "github.com/antlr4-go/antlr/v4"- parser "github.com/bytebase/parser/doris"+ "unicode/utf8"++ "github.com/bytebase/omni/doris/parser"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/parser/base"@@ -13,11 +14,78 @@ func init() {base.RegisterSplitterFunc(storepb.Engine_DORIS, SplitSQL)}-// SplitSQL splits the input into multiple SQL statements using semicolon as delimiter.+// SplitSQL splits the input into multiple SQL statements using the omni+// Doris splitter, then converts each Segment into a base.Statement with+// the position fields bytebase expects.+//+// Positions are computed in a single O(n) pass over the input.func SplitSQL(statement string) ([]base.Statement, error) {- lexer := parser.NewDorisLexer(antlr.NewInputStream(statement))- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)- stream.Fill()+ segs := parser.Split(statement)+ if len(segs) == 0 {+ return nil, nil+ }++ type pos struct{ line, col int }+ positions := make(map[int]pos, len(segs)*3)+ for _, seg := range segs {+ positions[seg.ByteStart] = pos{}+ positions[seg.ByteEnd] = pos{}+ // Also collect position immediately past the trailing ';' for End column.+ if seg.ByteEnd < len(statement) && statement[seg.ByteEnd] == ';' {+ positions[seg.ByteEnd+1] = pos{}+ }+ }++ line, col := 1, 1+ for i := 0; i <= len(statement); {+ if _, need := positions[i]; need {+ positions[i] = pos{line, col}+ }+ if i == len(statement) {+ break+ }+ r, size := utf8.DecodeRuneInString(statement[i:])+ if r == '\n' {+ line+++ col = 1+ } else {+ col+++ }+ i += size+ }- return base.SplitSQLByLexer(stream, parser.DorisLexerSEMICOLON)+ stmts := make([]base.Statement, 0, len(segs))+ for _, seg := range segs {+ // bytebase historically returns Text including the trailing ';' delimiter+ // (when present). omni's Segment.Text excludes it, so we re-attach when+ // the byte after ByteEnd is ';'.+ text := seg.Text+ end := seg.ByteEnd+ if end < len(statement) && statement[end] == ';' {+ text = statement[seg.ByteStart : end+1]+ end+++ }+ sp := positions[seg.ByteStart]+ ep, ok := positions[end]+ if !ok {+ ep = positions[seg.ByteEnd]+ }+ stmts = append(stmts, base.Statement{+ Text: text,+ Empty: seg.Empty(),+ Start: &storepb.Position{+ Line: int32(sp.line),+ Column: int32(sp.col),+ },+ End: &storepb.Position{+ Line: int32(ep.line),+ Column: int32(ep.col),+ },+ Range: &storepb.Range{+ Start: int32(seg.ByteStart),+ End: int32(end),+ },+ })+ }+ return stmts, nil}diff --git a/backend/plugin/parser/doris/statement_ranges.go b/backend/plugin/parser/doris/statement_ranges.goindex aff6d4b3f3..d99ec5ba62 100644--- a/backend/plugin/parser/doris/statement_ranges.go+++ b/backend/plugin/parser/doris/statement_ranges.go@@ -2,14 +2,15 @@ package dorisimport ("context"+ "strings"+ "unicode"+ "unicode/utf8"- "github.com/antlr4-go/antlr/v4"-- "github.com/bytebase/bytebase/backend/plugin/parser/base"-- parser "github.com/bytebase/parser/doris"+ protocol "github.com/bytebase/lsp-protocol"+ "github.com/bytebase/omni/doris/parser"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/parser/base")func init() {@@ -17,11 +18,251 @@ func init() {base.RegisterStatementRangesFunc(storepb.Engine_STARROCKS, GetStatementRanges)}+// GetStatementRanges returns the UTF-16 line/character ranges of statements+// in the input, formatted for the LSP protocol. Empty (whitespace-only)+// segments are skipped, and ranges include the trailing semicolon when present+// to match the prior ANTLR-based behaviour.func GetStatementRanges(_ context.Context, _ base.StatementRangeContext, statement string) ([]base.Range, error) {- createLexer := func(input antlr.CharStream) antlr.Lexer {- return parser.NewDorisLexer(input)+ trimmed := strings.TrimRightFunc(statement, unicode.IsSpace)++ segs := parser.Split(trimmed)+ if len(segs) == 0 {+ // omni's splitter drops comment-only buffers entirely. The legacy+ // ANTLR helper still emitted a range for trailing comment tokens, so+ // editor features that count statement positions don't lose ground+ // to a file that contains only `-- note` or `/* ... */`. Emit a+ // single range covering the non-whitespace span when present.+ return commentOnlyRanges(trimmed), nil+ }++ type pos struct{ line, char uint32 }+ wanted := make(map[int]pos, len(segs)*3)+ for _, seg := range segs {+ wanted[seg.ByteStart] = pos{}+ wanted[seg.ByteEnd] = pos{}+ if seg.ByteEnd < len(trimmed) && trimmed[seg.ByteEnd] == ';' {+ wanted[seg.ByteEnd+1] = pos{}+ }}- stream := base.PrepareANTLRTokenStream(statement, createLexer)- ranges := base.GetANTLRStatementRangesUTF16Position(stream, parser.DorisParserEOF, parser.DorisParserSEMICOLON)++ var line, char uint32+ for i := 0; i <= len(trimmed); {+ if _, need := wanted[i]; need {+ wanted[i] = pos{line, char}+ }+ if i == len(trimmed) {+ break+ }+ r, size := utf8.DecodeRuneInString(trimmed[i:])+ if r == '\n' {+ line+++ char = 0+ } else if r <= 0xFFFF {+ char+++ } else {+ char += 2 // UTF-16 surrogate pair+ }+ i += size+ }++ ranges := make([]base.Range, 0, len(segs))+ for _, seg := range segs {+ // Skip pure whitespace segments, but preserve comment-only segments+ // (the legacy ANTLR helper emits ranges for trailing comment tokens).+ if seg.Empty() && !hasNonWhitespace(trimmed[seg.ByteStart:seg.ByteEnd]) {+ continue+ }+ // Skip only leading whitespace — leading comments are kept inside the+ // range to match the prior ANTLR-helper behaviour, which includes+ // hidden-channel tokens but excludes pure whitespace.+ trimStart := seg.ByteStart+ for trimStart < seg.ByteEnd {+ c := trimmed[trimStart]+ if c == ' ' || c == '\t' || c == '\n' || c == '\r' {+ trimStart+++ continue+ }+ break+ }+ if _, ok := wanted[trimStart]; !ok {+ startPos := wanted[seg.ByteStart]+ l, c := startPos.line, startPos.char+ for i := seg.ByteStart; i < trimStart; {+ r, size := utf8.DecodeRuneInString(trimmed[i:])+ if r == '\n' {+ l+++ c = 0+ } else if r <= 0xFFFF {+ c+++ } else {+ c += 2+ }+ i += size+ }+ wanted[trimStart] = pos{l, c}+ }+ startPos, ok := wanted[trimStart]+ if !ok {+ // trimStart wasn't pre-collected; fall back to ByteStart.+ startPos = wanted[seg.ByteStart]+ }+ end := seg.ByteEnd+ if end < len(trimmed) && trimmed[end] == ';' {+ end+++ }+ endPos := wanted[end]+ ranges = append(ranges, base.Range{+ Start: protocol.Position{Line: startPos.line, Character: startPos.char},+ End: protocol.Position{Line: endPos.line, Character: endPos.char},+ })+ }+ // Trailing comment-only content: omni's Split filters out comment-only+ // segments, so any non-whitespace bytes between the last segment's end+ // and the end of the trimmed input would otherwise be lost. The legacy+ // ANTLR helper emits a final range for these — replicate that.+ if len(segs) > 0 {+ last := segs[len(segs)-1]+ tailStart := last.ByteEnd+ if tailStart < len(trimmed) && trimmed[tailStart] == ';' {+ tailStart+++ }+ // Find first non-whitespace byte after tailStart.+ i := tailStart+ for i < len(trimmed) {+ c := trimmed[i]+ if c != ' ' && c != '\t' && c != '\n' && c != '\r' {+ break+ }+ i+++ }+ if i < len(trimmed) {+ // There's a trailing comment / non-whitespace chunk. Emit a range.+ if _, ok := wanted[i]; !ok {+ startPos := wanted[tailStart]+ l, c := startPos.line, startPos.char+ for j := tailStart; j < i; {+ r, size := utf8.DecodeRuneInString(trimmed[j:])+ if r == '\n' {+ l+++ c = 0+ } else if r <= 0xFFFF {+ c+++ } else {+ c += 2+ }+ j += size+ }+ wanted[i] = pos{l, c}+ }+ startPos := wanted[i]+ endPos := pos{startPos.line, startPos.char}+ for j := i; j < len(trimmed); {+ r, size := utf8.DecodeRuneInString(trimmed[j:])+ if r == '\n' {+ endPos.line+++ endPos.char = 0+ } else if r <= 0xFFFF {+ endPos.char+++ } else {+ endPos.char += 2+ }+ j += size+ }+ ranges = append(ranges, base.Range{+ Start: protocol.Position{Line: startPos.line, Character: startPos.char},+ End: protocol.Position{Line: endPos.line, Character: endPos.char},+ })+ }+ }+return ranges, nil}++// commentOnlyRanges emits a single Range covering the non-whitespace span of+// `trimmed`, or nil when nothing in `trimmed` looks like a comment. Used+// when the splitter finds zero segments — typically that means comment-only+// input like `-- note` or `/* ... */`, which the legacy ANTLR helper would+// have emitted a range for. Pure delimiters (e.g. a single `;`) are NOT+// turned into ranges; the legacy helper dropped single-terminator inputs.+func commentOnlyRanges(trimmed string) []base.Range {+ if !containsCommentMarker(trimmed) {+ return nil+ }+ start := 0+ for start < len(trimmed) {+ c := trimmed[start]+ if c == ' ' || c == '\t' || c == '\n' || c == '\r' {+ start+++ continue+ }+ break+ }+ if start >= len(trimmed) {+ return nil+ }+ var startLine, startChar uint32+ for i := 0; i < start; {+ r, size := utf8.DecodeRuneInString(trimmed[i:])+ if r == '\n' {+ startLine+++ startChar = 0+ } else if r <= 0xFFFF {+ startChar+++ } else {+ startChar += 2+ }+ i += size+ }+ endLine, endChar := startLine, startChar+ for i := start; i < len(trimmed); {+ r, size := utf8.DecodeRuneInString(trimmed[i:])+ if r == '\n' {+ endLine+++ endChar = 0+ } else if r <= 0xFFFF {+ endChar+++ } else {+ endChar += 2+ }+ i += size+ }+ return []base.Range{{+ Start: protocol.Position{Line: startLine, Character: startChar},+ End: protocol.Position{Line: endLine, Character: endChar},+ }}+}++// containsCommentMarker reports whether s contains one of the SQL comment+// introducers (`--`, `/*`, `#`). Anything inside a string literal could+// false-positive, but the splitter only routes us here when there are+// no segments at all — i.e. no string literals to worry about.+func containsCommentMarker(s string) bool {+ for i := 0; i < len(s); i++ {+ c := s[i]+ if c == '#' {+ return true+ }+ if i+1 < len(s) {+ if c == '-' && s[i+1] == '-' {+ return true+ }+ if c == '/' && s[i+1] == '*' {+ return true+ }+ }+ }+ return false+}++// hasNonWhitespace reports whether s contains any non-whitespace character.+// Used to detect comment-only segments (omni's Segment.Empty() treats them+// as empty since they contain no SQL tokens, but bytebase wants their ranges).+func hasNonWhitespace(s string) bool {+ for i := 0; i < len(s); i++ {+ c := s[i]+ if c != ' ' && c != '\t' && c != '\n' && c != '\r' {+ return true+ }+ }+ return false+}diff --git a/backend/plugin/parser/doris/test-data/statement-ranges/standard.yaml b/backend/plugin/parser/doris/test-data/statement-ranges/standard.yamlindex ef84fc8605..184da30370 100644--- a/backend/plugin/parser/doris/test-data/statement-ranges/standard.yaml+++ b/backend/plugin/parser/doris/test-data/statement-ranges/standard.yaml@@ -212,3 +212,19 @@character: 36- statement: ;- statement: ;;+- statement: -- just a comment+ ranges:+ - start:+ line: 0+ character: 0+ end:+ line: 0+ character: 17+- statement: /* block comment */+ ranges:+ - start:+ line: 0+ character: 0+ end:+ line: 0+ character: 19