Skip to content

Commit fd1af1b

Browse files
authored
Add support for Arrow string view type (#1252)
1 parent 425cbd7 commit fd1af1b

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

data/arrow.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,12 @@ func initializeFrameField(field arrow.Field, idx int, nullable []bool, sdkField
369369
break
370370
}
371371
sdkField.vector = newStringVector(0)
372+
case arrow.STRING_VIEW:
373+
if nullable[idx] {
374+
sdkField.vector = newNullableStringVector(0)
375+
break
376+
}
377+
sdkField.vector = newStringVector(0)
372378
case arrow.INT8:
373379
if nullable[idx] {
374380
sdkField.vector = newNullableInt8Vector(0)
@@ -508,6 +514,21 @@ func parseColumn(col arrow.Array, i int, nullable []bool, frame *Frame) error {
508514
}
509515
frame.Fields[i].vector.Append(v.Value(rIdx))
510516
}
517+
case arrow.STRING_VIEW:
518+
v := array.NewStringViewData(col.Data())
519+
for rIdx := 0; rIdx < col.Len(); rIdx++ {
520+
if nullable[i] {
521+
if v.IsNull(rIdx) {
522+
var ns *string
523+
frame.Fields[i].vector.Append(ns)
524+
continue
525+
}
526+
rv := v.Value(rIdx)
527+
frame.Fields[i].vector.Append(&rv)
528+
continue
529+
}
530+
frame.Fields[i].vector.Append(v.Value(rIdx))
531+
}
511532
case arrow.INT8:
512533
v := array.NewInt8Data(col.Data())
513534
for rIdx := 0; rIdx < col.Len(); rIdx++ {

data/arrow_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import (
1010
"testing"
1111
"time"
1212

13+
"github.com/apache/arrow-go/v18/arrow"
14+
"github.com/apache/arrow-go/v18/arrow/array"
1315
"github.com/apache/arrow-go/v18/arrow/ipc"
16+
"github.com/apache/arrow-go/v18/arrow/memory"
1417
"github.com/google/go-cmp/cmp"
1518
"github.com/stretchr/testify/require"
1619

@@ -440,3 +443,39 @@ func TestFromRecord(t *testing.T) {
440443
t.Errorf("Result mismatch (-want +got):\n%s", diff)
441444
}
442445
}
446+
447+
func TestFromRecordStringView(t *testing.T) {
448+
pool := memory.NewGoAllocator()
449+
require.NotNil(t, pool)
450+
schema := arrow.NewSchema([]arrow.Field{
451+
arrow.Field{Name: "sv", Type: &arrow.StringViewType{}, Nullable: false},
452+
arrow.Field{Name: "svn", Type: &arrow.StringViewType{}, Nullable: true},
453+
}, nil)
454+
require.NotNil(t, schema)
455+
b := array.NewRecordBuilder(pool, schema)
456+
defer b.Release()
457+
458+
testStrings := []string{"foo", "", "", "🦥", "bar"}
459+
notNull := []bool{true, true, false, true, true}
460+
b.Field(0).(*array.StringViewBuilder).AppendValues(testStrings, nil)
461+
b.Field(1).(*array.StringViewBuilder).AppendValues(testStrings, notNull)
462+
record := b.NewRecord()
463+
defer record.Release()
464+
465+
got, err := data.FromArrowRecord(record)
466+
require.NoError(t, err)
467+
468+
want := data.NewFrame("",
469+
data.NewField("sv", data.Labels{}, testStrings),
470+
data.NewField("svn", data.Labels{}, []*string{
471+
stringPtr("foo"),
472+
stringPtr(""),
473+
nil,
474+
stringPtr("🦥"),
475+
stringPtr("bar"),
476+
}),
477+
)
478+
if diff := cmp.Diff(want, got, data.FrameTestCompareOptions()...); diff != "" {
479+
t.Errorf("Result mismatch (-want +got):\n%s", diff)
480+
}
481+
}

0 commit comments

Comments
 (0)