This is an automated email from the ASF dual-hosted git repository.
zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-go.git
The following commit(s) were added to refs/heads/main by this push:
new bb5fd913 fix(arrow/scalar): preserve concrete type in same-type CastTo
for LargeList, Map, and FixedSizeList (#1683)
bb5fd913 is described below
commit bb5fd913b874c97fadb9e1756dc5f0d8e60ded17
Author: 小哥不太逍遥 <[email protected]>
AuthorDate: Sat Nov 28 00:54:57 2026 +0800
fix(arrow/scalar): preserve concrete type in same-type CastTo for
LargeList, Map, and FixedSizeList (#1683)
### Rationale for this change
`LargeList`, `Map`, and `FixedSizeList` scalars embed `*List` without
overriding `CastTo`. When `CastTo` is called with the scalar's own
`DataType`, the inherited `(*List).CastTo` retains and returns the
embedded `*List` instead of the concrete `*LargeList`, `*Map`, or
`*FixedSizeList` receiver (unlike `ListView` and `LargeListView`, which
already override `CastTo`).
Returning the embedded `*List` breaks concrete type expectations and
downstream type switches:
- Calling `MakeArrayFromScalar` on a same-type cast `*FixedSizeList`
scalar enters `case *List, *LargeList:` and panics
(`*arrow.FixedSizeListType` does not implement `arrow.OffsetsDataType`).
- Calling `MakeArrayFromScalar` on a same-type cast `*Map` scalar enters
`case *List, *LargeList:` instead of `case *Map:`, failing to construct
the map's entry struct child array properly.
- Calling `Validate()` on a same-type cast `*FixedSizeList` scalar
bypasses `(*FixedSizeList).Validate()`'s element length check.
### What changes are included in this PR?
- `arrow/scalar/nested.go`:
- Add `CastTo` overrides on `*LargeList`, `*Map`, and `*FixedSizeList`
that retain and return the concrete receiver when `Valid &&
arrow.TypeEqual(Type, to)`, delegating all other cases (nulls, string
cast, unsupported targets) to embedded `(*List).CastTo` — matching
`ListView.CastTo` and `LargeListView.CastTo`.
- `arrow/scalar/scalar_test.go`:
- Add `TestListLikeScalarSameTypeCastTo` verifying concrete receiver
identity (`assert.Same`), reference ownership (`CheckedAllocator` stays
non-zero after releasing original and returns to `0` after releasing
cast result), `Validate()`, and `MakeArrayFromScalar` round-trip without
panic for `LargeList`, `Map`, and `FixedSizeList` (plus null same-type
casts).
### Are these changes tested?
Yes. Verified with `go test -race -count=1 -tags "assert,test"` and
`-tags "noasm,assert,test"` on `./arrow/scalar/...` and
`./arrow/compute/...`.
### Are there any user-facing changes?
Yes: calling `CastTo` with the same `DataType` on `*scalar.LargeList`,
`*scalar.Map`, and `*scalar.FixedSizeList` now returns the concrete
`*scalar.LargeList`, `*scalar.Map`, and `*scalar.FixedSizeList` receiver
instead of the embedded `*scalar.List`.
---
arrow/scalar/nested.go | 24 +++++++++++
arrow/scalar/scalar_test.go | 102 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 126 insertions(+)
diff --git a/arrow/scalar/nested.go b/arrow/scalar/nested.go
index a9f88c20..3aab1140 100644
--- a/arrow/scalar/nested.go
+++ b/arrow/scalar/nested.go
@@ -129,6 +129,14 @@ type LargeList struct {
*List
}
+func (l *LargeList) CastTo(to arrow.DataType) (Scalar, error) {
+ if l.Valid && arrow.TypeEqual(l.Type, to) {
+ l.Retain()
+ return l, nil
+ }
+ return l.List.CastTo(to)
+}
+
func NewLargeListScalar(val arrow.Array) *LargeList {
return &LargeList{&List{scalar{arrow.LargeListOf(val.DataType()),
true}, array.MakeFromData(val.Data())}}
}
@@ -194,6 +202,14 @@ type Map struct {
*List
}
+func (m *Map) CastTo(to arrow.DataType) (Scalar, error) {
+ if m.Valid && arrow.TypeEqual(m.Type, to) {
+ m.Retain()
+ return m, nil
+ }
+ return m.List.CastTo(to)
+}
+
func NewMapScalar(val arrow.Array) *Map {
return
&Map{&List{scalar{makeMapType(val.DataType().(*arrow.StructType)), true},
array.MakeFromData(val.Data())}}
}
@@ -202,6 +218,14 @@ type FixedSizeList struct {
*List
}
+func (f *FixedSizeList) CastTo(to arrow.DataType) (Scalar, error) {
+ if f.Valid && arrow.TypeEqual(f.Type, to) {
+ f.Retain()
+ return f, nil
+ }
+ return f.List.CastTo(to)
+}
+
func (f *FixedSizeList) Validate() (err error) {
if err = f.List.Validate(); err != nil {
return
diff --git a/arrow/scalar/scalar_test.go b/arrow/scalar/scalar_test.go
index 0b5654d4..bd3718cc 100644
--- a/arrow/scalar/scalar_test.go
+++ b/arrow/scalar/scalar_test.go
@@ -3802,3 +3802,105 @@ func TestExtensionIdentityCastOwnsStorage(t *testing.T)
{
require.Positive(t, mem.CurrentAlloc(), "cast result should still own
the UUID storage")
releaseScalar(result)
}
+
+func TestListLikeScalarSameTypeCastTo(t *testing.T) {
+ t.Run("large_list", func(t *testing.T) {
+ mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
+ defer mem.AssertSize(t, 0)
+
+ vals, _, err := array.FromJSON(mem, arrow.PrimitiveTypes.Int32,
strings.NewReader(`[1, 2]`))
+ require.NoError(t, err)
+ src := scalar.NewLargeListScalar(vals)
+ vals.Release()
+
+ got, err := src.CastTo(src.DataType())
+ require.NoError(t, err)
+ assert.Same(t, src, got)
+ _, ok := got.(*scalar.LargeList)
+ assert.True(t, ok, "expected *scalar.LargeList, got %T", got)
+
+ src.Release()
+ require.Positive(t, mem.CurrentAlloc(), "cast result should
still own the underlying array")
+
+ arr, err := scalar.MakeArrayFromScalar(got, 2, mem)
+ require.NoError(t, err)
+ assert.Equal(t, 2, arr.Len())
+ arr.Release()
+ releaseScalar(got)
+
+ nullSrc :=
scalar.MakeNullScalar(arrow.LargeListOf(arrow.PrimitiveTypes.Int32))
+ nullGot, err := nullSrc.CastTo(nullSrc.DataType())
+ require.NoError(t, err)
+ assert.False(t, nullGot.IsValid())
+ _, ok = nullGot.(*scalar.LargeList)
+ assert.True(t, ok, "expected null *scalar.LargeList, got %T",
nullGot)
+ })
+
+ t.Run("fixed_size_list", func(t *testing.T) {
+ mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
+ defer mem.AssertSize(t, 0)
+
+ vals, _, err := array.FromJSON(mem, arrow.PrimitiveTypes.Int32,
strings.NewReader(`[1, 2]`))
+ require.NoError(t, err)
+ src := scalar.NewFixedSizeListScalar(vals)
+ vals.Release()
+
+ got, err := src.CastTo(src.DataType())
+ require.NoError(t, err)
+ assert.Same(t, src, got)
+ _, ok := got.(*scalar.FixedSizeList)
+ assert.True(t, ok, "expected *scalar.FixedSizeList, got %T",
got)
+
+ src.Release()
+ require.Positive(t, mem.CurrentAlloc(), "cast result should
still own the underlying array")
+
+ arr, err := scalar.MakeArrayFromScalar(got, 2, mem)
+ require.NoError(t, err)
+ assert.Equal(t, 2, arr.Len())
+ arr.Release()
+ releaseScalar(got)
+
+ nullSrc := scalar.MakeNullScalar(arrow.FixedSizeListOf(2,
arrow.PrimitiveTypes.Int32))
+ nullGot, err := nullSrc.CastTo(nullSrc.DataType())
+ require.NoError(t, err)
+ assert.False(t, nullGot.IsValid())
+ _, ok = nullGot.(*scalar.FixedSizeList)
+ assert.True(t, ok, "expected null *scalar.FixedSizeList, got
%T", nullGot)
+ })
+
+ t.Run("map", func(t *testing.T) {
+ mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
+ defer mem.AssertSize(t, 0)
+
+ entryType := arrow.StructOf(
+ arrow.Field{Name: "key", Type:
arrow.BinaryTypes.String, Nullable: false},
+ arrow.Field{Name: "value", Type:
arrow.PrimitiveTypes.Int32, Nullable: true},
+ )
+ entries, _, err := array.FromJSON(mem, entryType,
strings.NewReader(`[{"key": "a", "value": 1}, {"key": "b", "value": 2}]`))
+ require.NoError(t, err)
+ src := scalar.NewMapScalar(entries)
+ entries.Release()
+
+ got, err := src.CastTo(src.DataType())
+ require.NoError(t, err)
+ assert.Same(t, src, got)
+ _, ok := got.(*scalar.Map)
+ assert.True(t, ok, "expected *scalar.Map, got %T", got)
+
+ src.Release()
+ require.Positive(t, mem.CurrentAlloc(), "cast result should
still own the underlying array")
+
+ arr, err := scalar.MakeArrayFromScalar(got, 2, mem)
+ require.NoError(t, err)
+ assert.Equal(t, 2, arr.Len())
+ arr.Release()
+ releaseScalar(got)
+
+ nullSrc :=
scalar.MakeNullScalar(arrow.MapOf(arrow.BinaryTypes.String,
arrow.PrimitiveTypes.Int32))
+ nullGot, err := nullSrc.CastTo(nullSrc.DataType())
+ require.NoError(t, err)
+ assert.False(t, nullGot.IsValid())
+ _, ok = nullGot.(*scalar.Map)
+ assert.True(t, ok, "expected null *scalar.Map, got %T", nullGot)
+ })
+}