Skip to content

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

feat(partiql): 4/4 replace ANTLR statement parser with omni Parse() (#19968)

h3n4laf1fba9436ad
diff --git a/backend/plugin/parser/partiql/partiql.go b/backend/plugin/parser/partiql/partiql.go
index d53347665c..d5c6bcf682 100644
--- a/backend/plugin/parser/partiql/partiql.go
+++ b/backend/plugin/parser/partiql/partiql.go
@@ -1,116 +1,71 @@
package partiql
import (
+ "errors"
"strings"
- "github.com/antlr4-go/antlr/v4"
-
- parser "github.com/bytebase/parser/partiql"
+ "github.com/bytebase/omni/partiql/ast"
+ "github.com/bytebase/omni/partiql/parser"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
- "github.com/bytebase/bytebase/backend/utils"
)
func init() {
base.RegisterParseStatementsFunc(storepb.Engine_DYNAMODB, parsePartiQLStatements)
}
+// omniAST wraps an omni AST node to implement the base.AST interface.
+type omniAST struct {
+ node ast.Node
+ startPos *storepb.Position
+}
+
+func (a *omniAST) ASTStartPosition() *storepb.Position {
+ return a.startPos
+}
+
// parsePartiQLStatements is the ParseStatementsFunc for PartiQL (DynamoDB).
// Returns []ParsedStatement with both text and AST populated.
func parsePartiQLStatements(statement string) ([]base.ParsedStatement, error) {
- // First split to get Statement with text and positions
+ // First split to get Statement with text and positions.
stmts, err := SplitSQL(statement)
if err != nil {
return nil, err
}
- // Then parse to get ASTs
- parseResults, err := ParsePartiQL(statement)
- if err != nil {
- return nil, err
- }
-
- // Combine: Statement provides text/positions, ANTLRAST provides AST
var result []base.ParsedStatement
- astIndex := 0
for _, stmt := range stmts {
ps := base.ParsedStatement{
Statement: stmt,
}
- if !stmt.Empty && astIndex < len(parseResults) {
- ps.AST = parseResults[astIndex]
- astIndex++
+ if !stmt.Empty {
+ if strings.TrimSpace(stmt.Text) == "" {
+ ps.Empty = true
+ } else {
+ // Parse the segment as-is so that ParseError byte offsets
+ // align with stmt.Text; convertParseError then offsets them
+ // by stmt.Start to refer back to the original script.
+ list, err := parser.Parse(stmt.Text)
+ if err != nil {
+ var parseErr *parser.ParseError
+ if errors.As(err, &parseErr) {
+ return nil, convertParseError(stmt.Text, parseErr, stmt.Start)
+ }
+ return nil, err
+ }
+ var node ast.Node
+ if len(list.Items) > 0 {
+ node = list.Items[0]
+ }
+ ps.AST = &omniAST{
+ node: node,
+ startPos: stmt.Start,
+ }
+ }
}
result = append(result, ps)
}
return result, nil
}
-
-// ParsePartiQL parses the given PartiQL statement by using antlr4. Returns a list of AST and token stream if no error.
-func ParsePartiQL(statement string) ([]*base.ANTLRAST, error) {
- stmts, err := SplitSQL(statement)
- if err != nil {
- return nil, err
- }
-
- var result []*base.ANTLRAST
- for _, stmt := range stmts {
- if stmt.Empty {
- continue
- }
-
- parseResult, err := parseSinglePartiQL(stmt.Text, stmt.BaseLine())
- if err != nil {
- return nil, err
- }
- result = append(result, parseResult)
- }
-
- return result, nil
-}
-
-func parseSinglePartiQL(statement string, baseLine int) (*base.ANTLRAST, error) {
- statement = strings.TrimRightFunc(statement, utils.IsSpaceOrSemicolon) + "\n;"
- inputStream := antlr.NewInputStream(statement)
- lexer := parser.NewPartiQLLexer(inputStream)
- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- p := parser.NewPartiQLParserParser(stream)
-
- // Remove default error listener and add our own error listener.
- startPosition := &storepb.Position{Line: int32(baseLine) + 1}
- lexer.RemoveErrorListeners()
- lexerErrorListener := &base.ParseErrorListener{
- Statement: statement,
- StartPosition: startPosition,
- }
- lexer.AddErrorListener(lexerErrorListener)
-
- p.RemoveErrorListeners()
- parserErrorListener := &base.ParseErrorListener{
- Statement: statement,
- StartPosition: startPosition,
- }
- p.AddErrorListener(parserErrorListener)
-
- p.BuildParseTrees = true
-
- tree := p.Script()
-
- if lexerErrorListener.Err != nil {
- return nil, lexerErrorListener.Err
- }
-
- if parserErrorListener.Err != nil {
- return nil, parserErrorListener.Err
- }
-
- result := &base.ANTLRAST{
- StartPosition: &storepb.Position{Line: int32(baseLine) + 1},
- Tree: tree,
- Tokens: stream,
- }
-
- return result, nil
-}
diff --git a/backend/plugin/parser/partiql/query.go b/backend/plugin/parser/partiql/query.go
index c339894327..f44094ca49 100644
--- a/backend/plugin/parser/partiql/query.go
+++ b/backend/plugin/parser/partiql/query.go
@@ -29,7 +29,7 @@ func validateQuery(statement string) (bool, bool, error) {
// syntactically valid but not read-only.
var parseErr *parser.ParseError
if errors.As(err, &parseErr) {
- return false, false, convertParseError(statement, parseErr)
+ return false, false, convertParseError(statement, parseErr, nil)
}
return false, false, nil
}
@@ -39,8 +39,20 @@ func validateQuery(statement string) (bool, bool, error) {
// 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 all omni parser adapters.
-func convertParseError(statement string, pe *parser.ParseError) *base.SyntaxError {
+//
+// 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
+ }
return &base.SyntaxError{
Position: &storepb.Position{
Line: int32(line),