Skip to content

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

fix(pg): make DML transform position mapping linear (#20564)

rebelice390e1079a266
diff --git a/backend/plugin/parser/pg/backup.go b/backend/plugin/parser/pg/backup.go
index aa263d16f5..43278d5e51 100644
--- a/backend/plugin/parser/pg/backup.go
+++ b/backend/plugin/parser/pg/backup.go
@@ -289,6 +289,9 @@ func prepareTransformation(ctx context.Context, tCtx base.TransformContext, stat
searchPath := metadata.GetSearchPath()
var dmls []statementInfo
+ // Node locations ascend across statements, so a single incremental mapper
+ // converts all offsets in one pass instead of rescanning from byte 0.
+ posMapper := base.NewByteOffsetPositionMapper(statement)
for i, stmt := range stmts {
if stmt.Empty() {
@@ -320,9 +323,9 @@ func prepareTransformation(ctx context.Context, tCtx base.TransformContext, stat
}
table.StatementType = StatementTypeUpdate
nodeLoc := n.Loc
- startPos := ByteOffsetToRunePosition(statement, nodeLoc.Start)
+ startPos := posMapper.Position(nodeLoc.Start)
startPos.Column-- // 0-based column to match existing convention
- endPos := ByteOffsetToRunePosition(statement, nodeLoc.End)
+ endPos := posMapper.Position(nodeLoc.End)
dmls = append(dmls, statementInfo{
offset: i,
statement: stmt.Text,
@@ -342,9 +345,9 @@ func prepareTransformation(ctx context.Context, tCtx base.TransformContext, stat
}
table.StatementType = StatementTypeDelete
nodeLoc := n.Loc
- startPos := ByteOffsetToRunePosition(statement, nodeLoc.Start)
+ startPos := posMapper.Position(nodeLoc.Start)
startPos.Column--
- endPos := ByteOffsetToRunePosition(statement, nodeLoc.End)
+ endPos := posMapper.Position(nodeLoc.End)
dmls = append(dmls, statementInfo{
offset: i,
statement: stmt.Text,
diff --git a/backend/plugin/parser/pg/omni.go b/backend/plugin/parser/pg/omni.go
index 3924891516..e3d1584513 100644
--- a/backend/plugin/parser/pg/omni.go
+++ b/backend/plugin/parser/pg/omni.go
@@ -1,8 +1,6 @@
package pg
import (
- "unicode/utf8"
-
omnipg "github.com/bytebase/omni/pg"
"github.com/bytebase/omni/pg/ast"
@@ -47,26 +45,5 @@ func GetOmniNode(a base.AST) (ast.Node, bool) {
// ByteOffsetToRunePosition converts a byte offset in sql to a 1-based line:column
// where column is measured in Unicode code points (runes), matching storepb.Position semantics.
func ByteOffsetToRunePosition(sql string, byteOffset int) *storepb.Position {
- if byteOffset > len(sql) {
- byteOffset = len(sql)
- }
-
- line := int32(1)
- runeCol := int32(0) // 0-based rune count on current line
- i := 0
- for i < byteOffset {
- r, size := utf8.DecodeRuneInString(sql[i:])
- if r == '\n' {
- line++
- runeCol = 0
- } else {
- runeCol++
- }
- i += size
- }
-
- return &storepb.Position{
- Line: line,
- Column: runeCol + 1, // convert to 1-based
- }
+ return base.NewByteOffsetPositionMapper(sql).Position(byteOffset)
}
diff --git a/backend/plugin/parser/pg/restore.go b/backend/plugin/parser/pg/restore.go
index 1e62a3026f..acb2cf934f 100644
--- a/backend/plugin/parser/pg/restore.go
+++ b/backend/plugin/parser/pg/restore.go
@@ -63,6 +63,7 @@ func findStatementAtPosition(statement string, backupItem *storepb.PriorBackupDe
return nil, errors.Wrap(err, "failed to parse statement")
}
+ posMapper := base.NewByteOffsetPositionMapper(statement)
for _, stmt := range stmts {
if stmt.Empty() {
continue
@@ -70,9 +71,9 @@ func findStatementAtPosition(statement string, backupItem *storepb.PriorBackupDe
switch stmt.AST.(type) {
case *ast.UpdateStmt, *ast.DeleteStmt:
nodeLoc := ast.NodeLoc(stmt.AST)
- startPos := ByteOffsetToRunePosition(statement, nodeLoc.Start)
+ startPos := posMapper.Position(nodeLoc.Start)
startPos.Column-- // 0-based to match backup convention
- endPos := ByteOffsetToRunePosition(statement, nodeLoc.End)
+ endPos := posMapper.Position(nodeLoc.End)
if inRange(startPos, endPos, backupItem.StartPosition, backupItem.EndPosition) {
return stmt.AST, nil
}
@@ -262,6 +263,7 @@ func extractStatement(statement string, backupItem *storepb.PriorBackupDetail_It
}
var sqls []string
+ posMapper := base.NewByteOffsetPositionMapper(statement)
for _, stmt := range stmts {
if stmt.Empty() {
continue
@@ -269,9 +271,9 @@ func extractStatement(statement string, backupItem *storepb.PriorBackupDetail_It
switch stmt.AST.(type) {
case *ast.UpdateStmt, *ast.DeleteStmt:
nodeLoc := ast.NodeLoc(stmt.AST)
- startPos := ByteOffsetToRunePosition(statement, nodeLoc.Start)
+ startPos := posMapper.Position(nodeLoc.Start)
startPos.Column-- // 0-based
- endPos := ByteOffsetToRunePosition(statement, nodeLoc.End)
+ endPos := posMapper.Position(nodeLoc.End)
if inRange(startPos, endPos, backupItem.StartPosition, backupItem.EndPosition) {
// Extract statement text without trailing semicolon/whitespace.
text := strings.TrimRight(strings.TrimSpace(stmt.Text), ";")