Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: standardize Position to use 1-based line and column numbering (#17761)
Danny Xuc061d2a44a37
diff --git a/backend/common/position.go b/backend/common/position.goindex 2bab5ee28d..51e486608f 100644--- a/backend/common/position.go+++ b/backend/common/position.go@@ -2,10 +2,6 @@package commonimport (- "unicode/utf8"-- lsp "github.com/bytebase/lsp-protocol"-storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -19,45 +15,39 @@ type ANTLRPosition struct {}// ConvertANTLRPositionToPosition converts an ANTLRPosition to a Position in a given text.+// ANTLRPosition uses 1-based line and 0-based character column.+// Returns a Position with 1-based line and 1-based character column.// Returns the end Position if the ANTLRPosition is out of the end of text,-// returns the previous Position if the ANTLRPosition is in the middle of a character,// returns the previous Position if the ANTLRPosition is out of the end of a line.func ConvertANTLRPositionToPosition(a *ANTLRPosition, text string) *storepb.Position {- bs := []byte(text)- line := int32(0)- byteOffset := int32(0)- characterOffset := int32(0)+ runes := []rune(text)+ line := int32(1) // Start at 1 for 1-based line numbering+ charOffsetInLine := int32(0)+ globalCharOffset := int32(0)- for bi := 0; bi < len(bs); {- if line < a.Line-1 {- if bs[bi] == '\n' {+ for globalCharOffset < int32(len(runes)) {+ // Skip lines before the target line+ if line < a.Line {+ if runes[globalCharOffset] == '\n' {line++}- bi+++ globalCharOffset++continue}- if characterOffset >= a.Column || bs[bi] == '\n' {+ // Stop if we've reached the target column or hit a newline+ if charOffsetInLine >= a.Column || runes[globalCharOffset] == '\n' {break}- r, sz := utf8.DecodeRune(bs[bi:])- if r == utf8.RuneError {- break- }- // Avoid endless loop.- if sz == 0 {- break- }-- byteOffset += int32(sz)- bi += sz- characterOffset+++ charOffsetInLine+++ globalCharOffset++}+ // Convert from 0-based to 1-based columnreturn &storepb.Position{Line: line,- Column: byteOffset,+ Column: charOffsetInLine + 1,}}@@ -71,9 +61,6 @@ func ConvertANTLRLineToPosition(line int) *storepb.Position {}}-// UTF16Position is a position in a text expressed as zero-based line and zero-based column counted in UTF-16 code units.-type UTF16Position = lsp.Position-func ConvertTiDBParserErrorPositionToPosition(line, column int) *storepb.Position {if line < 1 {line = 1@@ -82,21 +69,27 @@ func ConvertTiDBParserErrorPositionToPosition(line, column int) *storepb.Positiocolumn = 1}+ // TiDB parser provides 1-based line and column+ // Store them as 1-based in Positionreturn &storepb.Position{- Line: int32(line) - 1,- Column: int32(column) - 1,+ Line: int32(line),+ Column: int32(column),}}func ConvertPGParserErrorCursorPosToPosition(cursorPos int, text string) *storepb.Position {+ // PostgreSQL cursorPos is 1-based character (rune) position.+ // Cursorpos points to the character WHERE the error is.+ // We need to count characters BEFORE the error position.if cursorPos >= 1 {cursorPos--}- line := 0+ line := 1 // Start at 1 for 1-based line numberingcolumn := 0rText := []rune(text)for i, r := range rText {- if i >= cursorPos {+ // Stop when we reach the error position+ if i == cursorPos {break}if r == '\n' {@@ -104,10 +97,12 @@ func ConvertPGParserErrorCursorPosToPosition(cursorPos int, text string) *storepcolumn = 0continue}- column += len(string(r))+ // Count characters (not bytes)+ column++}+ // Convert from 0-based to 1-based columnreturn &storepb.Position{Line: int32(line),- Column: int32(column),+ Column: int32(column + 1),}}diff --git a/backend/common/position_test.go b/backend/common/position_test.goindex 050462ceb3..a0d854e0bc 100644--- a/backend/common/position_test.go+++ b/backend/common/position_test.go@@ -17,28 +17,109 @@ func TestConvertANTLRPositionToPosition(t *testing.T) {want *storepb.Position}{{- description: "empty text",- text: "",+ description: "first line first column",+ text: "hello, world",antlrPosition: &ANTLRPosition{Line: 1, Column: 0},- want: &storepb.Position{Line: 0, Column: 0},+ want: &storepb.Position{Line: 1, Column: 1},},{- description: "ascii",+ description: "ASCII text",text: "hello, world",antlrPosition: &ANTLRPosition{Line: 1, Column: 6},- want: &storepb.Position{Line: 0, Column: 6},+ want: &storepb.Position{Line: 1, Column: 7}, // ANTLR column 6 (0-based) -> column 7 (1-based)},{- description: "multi-bytes characters",+ description: "multi-byte characters",text: "你好\n世界",antlrPosition: &ANTLRPosition{Line: 2, Column: 1},- want: &storepb.Position{Line: 1, Column: 3},+ want: &storepb.Position{Line: 2, Column: 2}, // Character-based, not byte-based+ },+ }++ for _, tc := range testCases {+ t.Run(tc.description, func(t *testing.T) {+ got := ConvertANTLRPositionToPosition(tc.antlrPosition, tc.text)+ require.Equal(t, tc.want, got)+ })+ }+}++func TestConvertPGParserErrorCursorPosToPosition(t *testing.T) {+ testCases := []struct {+ name string+ cursorPos int+ text string+ expectedLine int32+ expectedColumn int32+ }{+ {+ name: "ASCII single line",+ cursorPos: 8,+ text: "SELECT * FROM t",+ expectedLine: 1,+ expectedColumn: 8,+ },+ {+ name: "multi-byte characters",+ cursorPos: 8,+ text: "SELECT 你好 FROM t",+ expectedLine: 1,+ expectedColumn: 8, // Character-based (8th character is '你')+ },+ {+ name: "multi-line",+ cursorPos: 20,+ text: "SELECT * FROM t\nWHERE x = 1",+ expectedLine: 2,+ expectedColumn: 4, // 4th character on line 2+ },+ }++ for _, tc := range testCases {+ t.Run(tc.name, func(t *testing.T) {+ pos := ConvertPGParserErrorCursorPosToPosition(tc.cursorPos, tc.text)+ require.Equal(t, tc.expectedLine, pos.Line, "line mismatch")+ require.Equal(t, tc.expectedColumn, pos.Column, "column mismatch")+ })+ }+}++func TestConvertTiDBParserErrorPositionToPosition(t *testing.T) {+ testCases := []struct {+ name string+ line int+ column int+ expectedLine int32+ expectedCol int32+ }{+ {+ name: "normal position",+ line: 2,+ column: 10,+ expectedLine: 2,+ expectedCol: 10,+ },+ {+ name: "line less than 1",+ line: 0,+ column: 5,+ expectedLine: 1,+ expectedCol: 5,+ },+ {+ name: "column less than 1",+ line: 3,+ column: 0,+ expectedLine: 3,+ expectedCol: 1,},}- a := require.New(t)for _, tc := range testCases {- got := ConvertANTLRPositionToPosition(tc.antlrPosition, tc.text)- a.Equalf(tc.want, got, "Test case: %s", tc.description)+ t.Run(tc.name, func(t *testing.T) {+ pos := ConvertTiDBParserErrorPositionToPosition(tc.line, tc.column)+ require.Equal(t, tc.expectedLine, pos.Line, "line mismatch")+ require.Equal(t, tc.expectedCol, pos.Column, "column mismatch")+ })}}diff --git a/backend/component/sheet/sheet.go b/backend/component/sheet/sheet.goindex 354d6543b9..e57548f2c7 100644--- a/backend/component/sheet/sheet.go+++ b/backend/component/sheet/sheet.go@@ -505,7 +505,7 @@ func calculatePostgresErrorLine(statement string) int {for _, singleSQL := range singleSQLs {if _, err := pgrawparser.Parse(pgrawparser.ParseContext{}, singleSQL.Text); err != nil {- return int(singleSQL.End.GetLine()) + 1+ return int(singleSQL.End.GetLine())}}diff --git a/backend/generated-go/store/common.pb.go b/backend/generated-go/store/common.pb.goindex 71f00c4e73..87e6d3daf6 100644--- a/backend/generated-go/store/common.pb.go+++ b/backend/generated-go/store/common.pb.go@@ -524,12 +524,35 @@ func (x *PageToken) GetOffset() int32 {return 0}-// Position in a text expressed as one-based line and one-based column.+// Position in a text.+// Line is 0-based, Column is 1-based.+//+// Why this mixed numbering?+// - Line is 0-based to match LSP (https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position)+// and common programming practice (arrays/lists are 0-indexed)+// - Column is 1-based to match user expectations (text editors show "column 1" for the first character)+//+// Handling unknown positions:+// - If the entire position is unknown, leave this field as nil/undefined+// - If only line is known, set line and leave column as 0 (e.g., line=5, column=0)+// - If only column is known (rare), set column and leave line as 0+// Frontends should check for nil/undefined/zero values and handle them appropriately.+//+// When displaying to users, convert to 1-based: display "line X" as "line X+1".type Position struct {state protoimpl.MessageState `protogen:"open.v1"`- // Line position in a text (one-based).+ // Line position in a text (zero-based).+ // First line of the text is line 0, second line is line 1, etc.Line int32 `protobuf:"varint,1,opt,name=line,proto3" json:"line,omitempty"`// Column position in a text (one-based).+ // Column is measured in Unicode code points (characters/runes), not bytes or grapheme clusters.+ // First character of the line is column 1.+ // A value of 0 indicates the column information is unknown.+ //+ // Examples:+ // - "SELECT * FROM t" - column 8 is '*'+ // - "SELECT 你好 FROM t" - column 8 is '你' (even though it's at byte offset 7)+ // - "SELECT 😀 FROM t" - column 8 is '😀' (even though it's 4 bytes in UTF-8)Column int32 `protobuf:"varint,2,opt,name=column,proto3" json:"column,omitempty"`unknownFields protoimpl.UnknownFieldssizeCache protoimpl.SizeCachediff --git a/backend/generated-go/v1/common.pb.go b/backend/generated-go/v1/common.pb.goindex 841bf9aa95..c456d17e42 100644--- a/backend/generated-go/v1/common.pb.go+++ b/backend/generated-go/v1/common.pb.go@@ -505,12 +505,35 @@ func (RiskLevel) EnumDescriptor() ([]byte, []int) {return file_v1_common_proto_rawDescGZIP(), []int{6}}-// Position in a text expressed as one-based line and one-based column.+// Position in a text.+// Line is 0-based, Column is 1-based.+//+// Why this mixed numbering?+// - Line is 0-based to match LSP (https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position)+// and common programming practice (arrays/lists are 0-indexed)+// - Column is 1-based to match user expectations (text editors show "column 1" for the first character)+//+// Handling unknown positions:+// - If the entire position is unknown, leave this field as nil/undefined+// - If only line is known, set line and leave column as 0 (e.g., line=5, column=0)+// - If only column is known (rare), set column and leave line as 0+// Frontends should check for nil/undefined/zero values and handle them appropriately.+//+// When displaying to users, convert to 1-based: display "line X" as "line X+1".type Position struct {state protoimpl.MessageState `protogen:"open.v1"`- // Line position in a text (one-based).+ // Line position in a text (zero-based).+ // First line of the text is line 0, second line is line 1, etc.Line int32 `protobuf:"varint,1,opt,name=line,proto3" json:"line,omitempty"`// Column position in a text (one-based).+ // Column is measured in Unicode code points (characters/runes), not bytes or grapheme clusters.+ // First character of the line is column 1.+ // A value of 0 indicates the column information is unknown.+ //+ // Examples:+ // - "SELECT * FROM t" - column 8 is '*'+ // - "SELECT 你好 FROM t" - column 8 is '你' (even though it's at byte offset 7)+ // - "SELECT 😀 FROM t" - column 8 is '😀' (even though it's 4 bytes in UTF-8)Column int32 `protobuf:"varint,2,opt,name=column,proto3" json:"column,omitempty"`unknownFields protoimpl.UnknownFieldssizeCache protoimpl.SizeCachediff --git a/backend/plugin/advisor/mysql/test/naming_identifier_no_keyword.yaml b/backend/plugin/advisor/mysql/test/naming_identifier_no_keyword.yamlindex 334fab27d2..5257abdb8e 100644--- a/backend/plugin/advisor/mysql/test/naming_identifier_no_keyword.yaml+++ b/backend/plugin/advisor/mysql/test/naming_identifier_no_keyword.yaml@@ -6,10 +6,10 @@- status: 3code: 201title: Syntax error- content: "Syntax error at line 1:13 \nrelated text: CREATE TABLE interval"+ content: "Syntax error at line 1:14 \nrelated text: CREATE TABLE interval"startposition:line: 1- column: 13+ column: 14endposition: null- statement: CREATE TABLE execute(id int)changeType: 1diff --git a/backend/plugin/advisor/pg/test/statement_disallow_on_del_cascade.yaml b/backend/plugin/advisor/pg/test/statement_disallow_on_del_cascade.yamlindex bd5362fab3..c857849dd3 100644--- a/backend/plugin/advisor/pg/test/statement_disallow_on_del_cascade.yaml+++ b/backend/plugin/advisor/pg/test/statement_disallow_on_del_cascade.yaml@@ -13,8 +13,8 @@title: statement.disallow-on-del-cascadecontent: The CASCADE option is not permitted for ON DELETE clausesstartposition:- line: 0- column: 0+ line: 1+ column: 1endposition: null- statement: |-CREATE TABLE users (@@ -39,6 +39,6 @@title: statement.disallow-on-del-cascadecontent: The CASCADE option is not permitted for ON DELETE clausesstartposition:- line: 1- column: 0+ line: 2+ column: 1endposition: nulldiff --git a/backend/plugin/advisor/pg/test/statement_disallow_rm_tbl_cascade.yaml b/backend/plugin/advisor/pg/test/statement_disallow_rm_tbl_cascade.yamlindex 5da1d4e384..e0b22c910e 100644--- a/backend/plugin/advisor/pg/test/statement_disallow_rm_tbl_cascade.yaml+++ b/backend/plugin/advisor/pg/test/statement_disallow_rm_tbl_cascade.yaml@@ -6,8 +6,8 @@title: statement.disallow-rm-tbl-cascadecontent: The use of CASCADE is not permitted when removing a tablestartposition:- line: 0- column: 0+ line: 1+ column: 1endposition: null- statement: |-CREATE TABLE users (@@ -39,14 +39,14 @@title: statement.disallow-rm-tbl-cascadecontent: The use of CASCADE is not permitted when removing a tablestartposition:- line: 0- column: 0+ line: 1+ column: 1endposition: null- status: 2code: 213title: statement.disallow-rm-tbl-cascadecontent: The use of CASCADE is not permitted when removing a tablestartposition:- line: 0- column: 0+ line: 1+ column: 1endposition: nulldiff --git a/backend/plugin/parser/base/base.go b/backend/plugin/parser/base/base.goindex 1ac792ccf4..db6d1d9b04 100644--- a/backend/plugin/parser/base/base.go+++ b/backend/plugin/parser/base/base.go@@ -71,13 +71,18 @@ func (l *ParseErrorListener) SyntaxError(_ antlr.Recognizer, token any, line, coerrMessage = fmt.Sprintf("related text: %s", stream.GetTextFromInterval(antlr.NewInterval(start, stop)))}if l.Statement == "" {+ // ANTLR provides 1-based line and 0-based column+ // Store as 1-based line and 1-based column in Position+ posLine := int32(line + l.BaseLine)+ posColumn := int32(column + 1)l.Err = &SyntaxError{Position: &storepb.Position{- Line: int32(line + l.BaseLine),- Column: int32(column),+ Line: posLine,+ Column: posColumn,},RawMessage: message,- Message: fmt.Sprintf("Syntax error at line %d:%d \n%s", line+l.BaseLine, column, errMessage),+ // Display directly (already 1-based)+ Message: fmt.Sprintf("Syntax error at line %d:%d \n%s", posLine, posColumn, errMessage),}return}diff --git a/backend/plugin/parser/base/diagnose.go b/backend/plugin/parser/base/diagnose.goindex e1388c985d..13cab27291 100644--- a/backend/plugin/parser/base/diagnose.go+++ b/backend/plugin/parser/base/diagnose.go@@ -15,6 +15,8 @@ type DiagnoseContext struct {type Diagnostic = lsp.Diagnostic// convertPositionToUTF16Position converts a Position to a UTF16Position in a given text.+// Position uses 1-based line and 1-based character column.+// LSP Position uses 0-based line and 0-based UTF-16 code unit offset.// If the Position is nil, it returns a UTF16Position with line and character set to 0.// If the line in Position is out of the end of text, replace it with the last line.// If the column in Position is out of the end of line, replace it with the last column.@@ -26,21 +28,34 @@ func convertPositionToUTF16Position(p *storepb.Position, text string) *lsp.Posit}}lines := strings.Split(text, "\n")- lineNumber := p.Line+ // Convert from 1-based to 0-based line+ lineNumber := p.Line - 1+ if lineNumber < 0 {+ lineNumber = 0+ }if lineNumber >= int32(len(lines)) {lineNumber = int32(len(lines)) - 1}- u16CodeUnits := 0- byteOffset := 0++ // Convert from 1-based character column to 0-based UTF-16 code unitsline := lines[lineNumber]- for _, r := range line {- if byteOffset >= int(p.Column) {- break- }- byteOffset += len(string(r))+ runes := []rune(line)++ // p.Column is 1-based, convert to 0-based character offset+ charOffset := int(p.Column) - 1+ if charOffset < 0 {+ charOffset = 0+ }+ if charOffset > len(runes) {+ charOffset = len(runes)+ }++ // Count UTF-16 code units up to the character offset+ u16CodeUnits := 0+ for i := 0; i < charOffset && i < len(runes); i++ {u16CodeUnits++- if r > 0xFFFF {- // Need surrogate pair.+ if runes[i] > 0xFFFF {+ // Characters outside BMP need surrogate pairs in UTF-16u16CodeUnits++}}diff --git a/backend/plugin/parser/base/diagnose_test.go b/backend/plugin/parser/base/diagnose_test.goindex ab14683145..1d85b27167 100644--- a/backend/plugin/parser/base/diagnose_test.go+++ b/backend/plugin/parser/base/diagnose_test.go@@ -19,23 +19,67 @@ func TestConvertPositionToUTF16Position(t *testing.T) {{description: "empty text",text: "",- position: &storepb.Position{Line: 0, Column: 0},+ position: &storepb.Position{Line: 1, Column: 1},want: &lsp.Position{Line: 0, Character: 0},},{description: "ascii",text: "hello, world",- position: &storepb.Position{Line: 0, Column: 6},- want: &lsp.Position{Line: 0, Character: 6},+ position: &storepb.Position{Line: 1, Column: 6}, // 1-based: 6th character = ','+ want: &lsp.Position{Line: 0, Character: 5}, // 0-based UTF-16: character 5},{description: "surrogate pairs",// 𝄞 encoded in utf16 with 2 code units, and 4 code units in utf8.+ // "abc𝄞def" - characters: a(1) b(2) c(3) 𝄞(4) d(5) e(6) f(7)text: "abc𝄞def",- position: &storepb.Position{Line: 0, Column: 7},+ position: &storepb.Position{Line: 1, Column: 5}, // 1-based: 5th character = 'd'want: &lsp.Position{Line: 0,- Character: 5,+ Character: 5, // 0-based UTF-16: a=0, b=1, c=2, 𝄞=3,4 (surrogate pair), d=5+ },+ },+ {+ description: "Chinese characters",+ // Chinese characters are 3 bytes in UTF-8, 1 code unit in UTF-16+ // "SELECT 你好 FROM" - characters: S(1) E(2) L(3) E(4) C(5) T(6) SPACE(7) 你(8) 好(9) SPACE(10) F(11)...+ text: "SELECT 你好 FROM t",+ position: &storepb.Position{Line: 1, Column: 8}, // 1-based: 8th character = '你'+ want: &lsp.Position{+ Line: 0,+ Character: 7, // 0-based UTF-16: '你' is at character 7+ },+ },+ {+ description: "Mixed ASCII and Chinese",+ // "你好world" - characters: 你(1) 好(2) w(3) o(4) r(5) l(6) d(7)+ text: "你好world",+ position: &storepb.Position{Line: 1, Column: 3}, // 1-based: 3rd character = 'w'+ want: &lsp.Position{+ Line: 0,+ Character: 2, // 0-based UTF-16: 你=0, 好=1, w=2+ },+ },+ {+ description: "Emoji",+ // Emoji like 😀 are 4 bytes in UTF-8, 2 code units in UTF-16 (surrogate pair)+ // "SELECT 😀 FROM" - characters: S(1) E(2) L(3) E(4) C(5) T(6) SPACE(7) 😀(8) SPACE(9) F(10)...+ text: "SELECT 😀 FROM t",+ position: &storepb.Position{Line: 1, Column: 9}, // 1-based: 9th character = space after emoji+ want: &lsp.Position{+ Line: 0,+ Character: 9, // 0-based UTF-16: S=0...SPACE=6, 😀=7,8 (surrogate pair), SPACE=9+ },+ },+ {+ description: "Multi-line with Chinese",+ // Line 1: "SELECT 1;"+ // Line 2: "SELECT 你好"+ text: "SELECT 1;\nSELECT 你好",+ position: &storepb.Position{Line: 2, Column: 8}, // 1-based: line 2, 8th character = '你'+ want: &lsp.Position{+ Line: 1,+ Character: 7, // 0-based UTF-16: line 1 (second line), character 7},},}diff --git a/backend/plugin/parser/bigquery/split_test.go b/backend/plugin/parser/bigquery/split_test.goindex 2a1a4bdfdf..d412e9d694 100644--- a/backend/plugin/parser/bigquery/split_test.go+++ b/backend/plugin/parser/bigquery/split_test.go@@ -27,8 +27,8 @@ func TestBigQuerySplitMultiSQL(t *testing.T) {{Text: "SELECT 1",BaseLine: 0,- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 0, Column: 7},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 1, Column: 8},Empty: false,},},@@ -41,15 +41,15 @@ func TestBigQuerySplitMultiSQL(t *testing.T) {{Text: "SELECT 1;",BaseLine: 0,- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 0, Column: 8},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 1, Column: 9},Empty: false,},{Text: "\n SELECT\n 33;",BaseLine: 0,- Start: &storepb.Position{Line: 1, Column: 1},- End: &storepb.Position{Line: 2, Column: 3},+ Start: &storepb.Position{Line: 2, Column: 2},+ End: &storepb.Position{Line: 3, Column: 4},Empty: false,},},diff --git a/backend/plugin/parser/doris/doris_test.go b/backend/plugin/parser/doris/doris_test.goindex 8a62d11c55..08fe9c4ece 100644--- a/backend/plugin/parser/doris/doris_test.go+++ b/backend/plugin/parser/doris/doris_test.go@@ -20,7 +20,7 @@ func TestDorisSQLParser(t *testing.T) {},{statement: "SELECT a > (select max(a) from t1) FROM",- errorMessage: "Syntax error at line 0:39 \nrelated text: 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",},}diff --git a/backend/plugin/parser/mysql/completion.go b/backend/plugin/parser/mysql/completion.goindex b8a84135c2..003e371056 100644--- a/backend/plugin/parser/mysql/completion.go+++ b/backend/plugin/parser/mysql/completion.go@@ -1233,10 +1233,9 @@ func skipHeadingSQLs(statement string, caretLine int, caretOffset int) (string,return statement, caretLine, caretOffset}- caretLine-- // Convert to 0-based.-start := 0for i, sql := range list {+ // sql.End uses 1-based line and columnsqlEndLine := int(sql.End.GetLine())sqlEndColumn := int(sql.End.GetColumn())if sqlEndLine > caretLine || (sqlEndLine == caretLine && sqlEndColumn >= caretOffset) {@@ -1247,11 +1246,11 @@ func skipHeadingSQLs(statement string, caretLine int, caretOffset int) (string,}previousSQLEndLine := int(list[i-1].End.GetLine())previousSQLEndColumn := int(list[i-1].End.GetColumn())- newCaretLine = caretLine - previousSQLEndLine + 1 // Convert to 1-based.+ newCaretLine = caretLine - previousSQLEndLine + 1if caretLine == previousSQLEndLine {// The caret is in the same line as the last line of the previous SQL statement.// We need to adjust the caret offset.- newCaretOffset = caretOffset - previousSQLEndColumn - 1 // Convert to 0-based.+ newCaretOffset = caretOffset - previousSQLEndColumn}break}diff --git a/backend/plugin/parser/mysql/mysql.go b/backend/plugin/parser/mysql/mysql.goindex df331660a1..bb84552009 100644--- a/backend/plugin/parser/mysql/mysql.go+++ b/backend/plugin/parser/mysql/mysql.go@@ -241,7 +241,8 @@ func parseInputStream(input *antlr.InputStream, statement string) ([]*ParseResulTokens: tokens,BaseLine: s.BaseLine,})- baseLine = int(s.End.Line)+ // s.End.Line is 1-based, but baseLine should be 0-based+ baseLine = int(s.End.Line) - 1}return result, nildiff --git a/backend/plugin/parser/mysql/mysql_test.go b/backend/plugin/parser/mysql/mysql_test.goindex 2660ce8afe..b4f85d8ceb 100644--- a/backend/plugin/parser/mysql/mysql_test.go+++ b/backend/plugin/parser/mysql/mysql_test.go@@ -72,7 +72,11 @@ func TestMySQLParser(t *testing.T) {},{statement: "aaa",- errorMessage: "Syntax error at line 1:0 \nrelated text: aaa",+ errorMessage: "Syntax error at line 1:1 \nrelated text: aaa",+ },+ {+ statement: "select 1;\n selec 5;\nselect 6;",+ errorMessage: "Syntax error at line 2:4 \nrelated text: \n selec",},{statement: "select * from t;\n -- comments",diff --git a/backend/plugin/parser/mysql/split_test.go b/backend/plugin/parser/mysql/split_test.goindex 28f80ab5fe..ba01eddd48 100644--- a/backend/plugin/parser/mysql/split_test.go+++ b/backend/plugin/parser/mysql/split_test.go@@ -65,28 +65,28 @@ func TestMySQLSplitMultiSQL(t *testing.T) {END WHILE label1;END;`,BaseLine: 2,- Start: &storepb.Position{Line: 2, Column: 3},- End: &storepb.Position{Line: 9, Column: 7},+ Start: &storepb.Position{Line: 3, Column: 4},+ End: &storepb.Position{Line: 10, Column: 8},},{Text: ` CALL dorepeat(1000);`,BaseLine: 11,- Start: &storepb.Position{Line: 11, Column: 3},- End: &storepb.Position{Line: 11, Column: 22},+ Start: &storepb.Position{Line: 12, Column: 4},+ End: &storepb.Position{Line: 12, Column: 23},},{Text: `SELECT x;`,BaseLine: 11,- Start: &storepb.Position{Line: 12, Column: 3},- End: &storepb.Position{Line: 12, Column: 11},+ Start: &storepb.Position{Line: 13, Column: 4},+ End: &storepb.Position{Line: 13, Column: 12},},{Text: "\n\t\t\t",BaseLine: 12,// TODO(zp): Wait, but why the start position is larger than the end position?- Start: &storepb.Position{Line: 13, Column: 3},- End: &storepb.Position{Line: 13, Column: 2},+ Start: &storepb.Position{Line: 14, Column: 4},+ End: &storepb.Position{Line: 14, Column: 3},Empty: true,},},@@ -303,14 +303,14 @@ func TestMySQLSplitMultiSQL(t *testing.T) {'adult')FROM t;`,- Start: &storepb.Position{Line: 1, Column: 3},- End: &storepb.Position{Line: 102, Column: 9},+ Start: &storepb.Position{Line: 2, Column: 4},+ End: &storepb.Position{Line: 103, Column: 10},},{Text: " SELECT * FROM t;",BaseLine: 102,- Start: &storepb.Position{Line: 102, Column: 11},- End: &storepb.Position{Line: 102, Column: 26},+ Start: &storepb.Position{Line: 103, Column: 12},+ End: &storepb.Position{Line: 103, Column: 27},},},},@@ -321,13 +321,13 @@ func TestMySQLSplitMultiSQL(t *testing.T) {res: []base.SingleSQL{{Text: `select * from t;`,- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 0, Column: 15},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 1, Column: 16},},{Text: `select "\"" where true;`,- Start: &storepb.Position{Line: 0, Column: 16},- End: &storepb.Position{Line: 0, Column: 38},+ Start: &storepb.Position{Line: 1, Column: 17},+ End: &storepb.Position{Line: 1, Column: 39},},},},@@ -340,8 +340,8 @@ func TestMySQLSplitMultiSQL(t *testing.T) {res: []base.SingleSQL{{Text: "-- klsjdfjasldf\n\t\t\t-- klsjdflkjaskldfj\n",- Start: &storepb.Position{Line: 2, Column: 0},- End: &storepb.Position{Line: 1, Column: 22},+ Start: &storepb.Position{Line: 3, Column: 1},+ End: &storepb.Position{Line: 2, Column: 23},Empty: true,},},@@ -354,14 +354,14 @@ func TestMySQLSplitMultiSQL(t *testing.T) {res: []base.SingleSQL{{Text: `select * from t;`,- End: &storepb.Position{Line: 0, Column: 15},- Start: &storepb.Position{Line: 0, Column: 0},+ End: &storepb.Position{Line: 1, Column: 16},+ Start: &storepb.Position{Line: 1, Column: 1},},{Text: "\n\t\t\t/* sdfasdf */",// TODO(zp): Wait, but why the start position is larger than the end position?- End: &storepb.Position{Line: 1, Column: 3},- Start: &storepb.Position{Line: 1, Column: 16},+ End: &storepb.Position{Line: 2, Column: 4},+ Start: &storepb.Position{Line: 2, Column: 17},Empty: true,},},@@ -375,20 +375,20 @@ func TestMySQLSplitMultiSQL(t *testing.T) {res: []base.SingleSQL{{Text: `select * from t;`,- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 0, Column: 15},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 1, Column: 16},},{Text: "\n\t\t\t/* sdfasdf */;",- End: &storepb.Position{Line: 1, Column: 16},- Start: &storepb.Position{Line: 1, Column: 16},+ End: &storepb.Position{Line: 2, Column: 17},+ Start: &storepb.Position{Line: 2, Column: 17},Empty: true,},{Text: "\n\t\t\tselect * from t;",BaseLine: 1,- End: &storepb.Position{Line: 2, Column: 18},- Start: &storepb.Position{Line: 2, Column: 3},+ End: &storepb.Position{Line: 3, Column: 19},+ Start: &storepb.Position{Line: 3, Column: 4},},},},@@ -425,8 +425,8 @@ func TestMySQLSplitMultiSQL(t *testing.T) {RETURN income;END ;`,- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 13, Column: 6},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 14, Column: 7},},},},@@ -437,8 +437,8 @@ func TestMySQLSplitMultiSQL(t *testing.T) {res: []base.SingleSQL{{Text: bigSQL,- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 0, Column: int32(len(bigSQL) - 1)},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 1, Column: int32(len(bigSQL))},},},},@@ -449,13 +449,13 @@ func TestMySQLSplitMultiSQL(t *testing.T) {res: []base.SingleSQL{{Text: " CREATE TABLE t(a int);",- Start: &storepb.Position{Line: 0, Column: 4},- End: &storepb.Position{Line: 0, Column: 25},+ Start: &storepb.Position{Line: 1, Column: 5},+ End: &storepb.Position{Line: 1, Column: 26},},{Text: " CREATE TABLE t1(a int)",- Start: &storepb.Position{Line: 0, Column: 27},- End: &storepb.Position{Line: 0, Column: 48},+ Start: &storepb.Position{Line: 1, Column: 28},+ End: &storepb.Position{Line: 1, Column: 49},},},},@@ -467,13 +467,13 @@ func TestMySQLSplitMultiSQL(t *testing.T) {res: []base.SingleSQL{{Text: "CREATE TABLE `tech_Book`(id int, name varchar(255));",- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 0, Column: 51},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 1, Column: 52},},{Text: "\nINSERT INTO `tech_Book` VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafa\\'jdfl;\"ka');",- Start: &storepb.Position{Line: 1, Column: 0},- End: &storepb.Position{Line: 1, Column: 77},+ Start: &storepb.Position{Line: 2, Column: 1},+ End: &storepb.Position{Line: 2, Column: 78},},},},@@ -490,20 +490,20 @@ func TestMySQLSplitMultiSQL(t *testing.T) {res: []base.SingleSQL{{Text: "\n\t\t\t\t\t\t/* this is the comment. */\n\t\t\t\t\t\tCREATE /* inline comment */TABLE tech_Book(id int, name varchar(255));",- Start: &storepb.Position{Line: 2, Column: 6},- End: &storepb.Position{Line: 2, Column: 75},+ Start: &storepb.Position{Line: 3, Column: 7},+ End: &storepb.Position{Line: 3, Column: 76},},{Text: "\n\t\t\t\t\t\t-- this is the comment.\n\t\t\t\t\t\tINSERT INTO tech_Book VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafa\\'jdfl;\"ka');",- Start: &storepb.Position{Line: 4, Column: 6},+ Start: &storepb.Position{Line: 5, Column: 7},BaseLine: 2,- End: &storepb.Position{Line: 4, Column: 81},+ End: &storepb.Position{Line: 5, Column: 82},},{Text: "\n\t\t\t\t\t\t# this is the comment.\n\t\t\t\t\t\tINSERT INTO tech_Book VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafa\\'jdfl;\"ka');",BaseLine: 4,- Start: &storepb.Position{Line: 6, Column: 6},- End: &storepb.Position{Line: 6, Column: 81},+ Start: &storepb.Position{Line: 7, Column: 7},+ End: &storepb.Position{Line: 7, Column: 82},},},},@@ -523,27 +523,27 @@ func TestMySQLSplitMultiSQL(t *testing.T) {res: []base.SingleSQL{{Text: "# test for defining stored programs\n\t\t\t\t\t\tCREATE PROCEDURE dorepeat(p1 INT)\n\t\t\t\t\t\tBEGIN\n\t\t\t\t\t\t\tSET @x = 0;\n\t\t\t\t\t\t\tREPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;\n\t\t\t\t\t\tEND\n\t\t\t\t\t\t;",- Start: &storepb.Position{Line: 1, Column: 6},- End: &storepb.Position{Line: 6, Column: 6},+ Start: &storepb.Position{Line: 2, Column: 7},+ End: &storepb.Position{Line: 7, Column: 7},},{Text: "\n\t\t\t\t\t\tCALL dorepeat(1000);",BaseLine: 6,- Start: &storepb.Position{Line: 7, Column: 6},- End: &storepb.Position{Line: 7, Column: 25},+ Start: &storepb.Position{Line: 8, Column: 7},+ End: &storepb.Position{Line: 8, Column: 26},},{Text: "\n\t\t\t\t\t\tSELECT @x;",BaseLine: 7,- Start: &storepb.Position{Line: 8, Column: 6},- End: &storepb.Position{Line: 8, Column: 15},+ Start: &storepb.Position{Line: 9, Column: 7},+ End: &storepb.Position{Line: 9, Column: 16},},{Text: "\n\t\t\t\t\t\t",BaseLine: 8,- Start: &storepb.Position{Line: 9, Column: 6},+ Start: &storepb.Position{Line: 10, Column: 7},// TODO(zp): Wait, but why the start position is larger than the end position?- End: &storepb.Position{Line: 9, Column: 5},+ End: &storepb.Position{Line: 10, Column: 6},Empty: true,},},@@ -565,26 +565,26 @@ func TestMySQLSplitMultiSQL(t *testing.T) {res: []base.SingleSQL{{Text: "# test for defining stored programs\n\t\t\t\t\t\tCREATE PROCEDURE dorepeat(p1 INT)\n\t\t\t\t\t\t/* This is a comment */\n\t\t\t\t\t\tBEGIN\n\t\t\t\t\t\t\tSET @x = 0;\n\t\t\t\t\t\t\tREPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;\n\t\t\t\t\t\tEND\n\t\t\t\t\t\t;",- End: &storepb.Position{Line: 7, Column: 6},- Start: &storepb.Position{Line: 1, Column: 6},+ End: &storepb.Position{Line: 8, Column: 7},+ Start: &storepb.Position{Line: 2, Column: 7},},{Text: "\n\t\t\t\t\t\tCALL dorepeat(1000);",BaseLine: 7,- Start: &storepb.Position{Line: 8, Column: 6},- End: &storepb.Position{Line: 8, Column: 25},+ Start: &storepb.Position{Line: 9, Column: 7},+ End: &storepb.Position{Line: 9, Column: 26},},{Text: "\n\t\t\t\t\t\tSELECT @x;",BaseLine: 8,- Start: &storepb.Position{Line: 9, Column: 6},- End: &storepb.Position{Line: 9, Column: 15},+ Start: &storepb.Position{Line: 10, Column: 7},+ End: &storepb.Position{Line: 10, Column: 16},},{Text: "\n\t\t\t\t\t\t",BaseLine: 9,- Start: &storepb.Position{Line: 10, Column: 6},- End: &storepb.Position{Line: 10, Column: 5},+ Start: &storepb.Position{Line: 11, Column: 7},+ End: &storepb.Position{Line: 11, Column: 6},Empty: true,},},@@ -598,14 +598,14 @@ func TestMySQLSplitMultiSQL(t *testing.T) {{Text: "CREATE TABLE t\r\n(a int);",BaseLine: 0,- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 1, Column: 7},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 2, Column: 8},},{Text: "\r\nCREATE TABLE t1(b int);",BaseLine: 1,- Start: &storepb.Position{Line: 2, Column: 0},- End: &storepb.Position{Line: 2, Column: 22},+ Start: &storepb.Position{Line: 3, Column: 1},+ End: &storepb.Position{Line: 3, Column: 23},},},},diff --git a/backend/plugin/parser/mysql/test-data/test_backup.yaml b/backend/plugin/parser/mysql/test-data/test_backup.yamlindex 577b3e193f..cb3f4594ea 100644--- a/backend/plugin/parser/mysql/test-data/test_backup.yaml+++ b/backend/plugin/parser/mysql/test-data/test_backup.yaml@@ -47,11 +47,11 @@sourcetablename: testtargettablename: _rollback_test_dbstartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 17- column: 77+ line: 18+ column: 78- input: |-WITH t_cte AS (SELECT * FROM test WHERE test.b1 > 10@@ -66,11 +66,11 @@sourcetablename: testtargettablename: _rollback_test_dbstartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 2- column: 65+ line: 3+ column: 66- input: |-WITH t_cte AS (SELECT * FROM test WHERE test.b1 > 10@@ -85,11 +85,11 @@sourcetablename: testtargettablename: _rollback_test_dbstartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 2- column: 61+ line: 3+ column: 62- input: |-UPDATE test SET test.c1 = 1 WHERE test.b1 = 1;UPDATE test SET test.c1 = 2 WHERE test.b1 = 2;@@ -118,11 +118,11 @@sourcetablename: testtargettablename: _rollback_test_dbstartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 6- column: 45+ line: 7+ column: 46- input: UPDATE t_generated SET a = 1 WHERE a = 2;result:- statement: |-@@ -132,11 +132,11 @@sourcetablename: t_generatedtargettablename: _rollback_t_generated_dbstartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 40+ line: 1+ column: 41- input: |-UPDATE db1.t1 SET c1 = 1 WHERE c1 = 2;UPDATE db2.t2 SET c1 = 1 WHERE c1 = 2;@@ -148,11 +148,11 @@sourcetablename: t1targettablename: _rollback_t1_db1startposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 37+ line: 1+ column: 38- statement: |-CREATE TABLE `backupDB`.`_rollback_t2_db2` LIKE `db2`.`t2`;INSERT INTO `backupDB`.`_rollback_t2_db2` SELECT `t2`.* FROM db2.t2 WHERE c1 = 2;@@ -160,11 +160,11 @@sourcetablename: t2targettablename: _rollback_t2_db2startposition:- line: 1- column: 0+ line: 2+ column: 1endposition:- line: 1- column: 37+ line: 2+ column: 38- input: DELETE test FROM test, test2 as t2 where test.id = t2.id;result:- statement: |-@@ -174,11 +174,11 @@sourcetablename: testtargettablename: _rollback_test_dbstartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 56+ line: 1+ column: 57- input: DELETE t1, t2 FROM test as t1, test2 as t2 WHERE t1.id = t2.id;result:- statement: |-@@ -188,11 +188,11 @@sourcetablename: test2targettablename: _rollback_test2_dbstartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 62+ line: 1+ column: 63- statement: |-CREATE TABLE `backupDB`.`_rollback_test_db` LIKE `db`.`test`;INSERT INTO `backupDB`.`_rollback_test_db` SELECT `t1`.* FROM test as t1, test2 as t2 WHERE t1.id = t2.id;@@ -200,11 +200,11 @@sourcetablename: testtargettablename: _rollback_test_dbstartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 62+ line: 1+ column: 63- input: DELETE FROM t1, t2 USING test as t1, test2 as t2 WHERE t1.id = t2.id;result:- statement: |-@@ -214,11 +214,11 @@sourcetablename: test2targettablename: _rollback_test2_dbstartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 68+ line: 1+ column: 69- statement: |-CREATE TABLE `backupDB`.`_rollback_test_db` LIKE `db`.`test`;INSERT INTO `backupDB`.`_rollback_test_db` SELECT `t1`.* FROM test as t1, test2 as t2 WHERE t1.id = t2.id;@@ -226,11 +226,11 @@sourcetablename: testtargettablename: _rollback_test_dbstartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 68+ line: 1+ column: 69- input: DELETE FROM test as t1 WHERE t1.c1 = 1;result:- statement: |-@@ -240,11 +240,11 @@sourcetablename: testtargettablename: _rollback_test_dbstartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 38+ line: 1+ column: 39- input: DELETE FROM test WHERE c1 = 1;result:- statement: |-@@ -254,11 +254,11 @@sourcetablename: testtargettablename: _rollback_test_dbstartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 29+ line: 1+ column: 30- input: UPDATE test x SET x.c1 = 1 WHERE x.c1 = 1result:- statement: |-@@ -268,11 +268,11 @@sourcetablename: testtargettablename: _rollback_test_dbstartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 40+ line: 1+ column: 41- input: UPDATE test SET c1 = 1 WHERE c1=2;result:- statement: |-@@ -282,11 +282,11 @@sourcetablename: testtargettablename: _rollback_test_dbstartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 33+ line: 1+ column: 34- input: UPDATE test t1, test2 t2 SET t1.c1 = 1 WHERE t1.c1 = t2.c1;result:- statement: |-@@ -296,11 +296,11 @@sourcetablename: testtargettablename: _rollback_test_dbstartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 58+ line: 1+ column: 59- input: UPDATE test t1, test2 t2 SET t1.c1 = 1, t2.c2 = 2 WHERE t1.c1 = t2.c1;result:- statement: |-@@ -310,11 +310,11 @@sourcetablename: test2targettablename: _rollback_test2_dbstartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 69+ line: 1+ column: 70- statement: |-CREATE TABLE `backupDB`.`_rollback_test_db` LIKE `db`.`test`;INSERT INTO `backupDB`.`_rollback_test_db` SELECT `t1`.* FROM test t1, test2 t2 WHERE t1.c1 = t2.c1;@@ -322,11 +322,11 @@sourcetablename: testtargettablename: _rollback_test_dbstartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 69+ line: 1+ column: 70- input: |-UPDATE test t1 SET t1.c1 = 2 WHERE t1.c1 = 1 ;UPDATE test t2 SET t2.c1 = 3 WHERE t2.c1 = 5 ;@@ -340,8 +340,8 @@sourcetablename: testtargettablename: _rollback_test_dbstartposition:- line: 0- column: 0- endposition:line: 1- column: 45+ column: 1+ endposition:+ line: 2+ column: 46diff --git a/backend/plugin/parser/partiql/split_test.go b/backend/plugin/parser/partiql/split_test.goindex 515c5502e3..f61e303c85 100644--- a/backend/plugin/parser/partiql/split_test.go+++ b/backend/plugin/parser/partiql/split_test.go@@ -19,36 +19,36 @@ func TestSplitSQL(t *testing.T) {{Text: "DELETE FROM Music WHERE Artist = 'Pink Floyd' AND SongTitle = 'Money';",BaseLine: 0,- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 0, Column: 69},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 1, Column: 70},Empty: false,},{Text: "\n\tINSERT INTO Music VALUE {'AlbumTitle': 'The Dark Side of the Moon', 'Artist': 'Pink Floyd', 'Awards': 300, 'SongTitle': 'Money'};",BaseLine: 0,- Start: &storepb.Position{Line: 1, Column: 1},- End: &storepb.Position{Line: 1, Column: 129},+ Start: &storepb.Position{Line: 2, Column: 2},+ End: &storepb.Position{Line: 2, Column: 130},Empty: false,},{Text: "\n\tINSERT INTO Music VALUE {'AlbumTitle': 'The Dark Side of the Moon', 'Artist': 'Pink Floyd', 'Awards': 300, 'SongTitle': 'Money02'};",BaseLine: 1,- Start: &storepb.Position{Line: 2, Column: 1},- End: &storepb.Position{Line: 2, Column: 131},+ Start: &storepb.Position{Line: 3, Column: 2},+ End: &storepb.Position{Line: 3, Column: 132},Empty: false,},{Text: "\n\tINSERT INTO Music VALUE {'AlbumTitle': 'The Dark Side of the Moon', 'Artist': 'Pink Floyd', 'Awards': 300, 'SongTitle': 'Money03'};",BaseLine: 2,- Start: &storepb.Position{Line: 3, Column: 1},- End: &storepb.Position{Line: 3, Column: 131},+ Start: &storepb.Position{Line: 4, Column: 2},+ End: &storepb.Position{Line: 4, Column: 132},Empty: false,},{Text: "\n\tDELETE FROM Music WHERE Artist = 'Pink Floyd' AND SongTitle = 'Money02'",BaseLine: 3,- Start: &storepb.Position{Line: 4, Column: 1},- End: &storepb.Position{Line: 4, Column: 72},+ Start: &storepb.Position{Line: 5, Column: 2},+ End: &storepb.Position{Line: 5, Column: 73},Empty: false,},}diff --git a/backend/plugin/parser/pg/completion.go b/backend/plugin/parser/pg/completion.goindex f593aeb4cf..c25c0b09c3 100644--- a/backend/plugin/parser/pg/completion.go+++ b/backend/plugin/parser/pg/completion.go@@ -1210,10 +1210,9 @@ func skipHeadingSQLs(statement string, caretLine int, caretOffset int) (string,return statement, caretLine, caretOffset}- caretLine-- // Convert caretLine to 0-based.-start := 0for i, sql := range list {+ // sql.End uses 1-based line and columnsqlEndLine := int(sql.End.GetLine())sqlEndColumn := int(sql.End.GetColumn())if sqlEndLine > caretLine || (sqlEndLine == caretLine && sqlEndColumn >= caretOffset) {@@ -1224,11 +1223,11 @@ func skipHeadingSQLs(statement string, caretLine int, caretOffset int) (string,}previousSQLEndLine := int(list[i-1].End.GetLine())previousSQLEndColumn := int(list[i-1].End.GetColumn())- newCaretLine = caretLine - previousSQLEndLine + 1 // Convert to 1-based.+ newCaretLine = caretLine - previousSQLEndLine + 1if caretLine == previousSQLEndLine {// The caret is in the same line as the last line of the previous SQL statement.// We need to adjust the caret offset.- newCaretOffset = caretOffset - previousSQLEndColumn - 1 // Convert to 0-based.+ newCaretOffset = caretOffset - previousSQLEndColumn}break}diff --git a/backend/plugin/parser/pg/pg_test.go b/backend/plugin/parser/pg/pg_test.goindex b60738a80e..fff725f25f 100644--- a/backend/plugin/parser/pg/pg_test.go+++ b/backend/plugin/parser/pg/pg_test.go@@ -20,7 +20,11 @@ func TestPostgreSQLParser(t *testing.T) {},{statement: "SELECT a > (select max(a) from t1) FROM",- errorMessage: "Syntax error at line 0:39 \nrelated text: 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",+ },+ {+ statement: "SELECT 1;\n SELEC 5;\nSELECT 6;",+ errorMessage: "Syntax error at line 2:4 \nrelated text: SELECT 1;\n SELEC",},}diff --git a/backend/plugin/parser/pg/split_test.go b/backend/plugin/parser/pg/split_test.goindex 238cae1f25..73e3e40c93 100644--- a/backend/plugin/parser/pg/split_test.go+++ b/backend/plugin/parser/pg/split_test.go@@ -44,8 +44,8 @@ func TestPGSplitMultiSQL(t *testing.T) {{Text: "-- klsjdfjasldf\n\t\t\t-- klsjdflkjaskldfj\n\t\t\t",BaseLine: 0,- Start: &storepb.Position{Line: 2, Column: 3},- End: &storepb.Position{Line: 2, Column: 2},+ Start: &storepb.Position{Line: 3, Column: 4},+ End: &storepb.Position{Line: 3, Column: 3},Empty: true,},},@@ -58,13 +58,13 @@ func TestPGSplitMultiSQL(t *testing.T) {res: []base.SingleSQL{{Text: `select * from t;`,- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 0, Column: 15},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 1, Column: 16},},{Text: "\n\t\t\t/* sdfasdf */",- End: &storepb.Position{Line: 1, Column: 3},- Start: &storepb.Position{Line: 1, Column: 16},+ End: &storepb.Position{Line: 2, Column: 4},+ Start: &storepb.Position{Line: 2, Column: 17},Empty: true,},},@@ -78,20 +78,20 @@ func TestPGSplitMultiSQL(t *testing.T) {res: []base.SingleSQL{{Text: `select * from t;`,- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 0, Column: 15},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 1, Column: 16},},{Text: "\n\t\t\t/* sdfasdf */;",- Start: &storepb.Position{Line: 1, Column: 16},- End: &storepb.Position{Line: 1, Column: 16},+ Start: &storepb.Position{Line: 2, Column: 17},+ End: &storepb.Position{Line: 2, Column: 17},Empty: true,},{Text: "\n\t\t\tselect * from t;",BaseLine: 1,- Start: &storepb.Position{Line: 2, Column: 3},- End: &storepb.Position{Line: 2, Column: 18},+ Start: &storepb.Position{Line: 3, Column: 4},+ End: &storepb.Position{Line: 3, Column: 19},},},},@@ -102,8 +102,8 @@ func TestPGSplitMultiSQL(t *testing.T) {res: []base.SingleSQL{{Text: bigSQL,- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 0, Column: int32(len(bigSQL)) - 1},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 1, Column: int32(len(bigSQL))},},},},@@ -114,13 +114,13 @@ func TestPGSplitMultiSQL(t *testing.T) {res: []base.SingleSQL{{Text: " CREATE TABLE t(a int);",- Start: &storepb.Position{Line: 0, Column: 4},- End: &storepb.Position{Line: 0, Column: 25},+ Start: &storepb.Position{Line: 1, Column: 5},+ End: &storepb.Position{Line: 1, Column: 26},},{Text: " CREATE TABLE t1(a int)",- Start: &storepb.Position{Line: 0, Column: 27},- End: &storepb.Position{Line: 0, Column: 48},+ Start: &storepb.Position{Line: 1, Column: 28},+ End: &storepb.Position{Line: 1, Column: 49},},},},@@ -132,13 +132,13 @@ func TestPGSplitMultiSQL(t *testing.T) {res: []base.SingleSQL{{Text: `CREATE TABLE "tech_Book"(id int, name varchar(255));`,- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 0, Column: 51},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 1, Column: 52},},{Text: "\n\t\t\t\t\t\tINSERT INTO \"tech_Book\" VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafajdfl;\"ka');",- Start: &storepb.Position{Line: 1, Column: 6},- End: &storepb.Position{Line: 1, Column: 81},+ Start: &storepb.Position{Line: 2, Column: 7},+ End: &storepb.Position{Line: 2, Column: 82},},},},@@ -153,14 +153,14 @@ func TestPGSplitMultiSQL(t *testing.T) {res: []base.SingleSQL{{Text: "\n\t\t\t\t\t\t/* this is the comment. */\n\t\t\t\t\t\tCREATE /* inline comment */TABLE \"tech_Book\"(id int, name varchar(255));",- Start: &storepb.Position{Line: 2, Column: 6},- End: &storepb.Position{Line: 2, Column: 77},+ Start: &storepb.Position{Line: 3, Column: 7},+ End: &storepb.Position{Line: 3, Column: 78},},{Text: "\n\t\t\t\t\t\t-- this is the comment.\n\t\t\t\t\t\tINSERT INTO \"tech_Book\" VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafajdfl;\"ka');",BaseLine: 2,- Start: &storepb.Position{Line: 4, Column: 6},- End: &storepb.Position{Line: 4, Column: 81},+ Start: &storepb.Position{Line: 5, Column: 7},+ End: &storepb.Position{Line: 5, Column: 82},},},},@@ -186,14 +186,14 @@ func TestPGSplitMultiSQL(t *testing.T) {-- this is the commentINSERT INTO tbl VALUES ('fasf_bkdjlfa');$$;`,- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 7, Column: 8},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 8, Column: 9},},{Text: "\n\t\t\t\t\t\tCREATE TABLE t(a int);",BaseLine: 7,- Start: &storepb.Position{Line: 8, Column: 6},- End: &storepb.Position{Line: 8, Column: 27},+ Start: &storepb.Position{Line: 9, Column: 7},+ End: &storepb.Position{Line: 9, Column: 28},},},},@@ -219,14 +219,14 @@ func TestPGSplitMultiSQL(t *testing.T) {-- this is the commentINSERT INTO tbl VALUES ('fasf_bkdjlfa');$tag_name$;`,- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 7, Column: 16},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 8, Column: 17},},{Text: "\n\t\t\t\t\t\tCREATE TABLE t(a int);",BaseLine: 7,- Start: &storepb.Position{Line: 8, Column: 6},- End: &storepb.Position{Line: 8, Column: 27},+ Start: &storepb.Position{Line: 9, Column: 7},+ End: &storepb.Position{Line: 9, Column: 28},},},},@@ -238,14 +238,14 @@ func TestPGSplitMultiSQL(t *testing.T) {res: []base.SingleSQL{{Text: "CREATE TABLE t\r\n(a int);",- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 1, Column: 7},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 2, Column: 8},},{Text: "\r\nCREATE TABLE t1(b int);",BaseLine: 1,- Start: &storepb.Position{Line: 2, Column: 0},- End: &storepb.Position{Line: 2, Column: 22},+ Start: &storepb.Position{Line: 3, Column: 1},+ End: &storepb.Position{Line: 3, Column: 23},},},},@@ -260,8 +260,8 @@ func TestPGSplitMultiSQL(t *testing.T) {res: []base.SingleSQL{{Text: "INSERT INTO \"public\".\"table\"(\"id\",\"content\")\n\t\t\tVALUES\n\t\t\t(12,'table column name () { :xna,sydfn,,kasdfyn;}; /////test string/// 0'),\n\t\t\t(133,'knuandfan public table id'';create table t(a int, b int);set @text=''\\\\\\\\kdaminxkljasdfiebkla.unkonwn''+''abcdef.xyz\\\\''; local xxxyy.abcddd.mysql @text;------- '),\n\t\t\t(1444,'table t xyz abc a''a\\\\\\\\\\\\\\\\''b\"c>?>xxxxxx%}}%%>c<[[?${12344556778990{%}}cake\\\\');",- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 4, Column: 91},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 5, Column: 92},},},},@@ -272,8 +272,8 @@ func TestPGSplitMultiSQL(t *testing.T) {res: []base.SingleSQL{{Text: "INSERT INTO t VALUES ('klajfas)",- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 0, Column: 22},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 1, Column: 23},Empty: false,},},@@ -285,8 +285,8 @@ func TestPGSplitMultiSQL(t *testing.T) {res: []base.SingleSQL{{Text: "INSERT INTO \"t VALUES ('klajfas)",- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 0, Column: 12},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 1, Column: 13},},},},@@ -298,8 +298,8 @@ func TestPGSplitMultiSQL(t *testing.T) {{Text: "/*INSERT INTO \"t VALUES ('klajfas)",BaseLine: 0,- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 0, Column: 0},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 1, Column: 1},Empty: false,},},@@ -311,8 +311,8 @@ func TestPGSplitMultiSQL(t *testing.T) {res: []base.SingleSQL{{Text: "$$INSERT INTO \"t VALUES ('klajfas)",- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 0, Column: 2},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 1, Column: 3},},},},diff --git a/backend/plugin/parser/plsql/plsql_test.go b/backend/plugin/parser/plsql/plsql_test.goindex 907dc74c59..79db1d07e0 100644--- a/backend/plugin/parser/plsql/plsql_test.go+++ b/backend/plugin/parser/plsql/plsql_test.go@@ -34,7 +34,11 @@ func TestPLSQLParser(t *testing.T) {},{statement: "SELECT * FROM t1 WHERE c1 = ",- errorMessage: "Syntax error at line 0:26 \nrelated text: SELECT * FROM t1 WHERE c1 =",+ errorMessage: "Syntax error at line 1:27 \nrelated text: SELECT * FROM t1 WHERE c1 =",+ },+ {+ statement: "SELECT 1 FROM DUAL;\n SELEC 5 FROM DUAL;\nSELECT 6 FROM DUAL;",+ errorMessage: "Syntax error at line 2:10 \nrelated text: SELECT 1 FROM DUAL;\n SELEC 5",},}diff --git a/backend/plugin/parser/plsql/split_test.go b/backend/plugin/parser/plsql/split_test.goindex bfa135ad2e..5a4dd41414 100644--- a/backend/plugin/parser/plsql/split_test.go+++ b/backend/plugin/parser/plsql/split_test.go@@ -35,15 +35,15 @@ func TestOracleSplitMultiSQL(t *testing.T) {Text: `select * from t`,ByteOffsetStart: 5,ByteOffsetEnd: 20,- Start: &storepb.Position{Line: 1, Column: 4},- End: &storepb.Position{Line: 1, Column: 18},+ Start: &storepb.Position{Line: 2, Column: 5},+ End: &storepb.Position{Line: 2, Column: 19},},{Text: `create table table$1 (id int)`,ByteOffsetStart: 26,ByteOffsetEnd: 56,- Start: &storepb.Position{Line: 2, Column: 4},- End: &storepb.Position{Line: 2, Column: 32},+ Start: &storepb.Position{Line: 3, Column: 5},+ End: &storepb.Position{Line: 3, Column: 33},},},},@@ -62,8 +62,8 @@ ONLINE;`,res: []base.SingleSQL{{Text: "ALTER TABLE DATA.TEST\nMODIFY PARTITION BY RANGE (TXN_DATE)\nINTERVAL (NUMTODSINTERVAL(1, 'DAY'))\n(\n\tPARTITION TEST_PO VALUES LESS THAN (\n\t\tTO_DATE('2000-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN')\n\t)\n)\nONLINE",- Start: &storepb.Position{Line: 0, Column: 0},- End: &storepb.Position{Line: 8, Column: 0},+ Start: &storepb.Position{Line: 1, Column: 1},+ End: &storepb.Position{Line: 9, Column: 1},ByteOffsetEnd: 233,},},diff --git a/backend/plugin/parser/snowflake/snowflake_test.go b/backend/plugin/parser/snowflake/snowflake_test.goindex d8d08f8a76..9f6a2f4300 100644--- a/backend/plugin/parser/snowflake/snowflake_test.go+++ b/backend/plugin/parser/snowflake/snowflake_test.go@@ -48,7 +48,11 @@ func TestParseSnowSQL(t *testing.T) {}{{sql: `SELECT t.a, t.b FRO table_name t;`,- err: "Syntax error at line 0:31 \nrelated text: SELECT t.a, t.b FRO table_name t",+ err: "Syntax error at line 1:32 \nrelated text: SELECT t.a, t.b FRO table_name t",+ },+ {+ sql: "SELECT 1;\n SELEC 5;\nSELECT 6;",+ err: "Syntax error at line 2:4 \nrelated text: SELECT 1;\n SELEC",},}diff --git a/backend/plugin/parser/tidb/test-data/test_backup.yaml b/backend/plugin/parser/tidb/test-data/test_backup.yamlindex 4f17f9e8af..592021d275 100644--- a/backend/plugin/parser/tidb/test-data/test_backup.yaml+++ b/backend/plugin/parser/tidb/test-data/test_backup.yaml@@ -26,11 +26,11 @@sourcetablename: testtargettablename: _rollback_test_dbstartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 6- column: 45+ line: 7+ column: 46- input: |-UPDATE db1.t1 SET c1 = 1 WHERE c1 = 2;UPDATE db2.t2 SET c1 = 1 WHERE c1 = 2;@@ -42,11 +42,11 @@sourcetablename: t1targettablename: _rollback_0_t1startposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 37+ line: 1+ column: 38- statement: |-CREATE TABLE `backupDB`.`_rollback_1_t2` LIKE `db2`.`t2`;INSERT INTO `backupDB`.`_rollback_1_t2` SELECT `t2`.* FROM db2.t2 WHERE c1 = 2;@@ -54,11 +54,11 @@sourcetablename: t2targettablename: _rollback_1_t2startposition:- line: 1- column: 0+ line: 2+ column: 1endposition:- line: 1- column: 37+ line: 2+ column: 38- input: DELETE test FROM test, test2 as t2 where test.id = t2.id;result:- statement: |-@@ -68,11 +68,11 @@sourcetablename: testtargettablename: _rollback_0_teststartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 56+ line: 1+ column: 57- input: DELETE t1, t2 FROM test as t1, test2 as t2 WHERE t1.id = t2.id;result:- statement: |-@@ -82,11 +82,11 @@sourcetablename: testtargettablename: _rollback_0_teststartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 62+ line: 1+ column: 63- statement: |-CREATE TABLE `backupDB`.`_rollback_0_test2` LIKE `db`.`test2`;INSERT INTO `backupDB`.`_rollback_0_test2` SELECT `t2`.* FROM test as t1, test2 as t2 WHERE t1.id = t2.id;@@ -94,11 +94,11 @@sourcetablename: test2targettablename: _rollback_0_test2startposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 62+ line: 1+ column: 63- input: DELETE FROM t1, t2 USING test as t1, test2 as t2 WHERE t1.id = t2.id;result:- statement: |-@@ -108,11 +108,11 @@sourcetablename: testtargettablename: _rollback_0_teststartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 68+ line: 1+ column: 69- statement: |-CREATE TABLE `backupDB`.`_rollback_0_test2` LIKE `db`.`test2`;INSERT INTO `backupDB`.`_rollback_0_test2` SELECT `t2`.* FROM test as t1, test2 as t2 WHERE t1.id = t2.id;@@ -120,11 +120,11 @@sourcetablename: test2targettablename: _rollback_0_test2startposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 68+ line: 1+ column: 69- input: DELETE FROM test as t1 WHERE t1.c1 = 1;result:- statement: |-@@ -134,11 +134,11 @@sourcetablename: testtargettablename: _rollback_0_teststartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 38+ line: 1+ column: 39- input: DELETE FROM test WHERE c1 = 1;result:- statement: |-@@ -148,11 +148,11 @@sourcetablename: testtargettablename: _rollback_0_teststartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 29+ line: 1+ column: 30- input: UPDATE test x SET x.c1 = 1 WHERE x.c1 = 1result:- statement: |-@@ -162,11 +162,11 @@sourcetablename: testtargettablename: _rollback_0_teststartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 40+ line: 1+ column: 41- input: UPDATE test SET c1 = 1 WHERE c1=2;result:- statement: |-@@ -176,11 +176,11 @@sourcetablename: testtargettablename: _rollback_0_teststartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 33+ line: 1+ column: 34- input: UPDATE test t1, test2 t2 SET t1.c1 = 1 WHERE t1.c1 = t2.c1;result:- statement: |-@@ -190,11 +190,11 @@sourcetablename: testtargettablename: _rollback_0_teststartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 58+ line: 1+ column: 59- input: UPDATE test t1, test2 t2 SET t1.c1 = 1, t2.c2 = 2 WHERE t1.c1 = t2.c1;result:- statement: |-@@ -204,11 +204,11 @@sourcetablename: testtargettablename: _rollback_0_teststartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 69+ line: 1+ column: 70- statement: |-CREATE TABLE `backupDB`.`_rollback_0_test2` LIKE `db`.`test2`;INSERT INTO `backupDB`.`_rollback_0_test2` SELECT `t2`.* FROM test t1, test2 t2 WHERE t1.c1 = t2.c1;@@ -216,11 +216,11 @@sourcetablename: test2targettablename: _rollback_0_test2startposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 69+ line: 1+ column: 70- input: |-UPDATE test t1 SET t1.c1 = 2 WHERE t1.c1 = 1 ;UPDATE test t2 SET t2.c1 = 3 WHERE t2.c1 = 5 ;@@ -232,11 +232,11 @@sourcetablename: testtargettablename: _rollback_0_teststartposition:- line: 0- column: 0+ line: 1+ column: 1endposition:- line: 0- column: 45+ line: 1+ column: 46- statement: |-CREATE TABLE `backupDB`.`_rollback_1_test` LIKE `db`.`test`;INSERT INTO `backupDB`.`_rollback_1_test` SELECT `t2`.* FROM test t2 WHERE t2.c1 = 5;@@ -244,8 +244,8 @@sourcetablename: testtargettablename: _rollback_1_teststartposition:- line: 1- column: 0+ line: 2+ column: 1endposition:- line: 1- column: 45+ line: 2+ column: 46diff --git a/backend/plugin/parser/tidb/tidb_test.go b/backend/plugin/parser/tidb/tidb_test.goindex 1c6d04bbe5..4f1cb665ab 100644--- a/backend/plugin/parser/tidb/tidb_test.go+++ b/backend/plugin/parser/tidb/tidb_test.go@@ -115,11 +115,39 @@ func TestMySQLCreateTableSetLine(t *testing.T) {}func TestTiDBParserError(t *testing.T) {- _, err := ParseTiDB("SELECT 𝄞𝄞hello TO world;", "", "")- require.Error(t, err)- syntaxErr, ok := err.(*base.SyntaxError)- require.True(t, ok)- require.Equal(t, int32(0), syntaxErr.Position.GetLine())- require.Equal(t, int32(22), syntaxErr.Position.GetColumn())- require.Equal(t, `line 1 column 23 near "TO world;" `, syntaxErr.Message)+ tests := []struct {+ statement string+ expectedLine int32+ expectedCol int32+ expectedMsg string+ }{+ {+ statement: "SELECT 𝄞𝄞hello TO world;",+ expectedLine: 1,+ expectedCol: 23,+ expectedMsg: `line 1 column 23 near "TO world;" `,+ },+ {+ statement: "SELECT 1;\nSELEC 5;\nSELECT 6;",+ expectedLine: 2,+ expectedCol: 6,+ expectedMsg: "line 2 column 6 near \"SELEC 5;\nSELECT 6;\" ",+ },+ {+ statement: "SELECT 1;\n SELEC 5;\nSELECT 6;",+ expectedLine: 2,+ expectedCol: 9,+ expectedMsg: "line 2 column 9 near \"SELEC 5;\nSELECT 6;\" ",+ },+ }++ for _, test := range tests {+ _, err := ParseTiDB(test.statement, "", "")+ require.Error(t, err)+ syntaxErr, ok := err.(*base.SyntaxError)+ require.True(t, ok)+ require.Equal(t, test.expectedLine, syntaxErr.Position.GetLine())+ require.Equal(t, test.expectedCol, syntaxErr.Position.GetColumn())+ require.Equal(t, test.expectedMsg, syntaxErr.Message)+ }}diff --git a/backend/plugin/parser/tsql/split_test.go b/backend/plugin/parser/tsql/split_test.goindex 881664b99e..6aa6cf8835 100644--- a/backend/plugin/parser/tsql/split_test.go+++ b/backend/plugin/parser/tsql/split_test.go@@ -40,36 +40,36 @@ go{Text: "-- first statement\ndeclare @temp table(a int)",BaseLine: 0,- Start: &storepb.Position{Line: 1, Column: 0},- End: &storepb.Position{Line: 1, Column: 25},+ Start: &storepb.Position{Line: 2, Column: 1},+ End: &storepb.Position{Line: 2, Column: 26},Empty: false,},{Text: "\n-- second statement\ninsert into @temp values(1)",BaseLine: 1,- Start: &storepb.Position{Line: 3, Column: 0},- End: &storepb.Position{Line: 3, Column: 26},+ Start: &storepb.Position{Line: 4, Column: 1},+ End: &storepb.Position{Line: 4, Column: 27},Empty: false,},{Text: "\n-- third statement\nselect * from @temp\n-- go statement\ngo",BaseLine: 3,- Start: &storepb.Position{Line: 5, Column: 0},- End: &storepb.Position{Line: 7, Column: 0},+ Start: &storepb.Position{Line: 6, Column: 1},+ End: &storepb.Position{Line: 8, Column: 1},Empty: false,},{Text: "\ngo",BaseLine: 7,- Start: &storepb.Position{Line: 8, Column: 0},- End: &storepb.Position{Line: 8, Column: 0},+ Start: &storepb.Position{Line: 9, Column: 1},+ End: &storepb.Position{Line: 9, Column: 1},Empty: false,},{Text: "\ngo\ngo",BaseLine: 8,- Start: &storepb.Position{Line: 9, Column: 0},- End: &storepb.Position{Line: 10, Column: 0},+ Start: &storepb.Position{Line: 10, Column: 1},+ End: &storepb.Position{Line: 11, Column: 1},Empty: false,},},diff --git a/backend/tests/test-data/sql_review_mysql.yaml b/backend/tests/test-data/sql_review_mysql.yamlindex 04c1b5dd33..af6da0ecd4 100644--- a/backend/tests/test-data/sql_review_mysql.yaml+++ b/backend/tests/test-data/sql_review_mysql.yaml@@ -12,14 +12,14 @@{"status": "ERROR","title": "Syntax error",- "content": "Syntax error at line 1:20 \nrelated text: CREATE TABLE user(id)",+ "content": "Syntax error at line 1:21 \nrelated text: CREATE TABLE user(id)","code": 201,"sqlReviewReport": {"line": 1,- "column": 20,+ "column": 21,"startPosition": {"line": 1,- "column": 20+ "column": 21}}}diff --git a/frontend/src/types/proto-es/v1/common_pb.d.ts b/frontend/src/types/proto-es/v1/common_pb.d.tsindex cbbf6a7d28..5e60c2329e 100644--- a/frontend/src/types/proto-es/v1/common_pb.d.ts+++ b/frontend/src/types/proto-es/v1/common_pb.d.ts@@ -11,13 +11,28 @@ import type { Message } from "@bufbuild/protobuf";export declare const file_v1_common: GenFile;/**- * Position in a text expressed as one-based line and one-based column.+ * Position in a text.+ * Line is 0-based, Column is 1-based.+ *+ * Why this mixed numbering?+ * - Line is 0-based to match LSP (https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position)+ * and common programming practice (arrays/lists are 0-indexed)+ * - Column is 1-based to match user expectations (text editors show "column 1" for the first character)+ *+ * Handling unknown positions:+ * - If the entire position is unknown, leave this field as nil/undefined+ * - If only line is known, set line and leave column as 0 (e.g., line=5, column=0)+ * - If only column is known (rare), set column and leave line as 0+ * Frontends should check for nil/undefined/zero values and handle them appropriately.+ *+ * When displaying to users, convert to 1-based: display "line X" as "line X+1".** @generated from message bytebase.v1.Position*/export declare type Position = Message<"bytebase.v1.Position"> & {/**- * Line position in a text (one-based).+ * Line position in a text (zero-based).+ * First line of the text is line 0, second line is line 1, etc.** @generated from field: int32 line = 1;*/@@ -25,6 +40,14 @@ export declare type Position = Message<"bytebase.v1.Position"> & {/*** Column position in a text (one-based).+ * Column is measured in Unicode code points (characters/runes), not bytes or grapheme clusters.+ * First character of the line is column 1.+ * A value of 0 indicates the column information is unknown.+ *+ * Examples:+ * - "SELECT * FROM t" - column 8 is '*'+ * - "SELECT 你好 FROM t" - column 8 is '你' (even though it's at byte offset 7)+ * - "SELECT 😀 FROM t" - column 8 is '😀' (even though it's 4 bytes in UTF-8)** @generated from field: int32 column = 2;*/diff --git a/proto/gen/grpc-doc/openapi.yaml b/proto/gen/grpc-doc/openapi.yamlindex 0e878049b7..b1566df7e3 100644--- a/proto/gen/grpc-doc/openapi.yaml+++ b/proto/gen/grpc-doc/openapi.yaml@@ -12427,13 +12427,30 @@ components:properties:line:type: integer- description: Line position in a text (one-based).+ description: |-+ Line position in a text (zero-based).+ First line of the text is line 0, second line is line 1, etc.format: int32column:type: integer- description: Column position in a text (one-based).+ description: "Column position in a text (one-based).\n Column is measured in Unicode code points (characters/runes), not bytes or grapheme clusters.\n First character of the line is column 1.\n A value of 0 indicates the column information is unknown.\n\n Examples:\n - \"SELECT * FROM t\" - column 8 is '*'\n - \"SELECT 你好 FROM t\" - column 8 is '你' (even though it's at byte offset 7)\n - \"SELECT \U0001F600 FROM t\" - column 8 is '\U0001F600' (even though it's 4 bytes in UTF-8)"format: int32- description: Position in a text expressed as one-based line and one-based column.+ description: |-+ Position in a text.+ Line is 0-based, Column is 1-based.++ Why this mixed numbering?+ - Line is 0-based to match LSP (https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position)+ and common programming practice (arrays/lists are 0-indexed)+ - Column is 1-based to match user expectations (text editors show "column 1" for the first character)++ Handling unknown positions:+ - If the entire position is unknown, leave this field as nil/undefined+ - If only line is known, set line and leave column as 0 (e.g., line=5, column=0)+ - If only column is known (rare), set column and leave line as 0+ Frontends should check for nil/undefined/zero values and handle them appropriately.++ When displaying to users, convert to 1-based: display "line X" as "line X+1".Postgres_Session:type: objectproperties:diff --git a/proto/gen/grpc-doc/store/README.md b/proto/gen/grpc-doc/store/README.mdindex ead04a10b8..0201a98e93 100644--- a/proto/gen/grpc-doc/store/README.md+++ b/proto/gen/grpc-doc/store/README.md@@ -360,13 +360,29 @@ PageToken is used internally for obfuscating pagination tokens.<a name="bytebase-store-Position"></a>### Position-Position in a text expressed as one-based line and one-based column.+Position in a text.+Line is 0-based, Column is 1-based.++Why this mixed numbering?+- Line is 0-based to match LSP (https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position)+ and common programming practice (arrays/lists are 0-indexed)+- Column is 1-based to match user expectations (text editors show "column 1" for the first character)++Handling unknown positions:+- If the entire position is unknown, leave this field as nil/undefined+- If only line is known, set line and leave column as 0 (e.g., line=5, column=0)+- If only column is known (rare), set column and leave line as 0+Frontends should check for nil/undefined/zero values and handle them appropriately.++When displaying to users, convert to 1-based: display "line X" as "line X+1".| Field | Type | Label | Description || ----- | ---- | ----- | ----------- |-| line | [int32](#int32) | | Line position in a text (one-based). |-| column | [int32](#int32) | | Column position in a text (one-based). |+| line | [int32](#int32) | | Line position in a text (zero-based). First line of the text is line 0, second line is line 1, etc. |+| column | [int32](#int32) | | Column position in a text (one-based). Column is measured in Unicode code points (characters/runes), not bytes or grapheme clusters. First character of the line is column 1. A value of 0 indicates the column information is unknown.++Examples: - "SELECT * FROM t" - column 8 is '*' - "SELECT 你好 FROM t" - column 8 is '你' (even though it's at byte offset 7) - "SELECT 😀 FROM t" - column 8 is '😀' (even though it's 4 bytes in UTF-8) |diff --git a/proto/gen/grpc-doc/store/index.html b/proto/gen/grpc-doc/store/index.htmlindex 1443623f3f..e5e90d7ad3 100644--- a/proto/gen/grpc-doc/store/index.html+++ b/proto/gen/grpc-doc/store/index.html@@ -1543,7 +1543,7 @@<h3 id="bytebase.store.Position">Position</h3>- <p>Position in a text expressed as one-based line and one-based column.</p>+ <p>Position in a text.</p><p>Line is 0-based, Column is 1-based.</p><p>Why this mixed numbering?</p><p>- Line is 0-based to match LSP (https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position)</p><p>and common programming practice (arrays/lists are 0-indexed)</p><p>- Column is 1-based to match user expectations (text editors show "column 1" for the first character)</p><p>Handling unknown positions:</p><p>- If the entire position is unknown, leave this field as nil/undefined</p><p>- If only line is known, set line and leave column as 0 (e.g., line=5, column=0)</p><p>- If only column is known (rare), set column and leave line as 0</p><p>Frontends should check for nil/undefined/zero values and handle them appropriately.</p><p>When displaying to users, convert to 1-based: display "line X" as "line X+1".</p><table class="field-table">@@ -1556,14 +1556,23 @@<td>line</td><td><a href="#int32">int32</a></td><td></td>- <td><p>Line position in a text (one-based). </p></td>+ <td><p>Line position in a text (zero-based).+First line of the text is line 0, second line is line 1, etc. </p></td></tr><tr><td>column</td><td><a href="#int32">int32</a></td><td></td>- <td><p>Column position in a text (one-based). </p></td>+ <td><p>Column position in a text (one-based).+Column is measured in Unicode code points (characters/runes), not bytes or grapheme clusters.+First character of the line is column 1.+A value of 0 indicates the column information is unknown.++Examples:+- "SELECT * FROM t" - column 8 is '*'+- "SELECT 你好 FROM t" - column 8 is '你' (even though it's at byte offset 7)+- "SELECT 😀 FROM t" - column 8 is '😀' (even though it's 4 bytes in UTF-8) </p></td></tr></tbody>diff --git a/proto/gen/grpc-doc/v1/README.md b/proto/gen/grpc-doc/v1/README.mdindex b729af618c..946fc3c94d 100644--- a/proto/gen/grpc-doc/v1/README.md+++ b/proto/gen/grpc-doc/v1/README.md@@ -760,13 +760,29 @@ Authorization method for RPC calls.<a name="bytebase-v1-Position"></a>### Position-Position in a text expressed as one-based line and one-based column.+Position in a text.+Line is 0-based, Column is 1-based.++Why this mixed numbering?+- Line is 0-based to match LSP (https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position)+ and common programming practice (arrays/lists are 0-indexed)+- Column is 1-based to match user expectations (text editors show "column 1" for the first character)++Handling unknown positions:+- If the entire position is unknown, leave this field as nil/undefined+- If only line is known, set line and leave column as 0 (e.g., line=5, column=0)+- If only column is known (rare), set column and leave line as 0+Frontends should check for nil/undefined/zero values and handle them appropriately.++When displaying to users, convert to 1-based: display "line X" as "line X+1".| Field | Type | Label | Description || ----- | ---- | ----- | ----------- |-| line | [int32](#int32) | | Line position in a text (one-based). |-| column | [int32](#int32) | | Column position in a text (one-based). |+| line | [int32](#int32) | | Line position in a text (zero-based). First line of the text is line 0, second line is line 1, etc. |+| column | [int32](#int32) | | Column position in a text (one-based). Column is measured in Unicode code points (characters/runes), not bytes or grapheme clusters. First character of the line is column 1. A value of 0 indicates the column information is unknown.++Examples: - "SELECT * FROM t" - column 8 is '*' - "SELECT 你好 FROM t" - column 8 is '你' (even though it's at byte offset 7) - "SELECT 😀 FROM t" - column 8 is '😀' (even though it's 4 bytes in UTF-8) |diff --git a/proto/gen/grpc-doc/v1/index.html b/proto/gen/grpc-doc/v1/index.htmlindex ac2dacd2ab..c3e6deef8c 100644--- a/proto/gen/grpc-doc/v1/index.html+++ b/proto/gen/grpc-doc/v1/index.html@@ -2974,7 +2974,7 @@ Example: UpdateRole with allow_missing requires both bb.roles.update<h3 id="bytebase.v1.Position">Position</h3>- <p>Position in a text expressed as one-based line and one-based column.</p>+ <p>Position in a text.</p><p>Line is 0-based, Column is 1-based.</p><p>Why this mixed numbering?</p><p>- Line is 0-based to match LSP (https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position)</p><p>and common programming practice (arrays/lists are 0-indexed)</p><p>- Column is 1-based to match user expectations (text editors show "column 1" for the first character)</p><p>Handling unknown positions:</p><p>- If the entire position is unknown, leave this field as nil/undefined</p><p>- If only line is known, set line and leave column as 0 (e.g., line=5, column=0)</p><p>- If only column is known (rare), set column and leave line as 0</p><p>Frontends should check for nil/undefined/zero values and handle them appropriately.</p><p>When displaying to users, convert to 1-based: display "line X" as "line X+1".</p><table class="field-table">@@ -2987,14 +2987,23 @@ Example: UpdateRole with allow_missing requires both bb.roles.update<td>line</td><td><a href="#int32">int32</a></td><td></td>- <td><p>Line position in a text (one-based). </p></td>+ <td><p>Line position in a text (zero-based).+First line of the text is line 0, second line is line 1, etc. </p></td></tr><tr><td>column</td><td><a href="#int32">int32</a></td><td></td>- <td><p>Column position in a text (one-based). </p></td>+ <td><p>Column position in a text (one-based).+Column is measured in Unicode code points (characters/runes), not bytes or grapheme clusters.+First character of the line is column 1.+A value of 0 indicates the column information is unknown.++Examples:+- "SELECT * FROM t" - column 8 is '*'+- "SELECT 你好 FROM t" - column 8 is '你' (even though it's at byte offset 7)+- "SELECT 😀 FROM t" - column 8 is '😀' (even though it's 4 bytes in UTF-8) </p></td></tr></tbody>diff --git a/proto/store/store/common.proto b/proto/store/store/common.protoindex 51051cff3d..3174dfa781 100644--- a/proto/store/store/common.proto+++ b/proto/store/store/common.proto@@ -100,10 +100,32 @@ enum SchemaChangeType {}// Position in a text expressed as one-based line and one-based column.+// We use 1-based numbering to match the majority of industry standards:+// - Monaco Editor uses 1-based (https://microsoft.github.io/monaco-editor/typedoc/interfaces/IPosition.html)+// - GitHub Actions uses 1-based (https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-error-message)+// - Most text editors display 1-based positions to users+// Note: LSP uses 0-based (https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position),+// but we follow the canonical user-facing standards.+//+// Handling unknown positions:+// - If the entire position is unknown, leave this field as nil/undefined+// - If only line is known, set line and leave column as 0 (e.g., line=5, column=0)+// - If only column is known (rare), set column and leave line as 0+// Frontends should check for nil/undefined/zero values and handle them appropriately.message Position {// Line position in a text (one-based).+ // First line of the text is line 1.+ // A value of 0 indicates the line information is unknown.int32 line = 1;// Column position in a text (one-based).+ // Column is measured in Unicode code points (characters/runes), not bytes or grapheme clusters.+ // First character of the line is column 1.+ // A value of 0 indicates the column information is unknown.+ //+ // Examples:+ // - "SELECT * FROM t" - column 8 is '*'+ // - "SELECT 你好 FROM t" - column 8 is '你' (even though it's at byte offset 7)+ // - "SELECT 😀 FROM t" - column 8 is '😀' (even though it's 4 bytes in UTF-8)int32 column = 2;}diff --git a/proto/v1/v1/common.proto b/proto/v1/v1/common.protoindex 3becac9a65..5248df82d5 100644--- a/proto/v1/v1/common.proto+++ b/proto/v1/v1/common.proto@@ -98,10 +98,32 @@ enum ExportFormat {}// Position in a text expressed as one-based line and one-based column.+// We use 1-based numbering to match the majority of industry standards:+// - Monaco Editor uses 1-based (https://microsoft.github.io/monaco-editor/typedoc/interfaces/IPosition.html)+// - GitHub Actions uses 1-based (https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-error-message)+// - Most text editors display 1-based positions to users+// Note: LSP uses 0-based (https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position),+// but we follow the canonical user-facing standards.+//+// Handling unknown positions:+// - If the entire position is unknown, leave this field as nil/undefined+// - If only line is known, set line and leave column as 0 (e.g., line=5, column=0)+// - If only column is known (rare), set column and leave line as 0+// Frontends should check for nil/undefined/zero values and handle them appropriately.message Position {// Line position in a text (one-based).+ // First line of the text is line 1.+ // A value of 0 indicates the line information is unknown.int32 line = 1;// Column position in a text (one-based).+ // Column is measured in Unicode code points (characters/runes), not bytes or grapheme clusters.+ // First character of the line is column 1.+ // A value of 0 indicates the column information is unknown.+ //+ // Examples:+ // - "SELECT * FROM t" - column 8 is '*'+ // - "SELECT 你好 FROM t" - column 8 is '你' (even though it's at byte offset 7)+ // - "SELECT 😀 FROM t" - column 8 is '😀' (even though it's 4 bytes in UTF-8)int32 column = 2;}