Skip to content

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

feat: effective splitter for PostgreSQL (#10359)

rebelice0be735d65ce9
diff --git a/backend/plugin/parser/mysql/split.go b/backend/plugin/parser/mysql/split.go
index 6b51b1ca73..b454cd7d20 100644
--- a/backend/plugin/parser/mysql/split.go
+++ b/backend/plugin/parser/mysql/split.go
@@ -103,7 +103,6 @@ func splitByParser(lexer *parser.MySQLLexer, stream *antlr.CommonTokenStream) ([
type openParenthesis struct {
tokenType int
pos int
- rightPos int
}
func splitMySQLStatement(stream *antlr.CommonTokenStream) ([]base.SingleSQL, error) {
@@ -194,9 +193,7 @@ func splitMySQLStatement(stream *antlr.CommonTokenStream) ([]base.SingleSQL, err
if len(ifStack) == 0 {
return nil, errors.New("invalid statement: failed to split multiple statements")
}
- ifStack[0].rightPos = i
- popIf := ifStack[len(ifStack)-1]
- semicolonStack = popSemicolonStack(semicolonStack, popIf)
+ semicolonStack = popSemicolonStack(semicolonStack, ifStack[0].pos)
ifStack = ifStack[:len(ifStack)-1]
case parser.MySQLParserLOOP_SYMBOL:
// For the LOOP symbol, MySQL only has LOOP with END LOOP statement.
@@ -205,9 +202,7 @@ func splitMySQLStatement(stream *antlr.CommonTokenStream) ([]base.SingleSQL, err
if len(loopStack) == 0 {
return nil, errors.New("invalid statement: failed to split multiple statements")
}
- loopStack[len(loopStack)-1].rightPos = i
- popLoop := loopStack[len(loopStack)-1]
- semicolonStack = popSemicolonStack(semicolonStack, popLoop)
+ semicolonStack = popSemicolonStack(semicolonStack, loopStack[len(loopStack)-1].pos)
loopStack = loopStack[:len(loopStack)-1]
case parser.MySQLParserWHILE_SYMBOL:
// For the WHILE symbol, MySQL only has WHILE with END WHILE statement.
@@ -216,9 +211,7 @@ func splitMySQLStatement(stream *antlr.CommonTokenStream) ([]base.SingleSQL, err
if len(whileStack) == 0 {
return nil, errors.New("invalid statement: failed to split multiple statements")
}
- whileStack[len(whileStack)-1].rightPos = i
- popWile := whileStack[len(whileStack)-1]
- semicolonStack = popSemicolonStack(semicolonStack, popWile)
+ semicolonStack = popSemicolonStack(semicolonStack, whileStack[len(whileStack)-1].pos)
whileStack = whileStack[:len(whileStack)-1]
case parser.MySQLParserREPEAT_SYMBOL:
// The are two types of REPEAT statement:
@@ -228,26 +221,20 @@ func splitMySQLStatement(stream *antlr.CommonTokenStream) ([]base.SingleSQL, err
if len(repeatStack) == 0 {
return nil, errors.New("invalid statement: failed to split multiple statements")
}
- repeatStack[0].rightPos = i
- popRepeat := repeatStack[len(repeatStack)-1]
- semicolonStack = popSemicolonStack(semicolonStack, popRepeat)
+ semicolonStack = popSemicolonStack(semicolonStack, repeatStack[0].pos)
repeatStack = repeatStack[:len(repeatStack)-1]
case parser.MySQLParserCASE_SYMBOL:
if len(beginCaseStack) == 0 {
return nil, errors.New("invalid statement: failed to split multiple statements")
}
- beginCaseStack[len(beginCaseStack)-1].rightPos = i
- popCase := beginCaseStack[len(beginCaseStack)-1]
- semicolonStack = popSemicolonStack(semicolonStack, popCase)
+ semicolonStack = popSemicolonStack(semicolonStack, beginCaseStack[len(beginCaseStack)-1].pos)
beginCaseStack = beginCaseStack[:len(beginCaseStack)-1]
default:
// is BEGIN ... END or CASE .. END case
if len(beginCaseStack) == 0 {
return nil, errors.New("invalid statement: failed to split multiple statements")
}
- beginCaseStack[len(beginCaseStack)-1].rightPos = i
- popCase := beginCaseStack[len(beginCaseStack)-1]
- semicolonStack = popSemicolonStack(semicolonStack, popCase)
+ semicolonStack = popSemicolonStack(semicolonStack, beginCaseStack[len(beginCaseStack)-1].pos)
beginCaseStack = beginCaseStack[:len(beginCaseStack)-1]
}
case parser.MySQLParserSEMICOLON_SYMBOL:
@@ -291,13 +278,13 @@ func splitMySQLStatement(stream *antlr.CommonTokenStream) ([]base.SingleSQL, err
return result, nil
}
-func popSemicolonStack(stack []int, node *openParenthesis) []int {
- if len(stack) == 0 || node.rightPos == 0 {
+func popSemicolonStack(stack []int, openParPos int) []int {
+ if len(stack) == 0 {
return stack
}
for i := len(stack) - 1; i >= 0; i-- {
- if stack[i] < node.pos {
+ if stack[i] < openParPos {
return stack[:i+1]
}
}
diff --git a/backend/plugin/parser/pg/split.go b/backend/plugin/parser/pg/split.go
index b8bd611a25..ad8e045bb0 100644
--- a/backend/plugin/parser/pg/split.go
+++ b/backend/plugin/parser/pg/split.go
@@ -1,6 +1,8 @@
package pg
import (
+ "log/slog"
+
"github.com/antlr4-go/antlr/v4"
"github.com/pkg/errors"
@@ -20,130 +22,208 @@ func init() {
func SplitSQL(statement string) ([]base.SingleSQL, error) {
lexer := parser.NewPostgreSQLLexer(antlr.NewInputStream(statement))
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- return splitSQLImpl(stream)
+ list, err := splitSQLImpl(stream)
+ if err != nil {
+ slog.Info("failed to split PostgreSQL statement", "statement", statement)
+ // Use parser to split statement.
+ return splitByParser(lexer, stream)
+ }
+ return list, nil
}
-func splitSQLImpl(stream *antlr.CommonTokenStream) ([]base.SingleSQL, error) {
+func splitByParser(lexer *parser.PostgreSQLLexer, stream *antlr.CommonTokenStream) ([]base.SingleSQL, error) {
+ p := parser.NewPostgreSQLParser(stream)
+ lexerErrorListener := &base.ParseErrorListener{}
+ lexer.RemoveErrorListeners()
+ lexer.AddErrorListener(lexerErrorListener)
+
+ parserErrorListener := &base.ParseErrorListener{}
+ p.RemoveErrorListeners()
+ p.AddErrorListener(parserErrorListener)
+
+ p.BuildParseTrees = true
+ p.SetErrorHandler(antlr.NewBailErrorStrategy())
+
+ tree := p.Root()
+ if lexerErrorListener.Err != nil {
+ return nil, lexerErrorListener.Err
+ }
+
+ if parserErrorListener.Err != nil {
+ return nil, parserErrorListener.Err
+ }
+
+ if tree == nil || tree.Stmtblock() == nil || tree.Stmtblock().Stmtmulti() == nil {
+ return nil, errors.New("failed to split multiple statements")
+ }
+
var result []base.SingleSQL
- stream.Fill()
tokens := stream.GetAllTokens()
+
start := 0
- // Splitting multiple statements by semicolon symbol should consider the special case.
- // For create function/procedure statements, the semicolon symbol is used as part of the statement.
- // We should skip the semicolon symbol between the "BEGIN ATOMIC" and "END" keywords.
- // So this is a parenthesis matching problem.
- type openParenthesis struct {
- tokenType int
- pos int
+ for _, semi := range tree.Stmtblock().Stmtmulti().AllSEMI() {
+ pos := semi.GetSymbol().GetStart()
+ line, col := base.FirstDefaultChannelTokenPosition(tokens[start : pos+1])
+ // From antlr4, the line is ONE based, and the column is ZERO based.
+ // So we should minus 1 for the line.
+ result = append(result, base.SingleSQL{
+ Text: stream.GetTextFromTokens(tokens[start], tokens[pos]),
+ BaseLine: tokens[start].GetLine() - 1,
+ LastLine: tokens[pos].GetLine() - 1,
+ LastColumn: tokens[pos].GetColumn(),
+ FirstStatementLine: line,
+ FirstStatementColumn: col,
+ Empty: base.IsEmpty(tokens[start:pos+1], parser.PostgreSQLParserSEMI),
+ })
+ start = pos + 1
}
- var stack []openParenthesis
- for i := 0; i < len(tokens); i++ {
- switch tokens[i].GetTokenType() {
- case parser.PostgreSQLLexerBEGIN_P:
+ // For the last statement, it may not end with semicolon symbol, EOF symbol instead.
+ eofPos := len(tokens) - 1
+ if start < eofPos {
+ line, col := base.FirstDefaultChannelTokenPosition(tokens[start:])
+ // From antlr4, the line is ONE based, and the column is ZERO based.
+ // So we should minus 1 for the line.
+ result = append(result, base.SingleSQL{
+ Text: stream.GetTextFromTokens(tokens[start], tokens[eofPos-1]),
+ BaseLine: tokens[start].GetLine() - 1,
+ LastLine: tokens[eofPos-1].GetLine() - 1,
+ LastColumn: tokens[eofPos-1].GetColumn(),
+ FirstStatementLine: line,
+ FirstStatementColumn: col,
+ Empty: base.IsEmpty(tokens[start:eofPos], parser.PostgreSQLParserSEMI),
+ })
+ }
+ return result, nil
+}
+
+type openParenthesis struct {
+ tokenType int
+ pos int
+}
+
+func splitSQLImpl(stream *antlr.CommonTokenStream) ([]base.SingleSQL, error) {
+ var result []base.SingleSQL
+ stream.Fill()
+ tokens := stream.GetAllTokens()
+
+ var beginCaseStack, ifStack, loopStack []openParenthesis
+ var semicolonStack []int
+
+ for i, token := range tokens {
+ switch token.GetTokenType() {
+ case parser.PostgreSQLParserBEGIN_P:
if isBeginTransaction(tokens, i) {
continue
}
- stack = append(stack, openParenthesis{
- tokenType: tokens[i].GetTokenType(),
+ beginCaseStack = append(beginCaseStack, openParenthesis{
+ tokenType: token.GetTokenType(),
pos: i,
})
- case parser.PostgreSQLLexerCASE:
- stack = append(stack, openParenthesis{
- tokenType: tokens[i].GetTokenType(),
+ case parser.PostgreSQLParserCASE:
+ beginCaseStack = append(beginCaseStack, openParenthesis{
+ tokenType: token.GetTokenType(),
pos: i,
})
- case parser.PostgreSQLLexerEND_P:
+ case parser.PostgreSQLParserIF_P:
+ ifStack = append(ifStack, openParenthesis{
+ tokenType: token.GetTokenType(),
+ pos: i,
+ })
+ case parser.PostgreSQLParserLOOP:
+ loopStack = append(loopStack, openParenthesis{
+ tokenType: token.GetTokenType(),
+ pos: i,
+ })
+ case parser.PostgreSQLParserSEMI:
+ semicolonStack = append(semicolonStack, i)
+ case parser.PostgreSQLParserEND_P:
if isEndTransaction(tokens, i) {
continue
}
- if len(stack) == 0 {
- return nil, errors.New("invalid statement: failed to split multiple statements")
- }
-
nextToken := base.GetDefaultChannelTokenType(tokens, i, 1)
switch nextToken {
- case parser.PostgreSQLLexerCASE:
- if stack[len(stack)-1].tokenType != parser.PostgreSQLLexerCASE {
- // Backtracking the process.
- i = stack[len(stack)-1].pos
- stack = stack[:len(stack)-1]
- continue
+ case parser.PostgreSQLParserIF_P:
+ if len(ifStack) == 0 {
+ return nil, errors.New("invalid statement: failed to split multiple statements")
}
- stack = stack[:len(stack)-1]
- continue
- case parser.PostgreSQLLexerIF_P:
- if stack[len(stack)-1].tokenType != parser.PostgreSQLLexerIF_P {
- // Backtracking the process.
- i = stack[len(stack)-1].pos
- stack = stack[:len(stack)-1]
- continue
+ // There are two cases:
+ // 1. The IF statement with END IF statement.
+ // 2. The IF statement without END IF statement.
+ // We should match the longest IF statement, so we should check the first IF statement in the stack.
+ semicolonStack = popSemicolonStack(semicolonStack, ifStack[0].pos)
+ ifStack = ifStack[:len(ifStack)-1]
+ case parser.PostgreSQLParserLOOP:
+ if len(loopStack) == 0 {
+ return nil, errors.New("invalid statement: failed to split multiple statements")
}
- stack = stack[:len(stack)-1]
- continue
- case parser.PostgreSQLLexerLOOP:
- if stack[len(stack)-1].tokenType != parser.PostgreSQLLexerLOOP {
- // Backtracking the process.
- i = stack[len(stack)-1].pos
- stack = stack[:len(stack)-1]
- continue
+ semicolonStack = popSemicolonStack(semicolonStack, loopStack[len(semicolonStack)-1].pos)
+ loopStack = loopStack[:len(loopStack)-1]
+ case parser.PostgreSQLParserCASE:
+ if len(beginCaseStack) == 0 {
+ return nil, errors.New("invalid statement: failed to split multiple statements")
}
- stack = stack[:len(stack)-1]
- continue
+ semicolonStack = popSemicolonStack(semicolonStack, beginCaseStack[len(beginCaseStack)-1].pos)
+ beginCaseStack = beginCaseStack[:len(beginCaseStack)-1]
default:
- leftTokenType := stack[len(stack)-1].tokenType
- if leftTokenType != parser.PostgreSQLLexerBEGIN_P && leftTokenType != parser.PostgreSQLLexerCASE {
- // Backtracking the process.
- i = stack[len(stack)-1].pos
- stack = stack[:len(stack)-1]
- continue
+ if len(beginCaseStack) == 0 {
+ return nil, errors.New("invalid statement: failed to split multiple statements")
}
- stack = stack[:len(stack)-1]
- }
- case parser.PostgreSQLLexerSEMI:
- if len(stack) > 0 {
- continue
+ semicolonStack = popSemicolonStack(semicolonStack, beginCaseStack[len(beginCaseStack)-1].pos)
+ beginCaseStack = beginCaseStack[:len(beginCaseStack)-1]
}
+ }
+ }
- line, col := base.FirstDefaultChannelTokenPosition(tokens[start:i])
- // From antlr4, the line is ONE based, and the column is ZERO based.
- // So we should minus 1 for the line.
- result = append(result, base.SingleSQL{
- Text: stream.GetTextFromTokens(tokens[start], tokens[i]),
- BaseLine: tokens[start].GetLine() - 1,
- LastLine: tokens[i].GetLine() - 1,
- LastColumn: tokens[i].GetColumn(),
- FirstStatementLine: line,
- FirstStatementColumn: col,
- Empty: base.IsEmpty(tokens[start:i+1], parser.PostgreSQLLexerSEMI),
- })
- start = i + 1
- case antlr.TokenEOF:
- if len(stack) > 0 {
- // Backtracking the process.
- i = stack[len(stack)-1].pos
- stack = stack[:len(stack)-1]
- continue
- }
+ start := 0
+ for _, pos := range semicolonStack {
+ line, col := base.FirstDefaultChannelTokenPosition(tokens[start : pos+1])
+ // From antlr4, the line is ONE based, and the column is ZERO based.
+ // So we should minus 1 for the line.
+ result = append(result, base.SingleSQL{
+ Text: stream.GetTextFromTokens(tokens[start], tokens[pos]),
+ BaseLine: tokens[start].GetLine() - 1,
+ LastLine: tokens[pos].GetLine() - 1,
+ LastColumn: tokens[pos].GetColumn(),
+ FirstStatementLine: line,
+ FirstStatementColumn: col,
+ Empty: base.IsEmpty(tokens[start:pos+1], parser.PostgreSQLParserSEMI),
+ })
+ start = pos + 1
+ }
+ // For the last statement, it may not end with semicolon symbol, EOF symbol instead.
+ eofPos := len(tokens) - 1
+ if start < eofPos {
+ line, col := base.FirstDefaultChannelTokenPosition(tokens[start:])
+ // From antlr4, the line is ONE based, and the column is ZERO based.
+ // So we should minus 1 for the line.
+ result = append(result, base.SingleSQL{
+ Text: stream.GetTextFromTokens(tokens[start], tokens[eofPos-1]),
+ BaseLine: tokens[start].GetLine() - 1,
+ LastLine: tokens[eofPos-1].GetLine() - 1,
+ LastColumn: tokens[eofPos-1].GetColumn(),
+ FirstStatementLine: line,
+ FirstStatementColumn: col,
+ Empty: base.IsEmpty(tokens[start:eofPos], parser.PostgreSQLParserSEMI),
+ })
+ }
- if start <= i-1 {
- line, col := base.FirstDefaultChannelTokenPosition(tokens[start:i])
- // From antlr4, the line is ONE based, and the column is ZERO based.
- // So we should minus 1 for the line.
- result = append(result, base.SingleSQL{
- Text: stream.GetTextFromTokens(tokens[start], tokens[i-1]),
- BaseLine: tokens[start].GetLine() - 1,
- LastLine: tokens[i-1].GetLine() - 1,
- LastColumn: tokens[i-1].GetColumn(),
- FirstStatementLine: line,
- FirstStatementColumn: col,
- Empty: base.IsEmpty(tokens[start:i], parser.PostgreSQLLexerSEMI),
- })
- }
+ return result, nil
+}
+
+func popSemicolonStack(semicolonStack []int, pos int) []int {
+ if len(semicolonStack) == 0 {
+ return semicolonStack
+ }
+
+ for i := len(semicolonStack) - 1; i >= 0; i-- {
+ if semicolonStack[i] < pos {
+ return semicolonStack[:i+1]
}
}
- return result, nil
+
+ return []int{}
}
func isEndTransaction(tokens []antlr.Token, index int) bool {
diff --git a/backend/plugin/parser/pg/split_test.go b/backend/plugin/parser/pg/split_test.go
index 750cb8453f..a80408d311 100644
--- a/backend/plugin/parser/pg/split_test.go
+++ b/backend/plugin/parser/pg/split_test.go
@@ -44,7 +44,7 @@ func TestPGSplitMultiSQL(t *testing.T) {
Text: "-- klsjdfjasldf\n\t\t\t-- klsjdflkjaskldfj\n\t\t\t",
BaseLine: 0,
FirstStatementLine: 2,
- FirstStatementColumn: 2,
+ FirstStatementColumn: 3,
LastLine: 2,
LastColumn: 2,
Empty: true,
@@ -67,7 +67,7 @@ func TestPGSplitMultiSQL(t *testing.T) {
LastLine: 1,
LastColumn: 3,
FirstStatementLine: 1,
- FirstStatementColumn: 3,
+ FirstStatementColumn: 16,
Empty: true,
},
},
@@ -87,7 +87,7 @@ func TestPGSplitMultiSQL(t *testing.T) {
{
Text: "\n\t\t\t/* sdfasdf */;",
FirstStatementLine: 1,
- FirstStatementColumn: 3,
+ FirstStatementColumn: 16,
LastLine: 1,
LastColumn: 16,
Empty: true,