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(sql): record the statement each result ran, and close the query plan design (#21669)
Danny Xua75400eb2a8d
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.goindex ff72907b8b..7c7803c375 100644--- a/backend/api/v1/sql_service.go+++ b/backend/api/v1/sql_service.go@@ -155,7 +155,8 @@ func (s *SQLService) AdminExecute(ctx context.Context, stream *connect.BidiStreaif queryErr != nil {response.Results = []*v1pb.QueryResult{{- Error: queryErr.Error(),+ Error: queryErr.Error(),+ Statement: request.Statement,},}} else {@@ -1082,7 +1083,11 @@ func queryRetryStopOnError(if err != nil {return nil, nil, 0, err}- return queryRetry(ctx, stores, user, instance, database, driver, conn, statements, statement, queryContext, licenseService, optionalAccessCheck, schemaSyncer, hasMultipleStatements(statements))+ results, spans, duration, err := queryRetry(ctx, stores, user, instance, database, driver, conn, statements, statement, queryContext, licenseService, optionalAccessCheck, schemaSyncer, hasMultipleStatements(statements))+ if len(results) == 0 {+ results = resultForTimeout(statement, err)+ }+ return results, spans, duration, err}// Split the statement into individual SQLs@@ -1093,7 +1098,11 @@ func queryRetryStopOnError(// statement as a single unit. Where GetQuerySpan is supported (e.g. MongoDB),// it re-parses the raw text and surfaces the parse error; otherwise// queryRetry returns nil spans (old behavior).- return queryRetry(ctx, stores, user, instance, database, driver, conn, []parserbase.Statement{{Text: statement}}, statement, queryContext, licenseService, optionalAccessCheck, schemaSyncer, false)+ results, spans, duration, err := queryRetry(ctx, stores, user, instance, database, driver, conn, []parserbase.Statement{{Text: statement}}, statement, queryContext, licenseService, optionalAccessCheck, schemaSyncer, false)+ if len(results) == 0 {+ results = resultForTimeout(statement, err)+ }+ return results, spans, duration, err}// A multi-statement batch can change the session schema mid-batch (e.g. SET search_path),@@ -1137,6 +1146,28 @@ func queryRetryStopOnError(return allResults, allSpans, totalDuration, nil}+// queryTimeoutError marks a run the server cut off at its deadline, apart from+// an error the engine reported, so a caller can answer it with the statement+// that was running.+type queryTimeoutError struct {+ timeout time.Duration+}++func (e *queryTimeoutError) Error() string {+ return fmt.Sprintf("timeout reached: %v", e.timeout)+}++// resultForTimeout names the statement a timed-out run was cut off in. No+// driver returns a result for a statement it never finished, so without this+// the editor has an error and nothing to point at.+func resultForTimeout(statement string, err error) []*v1pb.QueryResult {+ var timedOut *queryTimeoutError+ if !errors.As(err, &timedOut) {+ return nil+ }+ return []*v1pb.QueryResult{{Error: err.Error(), Statement: statement}}+}+func executeWithTimeout(ctx context.Context,driver db.Driver,@@ -1161,7 +1192,7 @@ func executeWithTimeout(select {case <-queryCtx.Done():// canceled or timed out- return nil, time.Since(start), errors.Errorf("timeout reached: %v", timeout)+ return nil, time.Since(start), &queryTimeoutError{timeout: timeout}default:// So the select will not block}diff --git a/backend/api/v1/sql_service_test.go b/backend/api/v1/sql_service_test.goindex 1f0058e5e2..5155742461 100644--- a/backend/api/v1/sql_service_test.go+++ b/backend/api/v1/sql_service_test.go@@ -4,9 +4,11 @@ import ("context""strings""testing"+ "time""connectrpc.com/connect"metadatapb "github.com/bytebase/omni/metadata"+ "github.com/pkg/errors""github.com/stretchr/testify/require""github.com/bytebase/bytebase/backend/common"@@ -668,3 +670,27 @@ func TestRemapBackupTableColumns(t *testing.T) {require.Equal(t, want, span.PredicateColumns)require.Equal(t, want, span.Results[0].SourceColumns)}++func TestResultForTimeoutNamesTheStatementItCutOff(t *testing.T) {+ t.Parallel()+ results := resultForTimeout("SELECT * FROM t", &queryTimeoutError{timeout: 30 * time.Second})+ require.Len(t, results, 1)+ require.Equal(t, "SELECT * FROM t", results[0].Statement)+ require.Equal(t, "timeout reached: 30s", results[0].Error)+}++func TestResultForTimeoutLeavesAnEngineErrorAlone(t *testing.T) {+ t.Parallel()+ // Only a run the server cut off has no result of its own; an engine error+ // already reaches the editor as the driver reported it.+ require.Nil(t, resultForTimeout("SELECT * FROM t", errors.New("syntax error")))+ require.Nil(t, resultForTimeout("SELECT * FROM t", nil))+}++func TestResultForTimeoutUnwrapsAWrappedTimeout(t *testing.T) {+ t.Parallel()+ err := errors.Wrap(&queryTimeoutError{timeout: time.Second}, "failed to query")+ results := resultForTimeout("SELECT 1", err)+ require.Len(t, results, 1)+ require.Equal(t, "SELECT 1", results[0].Statement)+}diff --git a/backend/plugin/db/databricks/databricks.go b/backend/plugin/db/databricks/databricks.goindex 80fb1b8740..9a0884f334 100644--- a/backend/plugin/db/databricks/databricks.go+++ b/backend/plugin/db/databricks/databricks.go@@ -114,6 +114,7 @@ func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, qresult.Rows = append(result.Rows, queryRow)}result.Latency = durationpb.New(time.Since(startTime))+ result.Statement = stmt.Textresult.RowsCount = int64(len(result.Rows))results = append(results, result)}diff --git a/backend/plugin/db/dynamodb/dynamodb.go b/backend/plugin/db/dynamodb/dynamodb.goindex 6b4852caf3..5403fbcd10 100644--- a/backend/plugin/db/dynamodb/dynamodb.go+++ b/backend/plugin/db/dynamodb/dynamodb.go@@ -193,7 +193,7 @@ func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, qstop = true}result.Latency = durationpb.New(time.Since(startTime))- result.Statement = statement+ result.Statement = singleSQL.Textresult.RowsCount = int64(len(result.Rows))results = append(results, result)if stop {diff --git a/backend/plugin/db/redis/redis.go b/backend/plugin/db/redis/redis.goindex 4643e1c09d..f3eecafabd 100644--- a/backend/plugin/db/redis/redis.go+++ b/backend/plugin/db/redis/redis.go@@ -183,20 +183,12 @@ func (d *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteOreturn 0, errors.New("redis: cannot create database")}- lines := strings.Split(statement, "\n")+ inputs, _, err := splitCommands(statement)+ if err != nil {+ return 0, err+ }if _, err := d.rdb.Pipelined(ctx, func(p redis.Pipeliner) error {- for _, line := range lines {- fields, err := shlex.Split(line)- if err != nil {- return errors.Wrapf(err, "failed to split command %s", line)- }- if len(fields) == 0 {- continue- }- var input []any- for _, v := range fields {- input = append(input, v)- }+ for _, input := range inputs {_ = p.Do(ctx, input...)}return nil@@ -207,6 +199,31 @@ func (d *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteOreturn 0, nil}+// splitCommands reads one command per line, skipping lines that hold none, and+// returns each command's arguments beside the line it came from. A result is+// reported per command rather than per line, so the two are built together to+// keep them aligned.+func splitCommands(statement string) ([][]any, []string, error) {+ var inputs [][]any+ var commands []string+ for _, line := range strings.Split(statement, "\n") {+ fields, err := shlex.Split(line)+ if err != nil {+ return nil, nil, errors.Wrapf(err, "failed to split command %s", line)+ }+ if len(fields) == 0 {+ continue+ }+ var input []any+ for _, v := range fields {+ input = append(input, v)+ }+ inputs = append(inputs, input)+ commands = append(commands, line)+ }+ return inputs, commands, nil+}+// Dump and restore// Dump the database, if dbName is empty, then dump all databases.// Redis is schemaless, we don't support dump Redis data currently.@@ -221,21 +238,9 @@ func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, q}startTime := time.Now()- lines := strings.Split(statement, "\n")- var inputs [][]any- for _, line := range lines {- fields, err := shlex.Split(line)- if err != nil {- return nil, errors.Wrapf(err, "failed to split command %s", line)- }- if len(fields) == 0 {- continue- }- var input []any- for _, v := range fields {- input = append(input, v)- }- inputs = append(inputs, input)+ inputs, commands, err := splitCommands(statement)+ if err != nil {+ return nil, err}results := make([]*v1pb.QueryResult, len(inputs))@@ -243,7 +248,7 @@ func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, qresults[i] = &v1pb.QueryResult{ColumnNames: []string{"#", "Value"},ColumnTypeNames: []string{"INT", "TEXT"},- Statement: lines[i],+ Statement: commands[i],}}diff --git a/backend/plugin/db/redis/redis_test.go b/backend/plugin/db/redis/redis_test.gonew file mode 100644index 0000000000..64855c679f--- /dev/null+++ b/backend/plugin/db/redis/redis_test.go@@ -0,0 +1,55 @@+package redis++import (+ "testing"++ "github.com/stretchr/testify/require"+)++func TestSplitCommands(t *testing.T) {+ tests := []struct {+ name string+ statement string+ wantArgs [][]any+ wantLines []string+ }{+ {+ name: "one command per line",+ statement: "GET a\nGET b",+ wantArgs: [][]any{{"GET", "a"}, {"GET", "b"}},+ wantLines: []string{"GET a", "GET b"},+ },+ {+ name: "a blank line does not shift the commands after it",+ statement: "GET a\n\nGET b\n \nGET c",+ wantArgs: [][]any{{"GET", "a"}, {"GET", "b"}, {"GET", "c"}},+ wantLines: []string{"GET a", "GET b", "GET c"},+ },+ {+ name: "quoted argument",+ statement: `SET a "two words"`,+ wantArgs: [][]any{{"SET", "a", "two words"}},+ wantLines: []string{`SET a "two words"`},+ },+ {+ name: "no commands",+ statement: "\n \n",+ wantArgs: nil,+ wantLines: nil,+ },+ }+ for _, tc := range tests {+ t.Run(tc.name, func(t *testing.T) {+ args, lines, err := splitCommands(tc.statement)+ require.NoError(t, err)+ require.Equal(t, tc.wantArgs, args)+ require.Equal(t, tc.wantLines, lines)+ require.Len(t, lines, len(args))+ })+ }+}++func TestSplitCommandsRejectsAnUnclosedQuote(t *testing.T) {+ _, _, err := splitCommands(`SET a "unclosed`)+ require.Error(t, err)+}diff --git a/docs/design/query-plan-visualizer.md b/docs/design/query-plan-visualizer.mdindex a87fa7162f..95338bd99f 100644--- a/docs/design/query-plan-visualizer.md+++ b/docs/design/query-plan-visualizer.md@@ -1,6 +1,6 @@# Query plan visualizer-**Status:** partly implemented, see §6 · **Scope:** SQL Editor · Code references are to `main` at `7664688752` unless a PR is named.+**Status:** implemented, see §6 · **Scope:** SQL Editor · Code references are to `main` at `7664688752`, the commit this design was written against, unless a PR is named; line numbers have since moved.Bytebase draws query plans, but few users reach the drawing. This covers thewhole journey: how you ask for a plan, when you get a picture, how you read it,@@ -368,8 +368,10 @@ cover a different query.That requires every driver that rewrites a statement to record what it actuallyran. SQL Server and Spanner did not until[#21476](https://github.com/bytebase/bytebase/pull/21476) and-[#21481](https://github.com/bytebase/bytebase/pull/21481); the timed-out case-still waits on the timeout response carrying its statement.+[#21481](https://github.com/bytebase/bytebase/pull/21481), and Redis, DynamoDB+and Databricks named the wrong statement or none at all. A run the server cuts+off at its timeout now names the statement it was running as well, which no+driver can do for a statement it never finished.**Say which one it is.** Measured and estimated carry a one-word badge at theend of the viewer's tab row, and the estimated badge's tooltip says *"A new@@ -456,10 +458,12 @@ Two ways that boundary leaked, both in `checkDatabaseAccess`engine folds case; one Bytebase does not track as a database — Oracle's`SYS`, Snowflake's `SNOWFLAKE` — is left to the connected database's check. Atyped `EXPLAIN ANALYZE` carries an `ExplainAnalyze` mark on its span and needs- the inner statement's permission as well. SQL Server's `SET STATISTICS XML- ON` also executes and returns a plan, and still passes on `bb.sql.select`- alone: the explain recognizer knows only SHOWPLAN. That is the one permission- item still open here.+ the inner statement's permission as well, and so does SQL Server's `SET+ STATISTICS XML ON`, which executes the statement and returns its plan beside+ its rows. Which plan options a batch has turned on is read from the parsed+ `SET` statement and carried across the batch, so a statement after one is+ marked too+ ([#21648](https://github.com/bytebase/bytebase/pull/21648)).- **A data access grant exempted it**([#21619](https://github.com/bytebase/bytebase/pull/21619)). The checkreturned early for a granted target before the permission was evaluated, so a@@ -531,7 +535,7 @@ live servers, September 2026.## 6. Status-Checked against `main` on 2026-10-26, after [#21652](https://github.com/bytebase/bytebase/pull/21652), plus the §3.1 and §3.3 work on the branch below.+Checked against `main` on 2026-10-26, at [#21668](https://github.com/bytebase/bytebase/pull/21668), plus [#21669](https://github.com/bytebase/bytebase/pull/21669) below.**Landed**@@ -574,7 +578,9 @@ Checked against `main` on 2026-10-26, after [#21652](https://github.com/bytebaseright-click → Explain, in the editor and the admin terminal, ask for thedrawable format, so the picture takes one call. No limit is added to anexplain, and there is no Explain analyze action; measured plans come from a- typed `EXPLAIN ANALYZE`. §3.1 is closed.+ typed `EXPLAIN ANALYZE`+ ([#21666](https://github.com/bytebase/bytebase/pull/21666),+ [#21668](https://github.com/bytebase/bytebase/pull/21668)). §3.1 is closed.- §3.3 — the rest of the reading changes. A measured plan is ranked by the timeeach operator spent — its own total less its children's — and says that is@@ -589,7 +595,16 @@ Checked against `main` on 2026-10-26, after [#21652](https://github.com/bytebaseresult tab. A node that reads a table Bytebase has synced shows that table'ssize and its indexes, which no standalone viewer can do; a table in anotherdatabase, or a bare name two schemas both use, is left unanswered rather than- answered with the wrong table's indexes. §3.3 is closed.+ answered with the wrong table's indexes+ ([#21668](https://github.com/bytebase/bytebase/pull/21668)). §3.3 is closed.++- §3.4 — every driver that runs a statement records that statement: Redis no+ longer shifts them by a blank line, DynamoDB no longer gives every result the+ whole request, Databricks records one where it recorded none, and a run cut+ off at its timeout carries the statement it was running. *Show in Editor* on a failed+ result reads through the `WITH result AS (…)` wrapper the row limit adds,+ which it could not find in the editor before+ ([#21669](https://github.com/bytebase/bytebase/pull/21669)).**Not yet**diff --git a/frontend/src/modules/sql-editor/components/ResultView/ErrorView.tsx b/frontend/src/modules/sql-editor/components/ResultView/ErrorView.tsxindex 5d0789ccac..9979612a6e 100644--- a/frontend/src/modules/sql-editor/components/ResultView/ErrorView.tsx+++ b/frontend/src/modules/sql-editor/components/ResultView/ErrorView.tsx@@ -9,6 +9,7 @@ import { activeSQLEditorRef } from "@/modules/sql-editor/components/StandardPaneimport { sqlEditorEvents } from "@/modules/sql-editor/model/events";import type { SQLEditorQueryParams, SQLResultSetV1 } from "@/types";import { PostgresError } from "./PostgresError";+import { statementAsTyped } from "./statementText";interface ErrorViewProps {error: string | undefined;@@ -42,14 +43,14 @@ export function ErrorView({const canShowInEditor = hasErrorPosition || !!failingStatement;const findStatementRange = (): IRange | undefined => {- let statement = failingStatement;- if (!statement) return undefined;+ if (!failingStatement) return undefined;const editor = activeSQLEditorRef.value;const model = editor?.getModel();if (!model) return undefined;- statement = statement.replace(/\s+LIMIT\s+\d+\s*;?\s*$/i, "").trim();+ const statement = statementAsTyped(failingStatement);+ if (!statement) return undefined;let matches = model.findMatches(statement,false,diff --git a/frontend/src/modules/sql-editor/components/ResultView/ResultView.tsx b/frontend/src/modules/sql-editor/components/ResultView/ResultView.tsxindex e0baf92b2e..040d8b8fa1 100644--- a/frontend/src/modules/sql-editor/components/ResultView/ResultView.tsx+++ b/frontend/src/modules/sql-editor/components/ResultView/ResultView.tsx@@ -258,7 +258,7 @@ export function ResultView({return (<divclassName={cn(- "relative flex flex-col justify-start items-start pb-1 overflow-y-auto h-full w-full",+ "relative flex flex-col justify-start pb-1 overflow-y-auto h-full w-full","bg-background ph-no-capture")}>diff --git a/frontend/src/modules/sql-editor/components/ResultView/statementText.test.ts b/frontend/src/modules/sql-editor/components/ResultView/statementText.test.tsnew file mode 100644index 0000000000..efe84710b3--- /dev/null+++ b/frontend/src/modules/sql-editor/components/ResultView/statementText.test.ts@@ -0,0 +1,66 @@+import { describe, expect, it } from "vitest";+import { statementAsTyped } from "./statementText";++describe("statementAsTyped", () => {+ it("returns a statement no rewrite touched", () => {+ expect(statementAsTyped("SELECT * FROM t")).toBe("SELECT * FROM t");+ });++ it("drops a LIMIT the parser fitted on", () => {+ expect(statementAsTyped("SELECT * FROM t LIMIT 1000;")).toBe(+ "SELECT * FROM t"+ );+ });++ it("unwraps the single-line wrapper", () => {+ expect(+ statementAsTyped(+ "WITH result AS (SELECT * FROM t) SELECT * FROM result LIMIT 1000;"+ )+ ).toBe("SELECT * FROM t");+ });++ it("unwraps the wrapper the Postgres parser writes over several lines", () => {+ expect(+ statementAsTyped(+ "WITH result AS (\nSELECT * FROM t\n) SELECT * FROM result LIMIT 1000;"+ )+ ).toBe("SELECT * FROM t");+ });++ it("unwraps SQL Server's TOP form", () => {+ expect(+ statementAsTyped(+ "WITH result AS (SELECT * FROM t) SELECT TOP 1000 * FROM result;"+ )+ ).toBe("SELECT * FROM t");+ });++ it("unwraps BigQuery's unlimited form", () => {+ expect(+ statementAsTyped("WITH result AS (SELECT * FROM t) SELECT * FROM result;")+ ).toBe("SELECT * FROM t");+ });++ it("keeps a LIMIT the user typed inside the wrapper", () => {+ expect(+ statementAsTyped(+ "WITH result AS (SELECT * FROM t LIMIT 5) SELECT * FROM result LIMIT 1000;"+ )+ ).toBe("SELECT * FROM t LIMIT 5");+ });++ it("unwraps a statement with a CTE of its own", () => {+ expect(+ statementAsTyped(+ "WITH result AS (WITH a AS (SELECT 1 AS x) SELECT x FROM a) SELECT * FROM result LIMIT 1000;"+ )+ ).toBe("WITH a AS (SELECT 1 AS x) SELECT x FROM a");+ });++ it("leaves a statement that only looks like the wrapper alone", () => {+ expect(+ statementAsTyped("WITH result AS (SELECT 1 AS x) SELECT x FROM result")+ ).toBe("WITH result AS (SELECT 1 AS x) SELECT x FROM result");+ });+});diff --git a/frontend/src/modules/sql-editor/components/ResultView/statementText.ts b/frontend/src/modules/sql-editor/components/ResultView/statementText.tsnew file mode 100644index 0000000000..afb42ac21b--- /dev/null+++ b/frontend/src/modules/sql-editor/components/ResultView/statementText.ts@@ -0,0 +1,20 @@+// A driver records the statement it ran, which for a row-limited query is not+// always what the user typed: the parsers either fit a LIMIT onto the statement+// or, where they cannot, wrap the whole of it in a `WITH result AS (…)` query.+const RESULT_LIMIT_WRAPPER =+ /^WITH\s+result\s+AS\s*\(([\s\S]*)\)\s*SELECT\s+(?:TOP\s+\d+\s+)?\*\s+FROM\s+result(?:\s+LIMIT\s+\d+)?\s*;?$/i;++const TRAILING_LIMIT = /\s+LIMIT\s+\d+\s*;?\s*$/i;++/**+ * Recovers the text the user typed from the statement a driver ran, so it can+ * be found in the editor. A statement no rewrite touched comes back unchanged.+ */+export function statementAsTyped(executed: string): string {+ const trimmed = executed.trim();+ const wrapped = trimmed.match(RESULT_LIMIT_WRAPPER);+ // The wrapper holds the statement verbatim, so its own LIMIT is the only one+ // to drop; a LIMIT inside it is the user's.+ if (wrapped) return wrapped[1].trim();+ return trimmed.replace(TRAILING_LIMIT, "").trim();+}