Repository navigation
Description
Summary
On the Thrift backend, database/sql scanning of an ARRAY, MAP or STRUCT column builds the JSON text with r = r + ... once per element (internal/rows/arrowbased/columnValues.go, listValueContainer.Value, mapValueContainer.Value, structValueContainer.Value). Each step copies the whole string built so far, so one cell costs O(n²) time and allocation in the number of elements.
Small containers are fine. Wide ones are not: an ARRAY<DOUBLE> with 1536 elements (a typical embedding) takes about 10 ms and 42 MB of allocation per cell.
The kernel path (internal/arrowscan) renders the same cell with append and stays linear, so the two backends differ by up to 270x for the same value.
Version
github.com/databricks/databricks-sql-go/v2 v2.0.0. The same code is on main today.
Measurement
One ARRAY<DOUBLE> cell with n elements per op, rendered through arrowValueContainerMaker (Thrift) and through arrowscan.ScanCellCached (kernel). Go 1.27.3, linux/amd64, Intel i7-13850HX. The benchmark is below; it needs no server.
| Elements | Thrift ns/op | Thrift B/op | Kernel ns/op | Kernel B/op | Thrift / kernel time |
|---|---|---|---|---|---|
| 5 | 546 | 584 | 405 | 288 | 1.3x |
| 50 | 16,680 | 45,345 | 3,235 | 2,312 | 5x |
| 500 | 1,068,599 | 4,481,042 | 36,805 | 38,297 | 29x |
| 1536 | 9,826,340 | 41,686,712 | 111,383 | 125,691 | 88x |
| 5000 | 104,779,768 | 449,745,782 | 389,425 | 423,749 | 269x |
From 500 to 5000 elements (10x) the Thrift time grows about 98x, the kernel time about 10.6x.
The existing BenchmarkRenderNested uses 3 to 5 elements per row, so it does not show this.
Impact
A result with 100,000 rows and one 1536-element array column spends about 16 minutes of CPU, and about 4 TB of short-lived allocation, only on building that column's text.
Suggested fix
Build the text in a []byte with append (the containers already call marshalScalar with a scratch buffer), or reuse the arrowscan renderer for the Thrift containers. The output does not need to change.
Benchmark
Put this in internal/rows/arrowbased/wide_bench_test.go and run
go test -run '^$' -bench BenchmarkRenderWideArray ./internal/rows/arrowbased/.
package arrowbased
import (
"strconv"
"testing"
"time"
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/array"
"github.com/apache/arrow-go/v18/arrow/memory"
"github.com/databricks/databricks-sql-go/v2/internal/arrowscan"
)
// BenchmarkRenderWideArray renders one ARRAY<DOUBLE> cell of n elements per op.
func BenchmarkRenderWideArray(b *testing.B) {
pool := memory.NewGoAllocator()
for _, n := range []int{5, 50, 500, 1536, 5000} {
lb := array.NewListBuilder(pool, arrow.PrimitiveTypes.Float64)
vb := lb.ValueBuilder().(*array.Float64Builder)
lb.Append(true)
for j := 0; j < n; j++ {
vb.Append(float64(j)/7 - 0.5)
}
arr := lb.NewArray()
b.Run("thrift/"+strconv.Itoa(n), func(b *testing.B) {
holder, err := (&arrowValueContainerMaker{}).makeColumnValueContainer(arr.DataType(), time.UTC, nil, nil)
if err != nil {
b.Fatal(err)
}
if err := holder.SetValueArray(arr.Data()); err != nil {
b.Fatal(err)
}
defer holder.Release()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := holder.Value(0); err != nil {
b.Fatal(err)
}
}
})
b.Run("kernel/"+strconv.Itoa(n), func(b *testing.B) {
keys := arrowscan.NewStructKeyCache()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := arrowscan.ScanCellCached(arr, 0, time.UTC, keys); err != nil {
b.Fatal(err)
}
}
})
arr.Release()
}
}
Activity
fix: render Thrift complex values in linear time
fix: render Thrift complex values in linear time
fix: render Thrift complex values in linear time
fix: render Thrift complex values in linear time
fix: render Thrift complex values in linear time
fix: render Thrift complex values in linear time
jakubhrncar commentedon Nov 27, 2026
I ran a quick benchmark on the produced code. From our side #597 does what the issue asked for. Thank you for the quick fix; we will move to the release that contains it.
fix: render Thrift complex values in linear time
fix: render Thrift complex values in linear time