diff --git a/CHANGELOG.md b/CHANGELOG.md index 77a60de2..62683ee8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased (v2.0.0 prototype) +- Support Volume PUT/GET/REMOVE through kernel ExecContext with staging roots, including resolved-symlink authorization and context cancellation. QueryContext retains raw instructions. - Fix the module-root package conflict when building, linting, or running Gazelle with the `tools` build tag (databricks/databricks-sql-go#162). - **Behavior change:** Report declared DECIMAL precision and scale through `sql.ColumnType.DecimalSize()` on Thrift and SEA, including zero-row results. Tools such as ORM migrators can now act on these qualifiers (databricks/databricks-sql-go#500). - **Breaking change:** Upgrade Apache Arrow Go from v12 to v18.8.0 (`github.com/apache/arrow-go/v18`). The public `rows.ArrowBatchIterator` now uses v18 `arrow.RecordBatch` and `*arrow.Schema` types; callers using Arrow batches must migrate their Arrow imports and dependent code. This requires a major driver release (v2.0.0), including a `/v2` driver module path before publication. diff --git a/KERNEL_REV b/KERNEL_REV index de1b52ca..28a4d122 100644 --- a/KERNEL_REV +++ b/KERNEL_REV @@ -1 +1 @@ -0b5d0edfed32c0745d5abc2468fc3070e2eb2a17 +d9fcf834d36dc7f3de1621225db716200151fa0f diff --git a/connection.go b/connection.go index b6269686..48cf6c31 100644 --- a/connection.go +++ b/connection.go @@ -820,25 +820,29 @@ func (c *conn) execStagingOperation( return nil } - if len(driverctx.StagingPathsFromContext(ctx)) != 0 { - // Telemetry callback for staging operation row fetching (chunk timing not tracked for staging ops). - telemetryUpdate := func(chunkCount int, bytesDownloaded int64, chunkIndex int, chunkLatencyMs int64, totalChunksPresent int32) { - if c.telemetry != nil { - c.telemetry.AddTag(ctx, telemetry.TagChunkCount, chunkCount) - c.telemetry.AddTag(ctx, telemetry.TagBytesDownloaded, bytesDownloaded) - } + if len(driverctx.StagingPathsFromContext(ctx)) == 0 { + return dbsqlerrint.NewDriverError(ctx, "staging ctx must be provided.", nil) + } + if native, ok := op.(interface{ CompleteStaging(context.Context) error }); ok { + if err := native.CompleteStaging(ctx); err != nil { + return dbsqlerrint.NewDriverError(ctx, "error performing staging operation", err) } - row, err = op.Results(ctx, &rows.TelemetryCallbacks{ - OnChunkFetched: telemetryUpdate, - }) - if err != nil { - return dbsqlerrint.NewDriverError(ctx, "error reading row.", err) + return nil + } + // Telemetry callback for staging operation row fetching (chunk timing not tracked for staging ops). + telemetryUpdate := func(chunkCount int, bytesDownloaded int64, chunkIndex int, chunkLatencyMs int64, totalChunksPresent int32) { + if c.telemetry != nil { + c.telemetry.AddTag(ctx, telemetry.TagChunkCount, chunkCount) + c.telemetry.AddTag(ctx, telemetry.TagBytesDownloaded, bytesDownloaded) } - defer row.Close() //nolint:errcheck - - } else { - return dbsqlerrint.NewDriverError(ctx, "staging ctx must be provided.", nil) } + row, err = op.Results(ctx, &rows.TelemetryCallbacks{ + OnChunkFetched: telemetryUpdate, + }) + if err != nil { + return dbsqlerrint.NewDriverError(ctx, "error reading row.", err) + } + defer row.Close() //nolint:errcheck var sqlRow []driver.Value colNames := row.Columns() diff --git a/connection_test.go b/connection_test.go index 9057e50f..c5ae12bd 100644 --- a/connection_test.go +++ b/connection_test.go @@ -17,7 +17,9 @@ import ( "github.com/apache/thrift/lib/go/thrift" "github.com/pkg/errors" + "github.com/databricks/databricks-sql-go/driverctx" dbsqlerr "github.com/databricks/databricks-sql-go/errors" + "github.com/databricks/databricks-sql-go/internal/backend" thriftbackend "github.com/databricks/databricks-sql-go/internal/backend/thrift" "github.com/databricks/databricks-sql-go/internal/cli_service" "github.com/databricks/databricks-sql-go/internal/client" @@ -1074,3 +1076,43 @@ func TestConn_handleStagingRetry(t *testing.T) { assert.Less(t, elapsed, 2*time.Second, "context cancel should abort PUT retry backoff promptly") }) } + +type nativeStagingStub struct { + backend.Operation + marked bool + completed bool + err error +} + +func (o *nativeStagingStub) StatementID() string { return "volume-test" } +func (o *nativeStagingStub) IsStaging(context.Context) (bool, error) { return o.marked, nil } +func (o *nativeStagingStub) CompleteStaging(context.Context) error { o.completed = true; return o.err } + +func TestConnNativeStagingDispatch(t *testing.T) { + completionErr := errors.New("native completion failed") + for _, tc := range []struct { + name string + marked, roots, wantCall, wantError bool + completionErr error + }{ + {"ordinary query", false, true, false, false, nil}, + {"missing roots", true, false, false, true, nil}, + {"native completion", true, true, true, false, nil}, + {"native completion error", true, true, true, true, completionErr}, + } { + t.Run(tc.name, func(t *testing.T) { + ctx := context.Background() + if tc.roots { + ctx = driverctx.NewContextWithStagingInfo(ctx, []string{t.TempDir()}) + } + op := &nativeStagingStub{marked: tc.marked, err: tc.completionErr} + err := (&conn{}).execStagingOperation(op, ctx) + assert.Equal(t, tc.wantError, err != nil) + assert.Equal(t, tc.wantCall, op.completed) + if tc.completionErr != nil { + assert.ErrorIs(t, err, dbsqlerr.DriverError) + assert.ErrorIs(t, err, tc.completionErr) + } + }) + } +} diff --git a/doc.go b/doc.go index b0cc32a0..5181b881 100644 --- a/doc.go +++ b/doc.go @@ -227,8 +227,7 @@ RetryWaitMin / RetryWaitMax / RetryMax, including the disable form, forwarded to kernel's HTTP retry config); and the TLS, proxy, and session-conf (query tags, statement timeout, time zone) options. Federated token providers use one token snapshot during setup. Nothing is silently ignored: WithTimeout and token-provider / external / -static authenticators are rejected at connect; staging (PUT/GET/REMOVE on a -Unity Catalog volume) is rejected at execute. WithMaxRows is accepted but inert (the +static authenticators are rejected at connect. WithMaxRows is accepted but inert (the kernel manages fetching below the C ABI). Kernel HTTP requests inherit the driver's ClientTimeout (900s by default). If @@ -368,6 +367,13 @@ After doing so, you can execute staging operations using this context using the _, err1 := db.ExecContext(ctx, `PUT 'staging/file.csv' INTO '/Volumes/main/staging_test/e2etests/file.csv' OVERWRITE`) +With the kernel backend, ExecContext completes the transfer and reports zero +rows affected. QueryContext returns raw instruction rows without file effects. +Both the requested local path and its resolved symlink target must remain +within the configured staging roots. Symlinks within those roots are permitted; +escapes are rejected, which is stricter than Thrift's requested-path check. +The staging context is required for REMOVE as well as PUT and GET. + # Errors There are three error types exposed via dbsql/errors diff --git a/examples/kernel/main.go b/examples/kernel/main.go index 51d99c31..e3f19775 100644 --- a/examples/kernel/main.go +++ b/examples/kernel/main.go @@ -87,14 +87,4 @@ func main() { log.Fatalf("query: %v", err) } fmt.Printf("server version: %s\n", version) - - // Options the kernel can't honor are rejected with an error wrapping - // ErrNotSupportedByKernel, never silently ignored. Staging (PUT/GET/REMOVE on a - // Unity Catalog volume) is one such feature, rejected at execute. - _, err = db.ExecContext(ctx, "PUT '/tmp/local.csv' INTO '/Volumes/main/default/vol/f.csv' OVERWRITE") - if errors.Is(err, dbsqlerr.ErrNotSupportedByKernel) { - fmt.Println("staging is not supported on the kernel backend (use Thrift for staging)") - } else if err != nil { - fmt.Printf("staging: %v\n", err) - } } diff --git a/internal/backend/kernel/backend.go b/internal/backend/kernel/backend.go index d8e20d08..bdb8c54f 100644 --- a/internal/backend/kernel/backend.go +++ b/internal/backend/kernel/backend.go @@ -72,7 +72,6 @@ import ( "sync/atomic" "time" - dbsqlerr "github.com/databricks/databricks-sql-go/errors" "github.com/databricks/databricks-sql-go/internal/backend" ) @@ -908,15 +907,5 @@ func (k *KernelBackend) SessionID() string { return k.sessionID } // parameters (req.Params) are bound onto the statement in execute (see // bindParams). func (k *KernelBackend) Execute(ctx context.Context, req backend.ExecRequest) (backend.Operation, error) { - // Staging (Unity Catalog volume PUT/GET/REMOVE) needs a local file transfer the - // kernel path can't perform, and the kernel C ABI surfaces no IsStagingOperation - // signal to drive conn.execStagingOperation. Reject it here rather than let - // IsStaging return false and report success with no file moved (silent data loss). - // (Bound parameters, by contrast, ARE now supported — bound in execute via the - // kernel's raw-param C ABI.) - if isStagingStatement(req.Query) { - return &kernelOp{}, fmt.Errorf("databricks: staging operations (PUT/GET/REMOVE on a volume) are %w; "+ - "use the default (Thrift) backend", dbsqlerr.ErrNotSupportedByKernel) - } return k.execute(ctx, req) } diff --git a/internal/backend/kernel/cgo.go b/internal/backend/kernel/cgo.go index a8a79f6d..f6ba5999 100644 --- a/internal/backend/kernel/cgo.go +++ b/internal/backend/kernel/cgo.go @@ -38,7 +38,9 @@ import "C" import ( "context" "fmt" + "os" "runtime" + "syscall" "unsafe" "github.com/databricks/databricks-sql-go/driverctx" @@ -171,6 +173,10 @@ func lastError(code C.KernelStatusCode) *KernelError { VendorCode: int32(e.vendor_code), HTTPStatus: uint16(e.http_status), Retryable: bool(e.retryable), + ErrorCode: C.GoString(C.kernel_get_last_error_code()), + } + if code := C.kernel_get_last_error_local_os_error(); code != 0 { + ke.cause = &os.PathError{Op: "file", Path: C.GoString(C.kernel_get_last_error_local_path()), Err: syscall.Errno(code)} } if e.sql_state != nil { ke.SQLState = C.GoString(e.sql_state) diff --git a/internal/backend/kernel/errors_classify.go b/internal/backend/kernel/errors_classify.go index 73b7c39c..7e07d065 100644 --- a/internal/backend/kernel/errors_classify.go +++ b/internal/backend/kernel/errors_classify.go @@ -49,8 +49,12 @@ type KernelError struct { HTTPStatus uint16 Retryable bool QueryID string + ErrorCode string + cause error } +func (e *KernelError) Unwrap() error { return e.cause } + func (e *KernelError) Error() string { // Append the server query id when present — it is the one correlation handle // to server-side query history, and on the execute-error path (no exec handle) diff --git a/internal/backend/kernel/include/databricks_kernel.h b/internal/backend/kernel/include/databricks_kernel.h index 965c09b6..b6e2ded7 100644 --- a/internal/backend/kernel/include/databricks_kernel.h +++ b/internal/backend/kernel/include/databricks_kernel.h @@ -190,6 +190,12 @@ typedef struct KernelError { * last kernel call cleared the buffer. */ bool kernel_get_last_error(KernelError* out); +/* These accessors preserve the last error. Strings are borrowed until the next + * error-clearing kernel call; absent strings are NULL, absent OS code is zero. */ +int32_t kernel_get_last_error_local_os_error(void); +const char* kernel_get_last_error_code(void); +const char* kernel_get_last_error_local_kind(void); +const char* kernel_get_last_error_local_path(void); /* ─── Opaque handles ────────────────────────────────────────────────── * @@ -369,6 +375,7 @@ KernelStatusCode kernel_session_config_set_sea_response_compression_enabled( /* Configure the total timeout for one HTTP request, from connection start * through response-body completion. A separate 30s connection timeout also * applies, so connection establishment is bounded by the shorter limit. + * Does not bound Volume storage requests or configure their inactivity limit. * `request_timeout_ms == 0` keeps the kernel default (120s). */ KernelStatusCode kernel_session_config_set_request_timeout(KernelSessionConfig* config, uint64_t request_timeout_ms); @@ -595,12 +602,12 @@ KernelStatusCode kernel_session_config_set_u2m_token_cache_config(KernelSessionC * * Recognised keys (case-insensitive): * - "cloudfetch_enabled" = "true"|"false" — false serves every result - * inline (SEA disposition = INLINE), mirroring JDBC EnableQueryResultDownload=0. + * through paged inline delivery without cloud-storage downloads. * - "cloudfetch_link_prefetch_window" = positive integer. * - "cloudfetch_max_chunks_in_memory" = positive integer (bounds peak RSS; * values above the kernel ceiling of 256 are clamped with a warning). * - "inline_max_chunks_in_memory" = positive integer (inline-Arrow prefetch - * window, used when cloudfetch_enabled = false). + * window for paged inline results). * - "complex_types_as_json" = "true"|"false" — render ARRAY / MAP / STRUCT / * VARIANT / GEOMETRY / GEOGRAPHY as JSON text (PySQL * _use_arrow_native_complex_types=False, JDBC complexDatatypeSupport=false). @@ -966,6 +973,17 @@ KernelStatusCode kernel_executed_async_statement_close( */ KernelStatusCode kernel_result_stream_get_schema(kernel_result_stream_t* stream, struct ArrowSchema* out); +/* Volume completion preserves raw rows. Roots are a JSON array of strings; + * out_status is 0=ordinary result, 1=input bytes required, 2=completed. + * The detached result-stream canceller can interrupt completion. */ +KernelStatusCode kernel_executed_statement_is_volume_operation( + const kernel_executed_statement_t* exec, bool* out); +KernelStatusCode kernel_result_stream_is_volume_operation( + const kernel_result_stream_t* stream, bool* out); +KernelStatusCode kernel_result_stream_complete_volume_operation( + kernel_result_stream_t* stream, const char* allowed_paths_json, + bool remove_not_found_is_success, int32_t* out_status); + KernelStatusCode kernel_result_stream_next_batch(kernel_result_stream_t* stream, struct ArrowArray* out_array, struct ArrowSchema* out_schema); diff --git a/internal/backend/kernel/kernel_test.go b/internal/backend/kernel/kernel_test.go index cd379412..3485fb78 100644 --- a/internal/backend/kernel/kernel_test.go +++ b/internal/backend/kernel/kernel_test.go @@ -531,33 +531,6 @@ func TestExecuteHandleLessOpContract(t *testing.T) { } } -// TestExecuteRejectsStaging drives Execute with a staging statement (not just the -// isStagingStatement detector in isolation) to pin the detector→Execute wiring: a -// refactor that dropped or reordered the check would silently reopen the -// silent-no-op data-loss path. Mirrors TestExecuteRejectsParams. -func TestExecuteRejectsStaging(t *testing.T) { - k := &KernelBackend{} - op, err := k.Execute(context.Background(), backend.ExecRequest{ - Query: "PUT '/tmp/f' INTO '/Volumes/main/s/e/f.csv'", - }) - if err == nil { - t.Fatal("expected an error for a staging statement, got nil") - } - if !errors.Is(err, dbsqlerr.ErrNotSupportedByKernel) { - t.Errorf("staging rejection should wrap ErrNotSupportedByKernel, got %v", err) - } - if op == nil { - t.Fatal("Execute must return a non-nil Operation per the Backend contract") - } - closed, closeErr := op.Close(context.Background()) - if closeErr != nil { - t.Errorf("Close error = %v, want nil", closeErr) - } - if closed { - t.Error("Close on a handle-less op must report closed=false (no CLOSE_STATEMENT)") - } -} - // ExecutionError must satisfy the same public contract as the Thrift path so the // errors.Is → errors.As → SqlState()/QueryId() recipe documented in doc.go works // on the kernel backend too (it previously returned a bare *KernelError that diff --git a/internal/backend/kernel/operation.go b/internal/backend/kernel/operation.go index e2f355f0..59956ffb 100644 --- a/internal/backend/kernel/operation.go +++ b/internal/backend/kernel/operation.go @@ -11,6 +11,7 @@ import "C" import ( "context" "database/sql/driver" + "encoding/json" "errors" "fmt" "sync" @@ -35,8 +36,7 @@ import ( // client-timeout path, let an in-flight cancel finish in the background so // it cannot extend the client deadline // -// Binds any query parameters (bindParams) before executing; staging statements -// are rejected up front by Execute, so none reach here. +// Binds any query parameters (bindParams) before executing. func (k *KernelBackend) execute(ctx context.Context, req backend.ExecRequest) (backend.Operation, error) { // Log the SQL length, not the text: query bodies can carry PII/secrets in // WHERE/INSERT/SET, and this goes to stderr. Matches the driver's own @@ -384,11 +384,70 @@ func (o *kernelOp) Results(ctx context.Context, callbacks *dbsqlrows.TelemetryCa return newKernelRows(ctx, o, stream, callbacks) } -// IsStaging reports whether this is a staging (PUT/GET/REMOVE) operation. Always -// false on the kernel path: staging statements are rejected up front in -// Execute (isStagingStatement), so no kernelOp is ever produced for one — there -// is nothing for conn.execStagingOperation to act on here. -func (o *kernelOp) IsStaging(ctx context.Context) (bool, error) { return false, nil } +func (o *kernelOp) IsStaging(ctx context.Context) (bool, error) { + if o.exec == nil { + return false, nil + } + var marked C.bool + err := call(func() C.KernelStatusCode { + return C.kernel_executed_statement_is_volume_operation(o.exec, &marked) + }) + return bool(marked), err +} + +func (o *kernelOp) CompleteStaging(ctx context.Context) error { + if err := ctx.Err(); err != nil { + return err + } + var stream *C.kernel_result_stream_t + if err := call(func() C.KernelStatusCode { + return C.kernel_executed_statement_get_result_stream(o.exec, &stream) + }); err != nil { + return toStatementError(err) + } + defer C.kernel_result_stream_close(stream) + var canceller *C.kernel_result_stream_canceller_t + if err := call(func() C.KernelStatusCode { + return C.kernel_result_stream_canceller_new(stream, &canceller) + }); err != nil { + return toStatementError(err) + } + defer C.kernel_result_stream_canceller_free(canceller) + done, stopped := make(chan struct{}), make(chan struct{}) + go func() { + defer close(stopped) + select { + case <-ctx.Done(): + C.kernel_result_stream_canceller_cancel(canceller) + case <-done: + } + }() + defer func() { close(done); <-stopped }() + encoded, err := json.Marshal(driverctx.StagingPathsFromContext(ctx)) + if err != nil { + return err + } + paths := newCStr(string(encoded)) + defer paths.free() + var outcome C.int32_t + err = call(func() C.KernelStatusCode { + return C.kernel_result_stream_complete_volume_operation(stream, paths.c, true, &outcome) + }) + if err != nil { + var ke *KernelError + if !errors.As(err, &ke) || (ke.ErrorCode != "VOLUME_STORAGE_HTTP_ERROR" && ke.ErrorCode != "VOLUME_STORAGE_NETWORK_ERROR") { + o.backend.evictIfSessionFatal(err) + } + if ctx.Err() != nil { + return fmt.Errorf("kernel: volume transfer cancelled: %w (kernel error: %w)", ctx.Err(), toStatementError(err)) + } + return toStatementError(err) + } + if outcome != 2 { + return errors.New("kernel: volume operation requires a local file instruction") + } + return nil +} // Close best-effort closes the executed statement and its statement handle. It is // idempotent (a second call, or a call after Rows.Close already tore the diff --git a/internal/backend/kernel/staging.go b/internal/backend/kernel/staging.go deleted file mode 100644 index c614169c..00000000 --- a/internal/backend/kernel/staging.go +++ /dev/null @@ -1,67 +0,0 @@ -package kernel - -import "strings" - -// This file is intentionally NOT behind the `cgo && databricks_kernel` build tag. -// Staging detection is pure Go (string inspection of the SQL), so keeping it -// untagged lets its test run under CGO_ENABLED=0. - -// isStagingStatement reports whether sql is a Unity Catalog volume staging -// command (PUT / GET / REMOVE). The Thrift backend learns a statement is staging -// from server result metadata (IsStagingOperation); the kernel C ABI exposes no -// such signal, and the kernel path cannot perform the local file transfer these -// commands require. We therefore detect them from the SQL and reject at execute -// time (see Execute) rather than silently returning success with no file moved — -// which is what returning false from IsStaging would do. Case-insensitive; skips -// leading whitespace AND leading SQL comments (`-- ...` and `/* ... */`), so a -// comment-prefixed staging command is still caught rather than slipping through -// as a silent no-op. Errs toward matching only a clear leading keyword — a false -// negative just yields the kernel's normal "unsupported syntax" server error, -// whereas a false positive would reject a legitimate query. -func isStagingStatement(sql string) bool { - s := stripLeadingSQLComments(sql) - for _, kw := range []string{"PUT", "GET", "REMOVE"} { - if len(s) > len(kw) && - strings.EqualFold(s[:len(kw)], kw) && - isSQLTokenBreak(s[len(kw)]) { - return true - } - } - return false -} - -// stripLeadingSQLComments removes leading whitespace and leading SQL comments -// (both `-- ...` line comments and `/* ... */` block comments), repeatedly, so -// the first real token is exposed. An unterminated block comment consumes the -// rest of the string (yielding ""). Comment bodies are not scanned for nested -// markers — SQL block comments do not nest — and this only touches the LEADING -// run, so a comment later in the statement is untouched. -func stripLeadingSQLComments(sql string) string { - s := strings.TrimSpace(sql) - for { - switch { - case strings.HasPrefix(s, "--"): - if i := strings.IndexByte(s, '\n'); i >= 0 { - s = s[i+1:] - } else { - return "" // line comment to end of input - } - case strings.HasPrefix(s, "/*"): - if i := strings.Index(s[2:], "*/"); i >= 0 { - s = s[2+i+2:] - } else { - return "" // unterminated block comment - } - default: - return s - } - s = strings.TrimSpace(s) - } -} - -// isSQLTokenBreak reports whether b ends a leading keyword — a space or a quote -// (staging commands take a quoted path, e.g. PUT '' INTO ''), so -// "PUT '..." matches but an identifier like "PUTS" or "GETTER" does not. -func isSQLTokenBreak(b byte) bool { - return b == ' ' || b == '\t' || b == '\n' || b == '\r' || b == '\'' || b == '"' -} diff --git a/internal/backend/kernel/staging_test.go b/internal/backend/kernel/staging_test.go deleted file mode 100644 index 91d69794..00000000 --- a/internal/backend/kernel/staging_test.go +++ /dev/null @@ -1,41 +0,0 @@ -package kernel - -import "testing" - -func TestIsStagingStatement(t *testing.T) { - staging := []string{ - "PUT '/tmp/f.csv' INTO '/Volumes/main/s/e/f.csv' OVERWRITE", - "GET '/Volumes/main/s/e/f.csv' TO 'local.csv'", - "REMOVE '/Volumes/main/s/e/f.csv'", - " put '/tmp/f' into '/Volumes/x'", // leading space + lowercase - "Get\t'/Volumes/x' to 'y'", // tab after keyword, mixed case - "/* audit */ PUT '/local' INTO '/Volumes/main/s/v'", // leading block comment - "/* a */ /* b */ PUT '/l' INTO '/Volumes/x'", // multiple block comments - "-- upload the file\nPUT '/l' INTO '/Volumes/x'", // leading line comment, then staging - " /* c */\n -- d\n REMOVE '/Volumes/x'", // mixed comments + whitespace - } - for _, sql := range staging { - if !isStagingStatement(sql) { - t.Errorf("expected staging: %q", sql) - } - } - - notStaging := []string{ - "SELECT 1", - "PUTS ON A SHOW", // PUT is a prefix of an identifier, not a token - "GETTER()", // likewise for GET - "REMOVED_AT FROM t", // likewise for REMOVE - "INSERT INTO t VALUES (1)", // contains INTO but is not staging - "", // empty - "-- PUT '/x' INTO '/y'", // whole line commented out; nothing after - "/* PUT '/x' INTO '/y' */", // whole statement inside a block comment - "SELECT 'PUT ' AS col", // keyword only inside a literal - "/* unterminated PUT '/x'", // unterminated block comment consumes the rest - "/* c */ SELECT 1", // real leading comment, but not a staging command - } - for _, sql := range notStaging { - if isStagingStatement(sql) { - t.Errorf("did not expect staging: %q", sql) - } - } -} diff --git a/staging_kernel_test.go b/staging_kernel_test.go new file mode 100644 index 00000000..fb769cc5 --- /dev/null +++ b/staging_kernel_test.go @@ -0,0 +1,163 @@ +//go:build cgo && databricks_kernel + +package dbsql + +import ( + "bytes" + "context" + "database/sql" + "database/sql/driver" + "encoding/base64" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "sync/atomic" + "testing" + "time" + + "github.com/apache/arrow-go/v18/arrow" + "github.com/apache/arrow-go/v18/arrow/array" + "github.com/apache/arrow-go/v18/arrow/ipc" + "github.com/apache/arrow-go/v18/arrow/memory" + "github.com/databricks/databricks-sql-go/driverctx" + "github.com/databricks/databricks-sql-go/internal/backend" + "github.com/databricks/databricks-sql-go/internal/backend/kernel" + "github.com/stretchr/testify/require" +) + +type cancelOnCompletionErrorContext struct { + context.Context + cancel context.CancelFunc + errCalls int +} + +func (c *cancelOnCompletionErrorContext) Err() error { + c.errCalls++ + // Deliver cancellation after native completion returns to make the error race deterministic. + if c.errCalls == 2 { + c.cancel() + } + return c.Context.Err() +} + +func TestKernelVolumeFailureOriginControlsSessionValidity(t *testing.T) { + for _, mode := range []string{"instruction401", "storage401", "storageBody", "instruction401Cancelled", "storage401Cancelled"} { + t.Run(mode, func(t *testing.T) { + directory := t.TempDir() + destination := filepath.Join(directory, "download.bin") + require.NoError(t, os.WriteFile(destination, []byte("old bytes"), 0600)) + var executions, storage atomic.Int64 + var result map[string]any + server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, _ = io.Copy(io.Discard, r.Body) + w.Header().Set("Content-Type", "application/json") + switch { + case r.Method == "POST" && strings.HasSuffix(r.URL.Path, "/sessions"): + _, _ = io.WriteString(w, `{"session_id":"volume-session"}`) + case r.Method == "POST" && r.URL.Path == "/api/2.0/sql/statements": + executions.Add(1) + _ = json.NewEncoder(w).Encode(result) + case r.URL.Path == "/api/2.0/sql/statements/volume-stmt/result/chunks/1": + w.WriteHeader(401) + _, _ = io.WriteString(w, `{"error_code":"SESSION_EXPIRED","message":"instruction session expired","sql_state":"08003"}`) + case r.URL.Path == "/object": + storage.Add(1) + if strings.HasPrefix(mode, "storage401") { + w.WriteHeader(401) + } else { + w.Header().Set("Content-Length", "20") + _, _ = io.WriteString(w, "short") + } + default: + _, _ = io.WriteString(w, `{}`) + } + })) + defer server.Close() + names := []string{"operation", "presignedUrl", "headers", "localFile"} + values := []string{"GET", server.URL + "/object", "{}", destination} + fields, columns := make([]arrow.Field, 4), make([]arrow.Array, 4) + manifestColumns := make([]map[string]any, 4) + for i, name := range names { + fields[i] = arrow.Field{Name: name, Type: arrow.BinaryTypes.String, Nullable: true} + builder := array.NewStringBuilder(memory.DefaultAllocator) + builder.Append(values[i]) + columns[i] = builder.NewStringArray() + builder.Release() + defer columns[i].Release() + manifestColumns[i] = map[string]any{"name": name, "type_name": "STRING", "type_text": "STRING", "position": i} + } + schema := arrow.NewSchema(fields, nil) + record := array.NewRecordBatch(schema, columns, 1) + defer record.Release() + var attachment bytes.Buffer + writer := ipc.NewWriter(&attachment, ipc.WithSchema(schema)) + require.NoError(t, writer.Write(record)) + require.NoError(t, writer.Close()) + chunk := map[string]any{"chunk_index": 0, "row_offset": 0, "row_count": 1, "attachment": base64.StdEncoding.EncodeToString(attachment.Bytes())} + chunks := 1 + if strings.HasPrefix(mode, "instruction401") { + chunk["next_chunk_index"], chunks = 1, 2 + } + result = map[string]any{ + "statement_id": "volume-stmt", "status": map[string]any{"state": "SUCCEEDED"}, + "manifest": map[string]any{"format": "ARROW_STREAM", "is_volume_operation": true, "schema": map[string]any{"column_count": 4, "columns": manifestColumns}, "total_row_count": 1, "total_chunk_count": chunks}, + "result": chunk, + } + connector, err := NewConnector(WithServerHostname(server.URL), WithHTTPPath("/sql/1.0/warehouses/test"), + WithAccessToken("test-token"), WithUseKernel(true), WithSkipTLSHostVerify(), WithRetries(-1, time.Millisecond, time.Millisecond)) + require.NoError(t, err) + db := sql.OpenDB(connector) + defer db.Close() + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + connection, err := db.Conn(ctx) + require.NoError(t, err) + defer connection.Close() + if strings.HasSuffix(mode, "Cancelled") { + err = connection.Raw(func(raw any) error { + operation, err := raw.(*conn).backend.Execute(ctx, backend.ExecRequest{Query: "GET instruction"}) + if err != nil { + return err + } + defer operation.Close(ctx) + cancelContext, cancelError := context.WithCancel(ctx) + defer cancelError() + stagingContext := driverctx.NewContextWithStagingInfo(&cancelOnCompletionErrorContext{Context: cancelContext, cancel: cancelError}, []string{directory}) + return operation.(interface{ CompleteStaging(context.Context) error }).CompleteStaging(stagingContext) + }) + require.ErrorIs(t, err, context.Canceled) + } else { + _, err = connection.ExecContext(driverctx.NewContextWithStagingInfo(ctx, []string{directory}), "GET instruction") + } + require.Error(t, err) + require.NotErrorIs(t, err, driver.ErrBadConn) + var detail *kernel.KernelError + require.ErrorAs(t, err, &detail) + wantCode := "VOLUME_STORAGE_HTTP_ERROR" + if strings.HasPrefix(mode, "instruction401") { + wantCode = "SESSION_EXPIRED" + } + if mode == "storageBody" { + wantCode = "VOLUME_STORAGE_NETWORK_ERROR" + } + require.Equal(t, wantCode, detail.ErrorCode) + require.NoError(t, connection.Raw(func(raw any) error { + require.Equal(t, !strings.HasPrefix(mode, "instruction401"), raw.(driver.Validator).IsValid()) + return nil + })) + data, err := os.ReadFile(destination) + require.NoError(t, err) + require.Equal(t, "old bytes", string(data)) + require.Equal(t, int64(1), executions.Load()) + wantStorage := int64(1) + if strings.HasPrefix(mode, "instruction401") { + wantStorage = 0 + } + require.Equal(t, wantStorage, storage.Load()) + }) + } +}