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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
- **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.
- Reduce Thrift row-scan CPU by binding scalar readers and null/type handling once per Arrow record (databricks/databricks-sql-go#602).
- **Behavior change:** Closed Thrift Arrow row scanners return an explicit error without logging an ERROR instead of stale values; a destination whose column count differs from the Arrow holders now errors instead of filling leading columns or NULL-padding missing columns. Column-value setup failures now stop scanning and are retained for subsequent scans instead of only being logged and ignored (databricks/databricks-sql-go#602).

## v2.0.0 (2026-11-23)

Expand Down
85 changes: 30 additions & 55 deletions internal/rows/arrowbased/arrowRows.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type arrowRowScanner struct {
currentBatch SparkArrowBatch

// Currently loaded field values for a set of rows
rowValues RowValues
rowValues *rowValues

// function to convert arrow timestamp when using native arrow format
toTimestampFn timeStampFn
Expand All @@ -77,6 +77,8 @@ type arrowRowScanner struct {
ctx context.Context

batchIterator BatchIterator
closed bool
scanError dbsqlerr.DBError
}

// Make sure arrowRowScanner fulfills the RowScanner interface
Expand Down Expand Up @@ -156,6 +158,7 @@ func NewArrowRowScanner(resultSetMetadata *cli_service.TGetResultSetMetadataResp

// Close is called when the Rows instance is closed.
func (ars *arrowRowScanner) Close() {
ars.closed = true
if ars.rowValues != nil {
ars.rowValues.Close()
}
Expand Down Expand Up @@ -197,57 +200,41 @@ func (ars *arrowRowScanner) ScanRow(
destination []driver.Value,
rowNumber int64) dbsqlerr.DBError {

// load the error batch for the specified row, if necessary
if ars != nil && ars.closed {
return dbsqlerrint.NewDriverError(ars.ctx, errArrowRowsScannerClosed, nil)
}

// load the Arrow batch for the specified row, if necessary
err := ars.loadBatchFor(rowNumber)
if err != nil {
return err
}
if len(destination) != len(ars.rowValues.readers) {
return dbsqlerrint.NewDriverError(ars.ctx, errArrowRowsReaderMismatch, nil)
}
readers := ars.rowValues.readers[:len(destination)]

// if no location is provided default to UTC
if ars.location == nil {
ars.location = time.UTC
}
rowIndex := int(rowNumber - ars.rowValues.Start())

// loop over the destination slice filling in values
for i := range destination {
// clear the destination
destination[i] = nil

// if there is a corresponding column and the value for the specified row
// is not null we put the value in the destination
if !ars.rowValues.IsNull(i, rowNumber) {

// Bound readers handle both NULLs and column-specific conversions.
var err1 error
destination[i], err1 = readers[i](rowIndex)
if err1 != nil {
col := ars.colInfo[i]
dbType := col.dbType

if isIntervalType(dbType) && ars.UseArrowNativeIntervalTypes {
// not yet fully supported
ars.Error().Msg(errArrowRowsUnsupportedNativeType(dbType.String()))
return dbsqlerrint.NewDriverError(ars.ctx, errArrowRowsUnsupportedNativeType(dbType.String()), nil)
}

// A top-level native-arrow DECIMAL column is returned as a lossless,
// scale-applied string so precision beyond float64 is preserved.
// Gated on the requested config (not merely the holder type) so that
// disabling native decimal keeps the prior float64 behavior even if
// the server-provided arrow schema carries a decimal128. Decimals
// nested in complex types take a different path: the complex
// container renders them losslessly via marshalScalar during JSON
// serialization, independent of this config gate.
// See databricks/databricks-sql-go#274 and #253.
if dbType == cli_service.TTypeId_DECIMAL_TYPE && ars.UseArrowNativeDecimal {
if s, ok := ars.rowValues.DecimalStringValue(i, rowNumber); ok {
destination[i] = s
continue
}
}

// get the value from the column values holder
var err1 error
destination[i], err1 = ars.rowValues.Value(i, rowNumber)
if err1 != nil {
err = dbsqlerrint.NewDriverError(ars.ctx, errArrowRowsColumnValue(col.name), err1)
if errors.Is(err1, errNativeInterval) {
ars.Error().Msg(errArrowRowsUnsupportedNativeType(col.dbType.String()))
return dbsqlerrint.NewDriverError(ars.ctx, errArrowRowsUnsupportedNativeType(col.dbType.String()), nil)
}
err = dbsqlerrint.NewDriverError(ars.ctx, errArrowRowsColumnValue(col.name), err1)
}
}

Expand All @@ -265,7 +252,6 @@ func (ars *arrowRowScanner) loadBatchFor(rowNumber int64) dbsqlerr.DBError {
if ars == nil {
return dbsqlerrint.NewDriverError(context.Background(), errArrowRowsNoArrowBatches, nil)
}

if ars.batchIterator == nil {
return dbsqlerrint.NewDriverError(ars.ctx, errArrowRowsNoArrowBatches, nil)
}
Expand All @@ -274,6 +260,9 @@ func (ars *arrowRowScanner) loadBatchFor(rowNumber int64) dbsqlerr.DBError {
if ars.rowValues != nil && ars.rowValues.Contains(rowNumber) {
return nil
}
if ars.scanError != nil {
return ars.scanError
}

// check for things like going backwards, rowNumber < 0, etc.
err := ars.validateRowNumber(rowNumber)
Expand Down Expand Up @@ -312,25 +301,11 @@ func (ars *arrowRowScanner) loadBatchFor(rowNumber int64) dbsqlerr.DBError {
}
}

// for each column we want to create an arrow array specific to the data type
for i, col := range sar.Columns() {
func() {
col.Retain()
defer col.Release()

colData := col.Data()
colData.Retain()
defer colData.Release()

err := ars.rowValues.SetColumnValues(i, colData)
if err != nil {
ars.Error().Msg(err.Error())
}
}()
if err := ars.rowValues.setRecord(sar, ars.colInfo, ars.ArrowConfig); err != nil {
ars.Err(err).Msg(errArrowRowsSetColumnValues)
ars.scanError = dbsqlerrint.NewDriverError(ars.ctx, errArrowRowsSetColumnValues, err)
return ars.scanError
}

// Update the delimiter in rowValues to reflect the currently loaded set of rows
ars.rowValues.SetDelimiter(rowscanner.NewDelimiter(sar.Start(), sar.Count()))
return nil
}

Expand Down Expand Up @@ -621,7 +596,7 @@ func (vcm *arrowValueContainerMaker) makeColumnValuesContainers(ars *arrowRowSca
columnValueHolders[i] = holder
}

ars.rowValues = NewRowValues(d, columnValueHolders)
ars.rowValues = newRowValues(d, columnValueHolders)
}

return nil
Expand Down
55 changes: 17 additions & 38 deletions internal/rows/arrowbased/arrowRows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@ func TestArrowRowScanner(t *testing.T) {

var ok bool

rowValues, ok := ars.rowValues.(*rowValues)
assert.True(t, ok)
rowValues := ars.rowValues

_, ok = rowValues.columnValueHolders[0].(*columnValuesTyped[*array.Boolean, bool])
assert.True(t, ok)
Expand Down Expand Up @@ -325,8 +324,7 @@ func TestArrowRowScanner(t *testing.T) {

var ok bool

rowValues, ok := ars.rowValues.(*rowValues)
assert.True(t, ok)
rowValues := ars.rowValues

// timestamp
_, ok = rowValues.columnValueHolders[8].(*timestampValueContainer)
Expand Down Expand Up @@ -454,7 +452,7 @@ func TestArrowRowScanner(t *testing.T) {
ars := d.(*arrowRowScanner)
var releaseCount int
fc := &fakeColumnValues{fnRelease: func() { releaseCount++ }}
ars.rowValues = NewRowValues(rowscanner.NewDelimiter(0, 1), []columnValues{fc, fc, fc})
ars.rowValues = newRowValues(rowscanner.NewDelimiter(0, 1), []columnValues{fc, fc, fc})
d.Close()
assert.Equal(t, 3, releaseCount)
})
Expand Down Expand Up @@ -512,31 +510,31 @@ func TestArrowRowScanner(t *testing.T) {
for i := range ars.arrowSchema.Fields() {
columnValueHolders[i] = &fakeColumnValues{}
}
ars.rowValues = NewRowValues(rowscanner.NewDelimiter(0, 0), make([]columnValues, len(ars.arrowSchema.Fields())))
ars.rowValues = newRowValues(rowscanner.NewDelimiter(0, 0), columnValueHolders)
return nil
}}

err := ars.loadBatchFor(0)
assert.Nil(t, err)
assert.Equal(t, len(metadataResp.Schema.Columns), ars.rowValues.NColumns())
assert.Len(t, ars.rowValues.columnValueHolders, len(metadataResp.Schema.Columns))
assert.Equal(t, 1, callCount)
assert.Equal(t, 1, fbi.callCount)

err = ars.loadBatchFor(1)
assert.Nil(t, err)
assert.Equal(t, len(metadataResp.Schema.Columns), ars.rowValues.NColumns())
assert.Len(t, ars.rowValues.columnValueHolders, len(metadataResp.Schema.Columns))
assert.Equal(t, 1, callCount)
assert.Equal(t, 1, fbi.callCount)

err = ars.loadBatchFor(2)
assert.Nil(t, err)
assert.Equal(t, len(metadataResp.Schema.Columns), ars.rowValues.NColumns())
assert.Len(t, ars.rowValues.columnValueHolders, len(metadataResp.Schema.Columns))
assert.Equal(t, 1, callCount)
assert.Equal(t, 1, fbi.callCount)

err = ars.loadBatchFor(5)
assert.Nil(t, err)
assert.Equal(t, len(metadataResp.Schema.Columns), ars.rowValues.NColumns())
assert.Len(t, ars.rowValues.columnValueHolders, len(metadataResp.Schema.Columns))
assert.Equal(t, 1, callCount)
assert.Equal(t, 2, fbi.callCount)

Expand Down Expand Up @@ -882,7 +880,7 @@ func TestArrowRowScanner(t *testing.T) {
for i := range ars.arrowSchema.Fields() {
columnValueHolders[i] = &fakeColumnValues{}
}
ars.rowValues = NewRowValues(rowscanner.NewDelimiter(0, 0), columnValueHolders)
ars.rowValues = newRowValues(rowscanner.NewDelimiter(0, 0), columnValueHolders)
return nil
}}

Expand Down Expand Up @@ -1615,9 +1613,10 @@ func TestArrowRowScanner(t *testing.T) {
}

type fakeColumnValues struct {
fnValue func(int) any
fnIsNull func(int) bool
fnRelease func()
fnValue func(int) any
fnIsNull func(int) bool
fnRelease func()
fnSetValueArray func(arrow.ArrayData) error
}

func (fc *fakeColumnValues) Value(i int) (any, error) {
Expand All @@ -1641,6 +1640,9 @@ func (fc *fakeColumnValues) Release() {
}

func (cv *fakeColumnValues) SetValueArray(colData arrow.ArrayData) error {
if cv.fnSetValueArray != nil {
return cv.fnSetValueArray(colData)
}
return nil
}

Expand Down Expand Up @@ -2139,29 +2141,6 @@ func TestDecimal128ContainerNativeDecimal(t *testing.T) {
assert.Equal(t, float64(5.15), v)
})

t.Run("rowValues.DecimalStringValue returns lossless string for a decimal holder", func(t *testing.T) {
c := newDecimal128Container(t, 10, 2, []string{"5.15", ""})
rv := NewRowValues(rowscanner.NewDelimiter(0, 2), []columnValues{c})

assert.False(t, rv.IsNull(0, 0))
s, ok := rv.DecimalStringValue(0, 0)
assert.True(t, ok)
assert.Equal(t, "5.15", s)

// Value still returns the legacy float64 (used for nested rendering).
v, err := rv.Value(0, 0)
assert.NoError(t, err)
assert.Equal(t, float64(5.15), v)

assert.True(t, rv.IsNull(0, 1))
})

t.Run("rowValues.DecimalStringValue reports ok=false for non-decimal holders", func(t *testing.T) {
rv := NewRowValues(rowscanner.NewDelimiter(0, 1), []columnValues{&columnValuesTyped[*array.String, string]{}})
s, ok := rv.DecimalStringValue(0, 0)
assert.False(t, ok)
assert.Equal(t, "", s)
})
}

// TestScanRowNativeDecimal drives the full ScanRow path with a real
Expand Down Expand Up @@ -2220,7 +2199,7 @@ func TestScanRowNativeDecimal(t *testing.T) {
// Install a real decimal128Container holding the high-precision value.
ars.valueContainerMaker = &fakeValueContainerMaker{fnMakeColumnValuesContainers: func(ars *arrowRowScanner, _ rowscanner.Delimiter) dbsqlerr.DBError {
c := newDecimal128Container(t, precision, scale, []string{highPrecision})
ars.rowValues = NewRowValues(rowscanner.NewDelimiter(0, 1), []columnValues{c})
ars.rowValues = newRowValues(rowscanner.NewDelimiter(0, 1), []columnValues{c})
return nil
}}

Expand Down
6 changes: 3 additions & 3 deletions internal/rows/arrowbased/arrowscan_parity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ func TestArrowbasedKernelVariantGeometryParity(t *testing.T) {
// - The Thrift *container* (decimal128Container.Value) converts a decimal128 to
// a lossy float64 — but that path is only reached when UseArrowNativeDecimal
// is on AND the value is read as a nested leaf. For a top-level column the
// scan uses DecimalStringValue (exact string) when the flag is on.
// scan uses ValueString (exact string) when the flag is on.
// - In the DEFAULT config the flag is off, so the server sends DECIMAL as a
// string column (DecimalAsArrow=false) — no decimal128 arrives at all, and the
// user gets the exact string.
Expand Down Expand Up @@ -683,14 +683,14 @@ func TestTopLevelDecimalRendering(t *testing.T) {
}

// Thrift container Value() is the lossy-float64 path (reached only with native
// decimal + nested read); DecimalStringValue is the exact top-level scan path.
// decimal + nested read); ValueString is the exact top-level scan path.
// Assert both so a change to either is caught.
holder := &decimal128Container{scale: dt.Scale}
if err := holder.SetValueArray(arr.Data()); err != nil {
t.Fatal(err)
}
if s := holder.ValueString(0); s != "1234567890123456.7890" {
t.Errorf("Thrift DecimalStringValue = %q, want exact string", s)
t.Errorf("Thrift ValueString = %q, want exact string", s)
}
// Value() is the lossy path: it returns a float64, and a 20-digit decimal
// cannot survive it — assert the actual rounded value, not just the type.
Expand Down
Loading
Loading