fix(sql): classify bd sql statements with the SQL parser; never drop result sets (#6932)
Proxied bd sql misclassified WITH [RECURSIVE] ... SELECT as a write and silently returned no rows; CALL result sets were dropped on both paths. Classify statements with the dolthub/vitess parser (internal/storage/sqlclass: Read/Write/Mixed); Mixed commits and renders any rows, so no path silently drops a result set. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
e7138e93af
commit
c4b1cb699d
11 changed files with 351 additions and 85 deletions
18
CHANGELOG.md
18
CHANGELOG.md
|
|
@ -9,6 +9,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
### Fixed
|
||||
|
||||
- **`bd sql` no longer drops the rows of CTE queries or CALL result sets**
|
||||
([#6932](https://github.com/gastownhall/beads/pull/6932)). In proxied-server
|
||||
mode, `WITH name(cols) AS (...) SELECT ...` and `WITH RECURSIVE ...` queries
|
||||
were misread as writes and printed `OK, 0 rows affected` with no error; they
|
||||
now return their rows. Statements are now classified with the Dolt SQL
|
||||
parser (shared by the proxied and direct paths): reads return rows, plain
|
||||
writes (including CTE-prefixed `UPDATE`/`DELETE`) commit and report
|
||||
`rows_affected`, and anything that may both write and return rows (`CALL`,
|
||||
`EXPLAIN ANALYZE` of a write, `RETURNING`) or that the parser cannot
|
||||
classify is committed and prints whatever rows it returns, so
|
||||
`CALL DOLT_BRANCH(...)`-style result sets are rendered instead of discarded.
|
||||
Visible changes for scripts: in direct server mode a multi-statement write,
|
||||
or a single write statement the parser cannot classify, now prints `OK` /
|
||||
`{"status":"ok"}` (as proxied mode already did) instead of
|
||||
`OK, N rows affected` / `{"rows_affected":N}`; and `--readonly` now refuses
|
||||
statements it cannot parse (for example `PRAGMA`) instead of treating them
|
||||
as reads.
|
||||
|
||||
- **`notion.token` is kept out of the Dolt database**
|
||||
([#6676](https://github.com/gastownhall/beads/issues/6676)). It was missing
|
||||
from the yaml-only key list that holds the other tracker secrets, so
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ go_deps.module_override(
|
|||
patches = ["//third_party/patches:gozstd_nocgo.patch"],
|
||||
path = "github.com/dolthub/gozstd",
|
||||
)
|
||||
use_repo(go_deps, "com_github_anthropics_anthropic_sdk_go", "com_github_burntsushi_toml", "com_github_cenkalti_backoff_v4", "com_github_charmbracelet_x_ansi", "com_github_data_dog_go_sqlmock", "com_github_dolthub_dolt_go", "com_github_dolthub_driver_v2", "com_github_dolthub_eventkit", "com_github_dolthub_go_mysql_server", "com_github_go_sql_driver_mysql", "com_github_google_uuid", "com_github_gowebpki_jcs", "com_github_invopop_jsonschema", "com_github_johanneskaufmann_html_to_markdown_v2", "com_github_mattn_go_sqlite3", "com_github_microcosm_cc_bluemonday", "com_github_oapi_codegen_oapi_codegen_v2", "com_github_olebedev_when", "com_github_spf13_cobra", "com_github_spf13_pflag", "com_github_spf13_viper", "com_github_stretchr_testify", "com_github_subosito_gotenv", "com_github_testcontainers_testcontainers_go", "com_github_testcontainers_testcontainers_go_modules_dolt", "com_github_yuin_goldmark", "in_gopkg_yaml_v3", "io_opentelemetry_go_otel", "io_opentelemetry_go_otel_exporters_otlp_otlpmetric_otlpmetrichttp", "io_opentelemetry_go_otel_exporters_stdout_stdoutmetric", "io_opentelemetry_go_otel_exporters_stdout_stdouttrace", "io_opentelemetry_go_otel_metric", "io_opentelemetry_go_otel_sdk", "io_opentelemetry_go_otel_sdk_metric", "io_opentelemetry_go_otel_trace", "io_rsc_script", "land_charm_glamour_v2", "land_charm_huh_v2", "land_charm_lipgloss_v2", "org_golang_x_mod", "org_golang_x_net", "org_golang_x_sync", "org_golang_x_sys", "org_golang_x_term")
|
||||
use_repo(go_deps, "com_github_anthropics_anthropic_sdk_go", "com_github_burntsushi_toml", "com_github_cenkalti_backoff_v4", "com_github_charmbracelet_x_ansi", "com_github_data_dog_go_sqlmock", "com_github_dolthub_dolt_go", "com_github_dolthub_driver_v2", "com_github_dolthub_eventkit", "com_github_dolthub_go_mysql_server", "com_github_dolthub_vitess", "com_github_go_sql_driver_mysql", "com_github_google_uuid", "com_github_gowebpki_jcs", "com_github_invopop_jsonschema", "com_github_johanneskaufmann_html_to_markdown_v2", "com_github_mattn_go_sqlite3", "com_github_microcosm_cc_bluemonday", "com_github_oapi_codegen_oapi_codegen_v2", "com_github_olebedev_when", "com_github_spf13_cobra", "com_github_spf13_pflag", "com_github_spf13_viper", "com_github_stretchr_testify", "com_github_subosito_gotenv", "com_github_testcontainers_testcontainers_go", "com_github_testcontainers_testcontainers_go_modules_dolt", "com_github_yuin_goldmark", "in_gopkg_yaml_v3", "io_opentelemetry_go_otel", "io_opentelemetry_go_otel_exporters_otlp_otlpmetric_otlpmetrichttp", "io_opentelemetry_go_otel_exporters_stdout_stdoutmetric", "io_opentelemetry_go_otel_exporters_stdout_stdouttrace", "io_opentelemetry_go_otel_metric", "io_opentelemetry_go_otel_sdk", "io_opentelemetry_go_otel_sdk_metric", "io_opentelemetry_go_otel_trace", "io_rsc_script", "land_charm_glamour_v2", "land_charm_huh_v2", "land_charm_lipgloss_v2", "org_golang_x_mod", "org_golang_x_net", "org_golang_x_sync", "org_golang_x_sys", "org_golang_x_term")
|
||||
|
||||
# Hermetic Dolt CLI for tests (tools/bazel/dolt.bzl pins the release to
|
||||
# scripts/ci/install-dolt.sh). //tools/bazel:dolt selects the repository for
|
||||
|
|
|
|||
|
|
@ -404,6 +404,7 @@ go_library(
|
|||
"//internal/storage/issueops",
|
||||
"//internal/storage/kvkeys",
|
||||
"//internal/storage/schema",
|
||||
"//internal/storage/sqlclass",
|
||||
"//internal/storage/uow",
|
||||
"//internal/storage/versioncontrolops",
|
||||
"//internal/telemetry",
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
"github.com/steveyegge/beads/internal/metrics"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/sqlclass"
|
||||
)
|
||||
|
||||
var sqlCmd = &cobra.Command{
|
||||
|
|
@ -25,9 +26,11 @@ Examples:
|
|||
bd sql 'DELETE FROM dirty_issues WHERE issue_id = "bd-abc123"'
|
||||
bd sql --csv 'SELECT id, title, status FROM issues'
|
||||
|
||||
The query is passed directly to the database. SELECT queries return results as a
|
||||
table (or JSON/CSV with --json/--csv). Non-SELECT queries (INSERT, UPDATE, DELETE)
|
||||
report the number of rows affected.
|
||||
The query is passed directly to the database. Reads (SELECT, WITH ... SELECT,
|
||||
SHOW, EXPLAIN) return results as a table (or JSON/CSV with --json/--csv). Writes
|
||||
(INSERT, UPDATE, DELETE, DDL) report the number of rows affected. Statements
|
||||
that may both write and return rows, such as CALL, are committed and print any
|
||||
rows they return (or "OK").
|
||||
|
||||
In proxied-server mode, multiple statements separated by ';' run as a single
|
||||
committed batch and report "OK", and --database runs the query against a
|
||||
|
|
@ -71,15 +74,13 @@ WARNING: Direct database access bypasses the storage layer. Use with caution.`,
|
|||
|
||||
ctx := rootCtx
|
||||
|
||||
trimmed := strings.TrimSpace(strings.ToUpper(query))
|
||||
isRead := strings.HasPrefix(trimmed, "SELECT") ||
|
||||
strings.HasPrefix(trimmed, "EXPLAIN") ||
|
||||
strings.HasPrefix(trimmed, "PRAGMA") ||
|
||||
strings.HasPrefix(trimmed, "SHOW") ||
|
||||
strings.HasPrefix(trimmed, "DESCRIBE") ||
|
||||
strings.HasPrefix(trimmed, "WITH")
|
||||
|
||||
if isRead {
|
||||
// Reads and statements that may return rows go through Query so a
|
||||
// result set is always rendered; only plain writes use Exec.
|
||||
kind := sqlclass.Classify(query)
|
||||
if kind != sqlclass.Write {
|
||||
if kind == sqlclass.Mixed {
|
||||
CheckReadonly("sql")
|
||||
}
|
||||
rows, err := db.QueryContext(ctx, query)
|
||||
if err != nil {
|
||||
return HandleErrorRespectJSON("query error: %v", err)
|
||||
|
|
@ -90,6 +91,9 @@ WARNING: Direct database access bypasses the storage layer. Use with caution.`,
|
|||
if err != nil {
|
||||
return HandleErrorRespectJSON("getting columns: %v", err)
|
||||
}
|
||||
if kind == sqlclass.Mixed && len(columns) == 0 {
|
||||
return printSQLStatusOK()
|
||||
}
|
||||
|
||||
allRows := make([]map[string]interface{}, 0)
|
||||
for rows.Next() {
|
||||
|
|
|
|||
|
|
@ -179,3 +179,80 @@ func sqlValueEquals(v any, want float64) bool {
|
|||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// TestProxiedServerSQLStatementRouting pins how proxied `bd sql` routes
|
||||
// statements that a prefix scan misreads: CTE reads must return their rows,
|
||||
// CTE writes must commit, and a statement that writes and returns a result set
|
||||
// must render that result set instead of reporting "0 rows affected".
|
||||
func TestProxiedServerSQLStatementRouting(t *testing.T) {
|
||||
requireSharedProxiedServer(t)
|
||||
t.Parallel()
|
||||
bd := buildEmbeddedBD(t)
|
||||
p := newSharedProxiedProject(t, bd, "sqlcte")
|
||||
|
||||
one := bdProxiedCreate(t, bd, p.dir, "CTE issue one", "--type", "task", "--priority", "1")
|
||||
two := bdProxiedCreate(t, bd, p.dir, "CTE issue two", "--type", "task", "--priority", "2")
|
||||
|
||||
t.Run("cte_column_list_select_returns_rows", func(t *testing.T) {
|
||||
rows := bdProxiedSQLJSON(t, bd, p.dir,
|
||||
"WITH t(issue_id) AS (SELECT id FROM issues WHERE id = '"+one.ID+"') SELECT issue_id FROM t")
|
||||
if len(rows) != 1 {
|
||||
t.Fatalf("expected 1 row, got %d: %v", len(rows), rows)
|
||||
}
|
||||
if rows[0]["issue_id"] != one.ID {
|
||||
t.Errorf("expected issue_id=%q, got %v", one.ID, rows[0]["issue_id"])
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("recursive_cte_select_returns_rows", func(t *testing.T) {
|
||||
query := "WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < 3) SELECT i FROM n"
|
||||
rows := bdProxiedSQLJSON(t, bd, p.dir, query)
|
||||
if len(rows) != 3 {
|
||||
t.Fatalf("expected 3 rows, got %d: %v", len(rows), rows)
|
||||
}
|
||||
stdout, _ := bdProxiedSQL(t, bd, p.dir, query)
|
||||
if !strings.Contains(stdout, "(3 rows)") {
|
||||
t.Errorf("expected '(3 rows)' in table output, got:\n%s", stdout)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("cte_write_commits", func(t *testing.T) {
|
||||
stdout, _ := bdProxiedSQL(t, bd, p.dir, "--json",
|
||||
"WITH t(issue_id) AS (SELECT id FROM issues WHERE id = '"+two.ID+"') "+
|
||||
"UPDATE issues SET priority = 0 WHERE id IN (SELECT issue_id FROM t)")
|
||||
start := strings.Index(stdout, "{")
|
||||
if start < 0 {
|
||||
t.Fatalf("no JSON object in exec output:\n%s", stdout)
|
||||
}
|
||||
var res map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(stdout[start:]), &res); err != nil {
|
||||
t.Fatalf("parse exec JSON: %v\nraw: %s", err, stdout[start:])
|
||||
}
|
||||
if !sqlValueEquals(res["rows_affected"], 1) {
|
||||
t.Errorf("expected rows_affected=1, got %v", res["rows_affected"])
|
||||
}
|
||||
|
||||
db := openProxiedDB(t, p)
|
||||
var priority int
|
||||
if err := db.QueryRow("SELECT priority FROM issues WHERE id = ?", two.ID).Scan(&priority); err != nil {
|
||||
t.Fatalf("query priority for %s: %v", two.ID, err)
|
||||
}
|
||||
if priority != 0 {
|
||||
t.Errorf("expected committed priority=0, got %d", priority)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("call_renders_result_set", func(t *testing.T) {
|
||||
rows := bdProxiedSQLJSON(t, bd, p.dir, "CALL DOLT_BRANCH('sql-routing-probe')")
|
||||
if len(rows) != 1 {
|
||||
t.Fatalf("expected the procedure's 1 status row, got %d: %v", len(rows), rows)
|
||||
}
|
||||
if _, ok := rows[0]["status"]; !ok {
|
||||
t.Errorf("expected a status column, got %v", rows[0])
|
||||
}
|
||||
branches := bdProxiedSQLJSON(t, bd, p.dir, "SELECT name FROM dolt_branches WHERE name = 'sql-routing-probe'")
|
||||
if len(branches) != 1 {
|
||||
t.Errorf("expected the CALL to have created branch sql-routing-probe, got %v", branches)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage/domain"
|
||||
"github.com/steveyegge/beads/internal/storage/sqlclass"
|
||||
"github.com/steveyegge/beads/internal/storage/uow"
|
||||
)
|
||||
|
||||
|
|
@ -16,9 +17,16 @@ func runSQLProxiedServer(ctx context.Context, query string, csvOutput bool) erro
|
|||
return HandleError("proxied-server UOW provider not initialized")
|
||||
}
|
||||
|
||||
multiStatement := isMultiStatementSQL(query)
|
||||
if isMultiStatementSQL(query) {
|
||||
CheckReadonly("sql")
|
||||
if _, err := runSQLProxiedExec(ctx, query); err != nil {
|
||||
return err
|
||||
}
|
||||
return printSQLStatusOK()
|
||||
}
|
||||
|
||||
if !multiStatement && sqlQueryIsRead(query) {
|
||||
switch sqlclass.Classify(query) {
|
||||
case sqlclass.Read:
|
||||
result, err := uow.RunTxRead(ctx, uowProvider, func(ctx context.Context, uw uow.UnitOfWork) (*domain.RawSQLResult, error) {
|
||||
return uw.RawSQLUseCase().Query(ctx, query)
|
||||
})
|
||||
|
|
@ -26,10 +34,45 @@ func runSQLProxiedServer(ctx context.Context, query string, csvOutput bool) erro
|
|||
return HandleErrorRespectJSON("query error: %v", err)
|
||||
}
|
||||
return renderRawSQLResult(result, csvOutput)
|
||||
|
||||
case sqlclass.Write:
|
||||
CheckReadonly("sql")
|
||||
affected, err := runSQLProxiedExec(ctx, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if jsonOutput {
|
||||
return outputJSON(map[string]interface{}{
|
||||
"rows_affected": affected,
|
||||
})
|
||||
}
|
||||
fmt.Printf("OK, %d rows affected\n", affected)
|
||||
return nil
|
||||
|
||||
default:
|
||||
// The statement may write and may return rows. Run it through Query
|
||||
// inside a committing transaction so a result set is rendered rather
|
||||
// than silently discarded, and any write is committed rather than
|
||||
// rolled back with a read transaction.
|
||||
CheckReadonly("sql")
|
||||
result, err := uow.RunTxResult(ctx, uowProvider, func(ctx context.Context, uw uow.UnitOfWork) (*domain.RawSQLResult, string, error) {
|
||||
result, err := uw.RawSQLUseCase().Query(ctx, query)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
return result, "bd sql: " + query, nil
|
||||
})
|
||||
if err != nil {
|
||||
return HandleErrorRespectJSON("query error: %v", err)
|
||||
}
|
||||
if len(result.Columns) == 0 {
|
||||
return printSQLStatusOK()
|
||||
}
|
||||
return renderRawSQLResult(result, csvOutput)
|
||||
}
|
||||
}
|
||||
|
||||
CheckReadonly("sql")
|
||||
|
||||
func runSQLProxiedExec(ctx context.Context, query string) (int64, error) {
|
||||
affected, err := uow.RunTxResult(ctx, uowProvider, func(ctx context.Context, uw uow.UnitOfWork) (int64, string, error) {
|
||||
affected, err := uw.RawSQLUseCase().Exec(ctx, query)
|
||||
if err != nil {
|
||||
|
|
@ -38,26 +81,18 @@ func runSQLProxiedServer(ctx context.Context, query string, csvOutput bool) erro
|
|||
return affected, "bd sql: " + query, nil
|
||||
})
|
||||
if err != nil {
|
||||
return HandleErrorRespectJSON("exec error: %v", err)
|
||||
}
|
||||
|
||||
if multiStatement {
|
||||
if jsonOutput {
|
||||
return outputJSON(map[string]interface{}{
|
||||
"status": "ok",
|
||||
})
|
||||
}
|
||||
fmt.Println("OK")
|
||||
return nil
|
||||
return 0, HandleErrorRespectJSON("exec error: %v", err)
|
||||
}
|
||||
return affected, nil
|
||||
}
|
||||
|
||||
func printSQLStatusOK() error {
|
||||
if jsonOutput {
|
||||
return outputJSON(map[string]interface{}{
|
||||
"rows_affected": affected,
|
||||
"status": "ok",
|
||||
})
|
||||
}
|
||||
|
||||
fmt.Printf("OK, %d rows affected\n", affected)
|
||||
fmt.Println("OK")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -134,59 +169,6 @@ func topLevelStatementCount(query string) int {
|
|||
return count
|
||||
}
|
||||
|
||||
func sqlQueryIsRead(query string) bool {
|
||||
trimmed := strings.TrimSpace(strings.ToUpper(query))
|
||||
switch {
|
||||
case strings.HasPrefix(trimmed, "SELECT"),
|
||||
strings.HasPrefix(trimmed, "EXPLAIN"),
|
||||
strings.HasPrefix(trimmed, "PRAGMA"),
|
||||
strings.HasPrefix(trimmed, "SHOW"),
|
||||
strings.HasPrefix(trimmed, "DESCRIBE"):
|
||||
return true
|
||||
case strings.HasPrefix(trimmed, "WITH"):
|
||||
return withOuterStatementIsRead(trimmed)
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func withOuterStatementIsRead(upperTrimmed string) bool {
|
||||
depth := 0
|
||||
var quote byte
|
||||
closedCTE := false
|
||||
for i := 0; i < len(upperTrimmed); i++ {
|
||||
c := upperTrimmed[i]
|
||||
if quote != 0 {
|
||||
if c == quote {
|
||||
quote = 0
|
||||
}
|
||||
continue
|
||||
}
|
||||
switch c {
|
||||
case '\'', '"', '`':
|
||||
quote = c
|
||||
case '(':
|
||||
depth++
|
||||
case ')':
|
||||
depth--
|
||||
if depth == 0 {
|
||||
closedCTE = true
|
||||
}
|
||||
case ' ', '\t', '\n', '\r', ',':
|
||||
if c == ',' && depth == 0 {
|
||||
closedCTE = false
|
||||
}
|
||||
default:
|
||||
if depth == 0 && closedCTE {
|
||||
rest := strings.TrimLeft(upperTrimmed[i:], " \t\n\r")
|
||||
return strings.HasPrefix(rest, "SELECT") ||
|
||||
strings.HasPrefix(rest, "EXPLAIN")
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func renderRawSQLResult(result *domain.RawSQLResult, csvOutput bool) error {
|
||||
columns := result.Columns
|
||||
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -154,7 +154,7 @@ require (
|
|||
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63 // indirect
|
||||
github.com/dolthub/ishell v0.0.0-20260414231531-5f031e3e9037 // indirect
|
||||
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71 // indirect
|
||||
github.com/dolthub/vitess v0.0.0-20260728212736-0542037326d7 // indirect
|
||||
github.com/dolthub/vitess v0.0.0-20260728212736-0542037326d7
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/ebitengine/purego v0.10.0 // indirect
|
||||
github.com/edsrzf/mmap-go v1.2.0 // indirect
|
||||
|
|
|
|||
|
|
@ -160,6 +160,7 @@ filegroup(
|
|||
"//internal/storage/rowid:go_srcs",
|
||||
"//internal/storage/schema:go_srcs",
|
||||
"//internal/storage/sqlbuild:go_srcs",
|
||||
"//internal/storage/sqlclass:go_srcs",
|
||||
"//internal/storage/storagecontract:go_srcs",
|
||||
"//internal/storage/uow:go_srcs",
|
||||
"//internal/storage/versioncontrolops:go_srcs",
|
||||
|
|
|
|||
28
internal/storage/sqlclass/BUILD.bazel
Normal file
28
internal/storage/sqlclass/BUILD.bazel
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
load("@rules_go//go:def.bzl", "go_library", "go_test")
|
||||
|
||||
go_library(
|
||||
name = "sqlclass",
|
||||
srcs = ["sqlclass.go"],
|
||||
importpath = "github.com/steveyegge/beads/internal/storage/sqlclass",
|
||||
visibility = ["//:__subpackages__"],
|
||||
deps = ["@com_github_dolthub_vitess//go/vt/sqlparser"],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "sqlclass_test",
|
||||
srcs = ["sqlclass_test.go"],
|
||||
embed = [":sqlclass"],
|
||||
)
|
||||
|
||||
# --- begin go_srcs (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) ---
|
||||
|
||||
filegroup(
|
||||
name = "go_srcs",
|
||||
srcs = glob(
|
||||
["**/*.go"],
|
||||
allow_empty = True,
|
||||
),
|
||||
visibility = ["//:__subpackages__"],
|
||||
)
|
||||
|
||||
# --- end go_srcs ---
|
||||
88
internal/storage/sqlclass/sqlclass.go
Normal file
88
internal/storage/sqlclass/sqlclass.go
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
// Package sqlclass classifies raw SQL statements for `bd sql` with the same
|
||||
// MySQL grammar the Dolt server parses, so callers can route a statement to a
|
||||
// read transaction, a committing exec, or a committing query without prefix
|
||||
// scanning.
|
||||
package sqlclass
|
||||
|
||||
import (
|
||||
"github.com/dolthub/vitess/go/vt/sqlparser"
|
||||
)
|
||||
|
||||
// Kind says how a single SQL statement must be executed so that writes are
|
||||
// committed and result sets are never dropped.
|
||||
type Kind int
|
||||
|
||||
const (
|
||||
// Read changes nothing and returns a result set: run it in a read
|
||||
// transaction and render its rows.
|
||||
Read Kind = iota
|
||||
// Write changes data and returns no result set: execute it in a
|
||||
// committing transaction and report the rows affected.
|
||||
Write
|
||||
// Mixed may change data and may also return a result set (CALL, EXPLAIN
|
||||
// ANALYZE of a write, RETURNING, or anything the parser cannot classify):
|
||||
// run it in a committing transaction and render any rows it returns.
|
||||
Mixed
|
||||
)
|
||||
|
||||
// String returns the kind's name for test and diagnostic output.
|
||||
func (k Kind) String() string {
|
||||
switch k {
|
||||
case Read:
|
||||
return "read"
|
||||
case Write:
|
||||
return "write"
|
||||
default:
|
||||
return "mixed"
|
||||
}
|
||||
}
|
||||
|
||||
// Classify classifies a single SQL statement by its main statement, so CTEs
|
||||
// (WITH [RECURSIVE] name(cols) AS (...)), comments, and string literals that
|
||||
// contain keywords do not affect the result. Input the parser rejects is
|
||||
// Mixed: it may still be valid for the server, and Mixed never discards rows
|
||||
// or rolls back writes.
|
||||
func Classify(query string) Kind {
|
||||
stmt, err := sqlparser.Parse(query)
|
||||
if err != nil || stmt == nil {
|
||||
return Mixed
|
||||
}
|
||||
return classify(stmt)
|
||||
}
|
||||
|
||||
func classify(stmt sqlparser.Statement) Kind {
|
||||
switch s := stmt.(type) {
|
||||
case *sqlparser.Select:
|
||||
if s.Into != nil {
|
||||
return Write
|
||||
}
|
||||
return Read
|
||||
case *sqlparser.SetOp, *sqlparser.Show, *sqlparser.OtherRead:
|
||||
return Read
|
||||
case *sqlparser.Explain:
|
||||
if !s.Analyze {
|
||||
return Read
|
||||
}
|
||||
if s.Statement != nil && classify(s.Statement) == Read {
|
||||
return Read
|
||||
}
|
||||
return Mixed
|
||||
case *sqlparser.Insert:
|
||||
return writeUnlessReturning(len(s.Returning))
|
||||
case *sqlparser.Update:
|
||||
return writeUnlessReturning(len(s.Returning))
|
||||
case *sqlparser.Delete:
|
||||
return writeUnlessReturning(len(s.Returning))
|
||||
case *sqlparser.DDL, *sqlparser.AlterTable, *sqlparser.DBDDL, *sqlparser.Set, *sqlparser.Use:
|
||||
return Write
|
||||
default:
|
||||
return Mixed
|
||||
}
|
||||
}
|
||||
|
||||
func writeUnlessReturning(returning int) Kind {
|
||||
if returning > 0 {
|
||||
return Mixed
|
||||
}
|
||||
return Write
|
||||
}
|
||||
67
internal/storage/sqlclass/sqlclass_test.go
Normal file
67
internal/storage/sqlclass/sqlclass_test.go
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
package sqlclass
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestClassify(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
query string
|
||||
want Kind
|
||||
}{
|
||||
{"select", "SELECT 1", Read},
|
||||
{"lowercase select", " select id from issues", Read},
|
||||
{"union", "SELECT 1 UNION SELECT 2", Read},
|
||||
{"parenthesized union", "(SELECT 1) UNION (SELECT 2)", Read},
|
||||
{"show", "SHOW TABLES", Read},
|
||||
{"describe", "DESCRIBE issues", Read},
|
||||
{"explain select", "EXPLAIN SELECT * FROM issues", Read},
|
||||
{"leading block comment", "/* note */ SELECT 1", Read},
|
||||
{"leading line comment", "-- note\nSELECT 1", Read},
|
||||
|
||||
// CTE reads: a prefix scanner that treats the column list after the CTE
|
||||
// name as the CTE body sees "AS" as the main verb and calls it a write.
|
||||
{"cte select", "WITH t AS (SELECT 1 AS n) SELECT n FROM t", Read},
|
||||
{"cte column list", "WITH t(n) AS (SELECT 1) SELECT n FROM t", Read},
|
||||
{"recursive cte column list", "WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < 3) SELECT i FROM n", Read},
|
||||
{"recursive cte", "WITH RECURSIVE t AS (SELECT 1 AS i UNION ALL SELECT i + 1 FROM t WHERE i < 3) SELECT * FROM t", Read},
|
||||
{"multiple ctes with column lists", "WITH a(x) AS (SELECT 1), b(y) AS (SELECT x FROM a) SELECT y FROM b", Read},
|
||||
{"cte nested parens", "WITH t AS (SELECT (1 + (2 * 3)) AS n) SELECT n FROM t", Read},
|
||||
{"cte keywords in literal", "WITH t AS (SELECT ') DELETE FROM issues' AS s) SELECT s FROM t", Read},
|
||||
{"cte escaped quote in literal", `WITH t AS (SELECT 'a\'(' AS s) SELECT s FROM t`, Read},
|
||||
{"cte comment with keywords", "WITH t AS (SELECT 1 AS n /* ) DELETE */) SELECT n FROM t", Read},
|
||||
{"cte union main", "WITH t(n) AS (SELECT 1) SELECT n FROM t UNION SELECT 2", Read},
|
||||
|
||||
// CTE writes must stay writes: a read transaction is rolled back.
|
||||
{"cte delete", "WITH t AS (SELECT id FROM issues) DELETE FROM issues WHERE id IN (SELECT id FROM t)", Write},
|
||||
{"cte column list update", "WITH t(i) AS (SELECT id FROM issues) UPDATE issues SET priority = 1 WHERE id IN (SELECT i FROM t)", Write},
|
||||
{"recursive cte delete", "WITH RECURSIVE t(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM t WHERE i < 3) DELETE FROM issues WHERE priority IN (SELECT i FROM t)", Write},
|
||||
{"cte escaped quote hides delete", `WITH t AS (SELECT 'a\'(' AS x) DELETE FROM issues`, Write},
|
||||
{"insert select with cte", "INSERT INTO labels (issue_id, label) WITH t AS (SELECT id FROM issues) SELECT id, 'x' FROM t", Write},
|
||||
|
||||
{"insert", "INSERT INTO labels (issue_id, label) VALUES ('a', 'b')", Write},
|
||||
{"replace", "REPLACE INTO labels (issue_id, label) VALUES ('a', 'b')", Write},
|
||||
{"update", "UPDATE issues SET priority = 1", Write},
|
||||
{"delete", "DELETE FROM issues", Write},
|
||||
{"create table", "CREATE TABLE x (id INT PRIMARY KEY)", Write},
|
||||
{"alter table", "ALTER TABLE issues ADD COLUMN x INT", Write},
|
||||
{"drop table", "DROP TABLE x", Write},
|
||||
{"set", "SET @x = 1", Write},
|
||||
{"select into variable", "SELECT 1 INTO @x", Write},
|
||||
|
||||
// Statements that may write AND return rows, or that the parser
|
||||
// cannot classify, must run in a committing transaction that still
|
||||
// renders any result set.
|
||||
{"call", "CALL DOLT_COMMIT('-Am', 'msg')", Mixed},
|
||||
{"explain analyze delete", "EXPLAIN ANALYZE DELETE FROM issues", Mixed},
|
||||
{"unparseable", "PRAGMA table_info(issues)", Mixed},
|
||||
{"garbage", "NOT SQL AT ALL (", Mixed},
|
||||
{"empty", "", Mixed},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := Classify(tc.query); got != tc.want {
|
||||
t.Errorf("Classify(%q) = %v, want %v", tc.query, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue