Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: prevent DML statements from being wrapped in CTEs in SQL Editor (#18220)
Vincent Huang6cf12492389c
diff --git a/backend/plugin/db/spanner/spanner.go b/backend/plugin/db/spanner/spanner.goindex dfc8a389ea..265c3606aa 100644--- a/backend/plugin/db/spanner/spanner.go+++ b/backend/plugin/db/spanner/spanner.go@@ -285,6 +285,13 @@ func getColumnTypeName(columnType *sppb.Type) (string, error) {return columnType.Code.String(), nil}+// getStatementWithResultLimit wraps a SQL statement in a CTE to enforce a result limit.+// This is a simple approach that works for SELECT queries but has a critical limitation:+// Spanner does NOT support DML statements (INSERT/UPDATE/DELETE) inside CTEs.+//+// This function should ONLY be called for SELECT statements (verify with util.IsSelect first).+// For a more robust parser-based approach that can handle complex queries, see the+// PostgreSQL/MySQL implementations which parse and inject LIMIT clauses directly.func getStatementWithResultLimit(stmt string, limit int) string {stmt = strings.TrimRightFunc(stmt, utils.IsSpaceOrSemicolon)limitPart := fmt.Sprintf(" LIMIT %d", limit)@@ -323,14 +330,15 @@ func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, qvar results []*v1pb.QueryResultfor _, statement := range stmts {- if queryContext.Limit > 0 {- statement = getStatementWithResultLimit(statement, queryContext.Limit)- }-startTime := time.Now()queryResult, err := func() (*v1pb.QueryResult, error) {if util.IsSelect(statement) {- return d.querySingleSQL(ctx, statement, queryContext)+ // Only apply limit wrapper for SELECT statements+ limitedStatement := statement+ if queryContext.Limit > 0 {+ limitedStatement = getStatementWithResultLimit(statement, queryContext.Limit)+ }+ return d.querySingleSQL(ctx, limitedStatement, queryContext)}if util.IsDDL(statement) {op, err := d.dbClient.UpdateDatabaseDdl(ctx, &databasepb.UpdateDatabaseDdlRequest{diff --git a/backend/plugin/db/util/statement_parser_test.go b/backend/plugin/db/util/statement_parser_test.goindex 5e21b94b9b..73969060d1 100644--- a/backend/plugin/db/util/statement_parser_test.go+++ b/backend/plugin/db/util/statement_parser_test.go@@ -492,3 +492,137 @@ CREATE INDEX bytebase_idx_migration_history_namespace_created ON migration_histoa.Equal(tc.want, got)}}++func TestIsSelect(t *testing.T) {+ tests := []struct {+ name string+ query string+ expected bool+ }{+ {+ name: "simple SELECT",+ query: "SELECT * FROM users",+ expected: true,+ },+ {+ name: "SELECT with WHERE clause",+ query: "SELECT id, name FROM users WHERE age > 18",+ expected: true,+ },+ {+ name: "SELECT lowercase",+ query: "select * from users",+ expected: true,+ },+ {+ name: "CTE with SELECT (starts with WITH)",+ query: "WITH cte AS (SELECT * FROM users) SELECT * FROM cte",+ expected: true,+ },+ {+ name: "INSERT statement",+ query: "INSERT INTO test_table (id, name) VALUES (1, 'test')",+ expected: false,+ },+ {+ name: "UPDATE statement",+ query: "UPDATE test_table SET name = 'updated' WHERE id = 1",+ expected: false,+ },+ {+ name: "DELETE statement",+ query: "DELETE FROM test_table WHERE id = 1",+ expected: false,+ },+ {+ name: "CREATE TABLE",+ query: "CREATE TABLE test (id INT64, name STRING(100))",+ expected: false,+ },+ {+ name: "DROP TABLE",+ query: "DROP TABLE test",+ expected: false,+ },+ {+ name: "ALTER TABLE",+ query: "ALTER TABLE test ADD COLUMN age INT64",+ expected: false,+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ a := require.New(t)+ result := IsSelect(tt.query)+ a.Equal(tt.expected, result)+ })+ }+}++func TestIsDDL(t *testing.T) {+ tests := []struct {+ name string+ query string+ expected bool+ }{+ {+ name: "CREATE TABLE",+ query: "CREATE TABLE test (id INT64, name STRING(100))",+ expected: true,+ },+ {+ name: "DROP TABLE",+ query: "DROP TABLE test",+ expected: true,+ },+ {+ name: "ALTER TABLE",+ query: "ALTER TABLE test ADD COLUMN age INT64",+ expected: true,+ },+ {+ name: "CREATE INDEX",+ query: "CREATE INDEX idx_name ON test(name)",+ expected: true,+ },+ {+ name: "DROP INDEX",+ query: "DROP INDEX idx_name",+ expected: true,+ },+ {+ name: "ALTER lowercase",+ query: "alter table test drop column age",+ expected: true,+ },+ {+ name: "SELECT statement",+ query: "SELECT * FROM users",+ expected: false,+ },+ {+ name: "INSERT statement",+ query: "INSERT INTO test_table (id, name) VALUES (1, 'test')",+ expected: false,+ },+ {+ name: "UPDATE statement",+ query: "UPDATE test_table SET name = 'updated' WHERE id = 1",+ expected: false,+ },+ {+ name: "DELETE statement",+ query: "DELETE FROM test_table WHERE id = 1",+ expected: false,+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ a := require.New(t)+ result := IsDDL(tt.query)+ a.Equal(tt.expected, result)+ })+ }+}