zeroshade commented on code in PR #1611:
URL: https://github.com/apache/arrow-go/pull/1611#discussion_r4620014847
##########
arrow/scalar/scalar.go:
##########
@@ -1043,6 +1045,86 @@ func MakeArrayFromScalar(sc Scalar, length int, mem
memory.Allocator) (arrow.Arr
data.Release()
}()
return array.MakeFromData(data), nil
+ case *SparseUnion:
+ if err := s.Validate(); err != nil {
+ return nil, err
+ }
+ dt := s.DataType().(*arrow.SparseUnionType)
+ childID := dt.ChildIDs()[s.TypeCode]
+
+ children := make([]arrow.ArrayData, dt.NumFields())
+ for i, f := range dt.Fields() {
+ var child arrow.Array
+ if i == childID && s.Valid {
+ var err error
+ if child, err = MakeArrayFromScalar(s.Value[i],
length, mem); err != nil {
+ return nil, err
+ }
+ } else {
+ child = MakeArrayOfNull(f.Type, length, mem)
+ }
+ defer child.Release()
+ children[i] = child.Data()
+ }
+
+ typeCodes := createBuffer([]byte{byte(s.TypeCode)})
+ defer typeCodes.Release()
+ data := array.NewData(dt, length, []*memory.Buffer{nil,
typeCodes}, children, 0, 0)
+ defer data.Release()
+ return array.MakeFromData(data), nil
+ case *DenseUnion:
+ if err := s.Validate(); err != nil {
Review Comment:
Calling `Validate()` here also runs it on null union scalars, and `Validate`
assumes `Value` is set. If a caller clears `Value` on a null dense union
scalar, `DenseUnion.Validate` panics at `s.Value.DataType()`. A null sparse
union with a nil `Value` fails with `sparse union scalar value had 2 fields but
type has 0 fields`. On main both returned an all-null array.
This only happens when `Value` is set to nil after the scalar is built,
since `MakeNullScalar` and the constructors always fill it in. Still, a
nil-pointer panic from a function that returns an error is worth avoiding.
Either make `DenseUnion.Validate` return an error for a nil `Value`, or check
only the type code when the scalar is null. Every child is null in that case,
so the value isn't used anyway.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]