Skip to content
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Driver changes

- Read buffered kernel result batches without a runtime hop, falling back to a blocking fetch when needed.
- **Behavior change:** Bound each whole-transfer Thrift CloudFetch download attempt, including `WithTransport`, by the existing 900s `ClientTimeout`, and omit the link's presigned query string from download errors (databricks/databricks-sql-go#475).
- **Behavior change:** Without direct results, `ArrowIPCStreamIterator.SchemaBytes()` and `ArrowBatchIterator.Schema()` now retrieve result metadata without fetching data and return the result schema, including for empty results, and can return request or schema-conversion errors. Schemas inferred from Thrift metadata may differ from CloudFetch file schemas when server ArrowSchema is absent (databricks/databricks-sql-go#312).
- Evict kernel connections when SEA reports a missing or invalid session, and reject later requests locally without replaying the failed statement.
Expand Down
33 changes: 28 additions & 5 deletions internal/backend/kernel/kernel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,30 @@ func TestKernelLogNoAllocWhenOff(t *testing.T) {
}
}

type kernelLogCapture struct {
mu sync.Mutex
buffer bytes.Buffer
}

func (capture *kernelLogCapture) Write(data []byte) (int, error) {
capture.mu.Lock()
defer capture.mu.Unlock()
return capture.buffer.Write(data)
}

func (capture *kernelLogCapture) takeProbe() []byte {
capture.mu.Lock()
defer capture.mu.Unlock()
var probe []byte
for _, line := range bytes.Split(capture.buffer.Bytes(), []byte{'\n'}) {
if bytes.Contains(line, []byte("[kernel] step execute")) {
probe = append(probe, line...)
}
}
capture.buffer.Reset()
return probe
}

// TestKernelLogCtxEmitsCorrelation proves the positive half of the contract the
// alloc test can't see: at debug level klogCtx actually EMITS through the shared
// logger AND attaches connId/corrId/queryId, and at the default warn level it emits
Expand All @@ -410,7 +434,7 @@ func TestKernelLogNoAllocWhenOff(t *testing.T) {
// also what "emit nothing" looks like).
func TestKernelLogCtxEmitsCorrelation(t *testing.T) {
prev := logger.Logger.GetLevel()
var buf bytes.Buffer
var buf kernelLogCapture
logger.SetLogOutput(&buf)
// Restore the package defaults (stderr sink, prior level) so we don't leak the
// buffer/level into sibling tests.
Expand All @@ -436,7 +460,7 @@ func TestKernelLogCtxEmitsCorrelation(t *testing.T) {
CorrID string `json:"corrId"`
QueryID string `json:"queryId"`
}
line := bytes.TrimSpace(buf.Bytes())
line := buf.takeProbe()
if len(line) == 0 {
t.Fatal("klogCtx emitted nothing at debug level, want one line")
}
Expand All @@ -454,13 +478,12 @@ func TestKernelLogCtxEmitsCorrelation(t *testing.T) {
rec.ConnID, rec.CorrID, rec.QueryID)
}

// Warn level (the default): nothing is emitted.
buf.Reset()
// Warn level (the default): the probe is not emitted.
if err := logger.SetLogLevel("warn"); err != nil {
t.Fatal(err)
}
klogCtx(ctx, "step %s", "execute")
if got := bytes.TrimSpace(buf.Bytes()); len(got) != 0 {
if got := buf.takeProbe(); len(got) != 0 {
t.Errorf("klogCtx emitted %q at warn level, want nothing", got)
}
}
Expand Down
16 changes: 16 additions & 0 deletions internal/backend/kernel/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build cgo && databricks_kernel

package kernel

import (
"os"
"os/signal"
"syscall"
"testing"
)

func TestMain(tests *testing.M) {
// TODO(databricks/databricks-sql-kernel#509): Remove once the bindings block SIGPIPE on kernel runtime threads.
signal.Ignore(syscall.SIGPIPE)
os.Exit(tests.Run())
}
23 changes: 17 additions & 6 deletions internal/backend/kernel/rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ var (
)

// kernelRows implements driver.Rows over the kernel result stream. It pulls one
// Arrow RecordBatch at a time via kernel_result_stream_next_batch (inline and
// CloudFetch are transparent below the C ABI), imports it zero-copy through the
// Arrow C Data Interface, and scans rows out on demand.
// Arrow RecordBatch at a time: nextBatch tries kernel_result_stream_try_next_batch
// first and uses kernel_result_stream_next_batch only as the blocking fallback.
// Inline and CloudFetch are transparent below the C ABI; batches are imported
// zero-copy through the Arrow C Data Interface and scanned on demand.
//
// The batch/row split mirrors the kernel's own ResultStream: next_batch does the
// per-batch network/decode work once; row reads walk the already-imported
// arrow.RecordBatch with O(1) indexing.
// The batch/row split mirrors the kernel's own ResultStream: nextBatch imports
// each batch once; row reads walk the already-imported arrow.RecordBatch with
// O(1) indexing.
type kernelRows struct {
// ctx is detached from the caller's QueryContext (via context2.WithoutCancel)
// so a submit-gating deadline can't truncate a large CloudFetch stream, matching
Expand All @@ -63,6 +64,8 @@ type kernelRows struct {
op *kernelOp
stream *C.kernel_result_stream_t
callbacks *dbsqlrows.TelemetryCallbacks
// beforeBlockingFetch is a test hook; nil in production.
beforeBlockingFetch func()

cols []string
colTypes []arrowscan.ColumnTypeInfo // per-column type metadata (PECOBLR-3692)
Expand Down Expand Up @@ -298,6 +301,14 @@ func (r *kernelRows) nextBatch() error {
var carr C.struct_ArrowArray
var csch C.struct_ArrowSchema
if err := call(func() C.KernelStatusCode {
var ready C.bool
status := C.kernel_result_stream_try_next_batch(r.stream, &ready, &carr, &csch)
if status != C.KernelStatusCode_Success || bool(ready) {
return status
}
if r.beforeBlockingFetch != nil {
r.beforeBlockingFetch()
}
return C.kernel_result_stream_next_batch(r.stream, &carr, &csch)
}); err != nil {
r.op.backend.evictIfSessionFatal(err)
Expand Down
Loading
Loading