Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
- Honor `WithPort` and the DSN port on the kernel backend, including after automatic Reyden recovery, instead of rejecting a non-default port. IPv6 hosts need brackets, and a port the kernel rejects returns an error wrapping both `ErrInvalidKernelConfig` and `ErrNotSupportedByKernel`.
- **Behavior change:** Nested `DATE` and `TIMESTAMP_NTZ` text changes in every session time zone, UTC included, with no option to restore the old text. This covers `ARRAY` elements, `MAP` values and keys, and `STRUCT` fields (databricks/databricks-sql-go#604).
- `DATE`: previously Arrow midnight shifted into the session zone, rendered with `time.Time.String()` layout `2006-01-02 15:04:05.999999999 -0700 MST`, e.g. `"2020-12-31 00:00:00 +0000 UTC"` (UTC) or `"2020-12-30 16:00:00 -0800 PST"` (America/Los_Angeles). Now `2006-01-02`, e.g. `"2020-12-31"`: the time (`00:00:00` in UTC) and zone are dropped, and the date is no longer shifted into the session zone.
Expand Down
10 changes: 4 additions & 6 deletions internal/backend/kernel/kernel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package kernel

import (
"bytes"
"context"
"database/sql/driver"
"encoding/json"
Expand Down Expand Up @@ -433,7 +432,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 @@ -459,7 +458,7 @@ func TestKernelLogCtxEmitsCorrelation(t *testing.T) {
CorrID string `json:"corrId"`
QueryID string `json:"queryId"`
}
line := bytes.TrimSpace(buf.Bytes())
line := buf.takeProbe("[kernel] step execute")
if len(line) == 0 {
t.Fatal("klogCtx emitted nothing at debug level, want one line")
}
Expand All @@ -477,13 +476,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("[kernel] step execute"); len(got) != 0 {
t.Errorf("klogCtx emitted %q at warn level, want nothing", got)
}
}
Expand Down
33 changes: 31 additions & 2 deletions internal/backend/kernel/logforward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,41 @@ import (
"encoding/json"
"os"
"strings"
"sync"
"testing"
"time"

"github.com/databricks/databricks-sql-go/v2/logger"
"github.com/rs/zerolog"
)

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(message string) []byte {
capture.mu.Lock()
defer capture.mu.Unlock()
var probe []byte
for _, line := range bytes.Split(capture.buffer.Bytes(), []byte{'\n'}) {
var record struct {
Message string `json:"message"`
}
if json.Unmarshal(line, &record) == nil && record.Message == message {
probe = append(probe, line...)
}
}
capture.buffer.Reset()
return probe
}

func TestLogSinkForwardMapsLevels(t *testing.T) {
cases := []struct {
level string
Expand All @@ -27,15 +55,16 @@ func TestLogSinkForwardMapsLevels(t *testing.T) {
t.Cleanup(func() { logger.SetLogOutput(os.Stderr) })
emittedAt := time.Now()
for _, tc := range cases {
var buf bytes.Buffer
var buf kernelLogCapture
// The sink forwards through the shared output; point it at a buffer to
// capture the single record and confirm it is one hook-free JSON line whose
// only timestamp is the emission time forward stamps.
logger.SetLogOutput(&buf)
sink := newLogSink()
sink.forward(emittedAt, "warn", "databricks::sql::kernel", "unrelated native warning")
sink.forward(emittedAt, tc.level, "databricks::sql::kernel", "hello")
var record map[string]any
if err := json.Unmarshal(bytes.TrimSpace(buf.Bytes()), &record); err != nil {
if err := json.Unmarshal(buf.takeProbe("hello"), &record); err != nil {
t.Fatalf("level %q: %v", tc.level, err)
}
if record["level"] != tc.want || record["target"] != "databricks::sql::kernel" || record["message"] != "hello" {
Expand Down
28 changes: 22 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 nil in production; returning false probes without blocking.
beforeBlockingFetch func() bool

cols []string
colTypes []arrowscan.ColumnTypeInfo // per-column type metadata (PECOBLR-3692)
Expand Down Expand Up @@ -297,12 +300,25 @@ func (r *kernelRows) nextBatch() error {
}
var carr C.struct_ArrowArray
var csch C.struct_ArrowSchema
probePending := false
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() {
probePending = true
return C.KernelStatusCode_Success
}
return C.kernel_result_stream_next_batch(r.stream, &carr, &csch)
}); err != nil {
r.op.backend.evictIfSessionFatal(err)
return fmt.Errorf("kernel: next_batch: %w", toStatementError(err))
}
if probePending {
return io.ErrNoProgress
}
if carr.release == nil {
r.eof = true
klogCtx(r.ctx, "nextBatch: EOF")
Expand Down
Loading
Loading