Skip to content

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

test(parser): add YAML-based split test infrastructure for all engines (#18679)

h3n4l34c381bb34df
diff --git a/backend/plugin/parser/base/split_test_runner.go b/backend/plugin/parser/base/split_test_runner.go
new file mode 100644
index 0000000000..10b8eb07bc
--- /dev/null
+++ b/backend/plugin/parser/base/split_test_runner.go
@@ -0,0 +1,238 @@
+package base
+
+import (
+ "flag"
+ "os"
+ "path/filepath"
+ "runtime"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+ "gopkg.in/yaml.v3"
+)
+
+// record is a flag to update expected results in YAML files.
+// Usage: go test -run TestXxxSplitSQL -args -record
+var record = flag.Bool("record", false, "update expected results in YAML test files")
+
+// SplitTestCase represents a single test case in YAML.
+type SplitTestCase struct {
+ Description string `yaml:"description"`
+ Input string `yaml:"input"`
+ Error string `yaml:"error,omitempty"`
+ Result []StatementResult `yaml:"result,omitempty"`
+}
+
+// StatementResult represents expected output for one statement.
+type StatementResult struct {
+ Text string `yaml:"text"`
+ BaseLine int `yaml:"baseline"`
+ Start PositionResult `yaml:"start"`
+ End PositionResult `yaml:"end"`
+ Range RangeResult `yaml:"range"`
+ Empty bool `yaml:"empty"`
+}
+
+// PositionResult represents a position in the YAML test data.
+type PositionResult struct {
+ Line int32 `yaml:"line"`
+ Column int32 `yaml:"column"`
+}
+
+// RangeResult represents a byte range in the YAML test data.
+type RangeResult struct {
+ Start int32 `yaml:"start"`
+ End int32 `yaml:"end"`
+}
+
+// SplitTestOptions configures how split tests are run.
+type SplitTestOptions struct {
+ // SplitFunc is the main split function to test (required).
+ SplitFunc func(string) ([]Statement, error)
+ // LexerSplitFunc is the lexer-based implementation (optional).
+ LexerSplitFunc func(string) ([]Statement, error)
+ // ParserSplitFunc is the parser-based implementation (optional).
+ ParserSplitFunc func(string) ([]Statement, error)
+}
+
+// loadTestCases loads test cases from a YAML file.
+// The path is relative to the caller's directory.
+func loadTestCases(t *testing.T, relativePath string, callerDepth int) []SplitTestCase {
+ t.Helper()
+
+ // Get the caller's file path to resolve relative paths
+ _, callerFile, _, ok := runtime.Caller(callerDepth)
+ require.True(t, ok, "failed to get caller info")
+
+ callerDir := filepath.Dir(callerFile)
+ fullPath := filepath.Join(callerDir, relativePath)
+
+ data, err := os.ReadFile(fullPath)
+ require.NoError(t, err, "failed to read test file: %s", fullPath)
+
+ var testCases []SplitTestCase
+ err = yaml.Unmarshal(data, &testCases)
+ require.NoError(t, err, "failed to parse YAML test file: %s", fullPath)
+
+ return testCases
+}
+
+// assertStatementsEqual compares actual statements against expected results.
+func assertStatementsEqual(t *testing.T, expected []StatementResult, actual []Statement, msgAndArgs ...any) {
+ t.Helper()
+
+ args := append([]any{"statement count mismatch"}, msgAndArgs...)
+ require.Equal(t, len(expected), len(actual), args...)
+
+ for i, exp := range expected {
+ act := actual[i]
+ msg := append([]any{"statement %d mismatch", i}, msgAndArgs...)
+
+ require.Equal(t, exp.Text, act.Text, msg...)
+ require.Equal(t, exp.BaseLine, act.BaseLine, msg...)
+ require.Equal(t, exp.Empty, act.Empty, msg...)
+
+ require.NotNil(t, act.Start, msg...)
+ require.Equal(t, exp.Start.Line, act.Start.Line, msg...)
+ require.Equal(t, exp.Start.Column, act.Start.Column, msg...)
+
+ require.NotNil(t, act.End, msg...)
+ require.Equal(t, exp.End.Line, act.End.Line, msg...)
+ require.Equal(t, exp.End.Column, act.End.Column, msg...)
+
+ require.NotNil(t, act.Range, msg...)
+ require.Equal(t, exp.Range.Start, act.Range.Start, msg...)
+ require.Equal(t, exp.Range.End, act.Range.End, msg...)
+ }
+}
+
+// RunSplitTests loads YAML test cases and runs them against a SplitSQL function.
+// For engines with dual implementations (lexer and parser), it enforces that
+// both produce identical results when both succeed.
+//
+// When run with -record flag, it updates the YAML file with actual results:
+//
+// go test -run TestXxxSplitSQL -args -record
+func RunSplitTests(t *testing.T, testDataPath string, opts SplitTestOptions) {
+ t.Helper()
+
+ require.NotNil(t, opts.SplitFunc, "SplitFunc is required")
+
+ // Get the full path to the test file
+ _, callerFile, _, ok := runtime.Caller(1)
+ require.True(t, ok, "failed to get caller info")
+ callerDir := filepath.Dir(callerFile)
+ fullPath := filepath.Join(callerDir, testDataPath)
+
+ // Caller depth 2: skip loadTestCases (1) and RunSplitTests (2) to get the actual test function
+ testCases := loadTestCases(t, testDataPath, 2)
+ require.NotEmpty(t, testCases, "no test cases found in %s", testDataPath)
+
+ // Record mode: update expected results in YAML file
+ if *record {
+ recordTestResults(t, fullPath, testCases, opts)
+ return
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.Description, func(t *testing.T) {
+ runSingleTest(t, tc, opts)
+ })
+ }
+}
+
+// recordTestResults runs all test cases and updates the YAML file with actual results.
+func recordTestResults(t *testing.T, fullPath string, testCases []SplitTestCase, opts SplitTestOptions) {
+ t.Helper()
+
+ updated := make([]SplitTestCase, len(testCases))
+ for i, tc := range testCases {
+ updated[i] = SplitTestCase{
+ Description: tc.Description,
+ Input: tc.Input,
+ }
+
+ result, err := opts.SplitFunc(tc.Input)
+ if err != nil {
+ updated[i].Error = err.Error()
+ continue
+ }
+
+ updated[i].Result = make([]StatementResult, len(result))
+ for j, stmt := range result {
+ updated[i].Result[j] = StatementResult{
+ Text: stmt.Text,
+ BaseLine: stmt.BaseLine,
+ Empty: stmt.Empty,
+ }
+ if stmt.Start != nil {
+ updated[i].Result[j].Start = PositionResult{
+ Line: stmt.Start.Line,
+ Column: stmt.Start.Column,
+ }
+ }
+ if stmt.End != nil {
+ updated[i].Result[j].End = PositionResult{
+ Line: stmt.End.Line,
+ Column: stmt.End.Column,
+ }
+ }
+ if stmt.Range != nil {
+ updated[i].Result[j].Range = RangeResult{
+ Start: stmt.Range.Start,
+ End: stmt.Range.End,
+ }
+ }
+ }
+ }
+
+ data, err := yaml.Marshal(updated)
+ require.NoError(t, err, "failed to marshal updated test cases")
+
+ err = os.WriteFile(fullPath, data, 0644)
+ require.NoError(t, err, "failed to write updated test file: %s", fullPath)
+
+ t.Logf("Updated %s with %d test cases", fullPath, len(updated))
+}
+
+func runSingleTest(t *testing.T, tc SplitTestCase, opts SplitTestOptions) {
+ t.Helper()
+
+ var lexerResult, parserResult []Statement
+ var lexerErr, parserErr error
+
+ // Run lexer implementation if provided
+ if opts.LexerSplitFunc != nil {
+ lexerResult, lexerErr = opts.LexerSplitFunc(tc.Input)
+ }
+
+ // Run parser implementation if provided
+ if opts.ParserSplitFunc != nil {
+ parserResult, parserErr = opts.ParserSplitFunc(tc.Input)
+ }
+
+ // MANDATORY: If both implementations succeed, results must be identical
+ if opts.LexerSplitFunc != nil && opts.ParserSplitFunc != nil {
+ if lexerErr == nil && parserErr == nil {
+ require.Equal(t, len(lexerResult), len(parserResult),
+ "lexer and parser produced different number of statements")
+ for i := range lexerResult {
+ require.Equal(t, lexerResult[i], parserResult[i],
+ "lexer and parser produced different result for statement %d", i)
+ }
+ }
+ }
+
+ // Run the main split function
+ result, err := opts.SplitFunc(tc.Input)
+
+ // Check error expectation
+ if tc.Error != "" {
+ require.Error(t, err)
+ require.Contains(t, err.Error(), tc.Error)
+ return
+ }
+
+ require.NoError(t, err)
+ assertStatementsEqual(t, tc.Result, result)
+}
diff --git a/backend/plugin/parser/bigquery/split_test.go b/backend/plugin/parser/bigquery/split_test.go
index 995499b03e..df2136815b 100644
--- a/backend/plugin/parser/bigquery/split_test.go
+++ b/backend/plugin/parser/bigquery/split_test.go
@@ -3,68 +3,11 @@ package bigquery
import (
"testing"
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-type splitTestData struct {
- statement string
- want resData
-}
-
-type resData struct {
- res []base.Statement
-}
-
-func TestBigQuerySplitMultiSQL(t *testing.T) {
- testCases := []splitTestData{
- {
- statement: "SELECT 1",
- want: resData{
- res: []base.Statement{
- {
- Text: "SELECT 1",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 9},
- Empty: false,
- Range: &storepb.Range{Start: 0, End: 8},
- },
- },
- },
- },
- {
- statement: "SELECT 1;\n SELECT\n 33;",
- want: resData{
- res: []base.Statement{
- {
- Text: "SELECT 1;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 10},
- Empty: false,
- Range: &storepb.Range{Start: 0, End: 9},
- },
- {
- Text: "\n SELECT\n 33;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 2, Column: 2},
- End: &storepb.Position{Line: 3, Column: 5},
- Empty: false,
- Range: &storepb.Range{Start: 9, End: 22},
- },
- },
- },
- },
- }
-
- for _, tc := range testCases {
- got, err := SplitSQL(tc.statement)
- if err != nil {
- t.Errorf("unexpected error: %v, statement: %s", err, tc.statement)
- }
- require.Equalf(t, tc.want.res, got, "statement: %s", tc.statement)
- }
+func TestBigQuerySplitSQL(t *testing.T) {
+ base.RunSplitTests(t, "test-data/test_split.yaml", base.SplitTestOptions{
+ SplitFunc: SplitSQL,
+ })
}
diff --git a/backend/plugin/parser/bigquery/test-data/test_split.yaml b/backend/plugin/parser/bigquery/test-data/test_split.yaml
new file mode 100644
index 0000000000..430a24714b
--- /dev/null
+++ b/backend/plugin/parser/bigquery/test-data/test_split.yaml
@@ -0,0 +1,25 @@
+- description: "single statement without semicolon"
+ input: "SELECT 1"
+ result:
+ - text: "SELECT 1"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 9}
+ range: {start: 0, end: 8}
+ empty: false
+
+- description: "multiple statements multi-line"
+ input: "SELECT 1;\n SELECT\n 33;"
+ result:
+ - text: "SELECT 1;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 10}
+ range: {start: 0, end: 9}
+ empty: false
+ - text: "\n SELECT\n 33;"
+ baseline: 0
+ start: {line: 2, column: 2}
+ end: {line: 3, column: 5}
+ range: {start: 9, end: 22}
+ empty: false
diff --git a/backend/plugin/parser/cassandra/split_test.go b/backend/plugin/parser/cassandra/split_test.go
index 337d3f7b7e..686d297db4 100644
--- a/backend/plugin/parser/cassandra/split_test.go
+++ b/backend/plugin/parser/cassandra/split_test.go
@@ -3,135 +3,11 @@ package cassandra
import (
"testing"
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-func TestSplitSQL(t *testing.T) {
- statement := `SELECT * FROM users;
- SELECT * FROM orders;
- SELECT * FROM products;`
- want := []base.Statement{
- {
- Text: "SELECT * FROM users;",
- BaseLine: 0,
- Range: &storepb.Range{Start: 0, End: 20},
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 21},
- Empty: false,
- },
- {
- Text: "\n\tSELECT * FROM orders;",
- BaseLine: 0,
- Range: &storepb.Range{Start: 20, End: 43},
- Start: &storepb.Position{Line: 2, Column: 2},
- End: &storepb.Position{Line: 2, Column: 23},
- Empty: false,
- },
- {
- Text: "\n\tSELECT * FROM products;",
- BaseLine: 1,
- Range: &storepb.Range{Start: 43, End: 68},
- Start: &storepb.Position{Line: 3, Column: 2},
- End: &storepb.Position{Line: 3, Column: 25},
- Empty: false,
- },
- }
-
- list, err := SplitSQL(statement)
- require.NoError(t, err)
- require.Equal(t, want, list)
-}
-
-func TestSplitSQLSingleStatement(t *testing.T) {
- statement := "SELECT * FROM users;"
- list, err := SplitSQL(statement)
- require.NoError(t, err)
- require.Equal(t, 1, len(list))
- require.Equal(t, "SELECT * FROM users;", list[0].Text)
- require.Equal(t, 0, list[0].BaseLine)
- require.False(t, list[0].Empty)
-}
-
-func TestSplitSQLEmpty(t *testing.T) {
- tests := []struct {
- name string
- statement string
- wantCount int
- }{
- {
- name: "Empty string",
- statement: "",
- wantCount: 0,
- },
- {
- name: "Only whitespace",
- statement: " \n \t ",
- wantCount: 0,
- },
- {
- name: "Only semicolon",
- statement: ";",
- wantCount: 1, // Semicolon creates one statement entry
- },
- {
- name: "Only comments and semicolon",
- statement: "-- comment\n;",
- wantCount: 1, // Cassandra might count this as one statement
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- list, err := SplitSQL(tt.statement)
- require.NoError(t, err)
- // Filter out empty statements
- nonEmpty := 0
- for _, sql := range list {
- if !sql.Empty {
- nonEmpty++
- }
- }
- require.Equal(t, tt.wantCount, nonEmpty)
- })
- }
-}
-
-func TestSplitSQLWithComments(t *testing.T) {
- tests := []struct {
- name string
- statement string
- wantCount int
- }{
- {
- name: "Statement with trailing comment",
- statement: "SELECT * FROM users; -- This is a comment\nSELECT * FROM orders;",
- wantCount: 2,
- },
- {
- name: "Statement with leading comment",
- statement: "-- Comment at start\nSELECT * FROM users;",
- wantCount: 1,
- },
- {
- name: "Only comment with semicolon",
- statement: "-- comment only\n;",
- wantCount: 1, // Semicolon makes it non-empty
- },
- {
- name: "Multiple statements with comments",
- statement: "-- First query\nSELECT * FROM users;\n-- Second query\nSELECT * FROM orders;",
- wantCount: 2,
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- list, err := SplitSQL(tt.statement)
- require.NoError(t, err)
- require.Equal(t, tt.wantCount, len(list))
- })
- }
+func TestCassandraSplitSQL(t *testing.T) {
+ base.RunSplitTests(t, "test-data/test_split.yaml", base.SplitTestOptions{
+ SplitFunc: SplitSQL,
+ })
}
diff --git a/backend/plugin/parser/cassandra/test-data/test_split.yaml b/backend/plugin/parser/cassandra/test-data/test_split.yaml
new file mode 100644
index 0000000000..0043d41600
--- /dev/null
+++ b/backend/plugin/parser/cassandra/test-data/test_split.yaml
@@ -0,0 +1,31 @@
+- description: "multiple statements on multiple lines"
+ input: "SELECT * FROM users;\n\tSELECT * FROM orders;\n\tSELECT * FROM products;"
+ result:
+ - text: "SELECT * FROM users;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 21}
+ range: {start: 0, end: 20}
+ empty: false
+ - text: "\n\tSELECT * FROM orders;"
+ baseline: 0
+ start: {line: 2, column: 2}
+ end: {line: 2, column: 23}
+ range: {start: 20, end: 43}
+ empty: false
+ - text: "\n\tSELECT * FROM products;"
+ baseline: 1
+ start: {line: 3, column: 2}
+ end: {line: 3, column: 25}
+ range: {start: 43, end: 68}
+ empty: false
+
+- description: "single statement"
+ input: "SELECT * FROM users;"
+ result:
+ - text: "SELECT * FROM users;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 21}
+ range: {start: 0, end: 20}
+ empty: false
diff --git a/backend/plugin/parser/cosmosdb/split_test.go b/backend/plugin/parser/cosmosdb/split_test.go
index e2fc3326bc..c40cfd191e 100644
--- a/backend/plugin/parser/cosmosdb/split_test.go
+++ b/backend/plugin/parser/cosmosdb/split_test.go
@@ -3,94 +3,11 @@ package cosmosdb
import (
"testing"
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-func TestSplitSQL(t *testing.T) {
- tests := []struct {
- name string
- statement string
- want *base.Statement // nil means no statements expected
- }{
- {
- name: "Single SELECT statement",
- statement: "SELECT * FROM c",
- want: &base.Statement{
- Text: "SELECT * FROM c",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 16}, // 1-based exclusive (after 'c')
- Range: &storepb.Range{Start: 0, End: 15},
- Empty: false,
- },
- },
- {
- name: "SELECT with WHERE",
- statement: "SELECT c.id FROM users c WHERE c.active = true",
- want: &base.Statement{
- Text: "SELECT c.id FROM users c WHERE c.active = true",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 47}, // 1-based exclusive (after 'e')
- Range: &storepb.Range{Start: 0, End: 46},
- Empty: false,
- },
- },
- {
- name: "Multi-line SELECT",
- statement: "SELECT\n * FROM c",
- want: &base.Statement{
- Text: "SELECT\n * FROM c",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 2, Column: 11}, // 1-based exclusive (after 'c')
- Range: &storepb.Range{Start: 0, End: 17},
- Empty: false,
- },
- },
- {
- name: "Empty string",
- statement: "",
- want: nil,
- },
- {
- name: "Only whitespace",
- statement: " \n \t ",
- want: nil,
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- list, err := SplitSQL(tt.statement)
- require.NoError(t, err)
-
- if tt.want == nil {
- require.Empty(t, list)
- return
- }
-
- require.Len(t, list, 1)
- got := list[0]
-
- require.Equal(t, tt.want.Text, got.Text, "Text mismatch")
- require.Equal(t, tt.want.BaseLine, got.BaseLine, "BaseLine mismatch")
- require.Equal(t, tt.want.Empty, got.Empty, "Empty mismatch")
-
- require.NotNil(t, got.Start, "Start should not be nil")
- require.Equal(t, tt.want.Start.Line, got.Start.Line, "Start.Line mismatch")
- require.Equal(t, tt.want.Start.Column, got.Start.Column, "Start.Column mismatch")
-
- require.NotNil(t, got.End, "End should not be nil")
- require.Equal(t, tt.want.End.Line, got.End.Line, "End.Line mismatch")
- require.Equal(t, tt.want.End.Column, got.End.Column, "End.Column mismatch")
-
- require.NotNil(t, got.Range, "Range should not be nil")
- require.Equal(t, tt.want.Range.Start, got.Range.Start, "Range.Start mismatch")
- require.Equal(t, tt.want.Range.End, got.Range.End, "Range.End mismatch")
- })
- }
+func TestCosmosDBSplitSQL(t *testing.T) {
+ base.RunSplitTests(t, "test-data/test_split.yaml", base.SplitTestOptions{
+ SplitFunc: SplitSQL,
+ })
}
diff --git a/backend/plugin/parser/cosmosdb/test-data/test_split.yaml b/backend/plugin/parser/cosmosdb/test-data/test_split.yaml
new file mode 100644
index 0000000000..8524b067ad
--- /dev/null
+++ b/backend/plugin/parser/cosmosdb/test-data/test_split.yaml
@@ -0,0 +1,29 @@
+- description: "single SELECT statement"
+ input: "SELECT * FROM c"
+ result:
+ - text: "SELECT * FROM c"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 16}
+ range: {start: 0, end: 15}
+ empty: false
+
+- description: "SELECT with WHERE"
+ input: "SELECT c.id FROM users c WHERE c.active = true"
+ result:
+ - text: "SELECT c.id FROM users c WHERE c.active = true"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 47}
+ range: {start: 0, end: 46}
+ empty: false
+
+- description: "multi-line SELECT"
+ input: "SELECT\n * FROM c"
+ result:
+ - text: "SELECT\n * FROM c"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 2, column: 11}
+ range: {start: 0, end: 17}
+ empty: false
diff --git a/backend/plugin/parser/doris/split_test.go b/backend/plugin/parser/doris/split_test.go
index bf407444f4..e906e73738 100644
--- a/backend/plugin/parser/doris/split_test.go
+++ b/backend/plugin/parser/doris/split_test.go
@@ -3,107 +3,11 @@ package doris
import (
"testing"
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-func TestSplitDorisSQLStatements(t *testing.T) {
- tests := []struct {
- name string
- statement string
- want []base.Statement
- }{
- {
- name: "simple single statement",
- statement: "SELECT 1;",
- want: []base.Statement{
- {
- Text: "SELECT 1;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 10},
- Range: &storepb.Range{Start: 0, End: 9},
- },
- },
- },
- {
- name: "multiple statements on one line",
- statement: "SELECT 1; SELECT 2;",
- want: []base.Statement{
- {
- Text: "SELECT 1;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 10},
- Range: &storepb.Range{Start: 0, End: 9},
- },
- {
- Text: " SELECT 2;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 11},
- End: &storepb.Position{Line: 1, Column: 20},
- Range: &storepb.Range{Start: 9, End: 19},
- },
- },
- },
- {
- name: "multi-line statement",
- statement: "SELECT\n 1;",
- want: []base.Statement{
- {
- Text: "SELECT\n 1;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 2, Column: 5},
- Range: &storepb.Range{Start: 0, End: 11},
- },
- },
- },
- {
- name: "multiple statements on multiple lines",
- statement: "SELECT 1;\nSELECT 2;",
- want: []base.Statement{
- {
- Text: "SELECT 1;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 10},
- Range: &storepb.Range{Start: 0, End: 9},
- },
- {
- Text: "\nSELECT 2;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 2, Column: 1},
- End: &storepb.Position{Line: 2, Column: 10},
- Range: &storepb.Range{Start: 9, End: 19},
- },
- },
- },
- }
-
- for _, tc := range tests {
- t.Run(tc.name, func(t *testing.T) {
- list, err := SplitSQL(tc.statement)
- require.NoError(t, err)
- require.Equal(t, len(tc.want), len(list), "statement count mismatch")
-
- for i, want := range tc.want {
- got := list[i]
- require.Equal(t, want.Text, got.Text, "Text mismatch at index %d", i)
- require.Equal(t, want.BaseLine, got.BaseLine, "BaseLine mismatch at index %d", i)
- require.NotNil(t, got.Start, "Start should not be nil at index %d", i)
- require.Equal(t, want.Start.Line, got.Start.Line, "Start.Line mismatch at index %d", i)
- require.Equal(t, want.Start.Column, got.Start.Column, "Start.Column mismatch at index %d", i)
- require.NotNil(t, got.End, "End should not be nil at index %d", i)
- require.Equal(t, want.End.Line, got.End.Line, "End.Line mismatch at index %d", i)
- require.Equal(t, want.End.Column, got.End.Column, "End.Column mismatch at index %d", i)
- if want.Range != nil {
- require.Equal(t, want.Range.Start, got.Range.Start, "Range.Start mismatch at index %d", i)
- require.Equal(t, want.Range.End, got.Range.End, "Range.End mismatch at index %d", i)
- }
- }
- })
- }
+func TestDorisSplitSQL(t *testing.T) {
+ base.RunSplitTests(t, "test-data/test_split.yaml", base.SplitTestOptions{
+ SplitFunc: SplitSQL,
+ })
}
diff --git a/backend/plugin/parser/doris/test-data/test_split.yaml b/backend/plugin/parser/doris/test-data/test_split.yaml
new file mode 100644
index 0000000000..3d29e14d8c
--- /dev/null
+++ b/backend/plugin/parser/doris/test-data/test_split.yaml
@@ -0,0 +1,51 @@
+- description: "simple single statement"
+ input: "SELECT 1;"
+ result:
+ - text: "SELECT 1;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 10}
+ range: {start: 0, end: 9}
+ empty: false
+
+- description: "multiple statements on one line"
+ input: "SELECT 1; SELECT 2;"
+ result:
+ - text: "SELECT 1;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 10}
+ range: {start: 0, end: 9}
+ empty: false
+ - text: " SELECT 2;"
+ baseline: 0
+ start: {line: 1, column: 11}
+ end: {line: 1, column: 20}
+ range: {start: 9, end: 19}
+ empty: false
+
+- description: "multiple statements on multiple lines"
+ input: "SELECT 1;\nSELECT 2;"
+ result:
+ - text: "SELECT 1;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 10}
+ range: {start: 0, end: 9}
+ empty: false
+ - text: "\nSELECT 2;"
+ baseline: 0
+ start: {line: 2, column: 1}
+ end: {line: 2, column: 10}
+ range: {start: 9, end: 19}
+ empty: false
+
+- description: "multi-line statement"
+ input: "SELECT\n 1;"
+ result:
+ - text: "SELECT\n 1;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 2, column: 5}
+ range: {start: 0, end: 11}
+ empty: false
diff --git a/backend/plugin/parser/mysql/split_test.go b/backend/plugin/parser/mysql/split_test.go
index 414774fce3..afd971ae78 100644
--- a/backend/plugin/parser/mysql/split_test.go
+++ b/backend/plugin/parser/mysql/split_test.go
@@ -1,676 +1,20 @@
package mysql
import (
- "fmt"
- "math/rand"
"testing"
- "time"
"github.com/antlr4-go/antlr/v4"
"github.com/stretchr/testify/require"
parser "github.com/bytebase/parser/mysql"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-type splitTestData struct {
- statement string
- want resData
-}
-
-type resData struct {
- res []base.Statement
- err string
-}
-
-func generateOneMBInsert() string {
- var rand = rand.New(rand.NewSource(time.Now().UnixNano()))
- letterList := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
- b := make([]byte, 1024*1024)
- for i := range b {
- b[i] = letterList[rand.Intn(len(letterList))]
- }
- return fmt.Sprintf("INSERT INTO t values('%s')", string(b))
-}
-
-func TestMySQLSplitMultiSQL(t *testing.T) {
- bigSQL := generateOneMBInsert()
- tests := []splitTestData{
- {
- statement: `
- DELIMITER ;;
- CREATE PROCEDURE dorepeat(p1 INT)
- BEGIN
- DECLARE x INT;
- SET x = 0;
- label1: WHILE x < p1 DO
- SET x = x + 1;
- END WHILE label1;
- END;;
- DELIMITER ;
- CALL dorepeat(1000);
- SELECT x;
- `,
- want: resData{
- res: []base.Statement{
- {
- Text: ` CREATE PROCEDURE dorepeat(p1 INT)
- BEGIN
- DECLARE x INT;
- SET x = 0;
- label1: WHILE x < p1 DO
- SET x = x + 1;
- END WHILE label1;
- END;`,
- BaseLine: 2,
- Start: &storepb.Position{Line: 3, Column: 4},
- End: &storepb.Position{Line: 10, Column: 9},
- Range: &storepb.Range{Start: 17, End: 175},
- },
- {
- Text: ` CALL dorepeat(1000);`,
- BaseLine: 11,
- Start: &storepb.Position{Line: 12, Column: 4},
- End: &storepb.Position{Line: 12, Column: 24},
- Range: &storepb.Range{Start: 191, End: 214},
- },
- {
- Text: `
- SELECT x;`,
- BaseLine: 11,
- Start: &storepb.Position{Line: 13, Column: 4},
- End: &storepb.Position{Line: 13, Column: 13},
- Range: &storepb.Range{Start: 214, End: 227},
- },
- {
- 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: 14, Column: 4},
- End: &storepb.Position{Line: 14, Column: 4},
- Empty: true,
- Range: &storepb.Range{Start: 227, End: 231},
- },
- },
- },
- },
- {
- // 20 IF symbol
- statement: `
- SELECT
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- )
- FROM t; SELECT * FROM t;`,
- want: resData{
- res: []base.Statement{
- {
- Text: `
- SELECT
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- ),
- IF (
- age < 18,
- 'child',
- 'adult'
- )
- FROM t;`,
- Start: &storepb.Position{Line: 2, Column: 4},
- End: &storepb.Position{Line: 103, Column: 11},
- Range: &storepb.Range{Start: 0, End: 1080},
- },
- {
- Text: " SELECT * FROM t;",
- BaseLine: 102,
- Start: &storepb.Position{Line: 103, Column: 12},
- End: &storepb.Position{Line: 103, Column: 28},
- Range: &storepb.Range{Start: 1080, End: 1097},
- },
- },
- },
- },
- {
- statement: `select * from t;select "\"" where true;`,
- want: resData{
- res: []base.Statement{
- {
- Text: `select * from t;`,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 17},
- Range: &storepb.Range{Start: 0, End: 16},
- },
- {
- Text: `select "\"" where true;`,
- Start: &storepb.Position{Line: 1, Column: 17},
- End: &storepb.Position{Line: 1, Column: 40},
- Range: &storepb.Range{Start: 16, End: 39},
- },
- },
- },
- },
- {
- statement: `-- klsjdfjasldf
- -- klsjdflkjaskldfj
-`,
- want: resData{
- res: []base.Statement{
- {
- Text: "-- klsjdfjasldf\n\t\t\t-- klsjdflkjaskldfj\n",
- Start: &storepb.Position{Line: 3, Column: 1},
- End: &storepb.Position{Line: 2, Column: 24},
- Empty: true,
- Range: &storepb.Range{Start: 0, End: 39},
- },
- },
- },
- },
- {
- statement: `select * from t;
- /* sdfasdf */`,
- want: resData{
- res: []base.Statement{
- {
- Text: `select * from t;`,
- End: &storepb.Position{Line: 1, Column: 17},
- Start: &storepb.Position{Line: 1, Column: 1},
- Range: &storepb.Range{Start: 0, End: 16},
- },
- {
- Text: "\n\t\t\t/* sdfasdf */",
- Start: &storepb.Position{Line: 2, Column: 17},
- End: &storepb.Position{Line: 2, Column: 17},
- Empty: true,
- Range: &storepb.Range{Start: 16, End: 33},
- },
- },
- },
- },
- {
- statement: `select * from t;
- /* sdfasdf */;
- select * from t;`,
- want: resData{
- res: []base.Statement{
- {
- Text: `select * from t;`,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 17},
- Range: &storepb.Range{Start: 0, End: 16},
- },
- {
- Text: "\n\t\t\t/* sdfasdf */;",
- End: &storepb.Position{Line: 2, Column: 18},
- Start: &storepb.Position{Line: 2, Column: 17},
- Empty: true,
- Range: &storepb.Range{Start: 16, End: 34},
- },
- {
- Text: "\n\t\t\tselect * from t;",
- BaseLine: 1,
- End: &storepb.Position{Line: 3, Column: 20},
- Start: &storepb.Position{Line: 3, Column: 4},
- Range: &storepb.Range{Start: 34, End: 54},
- },
- },
- },
- },
- {
- statement: "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int\n" +
- `BEGIN
-
- DECLARE income INT;
-
- SET income = 0;
-
- label1: WHILE income <= 3000 DO
- SET income = income + starting_value;
- END WHILE label1;
-
- RETURN income;
-
- END ;`,
- want: resData{
- res: []base.Statement{
- {
- Text: "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int\n" +
- `BEGIN
-
- DECLARE income INT;
-
- SET income = 0;
-
- label1: WHILE income <= 3000 DO
- SET income = income + starting_value;
- END WHILE label1;
-
- RETURN income;
-
- END ;`,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 14, Column: 8},
- Range: &storepb.Range{Start: 0, End: 268},
- },
- },
- },
- },
- {
- statement: bigSQL,
- want: resData{
- res: []base.Statement{
- {
- Text: bigSQL,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: int32(len(bigSQL) + 1)},
- Range: &storepb.Range{Start: 0, End: int32(len(bigSQL))},
- },
- },
- },
- },
- {
- statement: " CREATE TABLE t(a int); CREATE TABLE t1(a int)",
- want: resData{
- res: []base.Statement{
- {
- Text: " CREATE TABLE t(a int);",
- Start: &storepb.Position{Line: 1, Column: 5},
- End: &storepb.Position{Line: 1, Column: 27},
- Range: &storepb.Range{Start: 0, End: 26},
- },
- {
- Text: " CREATE TABLE t1(a int)",
- Start: &storepb.Position{Line: 1, Column: 28},
- End: &storepb.Position{Line: 1, Column: 50},
- Range: &storepb.Range{Start: 26, End: 49},
- },
- },
- },
- },
- {
- statement: "CREATE TABLE `tech_Book`(id int, name varchar(255));\n" +
- "INSERT INTO `tech_Book` VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafa\\'jdfl;\"ka');",
- want: resData{
- res: []base.Statement{
- {
- Text: "CREATE TABLE `tech_Book`(id int, name varchar(255));",
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 53},
- Range: &storepb.Range{Start: 0, End: 52},
- },
- {
- Text: "\nINSERT INTO `tech_Book` VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafa\\'jdfl;\"ka');",
- Start: &storepb.Position{Line: 2, Column: 1},
- End: &storepb.Position{Line: 2, Column: 79},
- Range: &storepb.Range{Start: 52, End: 131},
- },
- },
- },
- },
- {
- statement: `
- /* this is the comment. */
- CREATE /* inline comment */TABLE tech_Book(id int, name varchar(255));
- -- this is the comment.
- INSERT INTO tech_Book VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafa\'jdfl;"ka');
- # this is the comment.
- INSERT INTO tech_Book VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafa\'jdfl;"ka');`,
- want: resData{
- res: []base.Statement{
- {
- 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: 3, Column: 7},
- End: &storepb.Position{Line: 3, Column: 77},
- Range: &storepb.Range{Start: 0, End: 110},
- },
- {
- 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: 5, Column: 7},
- BaseLine: 2,
- End: &storepb.Position{Line: 5, Column: 83},
- Range: &storepb.Range{Start: 110, End: 223},
- },
- {
- 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: 7, Column: 7},
- End: &storepb.Position{Line: 7, Column: 83},
- Range: &storepb.Range{Start: 223, End: 335},
- },
- },
- },
- },
- {
- statement: `# test for defining stored programs
- CREATE PROCEDURE dorepeat(p1 INT)
- BEGIN
- SET @x = 0;
- REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;
- END
- ;
- CALL dorepeat(1000);
- SELECT @x;
- `,
- want: resData{
- res: []base.Statement{
- {
- 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: 2, Column: 7},
- End: &storepb.Position{Line: 7, Column: 8},
- Range: &storepb.Range{Start: 0, End: 181},
- },
- {
- Text: "\n\t\t\t\t\t\tCALL dorepeat(1000);",
- BaseLine: 6,
- Start: &storepb.Position{Line: 8, Column: 7},
- End: &storepb.Position{Line: 8, Column: 27},
- Range: &storepb.Range{Start: 181, End: 208},
- },
- {
- Text: "\n\t\t\t\t\t\tSELECT @x;",
- BaseLine: 7,
- Start: &storepb.Position{Line: 9, Column: 7},
- End: &storepb.Position{Line: 9, Column: 17},
- Range: &storepb.Range{Start: 208, End: 225},
- },
- {
- Text: "\n\t\t\t\t\t\t",
- BaseLine: 8,
- 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: 10, Column: 7},
- Empty: true,
- Range: &storepb.Range{Start: 225, End: 232},
- },
- },
- },
- },
- {
- statement: `# test for defining stored programs
- CREATE PROCEDURE dorepeat(p1 INT)
- /* This is a comment */
- BEGIN
- SET @x = 0;
- REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;
- END
- ;
- CALL dorepeat(1000);
- SELECT @x;
- `,
- want: resData{
- res: []base.Statement{
- {
- 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: 8, Column: 8},
- Start: &storepb.Position{Line: 2, Column: 7},
- Range: &storepb.Range{Start: 0, End: 211},
- },
- {
- Text: "\n\t\t\t\t\t\tCALL dorepeat(1000);",
- BaseLine: 7,
- Start: &storepb.Position{Line: 9, Column: 7},
- End: &storepb.Position{Line: 9, Column: 27},
- Range: &storepb.Range{Start: 211, End: 238},
- },
- {
- Text: "\n\t\t\t\t\t\tSELECT @x;",
- BaseLine: 8,
- Start: &storepb.Position{Line: 10, Column: 7},
- End: &storepb.Position{Line: 10, Column: 17},
- Range: &storepb.Range{Start: 238, End: 255},
- },
- {
- Text: "\n\t\t\t\t\t\t",
- BaseLine: 9,
- Start: &storepb.Position{Line: 11, Column: 7},
- End: &storepb.Position{Line: 11, Column: 7},
- Empty: true,
- Range: &storepb.Range{Start: 255, End: 262},
- },
- },
- },
- },
- {
- // test for Windows
- statement: `CREATE TABLE t` + "\r\n" + `(a int);` + "\r\n" + `CREATE TABLE t1(b int);`,
- want: resData{
- res: []base.Statement{
- {
- Text: "CREATE TABLE t\r\n(a int);",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 2, Column: 9},
- Range: &storepb.Range{Start: 0, End: 24},
- },
- {
- Text: "\r\nCREATE TABLE t1(b int);",
- BaseLine: 1,
- Start: &storepb.Position{Line: 3, Column: 1},
- End: &storepb.Position{Line: 3, Column: 24},
- Range: &storepb.Range{Start: 24, End: 49},
- },
- },
- },
- },
- {
- statement: "SELECT * FROM 表名; INSERT INTO 表 VALUES (1);",
- want: resData{
- res: []base.Statement{
- {
- Text: "SELECT * FROM 表名;",
- Range: &storepb.Range{Start: 0, End: 21}, // Byte offset 0-21 (not 0-17)
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 18},
- },
- {
- Text: " INSERT INTO 表 VALUES (1);",
- Range: &storepb.Range{Start: 21, End: 49}, // Byte offset 21-49 (not 17-43)
- Start: &storepb.Position{Line: 1, Column: 19},
- End: &storepb.Position{Line: 1, Column: 44},
- },
- },
- },
- },
- }
-
- for _, test := range tests {
- res, err := SplitSQL(test.statement)
- errStr := ""
- if err != nil {
- errStr = err.Error()
- }
- require.Equal(t, test.want, resData{res, errStr}, test.statement)
- }
+func TestMySQLSplitSQL(t *testing.T) {
+ base.RunSplitTests(t, "test-data/test_split.yaml", base.SplitTestOptions{
+ SplitFunc: SplitSQL,
+ })
}
func TestSplitMySQLStatements(t *testing.T) {
diff --git a/backend/plugin/parser/mysql/test-data/test_split.yaml b/backend/plugin/parser/mysql/test-data/test_split.yaml
new file mode 100644
index 0000000000..2c2b35db29
--- /dev/null
+++ b/backend/plugin/parser/mysql/test-data/test_split.yaml
@@ -0,0 +1,243 @@
+- description: "DELIMITER with stored procedure"
+ input: "\n\t\t\tDELIMITER ;;\n\t\t\tCREATE PROCEDURE dorepeat(p1 INT)\n\t\t\tBEGIN\n\t\t\t\tDECLARE x INT;\n\t\t\t\tSET x = 0;\n\t\t\t\tlabel1: WHILE x < p1 DO\n\t\t\t\t\tSET x = x + 1;\n\t\t\t\tEND WHILE label1;\n\t\t\tEND;;\n\t\t\tDELIMITER ;\n\t\t\tCALL dorepeat(1000);\n\t\t\tSELECT x;\n\t\t\t"
+ result:
+ - text: "\t\t\tCREATE PROCEDURE dorepeat(p1 INT)\n\t\t\tBEGIN\n\t\t\t\tDECLARE x INT;\n\t\t\t\tSET x = 0;\n\t\t\t\tlabel1: WHILE x < p1 DO\n\t\t\t\t\tSET x = x + 1;\n\t\t\t\tEND WHILE label1;\n\t\t\tEND;"
+ baseline: 2
+ start: {line: 3, column: 4}
+ end: {line: 10, column: 9}
+ range: {start: 17, end: 175}
+ empty: false
+ - text: "\t\t\tCALL dorepeat(1000);"
+ baseline: 11
+ start: {line: 12, column: 4}
+ end: {line: 12, column: 24}
+ range: {start: 191, end: 214}
+ empty: false
+ - text: "\n\t\t\tSELECT x;"
+ baseline: 11
+ start: {line: 13, column: 4}
+ end: {line: 13, column: 13}
+ range: {start: 214, end: 227}
+ empty: false
+ - text: "\n\t\t\t"
+ baseline: 12
+ start: {line: 14, column: 4}
+ end: {line: 14, column: 4}
+ range: {start: 227, end: 231}
+ empty: true
+
+- description: "escaped double quote in string"
+ input: "select * from t;select \"\\\"\" where true;"
+ result:
+ - text: "select * from t;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 17}
+ range: {start: 0, end: 16}
+ empty: false
+ - text: "select \"\\\"\" where true;"
+ baseline: 0
+ start: {line: 1, column: 17}
+ end: {line: 1, column: 40}
+ range: {start: 16, end: 39}
+ empty: false
+
+- description: "comments only creates empty statement"
+ input: "-- klsjdfjasldf\n\t\t\t-- klsjdflkjaskldfj\n"
+ result:
+ - text: "-- klsjdfjasldf\n\t\t\t-- klsjdflkjaskldfj\n"
+ baseline: 0
+ start: {line: 3, column: 1}
+ end: {line: 2, column: 24}
+ range: {start: 0, end: 39}
+ empty: true
+
+- description: "statement followed by trailing block comment"
+ input: "select * from t;\n\t\t\t/* sdfasdf */"
+ result:
+ - text: "select * from t;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 17}
+ range: {start: 0, end: 16}
+ empty: false
+ - text: "\n\t\t\t/* sdfasdf */"
+ baseline: 0
+ start: {line: 2, column: 17}
+ end: {line: 2, column: 17}
+ range: {start: 16, end: 33}
+ empty: true
+
+- description: "statement then empty comment statement then statement"
+ input: "select * from t;\n\t\t\t/* sdfasdf */;\n\t\t\tselect * from t;"
+ result:
+ - text: "select * from t;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 17}
+ range: {start: 0, end: 16}
+ empty: false
+ - text: "\n\t\t\t/* sdfasdf */;"
+ baseline: 0
+ start: {line: 2, column: 17}
+ end: {line: 2, column: 18}
+ range: {start: 16, end: 34}
+ empty: true
+ - text: "\n\t\t\tselect * from t;"
+ baseline: 1
+ start: {line: 3, column: 4}
+ end: {line: 3, column: 20}
+ range: {start: 34, end: 54}
+ empty: false
+
+- description: "function with BEGIN END block"
+ input: "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int\nBEGIN\n\n\t\t DECLARE income INT;\n\n\t\t SET income = 0;\n\n\t\t label1: WHILE income <= 3000 DO\n\t\t\t SET income = income + starting_value;\n\t\t END WHILE label1;\n\n\t\t RETURN income;\n\n\t\tEND ;"
+ result:
+ - text: "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int\nBEGIN\n\n\t\t DECLARE income INT;\n\n\t\t SET income = 0;\n\n\t\t label1: WHILE income <= 3000 DO\n\t\t\t SET income = income + starting_value;\n\t\t END WHILE label1;\n\n\t\t RETURN income;\n\n\t\tEND ;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 14, column: 8}
+ range: {start: 0, end: 268}
+ empty: false
+
+- description: "multiple statements with leading whitespace and no trailing semicolon"
+ input: " CREATE TABLE t(a int); CREATE TABLE t1(a int)"
+ result:
+ - text: " CREATE TABLE t(a int);"
+ baseline: 0
+ start: {line: 1, column: 5}
+ end: {line: 1, column: 27}
+ range: {start: 0, end: 26}
+ empty: false
+ - text: " CREATE TABLE t1(a int)"
+ baseline: 0
+ start: {line: 1, column: 28}
+ end: {line: 1, column: 50}
+ range: {start: 26, end: 49}
+ empty: false
+
+- description: "backtick identifiers with escaped strings"
+ input: "CREATE TABLE `tech_Book`(id int, name varchar(255));\nINSERT INTO `tech_Book` VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafa\\'jdfl;\"ka');"
+ result:
+ - text: "CREATE TABLE `tech_Book`(id int, name varchar(255));"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 53}
+ range: {start: 0, end: 52}
+ empty: false
+ - text: "\nINSERT INTO `tech_Book` VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafa\\'jdfl;\"ka');"
+ baseline: 0
+ start: {line: 2, column: 1}
+ end: {line: 2, column: 79}
+ range: {start: 52, end: 131}
+ empty: false
+
+- description: "block comment, line comment, and hash comment"
+ input: "\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));\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');\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');"
+ result:
+ - 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));"
+ baseline: 0
+ start: {line: 3, column: 7}
+ end: {line: 3, column: 77}
+ range: {start: 0, end: 110}
+ empty: false
+ - 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: 2
+ start: {line: 5, column: 7}
+ end: {line: 5, column: 83}
+ range: {start: 110, end: 223}
+ empty: false
+ - 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: {line: 7, column: 7}
+ end: {line: 7, column: 83}
+ range: {start: 223, end: 335}
+ empty: false
+
+- description: "procedure with REPEAT and hash comment"
+ input: "# 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;\n\t\t\t\t\t\tCALL dorepeat(1000);\n\t\t\t\t\t\tSELECT @x;\n\t\t\t\t\t\t"
+ result:
+ - 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;"
+ baseline: 0
+ start: {line: 2, column: 7}
+ end: {line: 7, column: 8}
+ range: {start: 0, end: 181}
+ empty: false
+ - text: "\n\t\t\t\t\t\tCALL dorepeat(1000);"
+ baseline: 6
+ start: {line: 8, column: 7}
+ end: {line: 8, column: 27}
+ range: {start: 181, end: 208}
+ empty: false
+ - text: "\n\t\t\t\t\t\tSELECT @x;"
+ baseline: 7
+ start: {line: 9, column: 7}
+ end: {line: 9, column: 17}
+ range: {start: 208, end: 225}
+ empty: false
+ - text: "\n\t\t\t\t\t\t"
+ baseline: 8
+ start: {line: 10, column: 7}
+ end: {line: 10, column: 7}
+ range: {start: 225, end: 232}
+ empty: true
+
+- description: "procedure with block comment inside"
+ input: "# 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;\n\t\t\t\t\t\tCALL dorepeat(1000);\n\t\t\t\t\t\tSELECT @x;\n\t\t\t\t\t\t"
+ result:
+ - 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;"
+ baseline: 0
+ start: {line: 2, column: 7}
+ end: {line: 8, column: 8}
+ range: {start: 0, end: 211}
+ empty: false
+ - text: "\n\t\t\t\t\t\tCALL dorepeat(1000);"
+ baseline: 7
+ start: {line: 9, column: 7}
+ end: {line: 9, column: 27}
+ range: {start: 211, end: 238}
+ empty: false
+ - text: "\n\t\t\t\t\t\tSELECT @x;"
+ baseline: 8
+ start: {line: 10, column: 7}
+ end: {line: 10, column: 17}
+ range: {start: 238, end: 255}
+ empty: false
+ - text: "\n\t\t\t\t\t\t"
+ baseline: 9
+ start: {line: 11, column: 7}
+ end: {line: 11, column: 7}
+ range: {start: 255, end: 262}
+ empty: true
+
+- description: "CRLF line endings"
+ input: "CREATE TABLE t\r\n(a int);\r\nCREATE TABLE t1(b int);"
+ result:
+ - text: "CREATE TABLE t\r\n(a int);"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 2, column: 9}
+ range: {start: 0, end: 24}
+ empty: false
+ - text: "\r\nCREATE TABLE t1(b int);"
+ baseline: 1
+ start: {line: 3, column: 1}
+ end: {line: 3, column: 24}
+ range: {start: 24, end: 49}
+ empty: false
+
+- description: "unicode in table names"
+ input: "SELECT * FROM 表名; INSERT INTO 表 VALUES (1);"
+ result:
+ - text: "SELECT * FROM 表名;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 18}
+ range: {start: 0, end: 21}
+ empty: false
+ - text: " INSERT INTO 表 VALUES (1);"
+ baseline: 0
+ start: {line: 1, column: 19}
+ end: {line: 1, column: 44}
+ range: {start: 21, end: 49}
+ empty: false
diff --git a/backend/plugin/parser/partiql/split_test.go b/backend/plugin/parser/partiql/split_test.go
index 47868d90fe..5f3a24a71a 100644
--- a/backend/plugin/parser/partiql/split_test.go
+++ b/backend/plugin/parser/partiql/split_test.go
@@ -3,62 +3,11 @@ package partiql
import (
"testing"
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-func TestSplitSQL(t *testing.T) {
- statement := `DELETE FROM Music WHERE Artist = 'Pink Floyd' AND SongTitle = 'Money';
- INSERT INTO Music VALUE {'AlbumTitle': 'The Dark Side of the Moon', 'Artist': 'Pink Floyd', 'Awards': 300, 'SongTitle': 'Money'};
- INSERT INTO Music VALUE {'AlbumTitle': 'The Dark Side of the Moon', 'Artist': 'Pink Floyd', 'Awards': 300, 'SongTitle': 'Money02'};
- INSERT INTO Music VALUE {'AlbumTitle': 'The Dark Side of the Moon', 'Artist': 'Pink Floyd', 'Awards': 300, 'SongTitle': 'Money03'};
- DELETE FROM Music WHERE Artist = 'Pink Floyd' AND SongTitle = 'Money02'`
- want := []base.Statement{
- {
- Text: "DELETE FROM Music WHERE Artist = 'Pink Floyd' AND SongTitle = 'Money';",
- BaseLine: 0,
- Range: &storepb.Range{Start: 0, End: 70},
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 71},
- Empty: false,
- },
- {
- Text: "\n\tINSERT INTO Music VALUE {'AlbumTitle': 'The Dark Side of the Moon', 'Artist': 'Pink Floyd', 'Awards': 300, 'SongTitle': 'Money'};",
- BaseLine: 0,
- Range: &storepb.Range{Start: 70, End: 201},
- Start: &storepb.Position{Line: 2, Column: 2},
- End: &storepb.Position{Line: 2, Column: 131},
- Empty: false,
- },
- {
- Text: "\n\tINSERT INTO Music VALUE {'AlbumTitle': 'The Dark Side of the Moon', 'Artist': 'Pink Floyd', 'Awards': 300, 'SongTitle': 'Money02'};",
- BaseLine: 1,
- Range: &storepb.Range{Start: 201, End: 334},
- Start: &storepb.Position{Line: 3, Column: 2},
- End: &storepb.Position{Line: 3, Column: 133},
- Empty: false,
- },
- {
- Text: "\n\tINSERT INTO Music VALUE {'AlbumTitle': 'The Dark Side of the Moon', 'Artist': 'Pink Floyd', 'Awards': 300, 'SongTitle': 'Money03'};",
- BaseLine: 2,
- Range: &storepb.Range{Start: 334, End: 467},
- Start: &storepb.Position{Line: 4, Column: 2},
- End: &storepb.Position{Line: 4, Column: 133},
- Empty: false,
- },
- {
- Text: "\n\tDELETE FROM Music WHERE Artist = 'Pink Floyd' AND SongTitle = 'Money02'",
- BaseLine: 3,
- Range: &storepb.Range{Start: 467, End: 540},
- Start: &storepb.Position{Line: 5, Column: 2},
- End: &storepb.Position{Line: 5, Column: 73},
- Empty: false,
- },
- }
-
- list, err := SplitSQL(statement)
- require.NoError(t, err)
- require.Equal(t, want, list)
+func TestPartiQLSplitSQL(t *testing.T) {
+ base.RunSplitTests(t, "test-data/test_split.yaml", base.SplitTestOptions{
+ SplitFunc: SplitSQL,
+ })
}
diff --git a/backend/plugin/parser/partiql/test-data/test_split.yaml b/backend/plugin/parser/partiql/test-data/test_split.yaml
new file mode 100644
index 0000000000..3962b333cb
--- /dev/null
+++ b/backend/plugin/parser/partiql/test-data/test_split.yaml
@@ -0,0 +1,33 @@
+- description: "multiple DML statements"
+ input: "DELETE FROM Music WHERE Artist = 'Pink Floyd' AND SongTitle = 'Money';\n\tINSERT INTO Music VALUE {'AlbumTitle': 'The Dark Side of the Moon', 'Artist': 'Pink Floyd', 'Awards': 300, 'SongTitle': 'Money'};\n\tINSERT INTO Music VALUE {'AlbumTitle': 'The Dark Side of the Moon', 'Artist': 'Pink Floyd', 'Awards': 300, 'SongTitle': 'Money02'};\n\tINSERT INTO Music VALUE {'AlbumTitle': 'The Dark Side of the Moon', 'Artist': 'Pink Floyd', 'Awards': 300, 'SongTitle': 'Money03'};\n\tDELETE FROM Music WHERE Artist = 'Pink Floyd' AND SongTitle = 'Money02'"
+ result:
+ - text: "DELETE FROM Music WHERE Artist = 'Pink Floyd' AND SongTitle = 'Money';"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 71}
+ range: {start: 0, end: 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: {line: 2, column: 2}
+ end: {line: 2, column: 131}
+ range: {start: 70, end: 201}
+ 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: {line: 3, column: 2}
+ end: {line: 3, column: 133}
+ range: {start: 201, end: 334}
+ 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: {line: 4, column: 2}
+ end: {line: 4, column: 133}
+ range: {start: 334, end: 467}
+ empty: false
+ - text: "\n\tDELETE FROM Music WHERE Artist = 'Pink Floyd' AND SongTitle = 'Money02'"
+ baseline: 3
+ start: {line: 5, column: 2}
+ end: {line: 5, column: 73}
+ range: {start: 467, end: 540}
+ empty: false
diff --git a/backend/plugin/parser/pg/split_test.go b/backend/plugin/parser/pg/split_test.go
index 3f79905cc9..5e8fba118a 100644
--- a/backend/plugin/parser/pg/split_test.go
+++ b/backend/plugin/parser/pg/split_test.go
@@ -1,373 +1,13 @@
package pg
import (
- "fmt"
- "math/rand"
"testing"
- "time"
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-type splitTestData struct {
- statement string
- want resData
-}
-
-type resData struct {
- res []base.Statement
- err string
-}
-
-func generateOneMBInsert() string {
- var rand = rand.New(rand.NewSource(time.Now().UnixNano()))
- letterList := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
- b := make([]byte, 1024*1024)
- for i := range b {
- b[i] = letterList[rand.Intn(len(letterList))]
- }
- return fmt.Sprintf("INSERT INTO t values('%s')", string(b))
-}
-
-func TestPGSplitMultiSQL(t *testing.T) {
- bigSQL := generateOneMBInsert()
- tests := []splitTestData{
- {
- statement: `-- klsjdfjasldf
- -- klsjdflkjaskldfj
- `,
- want: resData{
- res: []base.Statement{
- {
- Text: "-- klsjdfjasldf\n\t\t\t-- klsjdflkjaskldfj\n\t\t\t",
- BaseLine: 0,
- Range: &storepb.Range{Start: 0, End: 42},
- Start: &storepb.Position{Line: 3, Column: 4},
- End: &storepb.Position{Line: 3, Column: 4},
- Empty: true,
- },
- },
- },
- },
- {
- statement: `select * from t;
- /* sdfasdf */`,
- want: resData{
- res: []base.Statement{
- {
- Text: `select * from t;`,
- Range: &storepb.Range{Start: 0, End: 16},
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 17},
- },
- {
- Text: "\n\t\t\t/* sdfasdf */",
- Range: &storepb.Range{Start: 16, End: 33},
- Start: &storepb.Position{Line: 2, Column: 17},
- End: &storepb.Position{Line: 2, Column: 17},
- Empty: true,
- },
- },
- },
- },
- {
- statement: `select * from t;
- /* sdfasdf */;
- select * from t;`,
- want: resData{
- res: []base.Statement{
- {
- Text: `select * from t;`,
- Range: &storepb.Range{Start: 0, End: 16},
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 17},
- },
- {
- Text: "\n\t\t\t/* sdfasdf */;",
- Range: &storepb.Range{Start: 16, End: 34},
- Start: &storepb.Position{Line: 2, Column: 17},
- End: &storepb.Position{Line: 2, Column: 18},
- Empty: true,
- },
- {
- Text: "\n\t\t\tselect * from t;",
- BaseLine: 1,
- Range: &storepb.Range{Start: 34, End: 54},
- Start: &storepb.Position{Line: 3, Column: 4},
- End: &storepb.Position{Line: 3, Column: 20},
- },
- },
- },
- },
- {
- statement: bigSQL,
- want: resData{
- res: []base.Statement{
- {
- Text: bigSQL,
- Range: &storepb.Range{Start: 0, End: 1048600},
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: int32(len(bigSQL) + 1)},
- },
- },
- },
- },
- {
- statement: " CREATE TABLE t(a int); CREATE TABLE t1(a int)",
- want: resData{
- res: []base.Statement{
- {
- Text: " CREATE TABLE t(a int);",
- Range: &storepb.Range{Start: 0, End: 26},
- Start: &storepb.Position{Line: 1, Column: 5},
- End: &storepb.Position{Line: 1, Column: 27},
- },
- {
- Text: " CREATE TABLE t1(a int)",
- Range: &storepb.Range{Start: 26, End: 49},
- Start: &storepb.Position{Line: 1, Column: 28},
- End: &storepb.Position{Line: 1, Column: 50},
- },
- },
- },
- },
- {
- statement: `CREATE TABLE "tech_Book"(id int, name varchar(255));
- INSERT INTO "tech_Book" VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafajdfl;"ka');`,
- want: resData{
- res: []base.Statement{
- {
- Text: `CREATE TABLE "tech_Book"(id int, name varchar(255));`,
- Range: &storepb.Range{Start: 0, End: 52},
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 53},
- },
- {
- Text: "\n\t\t\t\t\t\tINSERT INTO \"tech_Book\" VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafajdfl;\"ka');",
- Range: &storepb.Range{Start: 52, End: 135},
- Start: &storepb.Position{Line: 2, Column: 7},
- End: &storepb.Position{Line: 2, Column: 83},
- },
- },
- },
- },
- {
- statement: `
- /* this is the comment. */
- CREATE /* inline comment */TABLE "tech_Book"(id int, name varchar(255));
- -- this is the comment.
- INSERT INTO "tech_Book" VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafajdfl;"ka');`,
- want: resData{
- res: []base.Statement{
- {
- 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));",
- Range: &storepb.Range{Start: 0, End: 112},
- Start: &storepb.Position{Line: 3, Column: 7},
- End: &storepb.Position{Line: 3, Column: 79},
- },
- {
- 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,
- Range: &storepb.Range{Start: 112, End: 225},
- Start: &storepb.Position{Line: 5, Column: 7},
- End: &storepb.Position{Line: 5, Column: 83},
- },
- },
- },
- },
- {
- statement: `CREATE PROCEDURE insert_data(a varchar(50), b varchar(50))
- LANGUAGE SQL
- AS $$
- /*this is the comment */
- INSERT /* inline comment */ INTO tbl VALUES ('lkjafd''lksjadlf;lk\\jasdflkasdf"asdklf\\');
- -- this is the comment
- INSERT INTO tbl VALUES ('fasf_bkdjlfa');
- $$;
- CREATE TABLE t(a int);`,
- want: resData{
- res: []base.Statement{
- {
- Text: `CREATE PROCEDURE insert_data(a varchar(50), b varchar(50))
- LANGUAGE SQL
- AS $$
- /*this is the comment */
- INSERT /* inline comment */ INTO tbl VALUES ('lkjafd''lksjadlf;lk\\jasdflkasdf"asdklf\\');
- -- this is the comment
- INSERT INTO tbl VALUES ('fasf_bkdjlfa');
- $$;`,
- Range: &storepb.Range{Start: 0, End: 303},
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 8, Column: 10},
- },
- {
- Text: "\n\t\t\t\t\t\tCREATE TABLE t(a int);",
- BaseLine: 7,
- Range: &storepb.Range{Start: 303, End: 332},
- Start: &storepb.Position{Line: 9, Column: 7},
- End: &storepb.Position{Line: 9, Column: 29},
- },
- },
- },
- },
- {
- statement: `CREATE PROCEDURE insert_data(a varchar(50), b varchar(50))
- LANGUAGE SQL
- AS $tag_name$
- /*this is the comment */
- INSERT /* inline comment */ INTO tbl VALUES ('lkjafd''lksjadlf;lkjasdflkasdf"asdklf');
- -- this is the comment
- INSERT INTO tbl VALUES ('fasf_bkdjlfa');
- $tag_name$;
- CREATE TABLE t(a int);`,
- want: resData{
- res: []base.Statement{
- {
- Text: `CREATE PROCEDURE insert_data(a varchar(50), b varchar(50))
- LANGUAGE SQL
- AS $tag_name$
- /*this is the comment */
- INSERT /* inline comment */ INTO tbl VALUES ('lkjafd''lksjadlf;lkjasdflkasdf"asdklf');
- -- this is the comment
- INSERT INTO tbl VALUES ('fasf_bkdjlfa');
- $tag_name$;`,
- Range: &storepb.Range{Start: 0, End: 315},
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 8, Column: 18},
- },
- {
- Text: "\n\t\t\t\t\t\tCREATE TABLE t(a int);",
- BaseLine: 7,
- Range: &storepb.Range{Start: 315, End: 344},
- Start: &storepb.Position{Line: 9, Column: 7},
- End: &storepb.Position{Line: 9, Column: 29},
- },
- },
- },
- },
- {
- // test for Windows
- statement: `CREATE TABLE t` + "\r\n" + `(a int);` + "\r\n" + `CREATE TABLE t1(b int);`,
- want: resData{
- res: []base.Statement{
- {
- Text: "CREATE TABLE t\r\n(a int);",
- Range: &storepb.Range{Start: 0, End: 24},
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 2, Column: 9},
- },
- {
- Text: "\r\nCREATE TABLE t1(b int);",
- BaseLine: 1,
- Range: &storepb.Range{Start: 24, End: 49},
- Start: &storepb.Position{Line: 3, Column: 1},
- End: &storepb.Position{Line: 3, Column: 24},
- },
- },
- },
- },
- {
- statement: `INSERT INTO "public"."table"("id","content")
- VALUES
- (12,'table column name () { :xna,sydfn,,kasdfyn;}; /////test string/// 0'),
- (133,'knuandfan public table id'';create table t(a int, b int);set @text=''\\\\kdaminxkljasdfiebkla.unkonwn''+''abcdef.xyz\\''; local xxxyy.abcddd.mysql @text;------- '),
- (1444,'table t xyz abc a''a\\\\\\\\''b"c>?>xxxxxx%}}%%>c<[[?${12344556778990{%}}cake\\');`,
- want: resData{
- res: []base.Statement{
- {
- 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\\\\');",
- Range: &storepb.Range{Start: 0, End: 400},
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 5, Column: 93},
- },
- },
- },
- },
- {
- statement: `INSERT INTO t VALUES ('klajfas)`,
- want: resData{
- res: []base.Statement{
- {
- Text: "INSERT INTO t VALUES ('klajfas)",
- Range: &storepb.Range{Start: 0, End: 31},
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 32},
- Empty: false,
- },
- },
- },
- },
- {
- statement: `INSERT INTO "t VALUES ('klajfas)`,
- want: resData{
- res: []base.Statement{
- {
- Text: "INSERT INTO \"t VALUES ('klajfas)",
- Range: &storepb.Range{Start: 0, End: 32},
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 33},
- },
- },
- },
- },
- {
- statement: `/*INSERT INTO "t VALUES ('klajfas)`,
- want: resData{
- res: []base.Statement{
- {
- Text: "/*INSERT INTO \"t VALUES ('klajfas)",
- BaseLine: 0,
- Range: &storepb.Range{Start: 0, End: 34},
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 35},
- Empty: false,
- },
- },
- },
- },
- {
- statement: `$$INSERT INTO "t VALUES ('klajfas)`,
- want: resData{
- res: []base.Statement{
- {
- Text: "$$INSERT INTO \"t VALUES ('klajfas)",
- Range: &storepb.Range{Start: 0, End: 34},
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 35},
- },
- },
- },
- },
- {
- statement: "SELECT * FROM 表名; INSERT INTO 表 VALUES (1);",
- want: resData{
- res: []base.Statement{
- {
- Text: "SELECT * FROM 表名;",
- Range: &storepb.Range{Start: 0, End: 21}, // Byte offset 0-21 (not 0-17)
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 18},
- },
- {
- Text: " INSERT INTO 表 VALUES (1);",
- Range: &storepb.Range{Start: 21, End: 49}, // Byte offset 21-49 (not 17-43)
- Start: &storepb.Position{Line: 1, Column: 19},
- End: &storepb.Position{Line: 1, Column: 44},
- },
- },
- },
- },
- }
-
- for _, test := range tests {
- res, err := SplitSQL(test.statement)
- errStr := ""
- if err != nil {
- errStr = err.Error()
- }
- require.Equal(t, test.want, resData{res, errStr}, test.statement)
- }
+func TestPGSplitSQL(t *testing.T) {
+ base.RunSplitTests(t, "test-data/test_split.yaml", base.SplitTestOptions{
+ SplitFunc: SplitSQL,
+ })
}
diff --git a/backend/plugin/parser/pg/test-data/test_split.yaml b/backend/plugin/parser/pg/test-data/test_split.yaml
new file mode 100644
index 0000000000..92e96fec00
--- /dev/null
+++ b/backend/plugin/parser/pg/test-data/test_split.yaml
@@ -0,0 +1,357 @@
+- description: "single statement with semicolon"
+ input: "SELECT * FROM users;"
+ result:
+ - text: "SELECT * FROM users;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 21}
+ range: {start: 0, end: 20}
+ empty: false
+
+- description: "single statement without semicolon"
+ input: "SELECT * FROM users"
+ result:
+ - text: "SELECT * FROM users"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 20}
+ range: {start: 0, end: 19}
+ empty: false
+
+- description: "multiple statements same line"
+ input: "SELECT 1; SELECT 2;"
+ result:
+ - text: "SELECT 1;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 10}
+ range: {start: 0, end: 9}
+ empty: false
+ - text: " SELECT 2;"
+ baseline: 0
+ start: {line: 1, column: 11}
+ end: {line: 1, column: 20}
+ range: {start: 9, end: 19}
+ empty: false
+
+- description: "leading whitespace preserved"
+ input: " SELECT 1; SELECT 2;"
+ result:
+ - text: " SELECT 1;"
+ baseline: 0
+ start: {line: 1, column: 5}
+ end: {line: 1, column: 14}
+ range: {start: 0, end: 13}
+ empty: false
+ - text: " SELECT 2;"
+ baseline: 0
+ start: {line: 1, column: 16}
+ end: {line: 1, column: 25}
+ range: {start: 13, end: 24}
+ empty: false
+
+- description: "unicode in table name and values"
+ input: "SELECT * FROM 表名; INSERT INTO 表 VALUES (1);"
+ result:
+ - text: "SELECT * FROM 表名;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 18}
+ range: {start: 0, end: 21}
+ empty: false
+ - text: " INSERT INTO 表 VALUES (1);"
+ baseline: 0
+ start: {line: 1, column: 19}
+ end: {line: 1, column: 44}
+ range: {start: 21, end: 49}
+ empty: false
+
+- description: "simple dollar-quoted string"
+ input: "SELECT $$hello; world$$;"
+ result:
+ - text: "SELECT $$hello; world$$;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 25}
+ range: {start: 0, end: 24}
+ empty: false
+
+- description: "tagged dollar-quoted string"
+ input: "SELECT $tag$hello; world$tag$;"
+ result:
+ - text: "SELECT $tag$hello; world$tag$;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 31}
+ range: {start: 0, end: 30}
+ empty: false
+
+- description: "dollar-quote does not split on internal semicolon"
+ input: "SELECT $$a;b$$; SELECT 2;"
+ result:
+ - text: "SELECT $$a;b$$;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 16}
+ range: {start: 0, end: 15}
+ empty: false
+ - text: " SELECT 2;"
+ baseline: 0
+ start: {line: 1, column: 17}
+ end: {line: 1, column: 26}
+ range: {start: 15, end: 25}
+ empty: false
+
+- description: "CRLF line endings"
+ input: "SELECT 1;\r\nSELECT 2;"
+ result:
+ - text: "SELECT 1;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 10}
+ range: {start: 0, end: 9}
+ empty: false
+ - text: "\r\nSELECT 2;"
+ baseline: 0
+ start: {line: 2, column: 1}
+ end: {line: 2, column: 10}
+ range: {start: 9, end: 20}
+ empty: false
+
+- description: "multi-line with CRLF"
+ input: "CREATE TABLE t\r\n(a int);\r\nCREATE TABLE t1(b int);"
+ result:
+ - text: "CREATE TABLE t\r\n(a int);"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 2, column: 9}
+ range: {start: 0, end: 24}
+ empty: false
+ - text: "\r\nCREATE TABLE t1(b int);"
+ baseline: 1
+ start: {line: 3, column: 1}
+ end: {line: 3, column: 24}
+ range: {start: 24, end: 49}
+ empty: false
+
+- description: "procedure with dollar-quoted body"
+ input: |-
+ CREATE PROCEDURE insert_data(a varchar(50))
+ LANGUAGE SQL
+ AS $$
+ INSERT INTO tbl VALUES ('test;value');
+ $$;
+ SELECT 1;
+ result:
+ - text: "CREATE PROCEDURE insert_data(a varchar(50))\nLANGUAGE SQL\nAS $$\n INSERT INTO tbl VALUES ('test;value');\n$$;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 5, column: 4}
+ range: {start: 0, end: 107}
+ empty: false
+ - text: "\nSELECT 1;"
+ baseline: 4
+ start: {line: 6, column: 1}
+ end: {line: 6, column: 10}
+ range: {start: 107, end: 117}
+ empty: false
+
+- description: "function with tagged dollar-quote"
+ input: |-
+ CREATE FUNCTION get_data()
+ RETURNS TEXT
+ AS $func$
+ SELECT 'value with $$ inside';
+ $func$;
+ SELECT 2;
+ result:
+ - text: "CREATE FUNCTION get_data()\nRETURNS TEXT\nAS $func$\n SELECT 'value with $$ inside';\n$func$;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 5, column: 8}
+ range: {start: 0, end: 90}
+ empty: false
+ - text: "\nSELECT 2;"
+ baseline: 4
+ start: {line: 6, column: 1}
+ end: {line: 6, column: 10}
+ range: {start: 90, end: 100}
+ empty: false
+
+- description: "comments only creates empty statement"
+ input: "-- klsjdfjasldf\n\t\t\t-- klsjdflkjaskldfj\n\t\t\t"
+ result:
+ - text: "-- klsjdfjasldf\n\t\t\t-- klsjdflkjaskldfj\n\t\t\t"
+ baseline: 0
+ start: {line: 3, column: 4}
+ end: {line: 3, column: 4}
+ range: {start: 0, end: 42}
+ empty: true
+
+- description: "statement followed by trailing comment"
+ input: "select * from t;\n\t\t\t/* sdfasdf */"
+ result:
+ - text: "select * from t;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 17}
+ range: {start: 0, end: 16}
+ empty: false
+ - text: "\n\t\t\t/* sdfasdf */"
+ baseline: 0
+ start: {line: 2, column: 17}
+ end: {line: 2, column: 17}
+ range: {start: 16, end: 33}
+ empty: true
+
+- description: "statement then empty comment statement then statement"
+ input: "select * from t;\n\t\t\t/* sdfasdf */;\n\t\t\tselect * from t;"
+ result:
+ - text: "select * from t;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 17}
+ range: {start: 0, end: 16}
+ empty: false
+ - text: "\n\t\t\t/* sdfasdf */;"
+ baseline: 0
+ start: {line: 2, column: 17}
+ end: {line: 2, column: 18}
+ range: {start: 16, end: 34}
+ empty: true
+ - text: "\n\t\t\tselect * from t;"
+ baseline: 1
+ start: {line: 3, column: 4}
+ end: {line: 3, column: 20}
+ range: {start: 34, end: 54}
+ empty: false
+
+- description: "double-quoted identifiers with special characters"
+ input: "CREATE TABLE \"tech_Book\"(id int, name varchar(255));\n\t\t\t\t\t\tINSERT INTO \"tech_Book\" VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafajdfl;\"ka');"
+ result:
+ - text: "CREATE TABLE \"tech_Book\"(id int, name varchar(255));"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 53}
+ range: {start: 0, end: 52}
+ empty: false
+ - text: "\n\t\t\t\t\t\tINSERT INTO \"tech_Book\" VALUES (0, 'abce_ksdf'), (1, 'lks''kjsafajdfl;\"ka');"
+ baseline: 0
+ start: {line: 2, column: 7}
+ end: {line: 2, column: 83}
+ range: {start: 52, end: 135}
+ empty: false
+
+- description: "block comment and inline comment with statement"
+ input: "\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));\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');"
+ result:
+ - 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));"
+ baseline: 0
+ start: {line: 3, column: 7}
+ end: {line: 3, column: 79}
+ range: {start: 0, end: 112}
+ empty: false
+ - 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: {line: 5, column: 7}
+ end: {line: 5, column: 83}
+ range: {start: 112, end: 225}
+ empty: false
+
+- description: "unterminated single quote"
+ input: "INSERT INTO t VALUES ('klajfas)"
+ result:
+ - text: "INSERT INTO t VALUES ('klajfas)"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 32}
+ range: {start: 0, end: 31}
+ empty: false
+
+- description: "unterminated double quote"
+ input: "INSERT INTO \"t VALUES ('klajfas)"
+ result:
+ - text: "INSERT INTO \"t VALUES ('klajfas)"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 33}
+ range: {start: 0, end: 32}
+ empty: false
+
+- description: "unterminated block comment"
+ input: "/*INSERT INTO \"t VALUES ('klajfas)"
+ result:
+ - text: "/*INSERT INTO \"t VALUES ('klajfas)"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 35}
+ range: {start: 0, end: 34}
+ empty: false
+
+- description: "unterminated dollar quote"
+ input: "$$INSERT INTO \"t VALUES ('klajfas)"
+ result:
+ - text: "$$INSERT INTO \"t VALUES ('klajfas)"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 35}
+ range: {start: 0, end: 34}
+ empty: false
+
+- description: "complex procedure with comments and escapes inside dollar-quote"
+ input: "CREATE PROCEDURE insert_data(a varchar(50), b varchar(50))\n\t\t\t\t\t\tLANGUAGE SQL\n\t\t\t\t\t\tAS $$\n\t\t\t\t\t\t/*this is the comment */\n\t\t\t\t\t\tINSERT /* inline comment */ INTO tbl VALUES ('lkjafd''lksjadlf;lk\\\\jasdflkasdf\"asdklf\\\\');\n\t\t\t\t\t\t-- this is the comment\n\t\t\t\t\t\tINSERT INTO tbl VALUES ('fasf_bkdjlfa');\n\t\t\t\t\t\t$$;\n\t\t\t\t\t\tCREATE TABLE t(a int);"
+ result:
+ - text: "CREATE PROCEDURE insert_data(a varchar(50), b varchar(50))\n\t\t\t\t\t\tLANGUAGE SQL\n\t\t\t\t\t\tAS $$\n\t\t\t\t\t\t/*this is the comment */\n\t\t\t\t\t\tINSERT /* inline comment */ INTO tbl VALUES ('lkjafd''lksjadlf;lk\\\\jasdflkasdf\"asdklf\\\\');\n\t\t\t\t\t\t-- this is the comment\n\t\t\t\t\t\tINSERT INTO tbl VALUES ('fasf_bkdjlfa');\n\t\t\t\t\t\t$$;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 8, column: 10}
+ range: {start: 0, end: 303}
+ empty: false
+ - text: "\n\t\t\t\t\t\tCREATE TABLE t(a int);"
+ baseline: 7
+ start: {line: 9, column: 7}
+ end: {line: 9, column: 29}
+ range: {start: 303, end: 332}
+ empty: false
+
+- description: "procedure with tagged dollar-quote containing nested dollar signs"
+ input: "CREATE PROCEDURE insert_data(a varchar(50), b varchar(50))\n\t\t\t\t\t\tLANGUAGE SQL\n\t\t\t\t\t\tAS $tag_name$\n\t\t\t\t\t\t/*this is the comment */\n\t\t\t\t\t\tINSERT /* inline comment */ INTO tbl VALUES ('lkjafd''lksjadlf;lkjasdflkasdf\"asdklf');\n\t\t\t\t\t\t-- this is the comment\n\t\t\t\t\t\tINSERT INTO tbl VALUES ('fasf_bkdjlfa');\n\t\t\t\t\t\t$tag_name$;\n\t\t\t\t\t\tCREATE TABLE t(a int);"
+ result:
+ - text: "CREATE PROCEDURE insert_data(a varchar(50), b varchar(50))\n\t\t\t\t\t\tLANGUAGE SQL\n\t\t\t\t\t\tAS $tag_name$\n\t\t\t\t\t\t/*this is the comment */\n\t\t\t\t\t\tINSERT /* inline comment */ INTO tbl VALUES ('lkjafd''lksjadlf;lkjasdflkasdf\"asdklf');\n\t\t\t\t\t\t-- this is the comment\n\t\t\t\t\t\tINSERT INTO tbl VALUES ('fasf_bkdjlfa');\n\t\t\t\t\t\t$tag_name$;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 8, column: 18}
+ range: {start: 0, end: 315}
+ empty: false
+ - text: "\n\t\t\t\t\t\tCREATE TABLE t(a int);"
+ baseline: 7
+ start: {line: 9, column: 7}
+ end: {line: 9, column: 29}
+ range: {start: 315, end: 344}
+ empty: false
+
+- description: "multiple statements with leading whitespace and no trailing semicolon"
+ input: " CREATE TABLE t(a int); CREATE TABLE t1(a int)"
+ result:
+ - text: " CREATE TABLE t(a int);"
+ baseline: 0
+ start: {line: 1, column: 5}
+ end: {line: 1, column: 27}
+ range: {start: 0, end: 26}
+ empty: false
+ - text: " CREATE TABLE t1(a int)"
+ baseline: 0
+ start: {line: 1, column: 28}
+ end: {line: 1, column: 50}
+ range: {start: 26, end: 49}
+ empty: false
+
+- description: "complex INSERT with escaped strings and special characters"
+ input: "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\\\\');"
+ result:
+ - 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\\\\');"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 5, column: 93}
+ range: {start: 0, end: 400}
+ empty: false
diff --git a/backend/plugin/parser/plsql/split_test.go b/backend/plugin/parser/plsql/split_test.go
index c79823801d..91d5c25126 100644
--- a/backend/plugin/parser/plsql/split_test.go
+++ b/backend/plugin/parser/plsql/split_test.go
@@ -3,234 +3,11 @@ package plsql
import (
"testing"
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-func TestOracleSplitMultiSQL(t *testing.T) {
- type resData struct {
- res []base.Statement
- err string
- }
- type testData struct {
- statement string
- want resData
- }
- tests := []testData{
- {
- statement: `-- klsjdfjasldf
- -- klsjdflkjaskldfj
- `,
- },
- {
- statement: `
- select * from t;
- create table table$1 (id int)
- `,
- want: resData{
- res: []base.Statement{
- {
- Text: `select * from t`,
- BaseLine: 1,
- Range: &storepb.Range{Start: int32(5),
- End: int32(20)},
- Start: &storepb.Position{Line: 2, Column: 5},
- End: &storepb.Position{Line: 2, Column: 19},
- },
- {
- Text: `create table table$1 (id int)`,
- BaseLine: 2,
- Range: &storepb.Range{Start: int32(26),
- End: int32(56)},
- Start: &storepb.Position{Line: 3, Column: 5},
- End: &storepb.Position{Line: 3, Column: 33},
- },
- },
- },
- },
- {
- statement: `ALTER TABLE DATA.TEST
-MODIFY PARTITION BY RANGE (TXN_DATE)
-INTERVAL (NUMTODSINTERVAL(1, 'DAY'))
-(
- PARTITION TEST_PO VALUES LESS THAN (
- TO_DATE('2000-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN')
- )
-)
-ONLINE;`,
- want: resData{
- res: []base.Statement{
- {
- 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: 1, Column: 1},
- End: &storepb.Position{Line: 9, Column: 1},
- Range: &storepb.Range{Start: 0, End: 233},
- },
- },
- },
- },
- // BYT-8268: Test cases for CALL keyword requirement
- // This prevents misidentifying keywords like CASCADE as procedure calls
- {
- statement: "DROP TABLESPACE xxx CASCADE",
- want: resData{
- err: "Syntax error at line 1:28 \nrelated text: DROP TABLESPACE xxx CASCADE;",
- },
- },
- {
- statement: "DROP TABLESPACE xxx; CASCADE",
- want: resData{
- err: "Syntax error at line 1:29 \nrelated text: DROP TABLESPACE xxx; CASCADE;",
- },
- },
- {
- statement: "DROP TABLESPACE xxx INCLUDING CONTENTS CASCADE CONSTRAINTS",
- want: resData{
- res: []base.Statement{
- {
- Text: "DROP TABLESPACE xxx INCLUDING CONTENTS CASCADE CONSTRAINTS",
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 48},
- Range: &storepb.Range{Start: 0, End: 58},
- },
- },
- },
- },
- {
- statement: "CALL my_procedure()",
- want: resData{
- res: []base.Statement{
- {
- Text: "CALL my_procedure()",
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 19},
- Range: &storepb.Range{Start: 0, End: 19},
- },
- },
- },
- },
- {
- statement: "SELECT * FROM t1; SELECT * FROM t2",
- want: resData{
- res: []base.Statement{
- {
- Text: "SELECT * FROM t1",
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 15},
- Range: &storepb.Range{Start: int32(0),
- End: int32(16)},
- },
- {
- Text: "SELECT * FROM t2",
- Start: &storepb.Position{Line: 1, Column: 19},
- End: &storepb.Position{Line: 1, Column: 33},
- Range: &storepb.Range{Start: int32(18),
- End: int32(34)},
- },
- },
- },
- },
- // Test forward slash (/) as statement separator in PL/SQL
- {
- statement: `CREATE OR REPLACE PROCEDURE test_proc IS
-BEGIN
- CALL DBMS_OUTPUT.PUT_LINE('Hello');
-END;
-/`,
- want: resData{
- res: []base.Statement{
- {
- Text: "CREATE OR REPLACE PROCEDURE test_proc IS\nBEGIN\n CALL DBMS_OUTPUT.PUT_LINE('Hello');\nEND;",
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 4, Column: 4},
- Range: &storepb.Range{Start: 0, End: 91},
- },
- },
- },
- },
- {
- statement: `CREATE OR REPLACE PROCEDURE proc1 IS
-BEGIN
- NULL;
-END;
-/
-CREATE OR REPLACE PROCEDURE proc2 IS
-BEGIN
- NULL;
-END;
-/`,
- want: resData{
- res: []base.Statement{
- {
- Text: "CREATE OR REPLACE PROCEDURE proc1 IS\nBEGIN\n NULL;\nEND;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 4, Column: 4},
- Range: &storepb.Range{Start: int32(0),
- End: int32(57)},
- },
- {
- Text: "CREATE OR REPLACE PROCEDURE proc2 IS\nBEGIN\n NULL;\nEND;",
- BaseLine: 5,
- Start: &storepb.Position{Line: 6, Column: 1},
- End: &storepb.Position{Line: 9, Column: 4},
- Range: &storepb.Range{Start: int32(60),
- End: int32(117)},
- },
- },
- },
- },
- {
- statement: `SELECT * FROM t1;
-/
-SELECT * FROM t2;`,
- want: resData{
- res: []base.Statement{
- {
- Text: "SELECT * FROM t1",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 15},
- Range: &storepb.Range{Start: int32(0),
- End: int32(16)},
- },
- {
- Text: "SELECT * FROM t2",
- BaseLine: 2,
- Start: &storepb.Position{Line: 3, Column: 1},
- End: &storepb.Position{Line: 3, Column: 15},
- Range: &storepb.Range{Start: int32(20),
- End: int32(36)},
- },
- },
- },
- },
- {
- statement: `BEGIN
- CALL DBMS_OUTPUT.PUT_LINE('Test');
-END;
-/`,
- want: resData{
- res: []base.Statement{
- {
- Text: "BEGIN\n CALL DBMS_OUTPUT.PUT_LINE('Test');\nEND;",
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 3, Column: 4},
- Range: &storepb.Range{Start: 0, End: 49},
- },
- },
- },
- },
- }
-
- for _, test := range tests {
- res, err := SplitSQL(test.statement)
- errStr := ""
- if err != nil {
- errStr = err.Error()
- }
- require.Equal(t, test.want, resData{res, errStr}, test.statement)
- }
+func TestPLSQLSplitSQL(t *testing.T) {
+ base.RunSplitTests(t, "test-data/test_split.yaml", base.SplitTestOptions{
+ SplitFunc: SplitSQL,
+ })
}
diff --git a/backend/plugin/parser/plsql/test-data/test_split.yaml b/backend/plugin/parser/plsql/test-data/test_split.yaml
new file mode 100644
index 0000000000..27300d3f40
--- /dev/null
+++ b/backend/plugin/parser/plsql/test-data/test_split.yaml
@@ -0,0 +1,121 @@
+- description: "multiple SELECT statements"
+ input: "SELECT * FROM t1; SELECT * FROM t2"
+ result:
+ - text: "SELECT * FROM t1"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 15}
+ range: {start: 0, end: 16}
+ empty: false
+ - text: "SELECT * FROM t2"
+ baseline: 0
+ start: {line: 1, column: 19}
+ end: {line: 1, column: 33}
+ range: {start: 18, end: 34}
+ empty: false
+
+- description: "multiple statements with newlines"
+ input: "\n\t\t\t\tselect * from t;\n\t\t\t\tcreate table table$1 (id int)\n\t\t\t"
+ result:
+ - text: "select * from t"
+ baseline: 1
+ start: {line: 2, column: 5}
+ end: {line: 2, column: 19}
+ range: {start: 5, end: 20}
+ empty: false
+ - text: "create table table$1 (id int)"
+ baseline: 2
+ start: {line: 3, column: 5}
+ end: {line: 3, column: 33}
+ range: {start: 26, end: 56}
+ empty: false
+
+- description: "procedure with forward slash separator"
+ input: "CREATE OR REPLACE PROCEDURE test_proc IS\nBEGIN\n CALL DBMS_OUTPUT.PUT_LINE('Hello');\nEND;\n/"
+ result:
+ - text: "CREATE OR REPLACE PROCEDURE test_proc IS\nBEGIN\n CALL DBMS_OUTPUT.PUT_LINE('Hello');\nEND;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 4, column: 4}
+ range: {start: 0, end: 91}
+ empty: false
+
+- description: "two procedures with forward slash separator"
+ input: "CREATE OR REPLACE PROCEDURE proc1 IS\nBEGIN\n NULL;\nEND;\n/\nCREATE OR REPLACE PROCEDURE proc2 IS\nBEGIN\n NULL;\nEND;\n/"
+ result:
+ - text: "CREATE OR REPLACE PROCEDURE proc1 IS\nBEGIN\n NULL;\nEND;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 4, column: 4}
+ range: {start: 0, end: 57}
+ empty: false
+ - text: "CREATE OR REPLACE PROCEDURE proc2 IS\nBEGIN\n NULL;\nEND;"
+ baseline: 5
+ start: {line: 6, column: 1}
+ end: {line: 9, column: 4}
+ range: {start: 60, end: 117}
+ empty: false
+
+- description: "SELECT statements separated by forward slash"
+ input: "SELECT * FROM t1;\n/\nSELECT * FROM t2;"
+ result:
+ - text: "SELECT * FROM t1"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 15}
+ range: {start: 0, end: 16}
+ empty: false
+ - text: "SELECT * FROM t2"
+ baseline: 2
+ start: {line: 3, column: 1}
+ end: {line: 3, column: 15}
+ range: {start: 20, end: 36}
+ empty: false
+
+- description: "anonymous block with forward slash"
+ input: "BEGIN\n CALL DBMS_OUTPUT.PUT_LINE('Test');\nEND;\n/"
+ result:
+ - text: "BEGIN\n CALL DBMS_OUTPUT.PUT_LINE('Test');\nEND;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 3, column: 4}
+ range: {start: 0, end: 49}
+ empty: false
+
+- description: "ALTER TABLE with PARTITION"
+ input: "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;"
+ result:
+ - 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"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 9, column: 1}
+ range: {start: 0, end: 233}
+ empty: false
+
+- description: "CALL procedure"
+ input: "CALL my_procedure()"
+ result:
+ - text: "CALL my_procedure()"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 19}
+ range: {start: 0, end: 19}
+ empty: false
+
+- description: "DROP TABLESPACE with CASCADE CONSTRAINTS"
+ input: "DROP TABLESPACE xxx INCLUDING CONTENTS CASCADE CONSTRAINTS"
+ result:
+ - text: "DROP TABLESPACE xxx INCLUDING CONTENTS CASCADE CONSTRAINTS"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 48}
+ range: {start: 0, end: 58}
+ empty: false
+
+- description: "DROP TABLESPACE CASCADE alone should error"
+ input: "DROP TABLESPACE xxx CASCADE"
+ error: "Syntax error at line 1:28 \nrelated text: DROP TABLESPACE xxx CASCADE;"
+
+- description: "DROP TABLESPACE then CASCADE should error"
+ input: "DROP TABLESPACE xxx; CASCADE"
+ error: "Syntax error at line 1:29 \nrelated text: DROP TABLESPACE xxx; CASCADE;"
diff --git a/backend/plugin/parser/redshift/split_test.go b/backend/plugin/parser/redshift/split_test.go
index 489db23abe..23791b6d57 100644
--- a/backend/plugin/parser/redshift/split_test.go
+++ b/backend/plugin/parser/redshift/split_test.go
@@ -3,110 +3,11 @@ package redshift
import (
"testing"
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-func TestSplitSQL(t *testing.T) {
- tests := []struct {
- name string
- statement string
- want []base.Statement
- }{
- {
- name: "simple single statement",
- statement: "SELECT 1;",
- want: []base.Statement{
- {
- Text: "SELECT 1;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 10},
- Range: &storepb.Range{Start: 0, End: 9},
- },
- },
- },
- {
- name: "multiple statements",
- statement: "SELECT 1; SELECT 2;",
- want: []base.Statement{
- {
- Text: "SELECT 1;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 10},
- Range: &storepb.Range{Start: 0, End: 9},
- },
- {
- Text: " SELECT 2;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 11},
- End: &storepb.Position{Line: 1, Column: 20},
- Range: &storepb.Range{Start: 9, End: 19},
- },
- },
- },
- {
- name: "multi-line statement",
- statement: "SELECT\n 1;",
- want: []base.Statement{
- {
- Text: "SELECT\n 1;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 2, Column: 5},
- Range: &storepb.Range{Start: 0, End: 11},
- },
- },
- },
- {
- name: "multiple statements on multiple lines",
- statement: "SELECT 1;\nSELECT 2;",
- want: []base.Statement{
- {
- Text: "SELECT 1;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 10},
- Range: &storepb.Range{Start: 0, End: 9},
- },
- {
- Text: "\nSELECT 2;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 2, Column: 1},
- End: &storepb.Position{Line: 2, Column: 10},
- Range: &storepb.Range{Start: 9, End: 19},
- },
- },
- },
- }
-
- for _, tc := range tests {
- t.Run(tc.name, func(t *testing.T) {
- list, err := SplitSQL(tc.statement)
- require.NoError(t, err)
- require.Equal(t, len(tc.want), len(list), "statement count mismatch")
-
- for i, want := range tc.want {
- got := list[i]
- require.Equal(t, want.Text, got.Text, "Text mismatch at index %d", i)
- require.Equal(t, want.BaseLine, got.BaseLine, "BaseLine mismatch at index %d", i)
-
- require.NotNil(t, got.Start, "Start should not be nil at index %d", i)
- require.Equal(t, want.Start.Line, got.Start.Line, "Start.Line mismatch at index %d", i)
- require.Equal(t, want.Start.Column, got.Start.Column, "Start.Column mismatch at index %d", i)
-
- require.NotNil(t, got.End, "End should not be nil at index %d", i)
- require.Equal(t, want.End.Line, got.End.Line, "End.Line mismatch at index %d", i)
- require.Equal(t, want.End.Column, got.End.Column, "End.Column mismatch at index %d", i)
-
- if want.Range != nil {
- require.Equal(t, want.Range.Start, got.Range.Start, "Range.Start mismatch at index %d", i)
- require.Equal(t, want.Range.End, got.Range.End, "Range.End mismatch at index %d", i)
- }
- }
- })
- }
+func TestRedshiftSplitSQL(t *testing.T) {
+ base.RunSplitTests(t, "test-data/test_split.yaml", base.SplitTestOptions{
+ SplitFunc: SplitSQL,
+ })
}
diff --git a/backend/plugin/parser/redshift/test-data/test_split.yaml b/backend/plugin/parser/redshift/test-data/test_split.yaml
new file mode 100644
index 0000000000..e2ef44caae
--- /dev/null
+++ b/backend/plugin/parser/redshift/test-data/test_split.yaml
@@ -0,0 +1,51 @@
+- description: "simple single statement"
+ input: "SELECT 1;"
+ result:
+ - text: "SELECT 1;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 10}
+ range: {start: 0, end: 9}
+ empty: false
+
+- description: "multiple statements"
+ input: "SELECT 1; SELECT 2;"
+ result:
+ - text: "SELECT 1;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 10}
+ range: {start: 0, end: 9}
+ empty: false
+ - text: " SELECT 2;"
+ baseline: 0
+ start: {line: 1, column: 11}
+ end: {line: 1, column: 20}
+ range: {start: 9, end: 19}
+ empty: false
+
+- description: "multiple statements on multiple lines"
+ input: "SELECT 1;\nSELECT 2;"
+ result:
+ - text: "SELECT 1;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 10}
+ range: {start: 0, end: 9}
+ empty: false
+ - text: "\nSELECT 2;"
+ baseline: 0
+ start: {line: 2, column: 1}
+ end: {line: 2, column: 10}
+ range: {start: 9, end: 19}
+ empty: false
+
+- description: "multi-line statement"
+ input: "SELECT\n 1;"
+ result:
+ - text: "SELECT\n 1;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 2, column: 5}
+ range: {start: 0, end: 11}
+ empty: false
diff --git a/backend/plugin/parser/snowflake/split_test.go b/backend/plugin/parser/snowflake/split_test.go
index 6caea57c5c..2dcf323d66 100644
--- a/backend/plugin/parser/snowflake/split_test.go
+++ b/backend/plugin/parser/snowflake/split_test.go
@@ -3,143 +3,11 @@ package snowflake
import (
"testing"
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-func TestSplitSQL(t *testing.T) {
- statement := `SELECT * FROM users;
- SELECT * FROM orders;
- SELECT * FROM products;`
- want := []base.Statement{
- {
- Text: "SELECT * FROM users;",
- BaseLine: 0,
- Range: &storepb.Range{Start: 0, End: 20},
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 21},
- Empty: false,
- },
- {
- Text: "\n\tSELECT * FROM orders;",
- BaseLine: 0,
- Range: &storepb.Range{Start: 20, End: 43},
- Start: &storepb.Position{Line: 2, Column: 2},
- End: &storepb.Position{Line: 2, Column: 23},
- Empty: false,
- },
- {
- Text: "\n\tSELECT * FROM products;",
- BaseLine: 1,
- Range: &storepb.Range{Start: 43, End: 68},
- Start: &storepb.Position{Line: 3, Column: 2},
- End: &storepb.Position{Line: 3, Column: 25},
- Empty: false,
- },
- }
-
- list, err := SplitSQL(statement)
- require.NoError(t, err)
- require.Equal(t, want, list)
-}
-
-func TestSplitSQLSingleStatement(t *testing.T) {
- statement := "SELECT * FROM users;"
- list, err := SplitSQL(statement)
- require.NoError(t, err)
- require.Equal(t, 1, len(list))
- require.Equal(t, "SELECT * FROM users;", list[0].Text)
- require.Equal(t, 0, list[0].BaseLine)
- require.False(t, list[0].Empty)
-}
-
-func TestSplitSQLEmpty(t *testing.T) {
- tests := []struct {
- name string
- statement string
- wantCount int
- }{
- {
- name: "Empty string",
- statement: "",
- wantCount: 0,
- },
- {
- name: "Only whitespace",
- statement: " \n \t ",
- wantCount: 0,
- },
- {
- name: "Only semicolon",
- statement: ";",
- wantCount: 1,
- },
- {
- name: "Only comments and semicolon",
- statement: "-- comment\n;",
- wantCount: 1,
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- list, err := SplitSQL(tt.statement)
- require.NoError(t, err)
- nonEmpty := 0
- for _, sql := range list {
- if !sql.Empty {
- nonEmpty++
- }
- }
- require.Equal(t, tt.wantCount, nonEmpty)
- })
- }
-}
-
-func TestSplitSQLWithComments(t *testing.T) {
- tests := []struct {
- name string
- statement string
- wantCount int
- }{
- {
- name: "Statement with trailing comment",
- statement: "SELECT * FROM users; -- This is a comment\nSELECT * FROM orders;",
- wantCount: 2,
- },
- {
- name: "Statement with leading comment",
- statement: "-- Comment at start\nSELECT * FROM users;",
- wantCount: 1,
- },
- {
- name: "Only comment with semicolon",
- statement: "-- comment only\n;",
- wantCount: 1,
- },
- {
- name: "Multiple statements with comments",
- statement: "-- First query\nSELECT * FROM users;\n-- Second query\nSELECT * FROM orders;",
- wantCount: 2,
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- list, err := SplitSQL(tt.statement)
- require.NoError(t, err)
- require.Equal(t, tt.wantCount, len(list))
- })
- }
-}
-
-func TestSplitSQLWithoutSemicolon(t *testing.T) {
- statement := "SELECT * FROM users"
- list, err := SplitSQL(statement)
- require.NoError(t, err)
- require.Equal(t, 1, len(list))
- require.Equal(t, "SELECT * FROM users", list[0].Text)
- require.False(t, list[0].Empty)
+func TestSnowflakeSplitSQL(t *testing.T) {
+ base.RunSplitTests(t, "test-data/test_split.yaml", base.SplitTestOptions{
+ SplitFunc: SplitSQL,
+ })
}
diff --git a/backend/plugin/parser/snowflake/test-data/test_split.yaml b/backend/plugin/parser/snowflake/test-data/test_split.yaml
new file mode 100644
index 0000000000..195f6fee52
--- /dev/null
+++ b/backend/plugin/parser/snowflake/test-data/test_split.yaml
@@ -0,0 +1,41 @@
+- description: "multiple statements on multiple lines"
+ input: "SELECT * FROM users;\n\tSELECT * FROM orders;\n\tSELECT * FROM products;"
+ result:
+ - text: "SELECT * FROM users;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 21}
+ range: {start: 0, end: 20}
+ empty: false
+ - text: "\n\tSELECT * FROM orders;"
+ baseline: 0
+ start: {line: 2, column: 2}
+ end: {line: 2, column: 23}
+ range: {start: 20, end: 43}
+ empty: false
+ - text: "\n\tSELECT * FROM products;"
+ baseline: 1
+ start: {line: 3, column: 2}
+ end: {line: 3, column: 25}
+ range: {start: 43, end: 68}
+ empty: false
+
+- description: "single statement"
+ input: "SELECT * FROM users;"
+ result:
+ - text: "SELECT * FROM users;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 21}
+ range: {start: 0, end: 20}
+ empty: false
+
+- description: "statement without semicolon"
+ input: "SELECT * FROM users"
+ result:
+ - text: "SELECT * FROM users"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 20}
+ range: {start: 0, end: 19}
+ empty: false
diff --git a/backend/plugin/parser/spanner/split_test.go b/backend/plugin/parser/spanner/split_test.go
index 34399e8a3d..f35a856c8d 100644
--- a/backend/plugin/parser/spanner/split_test.go
+++ b/backend/plugin/parser/spanner/split_test.go
@@ -3,54 +3,11 @@ package spanner
import (
"testing"
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-func TestSplitSQL(t *testing.T) {
- statement := `SELECT * FROM users;
- SELECT * FROM orders;
- SELECT * FROM products;`
- want := []base.Statement{
- {
- Text: "SELECT * FROM users;",
- BaseLine: 0,
- Range: &storepb.Range{Start: 0, End: 20},
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 21},
- Empty: false,
- },
- {
- Text: "\n\tSELECT * FROM orders;",
- BaseLine: 0,
- Range: &storepb.Range{Start: 20, End: 43},
- Start: &storepb.Position{Line: 2, Column: 2},
- End: &storepb.Position{Line: 2, Column: 23},
- Empty: false,
- },
- {
- Text: "\n\tSELECT * FROM products;",
- BaseLine: 1,
- Range: &storepb.Range{Start: 43, End: 68},
- Start: &storepb.Position{Line: 3, Column: 2},
- End: &storepb.Position{Line: 3, Column: 25},
- Empty: false,
- },
- }
-
- list, err := SplitSQL(statement)
- require.NoError(t, err)
- require.Equal(t, want, list)
-}
-
-func TestSplitSQLSingleStatement(t *testing.T) {
- statement := "SELECT * FROM users;"
- list, err := SplitSQL(statement)
- require.NoError(t, err)
- require.Equal(t, 1, len(list))
- require.Equal(t, "SELECT * FROM users;", list[0].Text)
- require.Equal(t, 0, list[0].BaseLine)
- require.False(t, list[0].Empty)
+func TestSpannerSplitSQL(t *testing.T) {
+ base.RunSplitTests(t, "test-data/test_split.yaml", base.SplitTestOptions{
+ SplitFunc: SplitSQL,
+ })
}
diff --git a/backend/plugin/parser/spanner/test-data/test_split.yaml b/backend/plugin/parser/spanner/test-data/test_split.yaml
new file mode 100644
index 0000000000..0043d41600
--- /dev/null
+++ b/backend/plugin/parser/spanner/test-data/test_split.yaml
@@ -0,0 +1,31 @@
+- description: "multiple statements on multiple lines"
+ input: "SELECT * FROM users;\n\tSELECT * FROM orders;\n\tSELECT * FROM products;"
+ result:
+ - text: "SELECT * FROM users;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 21}
+ range: {start: 0, end: 20}
+ empty: false
+ - text: "\n\tSELECT * FROM orders;"
+ baseline: 0
+ start: {line: 2, column: 2}
+ end: {line: 2, column: 23}
+ range: {start: 20, end: 43}
+ empty: false
+ - text: "\n\tSELECT * FROM products;"
+ baseline: 1
+ start: {line: 3, column: 2}
+ end: {line: 3, column: 25}
+ range: {start: 43, end: 68}
+ empty: false
+
+- description: "single statement"
+ input: "SELECT * FROM users;"
+ result:
+ - text: "SELECT * FROM users;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 21}
+ range: {start: 0, end: 20}
+ empty: false
diff --git a/backend/plugin/parser/standard/split_test.go b/backend/plugin/parser/standard/split_test.go
index d17110ecd6..fca3982d71 100644
--- a/backend/plugin/parser/standard/split_test.go
+++ b/backend/plugin/parser/standard/split_test.go
@@ -9,10 +9,15 @@ import (
"github.com/stretchr/testify/require"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
+func TestStandardSplitSQL(t *testing.T) {
+ base.RunSplitTests(t, "test-data/test_split.yaml", base.SplitTestOptions{
+ SplitFunc: SplitSQL,
+ })
+}
+
func generateOneMBInsert() string {
var rand = rand.New(rand.NewSource(time.Now().UnixNano()))
letterList := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
@@ -70,146 +75,3 @@ func TestApplyMultiStatements(t *testing.T) {
require.Equal(t, test.total, total)
}
}
-
-func TestSplitSQL(t *testing.T) {
- tests := []struct {
- name string
- statement string
- want []base.Statement
- }{
- {
- name: "simple single statement",
- statement: "SELECT 1;",
- want: []base.Statement{
- {
- Text: "SELECT 1;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 10},
- Range: &storepb.Range{Start: 0, End: 9},
- Empty: false,
- },
- },
- },
- {
- name: "multi-line statement",
- statement: "SELECT\n 1;",
- want: []base.Statement{
- {
- Text: "SELECT\n 1;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 2, Column: 5},
- Range: &storepb.Range{Start: 0, End: 11},
- Empty: false,
- },
- },
- },
- {
- name: "multiple statements",
- statement: "SELECT 1;\nSELECT 2;",
- want: []base.Statement{
- {
- Text: "SELECT 1;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 10},
- Range: &storepb.Range{Start: 0, End: 9},
- Empty: false,
- },
- {
- Text: "SELECT 2;",
- BaseLine: 1,
- Start: &storepb.Position{Line: 2, Column: 1},
- End: &storepb.Position{Line: 2, Column: 10},
- Range: &storepb.Range{Start: 10, End: 19},
- Empty: false,
- },
- },
- },
- {
- name: "multi-byte characters - Chinese",
- statement: "SELECT '中文';",
- want: []base.Statement{
- {
- Text: "SELECT '中文';",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- // Column is 1-based character offset: S(1) E(2) L(3) E(4) C(5) T(6) ' '(7) '(8) 中(9) 文(10) '(11) ;(12) = 12 chars, End is exclusive so 13
- End: &storepb.Position{Line: 1, Column: 13},
- Range: &storepb.Range{Start: 0, End: 16}, // byte length: 8 + 3 + 3 + 2 = 16
- Empty: false,
- },
- },
- },
- {
- name: "multi-byte characters - emoji",
- statement: "SELECT '🎉';",
- want: []base.Statement{
- {
- Text: "SELECT '🎉';",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- // Column is 1-based character offset: S(1) E(2) L(3) E(4) C(5) T(6) ' '(7) '(8) 🎉(9) '(10) ;(11) = 11 chars, End is exclusive so 12
- End: &storepb.Position{Line: 1, Column: 12},
- Range: &storepb.Range{Start: 0, End: 14}, // byte length: 8 + 4 + 2 = 14
- Empty: false,
- },
- },
- },
- {
- name: "multi-byte on second line",
- statement: "SELECT\n '中文';",
- want: []base.Statement{
- {
- Text: "SELECT\n '中文';",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- // Line 2 (1-based): ' '(1) ' '(2) '(3) 中(4) 文(5) '(6) ;(7) = 7 chars, End is exclusive so 8
- End: &storepb.Position{Line: 2, Column: 8},
- Range: &storepb.Range{Start: 0, End: 18}, // 7 + 3 + 3 + 3 + 2 = 18
- Empty: false,
- },
- },
- },
- {
- name: "multiple statements with multi-byte on separate lines",
- statement: "SELECT '中';\nSELECT '文';",
- want: []base.Statement{
- {
- Text: "SELECT '中';",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- // S(1) E(2) L(3) E(4) C(5) T(6) ' '(7) '(8) 中(9) '(10) ;(11) = 11 chars, End is exclusive so 12
- End: &storepb.Position{Line: 1, Column: 12},
- Range: &storepb.Range{Start: 0, End: 13}, // 8 + 3 + 2 = 13
- Empty: false,
- },
- {
- Text: "SELECT '文';",
- BaseLine: 1,
- Start: &storepb.Position{Line: 2, Column: 1},
- End: &storepb.Position{Line: 2, Column: 12},
- Range: &storepb.Range{Start: 14, End: 27}, // starts after newline
- Empty: false,
- },
- },
- },
- }
-
- for _, tc := range tests {
- t.Run(tc.name, func(t *testing.T) {
- got, err := SplitSQL(tc.statement)
- require.NoError(t, err)
- require.Equal(t, len(tc.want), len(got), "number of statements mismatch")
- for i, want := range tc.want {
- require.Equal(t, want.Text, got[i].Text, "Text mismatch at index %d", i)
- require.Equal(t, want.BaseLine, got[i].BaseLine, "BaseLine mismatch at index %d", i)
- require.Equal(t, want.Start, got[i].Start, "Start mismatch at index %d", i)
- require.Equal(t, want.End, got[i].End, "End mismatch at index %d", i)
- require.Equal(t, want.Range, got[i].Range, "Range mismatch at index %d", i)
- require.Equal(t, want.Empty, got[i].Empty, "Empty mismatch at index %d", i)
- }
- })
- }
-}
diff --git a/backend/plugin/parser/standard/test-data/test_split.yaml b/backend/plugin/parser/standard/test-data/test_split.yaml
new file mode 100644
index 0000000000..68a57e60d4
--- /dev/null
+++ b/backend/plugin/parser/standard/test-data/test_split.yaml
@@ -0,0 +1,81 @@
+- description: "simple single statement"
+ input: "SELECT 1;"
+ result:
+ - text: "SELECT 1;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 10}
+ range: {start: 0, end: 9}
+ empty: false
+
+- description: "multi-line statement"
+ input: "SELECT\n 1;"
+ result:
+ - text: "SELECT\n 1;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 2, column: 5}
+ range: {start: 0, end: 11}
+ empty: false
+
+- description: "multiple statements"
+ input: "SELECT 1;\nSELECT 2;"
+ result:
+ - text: "SELECT 1;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 10}
+ range: {start: 0, end: 9}
+ empty: false
+ - text: "SELECT 2;"
+ baseline: 1
+ start: {line: 2, column: 1}
+ end: {line: 2, column: 10}
+ range: {start: 10, end: 19}
+ empty: false
+
+- description: "multi-byte characters - Chinese"
+ input: "SELECT '中文';"
+ result:
+ - text: "SELECT '中文';"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 13}
+ range: {start: 0, end: 16}
+ empty: false
+
+- description: "multi-byte characters - emoji"
+ input: "SELECT '🎉';"
+ result:
+ - text: "SELECT '🎉';"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 12}
+ range: {start: 0, end: 14}
+ empty: false
+
+- description: "multi-byte on second line"
+ input: "SELECT\n '中文';"
+ result:
+ - text: "SELECT\n '中文';"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 2, column: 8}
+ range: {start: 0, end: 18}
+ empty: false
+
+- description: "multiple statements with multi-byte on separate lines"
+ input: "SELECT '中';\nSELECT '文';"
+ result:
+ - text: "SELECT '中';"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 12}
+ range: {start: 0, end: 13}
+ empty: false
+ - text: "SELECT '文';"
+ baseline: 1
+ start: {line: 2, column: 1}
+ end: {line: 2, column: 12}
+ range: {start: 14, end: 27}
+ empty: false
diff --git a/backend/plugin/parser/tidb/split_test.go b/backend/plugin/parser/tidb/split_test.go
index 9581183fd4..060729e5cd 100644
--- a/backend/plugin/parser/tidb/split_test.go
+++ b/backend/plugin/parser/tidb/split_test.go
@@ -3,163 +3,11 @@ package tidb
import (
"testing"
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-func TestSplitSQL(t *testing.T) {
- tests := []struct {
- name string
- statement string
- want []base.Statement
- }{
- {
- name: "simple single statement",
- statement: "SELECT 1;",
- want: []base.Statement{
- {
- Text: "SELECT 1;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 10}, // After semicolon (1-based exclusive)
- Range: &storepb.Range{Start: 0, End: 9},
- Empty: false,
- },
- },
- },
- {
- name: "multi-line statement",
- statement: "SELECT\n 1;",
- want: []base.Statement{
- {
- Text: "SELECT\n 1;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 2, Column: 5}, // After semicolon on line 2
- Range: &storepb.Range{Start: 0, End: 11},
- Empty: false,
- },
- },
- },
- {
- name: "multiple statements",
- statement: "SELECT 1;\nSELECT 2;",
- want: []base.Statement{
- {
- Text: "SELECT 1;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 10},
- Range: &storepb.Range{Start: 0, End: 9},
- Empty: false,
- },
- {
- Text: "SELECT 2;",
- BaseLine: 1,
- Start: &storepb.Position{Line: 2, Column: 1},
- End: &storepb.Position{Line: 2, Column: 10},
- Range: &storepb.Range{Start: 10, End: 19},
- Empty: false,
- },
- },
- },
- {
- name: "multi-byte characters - Chinese",
- statement: "SELECT '中文';",
- want: []base.Statement{
- {
- Text: "SELECT '中文';",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- // Column is 1-based exclusive: S(1)..;(12), after = 13
- End: &storepb.Position{Line: 1, Column: 13},
- Range: &storepb.Range{Start: 0, End: 16}, // byte length: 8 + 3 + 3 + 2 = 16
- Empty: false,
- },
- },
- },
- {
- name: "multi-byte characters - emoji",
- statement: "SELECT '🎉';",
- want: []base.Statement{
- {
- Text: "SELECT '🎉';",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- // Column is 1-based exclusive: S(1)..;(11), after = 12
- End: &storepb.Position{Line: 1, Column: 12},
- Range: &storepb.Range{Start: 0, End: 14}, // byte length: 8 + 4 + 2 = 14
- Empty: false,
- },
- },
- },
- {
- name: "multi-byte on second line",
- statement: "SELECT\n '中文';",
- want: []base.Statement{
- {
- Text: "SELECT\n '中文';",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 2, Column: 8}, // After semicolon on line 2
- Range: &storepb.Range{Start: 0, End: 18}, // 7 + 3 + 3 + 3 + 2 = 18
- Empty: false,
- },
- },
- },
- {
- name: "multiple statements with multi-byte",
- statement: "SELECT '中';\nSELECT '文';",
- want: []base.Statement{
- {
- Text: "SELECT '中';",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 12}, // After semicolon
- Range: &storepb.Range{Start: 0, End: 13}, // 8 + 3 + 2 = 13
- Empty: false,
- },
- {
- Text: "SELECT '文';",
- BaseLine: 1,
- Start: &storepb.Position{Line: 2, Column: 1},
- End: &storepb.Position{Line: 2, Column: 12},
- Range: &storepb.Range{Start: 14, End: 27},
- Empty: false,
- },
- },
- },
- {
- name: "statement with leading spaces and multi-byte",
- statement: " SELECT '中';",
- want: []base.Statement{
- {
- Text: "SELECT '中';",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 3}, // starts after 2 spaces (1-based: column 3)
- End: &storepb.Position{Line: 1, Column: 14}, // 2 leading spaces + 11 chars + 1 = 14
- Range: &storepb.Range{Start: 2, End: 15}, // 2 + 8 + 3 + 2 = 15
- Empty: false,
- },
- },
- },
- }
-
- for _, tc := range tests {
- t.Run(tc.name, func(t *testing.T) {
- got, err := SplitSQL(tc.statement)
- require.NoError(t, err)
- require.Equal(t, len(tc.want), len(got), "number of statements mismatch")
- for i, want := range tc.want {
- require.Equal(t, want.Text, got[i].Text, "Text mismatch at index %d", i)
- require.Equal(t, want.BaseLine, got[i].BaseLine, "BaseLine mismatch at index %d", i)
- require.Equal(t, want.Start, got[i].Start, "Start mismatch at index %d", i)
- require.Equal(t, want.End, got[i].End, "End mismatch at index %d", i)
- require.Equal(t, want.Range, got[i].Range, "Range mismatch at index %d", i)
- require.Equal(t, want.Empty, got[i].Empty, "Empty mismatch at index %d", i)
- }
- })
- }
+func TestTiDBSplitSQL(t *testing.T) {
+ base.RunSplitTests(t, "test-data/test_split.yaml", base.SplitTestOptions{
+ SplitFunc: SplitSQL,
+ })
}
diff --git a/backend/plugin/parser/tidb/test-data/test_split.yaml b/backend/plugin/parser/tidb/test-data/test_split.yaml
new file mode 100644
index 0000000000..18ce18cd61
--- /dev/null
+++ b/backend/plugin/parser/tidb/test-data/test_split.yaml
@@ -0,0 +1,91 @@
+- description: "simple single statement"
+ input: "SELECT 1;"
+ result:
+ - text: "SELECT 1;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 10}
+ range: {start: 0, end: 9}
+ empty: false
+
+- description: "multi-line statement"
+ input: "SELECT\n 1;"
+ result:
+ - text: "SELECT\n 1;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 2, column: 5}
+ range: {start: 0, end: 11}
+ empty: false
+
+- description: "multiple statements"
+ input: "SELECT 1;\nSELECT 2;"
+ result:
+ - text: "SELECT 1;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 10}
+ range: {start: 0, end: 9}
+ empty: false
+ - text: "SELECT 2;"
+ baseline: 1
+ start: {line: 2, column: 1}
+ end: {line: 2, column: 10}
+ range: {start: 10, end: 19}
+ empty: false
+
+- description: "multi-byte characters - Chinese"
+ input: "SELECT '中文';"
+ result:
+ - text: "SELECT '中文';"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 13}
+ range: {start: 0, end: 16}
+ empty: false
+
+- description: "multi-byte characters - emoji"
+ input: "SELECT '🎉';"
+ result:
+ - text: "SELECT '🎉';"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 12}
+ range: {start: 0, end: 14}
+ empty: false
+
+- description: "multi-byte on second line"
+ input: "SELECT\n '中文';"
+ result:
+ - text: "SELECT\n '中文';"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 2, column: 8}
+ range: {start: 0, end: 18}
+ empty: false
+
+- description: "multiple statements with multi-byte"
+ input: "SELECT '中';\nSELECT '文';"
+ result:
+ - text: "SELECT '中';"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 12}
+ range: {start: 0, end: 13}
+ empty: false
+ - text: "SELECT '文';"
+ baseline: 1
+ start: {line: 2, column: 1}
+ end: {line: 2, column: 12}
+ range: {start: 14, end: 27}
+ empty: false
+
+- description: "statement with leading spaces and multi-byte"
+ input: " SELECT '中';"
+ result:
+ - text: "SELECT '中';"
+ baseline: 0
+ start: {line: 1, column: 3}
+ end: {line: 1, column: 14}
+ range: {start: 2, end: 15}
+ empty: false
diff --git a/backend/plugin/parser/trino/split_test.go b/backend/plugin/parser/trino/split_test.go
index 51ca96890f..1718f23b34 100644
--- a/backend/plugin/parser/trino/split_test.go
+++ b/backend/plugin/parser/trino/split_test.go
@@ -3,209 +3,13 @@ package trino
import (
"testing"
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-type splitTestData struct {
- statement string
- want resData
-}
-
-type resData struct {
- res []base.Statement
- err string
-}
-
-func TestTrinoSplitMultiSQL(t *testing.T) {
- // Split SQL functionality is implemented
- tests := []splitTestData{
- {
- statement: "SELECT * FROM users; SELECT * FROM orders;",
- want: resData{
- res: []base.Statement{
- {
- Text: "SELECT * FROM users;",
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 21},
- },
- {
- Text: "SELECT * FROM orders;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 22},
- End: &storepb.Position{Line: 1, Column: 43},
- },
- },
- },
- },
- {
- statement: `
- -- This is a comment
- SELECT
- id,
- name
- FROM users
- WHERE status = 'active';
-
- /* This is a multi-line
- comment */
- SELECT * FROM orders;`,
- want: resData{
- res: []base.Statement{
- {
- Text: `SELECT
- id,
- name
- FROM users
- WHERE status = 'active';`,
- BaseLine: 2,
- Start: &storepb.Position{Line: 3, Column: 5},
- End: &storepb.Position{Line: 7, Column: 29},
- },
- {
- Text: `SELECT * FROM orders;`,
- BaseLine: 10,
- Start: &storepb.Position{Line: 11, Column: 5},
- End: &storepb.Position{Line: 11, Column: 26},
- },
- },
- },
- },
- {
- statement: `WITH orders_cte AS (
- SELECT * FROM orders
- )
- SELECT u.id, u.name, o.order_id
- FROM users u
- JOIN orders_cte o ON u.id = o.user_id;
-
- SELECT * FROM products;`,
- want: resData{
- res: []base.Statement{
- {
- Text: `WITH orders_cte AS (
- SELECT * FROM orders
- )
- SELECT u.id, u.name, o.order_id
- FROM users u
- JOIN orders_cte o ON u.id = o.user_id;`,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 6, Column: 43},
- },
- {
- Text: `SELECT * FROM products;`,
- BaseLine: 7,
- Start: &storepb.Position{Line: 8, Column: 5},
- End: &storepb.Position{Line: 8, Column: 28},
- },
- },
- },
- },
- {
- // Note: This test falls back to tokenizer path (parser fails with Chinese chars)
- // Tokenizer uses character-based column offsets
- statement: "SELECT * FROM 表名; INSERT INTO 表 VALUES (1);",
- want: resData{
- res: []base.Statement{
- {
- Text: "SELECT * FROM 表名;",
- Range: &storepb.Range{Start: 0, End: 21}, // Byte offset 0-21
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 18}, // Character-based: 17 chars + 1
- },
- {
- Text: "INSERT INTO 表 VALUES (1);",
- Range: &storepb.Range{Start: 22, End: 49}, // Byte offset 22-49
- Start: &storepb.Position{Line: 1, Column: 19},
- End: &storepb.Position{Line: 1, Column: 44}, // Character-based exclusive
- },
- },
- },
- },
- }
-
- for _, test := range tests {
- // Split the SQL and check results
- res, err := SplitSQL(test.statement)
- errStr := ""
- if err != nil {
- errStr = err.Error()
- }
-
- require.Equal(t, len(test.want.res), len(res),
- "Expected %d statements, got %d", len(test.want.res), len(res))
- require.Equal(t, test.want.err, errStr,
- "Expected error '%s', got '%s'", test.want.err, errStr)
-
- // Check each statement's fields
- for i, want := range test.want.res {
- got := res[i]
- require.Equal(t, want.Text, got.Text, "Text mismatch at index %d", i)
- require.Equal(t, want.Start, got.Start, "Start mismatch at index %d", i)
- require.Equal(t, want.End, got.End, "End mismatch at index %d", i)
- if want.Range != nil {
- require.Equal(t, want.Range, got.Range, "Range mismatch at index %d", i)
- }
- }
- }
-}
-
-// TestSplitByParserAndTokenizerConsistency verifies that both splitByParser and
-// splitByTokenizer return consistent results for the same valid SQL input.
-func TestSplitByParserAndTokenizerConsistency(t *testing.T) {
- tests := []struct {
- name string
- statement string
- }{
- {
- name: "simple single statement",
- statement: "SELECT 1;",
- },
- {
- name: "multiple statements on one line",
- statement: "SELECT 1; SELECT 2;",
- },
- {
- name: "multi-line statement",
- statement: "SELECT\n 1;",
- },
- {
- name: "multiple multi-line statements",
- statement: "SELECT\n 1;\nSELECT\n 2;",
- },
- {
- name: "statement with trailing whitespace",
- statement: "SELECT 1; \n ",
- },
- }
-
- for _, tc := range tests {
- t.Run(tc.name, func(t *testing.T) {
- parserRes, parserErr := splitByParser(tc.statement)
- tokenizerRes, tokenizerErr := splitByTokenizer(tc.statement)
-
- // Both should succeed for valid SQL
- require.NoError(t, parserErr, "parser should not error")
- require.NoError(t, tokenizerErr, "tokenizer should not error")
-
- // Should return same number of statements
- require.Equal(t, len(parserRes), len(tokenizerRes),
- "statement count mismatch: parser=%d, tokenizer=%d", len(parserRes), len(tokenizerRes))
-
- // Compare each statement's fields
- for i := range parserRes {
- p := parserRes[i]
- tok := tokenizerRes[i]
-
- require.Equal(t, p.Text, tok.Text, "Text mismatch at index %d", i)
- require.Equal(t, p.BaseLine, tok.BaseLine, "BaseLine mismatch at index %d", i)
- require.Equal(t, p.Start, tok.Start, "Start mismatch at index %d", i)
- require.Equal(t, p.End, tok.End, "End mismatch at index %d", i)
- require.Equal(t, p.Range, tok.Range, "Range mismatch at index %d", i)
- require.Equal(t, p.Empty, tok.Empty, "Empty mismatch at index %d", i)
- }
- })
- }
+func TestTrinoSplitSQL(t *testing.T) {
+ base.RunSplitTests(t, "test-data/test_split.yaml", base.SplitTestOptions{
+ SplitFunc: SplitSQL,
+ LexerSplitFunc: splitByTokenizer,
+ ParserSplitFunc: splitByParser,
+ })
}
diff --git a/backend/plugin/parser/trino/test-data/test_split.yaml b/backend/plugin/parser/trino/test-data/test_split.yaml
new file mode 100644
index 0000000000..f0c2ee8ea9
--- /dev/null
+++ b/backend/plugin/parser/trino/test-data/test_split.yaml
@@ -0,0 +1,31 @@
+- description: "multiple statements same line"
+ input: "SELECT * FROM users; SELECT * FROM orders;"
+ result:
+ - text: "SELECT * FROM users;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 21}
+ range: {start: 0, end: 20}
+ empty: false
+ - text: "SELECT * FROM orders;"
+ baseline: 0
+ start: {line: 1, column: 22}
+ end: {line: 1, column: 43}
+ range: {start: 21, end: 42}
+ empty: false
+
+- description: "unicode in table names"
+ input: "SELECT * FROM 表名; INSERT INTO 表 VALUES (1);"
+ result:
+ - text: "SELECT * FROM 表名;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 18}
+ range: {start: 0, end: 21}
+ empty: false
+ - text: "INSERT INTO 表 VALUES (1);"
+ baseline: 0
+ start: {line: 1, column: 19}
+ end: {line: 1, column: 44}
+ range: {start: 22, end: 49}
+ empty: false
diff --git a/backend/plugin/parser/tsql/split_test.go b/backend/plugin/parser/tsql/split_test.go
index 954b529509..7bec5b71b5 100644
--- a/backend/plugin/parser/tsql/split_test.go
+++ b/backend/plugin/parser/tsql/split_test.go
@@ -3,163 +3,11 @@ package tsql
import (
"testing"
- "github.com/stretchr/testify/require"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-type splitTestData struct {
- statement string
- want resData
-}
-
-type resData struct {
- res []base.Statement
- err string
-}
-
-func TestSplitSQL(t *testing.T) {
- // TODO(parser): `go` should not be recognized as execute_body_batch.
- tests := []splitTestData{
- {
- statement: `-- first statement
-declare @temp table(a int)
--- second statement
-insert into @temp values(1)
--- third statement
-select * from @temp
--- go statement
-go
-go
-go
-go
- `,
- want: resData{
- res: []base.Statement{
- {
- Text: "-- first statement\ndeclare @temp table(a int)",
- BaseLine: 0,
- Start: &storepb.Position{Line: 2, Column: 1},
- End: &storepb.Position{Line: 2, Column: 27},
- Empty: false,
- Range: &storepb.Range{Start: 0, End: 45},
- },
- {
- Text: "\n-- second statement\ninsert into @temp values(1)",
- BaseLine: 1,
- Start: &storepb.Position{Line: 4, Column: 1},
- End: &storepb.Position{Line: 4, Column: 28},
- Empty: false,
- Range: &storepb.Range{Start: 45, End: 93},
- },
- {
- Text: "\n-- third statement\nselect * from @temp\n-- go statement\ngo",
- BaseLine: 3,
- Start: &storepb.Position{Line: 6, Column: 1},
- End: &storepb.Position{Line: 8, Column: 3},
- Empty: false,
- Range: &storepb.Range{Start: 93, End: 151},
- },
- {
- Text: "\ngo",
- BaseLine: 7,
- Start: &storepb.Position{Line: 9, Column: 1},
- End: &storepb.Position{Line: 9, Column: 3},
- Empty: false,
- Range: &storepb.Range{Start: 151, End: 154},
- },
- {
- Text: "\ngo\ngo",
- BaseLine: 8,
- Start: &storepb.Position{Line: 10, Column: 1},
- End: &storepb.Position{Line: 11, Column: 3},
- Empty: false,
- Range: &storepb.Range{Start: 154, End: 160},
- },
- },
- },
- },
- {
- statement: `
-
-
-
-
-
-
-
-
-UPDATE SalesLT.Address SET City = "Shanghai";
-
-UPDATE SalesLT.Address SET PostalCode = 0;
-
-
-UPDATE SalesLT.ProductModelProductDescription SET Culture = "zh-cn";
-
-
-
-`,
- want: resData{
- res: []base.Statement{
- {
- Text: "\n\n\n\n\n\n\n\n\nUPDATE SalesLT.Address SET City = \"Shanghai\";",
- BaseLine: 0,
- Start: &storepb.Position{Line: 10, Column: 1},
- End: &storepb.Position{Line: 10, Column: 46},
- Empty: false,
- Range: &storepb.Range{Start: 0, End: 54},
- },
- {
- Text: "\n\nUPDATE SalesLT.Address SET PostalCode = 0;",
- BaseLine: 9,
- Start: &storepb.Position{Line: 12, Column: 1},
- End: &storepb.Position{Line: 12, Column: 43},
- Empty: false,
- Range: &storepb.Range{Start: 54, End: 98},
- },
- {
- Text: "\n\n\nUPDATE SalesLT.ProductModelProductDescription SET Culture = \"zh-cn\";",
- BaseLine: 11,
- Start: &storepb.Position{Line: 15, Column: 1},
- End: &storepb.Position{Line: 15, Column: 69},
- Empty: false,
- Range: &storepb.Range{Start: 98, End: 169},
- },
- },
- },
- },
- {
- statement: "SELECT * FROM 表名; INSERT INTO 表 VALUES (1);",
- want: resData{
- res: []base.Statement{
- {
- Text: "SELECT * FROM 表名;",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 1},
- End: &storepb.Position{Line: 1, Column: 18},
- Empty: false,
- Range: &storepb.Range{Start: 0, End: 21}, // Byte offset 0-21 (not 0-17)
- },
- {
- Text: " INSERT INTO 表 VALUES (1);",
- BaseLine: 0,
- Start: &storepb.Position{Line: 1, Column: 19},
- End: &storepb.Position{Line: 1, Column: 44},
- Empty: false,
- Range: &storepb.Range{Start: 21, End: 49}, // Byte offset 21-49 (not 17-43)
- },
- },
- },
- },
- }
-
- for _, test := range tests {
- res, err := SplitSQL(test.statement)
- errStr := ""
- if err != nil {
- errStr = err.Error()
- }
- require.Equal(t, test.want, resData{res, errStr}, test.statement)
- }
+func TestTSQLSplitSQL(t *testing.T) {
+ base.RunSplitTests(t, "test-data/test_split.yaml", base.SplitTestOptions{
+ SplitFunc: SplitSQL,
+ })
}
diff --git a/backend/plugin/parser/tsql/test-data/test_split.yaml b/backend/plugin/parser/tsql/test-data/test_split.yaml
new file mode 100644
index 0000000000..b41757c1b5
--- /dev/null
+++ b/backend/plugin/parser/tsql/test-data/test_split.yaml
@@ -0,0 +1,71 @@
+- description: "GO batch separator with multiple batches"
+ input: "-- first statement\ndeclare @temp table(a int)\n-- second statement\ninsert into @temp values(1)\n-- third statement\nselect * from @temp\n-- go statement\ngo\ngo\ngo\ngo\n\t\t\t"
+ result:
+ - text: "-- first statement\ndeclare @temp table(a int)"
+ baseline: 0
+ start: {line: 2, column: 1}
+ end: {line: 2, column: 27}
+ range: {start: 0, end: 45}
+ empty: false
+ - text: "\n-- second statement\ninsert into @temp values(1)"
+ baseline: 1
+ start: {line: 4, column: 1}
+ end: {line: 4, column: 28}
+ range: {start: 45, end: 93}
+ empty: false
+ - text: "\n-- third statement\nselect * from @temp\n-- go statement\ngo"
+ baseline: 3
+ start: {line: 6, column: 1}
+ end: {line: 8, column: 3}
+ range: {start: 93, end: 151}
+ empty: false
+ - text: "\ngo"
+ baseline: 7
+ start: {line: 9, column: 1}
+ end: {line: 9, column: 3}
+ range: {start: 151, end: 154}
+ empty: false
+ - text: "\ngo\ngo"
+ baseline: 8
+ start: {line: 10, column: 1}
+ end: {line: 11, column: 3}
+ range: {start: 154, end: 160}
+ empty: false
+
+- description: "multiple UPDATE statements with blank lines"
+ input: "\n\n\n\n\n\n\n\n\nUPDATE SalesLT.Address SET City = \"Shanghai\";\n\nUPDATE SalesLT.Address SET PostalCode = 0;\n\n\nUPDATE SalesLT.ProductModelProductDescription SET Culture = \"zh-cn\";\n\n\n\n"
+ result:
+ - text: "\n\n\n\n\n\n\n\n\nUPDATE SalesLT.Address SET City = \"Shanghai\";"
+ baseline: 0
+ start: {line: 10, column: 1}
+ end: {line: 10, column: 46}
+ range: {start: 0, end: 54}
+ empty: false
+ - text: "\n\nUPDATE SalesLT.Address SET PostalCode = 0;"
+ baseline: 9
+ start: {line: 12, column: 1}
+ end: {line: 12, column: 43}
+ range: {start: 54, end: 98}
+ empty: false
+ - text: "\n\n\nUPDATE SalesLT.ProductModelProductDescription SET Culture = \"zh-cn\";"
+ baseline: 11
+ start: {line: 15, column: 1}
+ end: {line: 15, column: 69}
+ range: {start: 98, end: 169}
+ empty: false
+
+- description: "unicode in table names"
+ input: "SELECT * FROM 表名; INSERT INTO 表 VALUES (1);"
+ result:
+ - text: "SELECT * FROM 表名;"
+ baseline: 0
+ start: {line: 1, column: 1}
+ end: {line: 1, column: 18}
+ range: {start: 0, end: 21}
+ empty: false
+ - text: " INSERT INTO 表 VALUES (1);"
+ baseline: 0
+ start: {line: 1, column: 19}
+ end: {line: 1, column: 44}
+ range: {start: 21, end: 49}
+ empty: false