Copilot commented on code in PR #1611: URL: https://github.com/apache/arrow-go/pull/1611#discussion_r4619572624
########## arrow/scalar/make_array_from_scalar_test.go: ########## @@ -0,0 +1,217 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package scalar_test + +import ( + "testing" + + "github.com/apache/arrow-go/v18/arrow" + "github.com/apache/arrow-go/v18/arrow/array" + "github.com/apache/arrow-go/v18/arrow/extensions" + "github.com/apache/arrow-go/v18/arrow/memory" + "github.com/apache/arrow-go/v18/arrow/scalar" + "github.com/google/uuid" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestMakeArrayFromLargeListScalar(t *testing.T) { + mem := memory.NewCheckedAllocator(memory.NewGoAllocator()) + defer mem.AssertSize(t, 0) + + bldr := array.NewInt32Builder(mem) + defer bldr.Release() + bldr.AppendValues([]int32{1, 2, 3}, nil) + values := bldr.NewArray() + defer values.Release() + sc := scalar.NewLargeListScalar(values) + defer sc.Release() + + got, err := scalar.MakeArrayFromScalar(sc, 3, mem) + require.NoError(t, err) + defer got.Release() + require.NoError(t, array.ValidateFull(got)) + + listBldr := array.NewLargeListBuilder(mem, arrow.PrimitiveTypes.Int32) + defer listBldr.Release() + for range 3 { + listBldr.Append(true) + listBldr.ValueBuilder().(*array.Int32Builder).AppendValues([]int32{1, 2, 3}, nil) + } + want := listBldr.NewArray() + defer want.Release() + assert.Truef(t, array.Equal(want, got), "want %s, got %s", want, got) +} + +func TestMakeArrayFromSparseUnionScalar(t *testing.T) { + mem := memory.NewCheckedAllocator(memory.NewGoAllocator()) + defer mem.AssertSize(t, 0) + + dt := arrow.SparseUnionOf([]arrow.Field{ + {Name: "i", Type: arrow.PrimitiveTypes.Int32, Nullable: true}, + {Name: "s", Type: arrow.BinaryTypes.String, Nullable: true}, + }, []arrow.UnionTypeCode{5, 7}) + staleChildID := scalar.NewSparseUnionScalar( + []scalar.Scalar{scalar.NewInt32Scalar(10), scalar.NewStringScalar("hi")}, 7, dt) + staleChildID.ChildID = 0 + + tests := []struct { + name string + sc scalar.Scalar + // nonNull is the index of the child that holds the string "hi" in every + // slot, or -1 when every child is null. + nonNull int + }{ + {name: "valid with other children set", sc: scalar.NewSparseUnionScalar( + []scalar.Scalar{scalar.NewInt32Scalar(10), scalar.NewStringScalar("hi")}, 7, dt), nonNull: 1}, + {name: "child id disagrees with the type code", sc: staleChildID, nonNull: 1}, + {name: "null with a later type code", sc: scalar.NewSparseUnionScalarFromValue( + scalar.MakeNullScalar(arrow.BinaryTypes.String), 1, dt), nonNull: -1}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + const length = 3 + got, err := scalar.MakeArrayFromScalar(tc.sc, length, mem) + require.NoError(t, err) + defer got.Release() + require.NoError(t, array.ValidateFull(got)) + require.Equal(t, length, got.Len()) + + union := got.(*array.SparseUnion) + for i := range length { + assert.Equal(t, arrow.UnionTypeCode(7), union.TypeCode(i), "slot %d", i) + } + for child := range dt.NumFields() { + field := union.Field(child) Review Comment: `for range <int>` and `range length` require Go 1.22+ (range over integers). If this repo’s `go.mod` Go version is < 1.22, these tests won’t compile. Consider using a classic indexed loop (e.g., `for i := 0; i < n; i++ {}`) or bumping the module Go version if 1.22+ is required. ########## arrow/scalar/make_array_from_scalar_test.go: ########## @@ -0,0 +1,217 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package scalar_test + +import ( + "testing" + + "github.com/apache/arrow-go/v18/arrow" + "github.com/apache/arrow-go/v18/arrow/array" + "github.com/apache/arrow-go/v18/arrow/extensions" + "github.com/apache/arrow-go/v18/arrow/memory" + "github.com/apache/arrow-go/v18/arrow/scalar" + "github.com/google/uuid" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestMakeArrayFromLargeListScalar(t *testing.T) { + mem := memory.NewCheckedAllocator(memory.NewGoAllocator()) + defer mem.AssertSize(t, 0) + + bldr := array.NewInt32Builder(mem) + defer bldr.Release() + bldr.AppendValues([]int32{1, 2, 3}, nil) + values := bldr.NewArray() + defer values.Release() + sc := scalar.NewLargeListScalar(values) + defer sc.Release() + + got, err := scalar.MakeArrayFromScalar(sc, 3, mem) + require.NoError(t, err) + defer got.Release() + require.NoError(t, array.ValidateFull(got)) + + listBldr := array.NewLargeListBuilder(mem, arrow.PrimitiveTypes.Int32) + defer listBldr.Release() + for range 3 { Review Comment: `for range <int>` and `range length` require Go 1.22+ (range over integers). If this repo’s `go.mod` Go version is < 1.22, these tests won’t compile. Consider using a classic indexed loop (e.g., `for i := 0; i < n; i++ {}`) or bumping the module Go version if 1.22+ is required. ########## arrow/scalar/make_array_from_scalar_test.go: ########## @@ -0,0 +1,217 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package scalar_test + +import ( + "testing" + + "github.com/apache/arrow-go/v18/arrow" + "github.com/apache/arrow-go/v18/arrow/array" + "github.com/apache/arrow-go/v18/arrow/extensions" + "github.com/apache/arrow-go/v18/arrow/memory" + "github.com/apache/arrow-go/v18/arrow/scalar" + "github.com/google/uuid" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestMakeArrayFromLargeListScalar(t *testing.T) { + mem := memory.NewCheckedAllocator(memory.NewGoAllocator()) + defer mem.AssertSize(t, 0) + + bldr := array.NewInt32Builder(mem) + defer bldr.Release() + bldr.AppendValues([]int32{1, 2, 3}, nil) + values := bldr.NewArray() + defer values.Release() + sc := scalar.NewLargeListScalar(values) + defer sc.Release() + + got, err := scalar.MakeArrayFromScalar(sc, 3, mem) + require.NoError(t, err) + defer got.Release() + require.NoError(t, array.ValidateFull(got)) + + listBldr := array.NewLargeListBuilder(mem, arrow.PrimitiveTypes.Int32) + defer listBldr.Release() + for range 3 { + listBldr.Append(true) + listBldr.ValueBuilder().(*array.Int32Builder).AppendValues([]int32{1, 2, 3}, nil) + } + want := listBldr.NewArray() + defer want.Release() + assert.Truef(t, array.Equal(want, got), "want %s, got %s", want, got) +} + +func TestMakeArrayFromSparseUnionScalar(t *testing.T) { + mem := memory.NewCheckedAllocator(memory.NewGoAllocator()) + defer mem.AssertSize(t, 0) + + dt := arrow.SparseUnionOf([]arrow.Field{ + {Name: "i", Type: arrow.PrimitiveTypes.Int32, Nullable: true}, + {Name: "s", Type: arrow.BinaryTypes.String, Nullable: true}, + }, []arrow.UnionTypeCode{5, 7}) + staleChildID := scalar.NewSparseUnionScalar( + []scalar.Scalar{scalar.NewInt32Scalar(10), scalar.NewStringScalar("hi")}, 7, dt) + staleChildID.ChildID = 0 + + tests := []struct { + name string + sc scalar.Scalar + // nonNull is the index of the child that holds the string "hi" in every + // slot, or -1 when every child is null. + nonNull int + }{ + {name: "valid with other children set", sc: scalar.NewSparseUnionScalar( + []scalar.Scalar{scalar.NewInt32Scalar(10), scalar.NewStringScalar("hi")}, 7, dt), nonNull: 1}, + {name: "child id disagrees with the type code", sc: staleChildID, nonNull: 1}, + {name: "null with a later type code", sc: scalar.NewSparseUnionScalarFromValue( + scalar.MakeNullScalar(arrow.BinaryTypes.String), 1, dt), nonNull: -1}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + const length = 3 + got, err := scalar.MakeArrayFromScalar(tc.sc, length, mem) + require.NoError(t, err) + defer got.Release() + require.NoError(t, array.ValidateFull(got)) + require.Equal(t, length, got.Len()) + + union := got.(*array.SparseUnion) + for i := range length { + assert.Equal(t, arrow.UnionTypeCode(7), union.TypeCode(i), "slot %d", i) + } + for child := range dt.NumFields() { + field := union.Field(child) + require.Equal(t, length, field.Len(), "child %d", child) + if child != tc.nonNull { + assert.Equal(t, length, field.NullN(), "child %d", child) + continue + } + assert.Zero(t, field.NullN(), "child %d", child) + for i := range length { + assert.Equal(t, "hi", field.(*array.String).Value(i), "slot %d", i) + } + } + }) + } +} + +func TestMakeArrayFromDenseUnionScalar(t *testing.T) { + mem := memory.NewCheckedAllocator(memory.NewGoAllocator()) + defer mem.AssertSize(t, 0) + + dt := arrow.DenseUnionOf([]arrow.Field{ + {Name: "i", Type: arrow.PrimitiveTypes.Int32, Nullable: true}, + {Name: "s", Type: arrow.BinaryTypes.String, Nullable: true}, + }, []arrow.UnionTypeCode{5, 7}) + + tests := []struct { + name string + sc scalar.Scalar + valid bool + }{ + {name: "valid", sc: scalar.NewDenseUnionScalar(scalar.NewStringScalar("hi"), 7, dt), valid: true}, + {name: "null with a later type code", sc: scalar.NewDenseUnionScalar( + scalar.MakeNullScalar(arrow.BinaryTypes.String), 7, dt)}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + const length = 3 + got, err := scalar.MakeArrayFromScalar(tc.sc, length, mem) + require.NoError(t, err) + defer got.Release() + require.NoError(t, array.ValidateFull(got)) + require.Equal(t, length, got.Len()) + + union := got.(*array.DenseUnion) + for i := range length { + assert.Equal(t, arrow.UnionTypeCode(7), union.TypeCode(i), "slot %d", i) + assert.Zero(t, union.ValueOffset(i), "slot %d", i) + } Review Comment: `for range <int>` and `range length` require Go 1.22+ (range over integers). If this repo’s `go.mod` Go version is < 1.22, these tests won’t compile. Consider using a classic indexed loop (e.g., `for i := 0; i < n; i++ {}`) or bumping the module Go version if 1.22+ is required. ########## arrow/scalar/make_array_from_scalar_test.go: ########## @@ -0,0 +1,217 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package scalar_test + +import ( + "testing" + + "github.com/apache/arrow-go/v18/arrow" + "github.com/apache/arrow-go/v18/arrow/array" + "github.com/apache/arrow-go/v18/arrow/extensions" + "github.com/apache/arrow-go/v18/arrow/memory" + "github.com/apache/arrow-go/v18/arrow/scalar" + "github.com/google/uuid" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestMakeArrayFromLargeListScalar(t *testing.T) { + mem := memory.NewCheckedAllocator(memory.NewGoAllocator()) + defer mem.AssertSize(t, 0) + + bldr := array.NewInt32Builder(mem) + defer bldr.Release() + bldr.AppendValues([]int32{1, 2, 3}, nil) + values := bldr.NewArray() + defer values.Release() + sc := scalar.NewLargeListScalar(values) + defer sc.Release() + + got, err := scalar.MakeArrayFromScalar(sc, 3, mem) + require.NoError(t, err) + defer got.Release() + require.NoError(t, array.ValidateFull(got)) + + listBldr := array.NewLargeListBuilder(mem, arrow.PrimitiveTypes.Int32) + defer listBldr.Release() + for range 3 { + listBldr.Append(true) + listBldr.ValueBuilder().(*array.Int32Builder).AppendValues([]int32{1, 2, 3}, nil) + } + want := listBldr.NewArray() + defer want.Release() + assert.Truef(t, array.Equal(want, got), "want %s, got %s", want, got) +} + +func TestMakeArrayFromSparseUnionScalar(t *testing.T) { + mem := memory.NewCheckedAllocator(memory.NewGoAllocator()) + defer mem.AssertSize(t, 0) + + dt := arrow.SparseUnionOf([]arrow.Field{ + {Name: "i", Type: arrow.PrimitiveTypes.Int32, Nullable: true}, + {Name: "s", Type: arrow.BinaryTypes.String, Nullable: true}, + }, []arrow.UnionTypeCode{5, 7}) + staleChildID := scalar.NewSparseUnionScalar( + []scalar.Scalar{scalar.NewInt32Scalar(10), scalar.NewStringScalar("hi")}, 7, dt) + staleChildID.ChildID = 0 + + tests := []struct { + name string + sc scalar.Scalar + // nonNull is the index of the child that holds the string "hi" in every + // slot, or -1 when every child is null. + nonNull int + }{ + {name: "valid with other children set", sc: scalar.NewSparseUnionScalar( + []scalar.Scalar{scalar.NewInt32Scalar(10), scalar.NewStringScalar("hi")}, 7, dt), nonNull: 1}, + {name: "child id disagrees with the type code", sc: staleChildID, nonNull: 1}, + {name: "null with a later type code", sc: scalar.NewSparseUnionScalarFromValue( + scalar.MakeNullScalar(arrow.BinaryTypes.String), 1, dt), nonNull: -1}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + const length = 3 + got, err := scalar.MakeArrayFromScalar(tc.sc, length, mem) + require.NoError(t, err) + defer got.Release() + require.NoError(t, array.ValidateFull(got)) + require.Equal(t, length, got.Len()) + + union := got.(*array.SparseUnion) + for i := range length { + assert.Equal(t, arrow.UnionTypeCode(7), union.TypeCode(i), "slot %d", i) + } + for child := range dt.NumFields() { + field := union.Field(child) + require.Equal(t, length, field.Len(), "child %d", child) + if child != tc.nonNull { + assert.Equal(t, length, field.NullN(), "child %d", child) + continue + } + assert.Zero(t, field.NullN(), "child %d", child) + for i := range length { + assert.Equal(t, "hi", field.(*array.String).Value(i), "slot %d", i) + } Review Comment: `for range <int>` and `range length` require Go 1.22+ (range over integers). If this repo’s `go.mod` Go version is < 1.22, these tests won’t compile. Consider using a classic indexed loop (e.g., `for i := 0; i < n; i++ {}`) or bumping the module Go version if 1.22+ is required. ########## arrow/scalar/make_array_from_scalar_test.go: ########## @@ -0,0 +1,217 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package scalar_test + +import ( + "testing" + + "github.com/apache/arrow-go/v18/arrow" + "github.com/apache/arrow-go/v18/arrow/array" + "github.com/apache/arrow-go/v18/arrow/extensions" + "github.com/apache/arrow-go/v18/arrow/memory" + "github.com/apache/arrow-go/v18/arrow/scalar" + "github.com/google/uuid" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestMakeArrayFromLargeListScalar(t *testing.T) { + mem := memory.NewCheckedAllocator(memory.NewGoAllocator()) + defer mem.AssertSize(t, 0) + + bldr := array.NewInt32Builder(mem) + defer bldr.Release() + bldr.AppendValues([]int32{1, 2, 3}, nil) + values := bldr.NewArray() + defer values.Release() + sc := scalar.NewLargeListScalar(values) + defer sc.Release() + + got, err := scalar.MakeArrayFromScalar(sc, 3, mem) + require.NoError(t, err) + defer got.Release() + require.NoError(t, array.ValidateFull(got)) + + listBldr := array.NewLargeListBuilder(mem, arrow.PrimitiveTypes.Int32) + defer listBldr.Release() + for range 3 { + listBldr.Append(true) + listBldr.ValueBuilder().(*array.Int32Builder).AppendValues([]int32{1, 2, 3}, nil) + } + want := listBldr.NewArray() + defer want.Release() + assert.Truef(t, array.Equal(want, got), "want %s, got %s", want, got) +} + +func TestMakeArrayFromSparseUnionScalar(t *testing.T) { + mem := memory.NewCheckedAllocator(memory.NewGoAllocator()) + defer mem.AssertSize(t, 0) + + dt := arrow.SparseUnionOf([]arrow.Field{ + {Name: "i", Type: arrow.PrimitiveTypes.Int32, Nullable: true}, + {Name: "s", Type: arrow.BinaryTypes.String, Nullable: true}, + }, []arrow.UnionTypeCode{5, 7}) + staleChildID := scalar.NewSparseUnionScalar( + []scalar.Scalar{scalar.NewInt32Scalar(10), scalar.NewStringScalar("hi")}, 7, dt) + staleChildID.ChildID = 0 + + tests := []struct { + name string + sc scalar.Scalar + // nonNull is the index of the child that holds the string "hi" in every + // slot, or -1 when every child is null. + nonNull int + }{ + {name: "valid with other children set", sc: scalar.NewSparseUnionScalar( + []scalar.Scalar{scalar.NewInt32Scalar(10), scalar.NewStringScalar("hi")}, 7, dt), nonNull: 1}, + {name: "child id disagrees with the type code", sc: staleChildID, nonNull: 1}, + {name: "null with a later type code", sc: scalar.NewSparseUnionScalarFromValue( + scalar.MakeNullScalar(arrow.BinaryTypes.String), 1, dt), nonNull: -1}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + const length = 3 + got, err := scalar.MakeArrayFromScalar(tc.sc, length, mem) + require.NoError(t, err) + defer got.Release() + require.NoError(t, array.ValidateFull(got)) + require.Equal(t, length, got.Len()) + + union := got.(*array.SparseUnion) + for i := range length { + assert.Equal(t, arrow.UnionTypeCode(7), union.TypeCode(i), "slot %d", i) + } + for child := range dt.NumFields() { + field := union.Field(child) + require.Equal(t, length, field.Len(), "child %d", child) + if child != tc.nonNull { + assert.Equal(t, length, field.NullN(), "child %d", child) + continue + } + assert.Zero(t, field.NullN(), "child %d", child) + for i := range length { + assert.Equal(t, "hi", field.(*array.String).Value(i), "slot %d", i) + } + } + }) + } +} + +func TestMakeArrayFromDenseUnionScalar(t *testing.T) { + mem := memory.NewCheckedAllocator(memory.NewGoAllocator()) + defer mem.AssertSize(t, 0) + + dt := arrow.DenseUnionOf([]arrow.Field{ + {Name: "i", Type: arrow.PrimitiveTypes.Int32, Nullable: true}, + {Name: "s", Type: arrow.BinaryTypes.String, Nullable: true}, + }, []arrow.UnionTypeCode{5, 7}) + + tests := []struct { + name string + sc scalar.Scalar + valid bool + }{ + {name: "valid", sc: scalar.NewDenseUnionScalar(scalar.NewStringScalar("hi"), 7, dt), valid: true}, + {name: "null with a later type code", sc: scalar.NewDenseUnionScalar( + scalar.MakeNullScalar(arrow.BinaryTypes.String), 7, dt)}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + const length = 3 + got, err := scalar.MakeArrayFromScalar(tc.sc, length, mem) + require.NoError(t, err) + defer got.Release() + require.NoError(t, array.ValidateFull(got)) + require.Equal(t, length, got.Len()) + + union := got.(*array.DenseUnion) + for i := range length { + assert.Equal(t, arrow.UnionTypeCode(7), union.TypeCode(i), "slot %d", i) + assert.Zero(t, union.ValueOffset(i), "slot %d", i) + } + assert.Zero(t, union.Field(0).Len()) + str := union.Field(1) + require.Equal(t, 1, str.Len()) + if tc.valid { + assert.Equal(t, "hi", str.(*array.String).Value(0)) + } else { + assert.True(t, str.IsNull(0)) + } + }) + } +} + +func TestMakeArrayFromExtensionScalar(t *testing.T) { + mem := memory.NewCheckedAllocator(memory.NewGoAllocator()) + defer mem.AssertSize(t, 0) + + dt := extensions.NewUUIDType() + id := [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16} + storage := scalar.NewFixedSizeBinaryScalar(memory.NewBufferBytes(id[:]), dt.StorageType()) + + const length = 3 + got, err := scalar.MakeArrayFromScalar(scalar.NewExtensionScalar(storage, dt), length, mem) + require.NoError(t, err) + defer got.Release() + require.NoError(t, array.ValidateFull(got)) + require.Equal(t, length, got.Len()) + + uuids := got.(*extensions.UUIDArray) + for i := range length { Review Comment: `for range <int>` and `range length` require Go 1.22+ (range over integers). If this repo’s `go.mod` Go version is < 1.22, these tests won’t compile. Consider using a classic indexed loop (e.g., `for i := 0; i < n; i++ {}`) or bumping the module Go version if 1.22+ is required. ########## arrow/scalar/make_array_from_scalar_test.go: ########## @@ -0,0 +1,217 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package scalar_test + +import ( + "testing" + + "github.com/apache/arrow-go/v18/arrow" + "github.com/apache/arrow-go/v18/arrow/array" + "github.com/apache/arrow-go/v18/arrow/extensions" + "github.com/apache/arrow-go/v18/arrow/memory" + "github.com/apache/arrow-go/v18/arrow/scalar" + "github.com/google/uuid" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestMakeArrayFromLargeListScalar(t *testing.T) { + mem := memory.NewCheckedAllocator(memory.NewGoAllocator()) + defer mem.AssertSize(t, 0) + + bldr := array.NewInt32Builder(mem) + defer bldr.Release() + bldr.AppendValues([]int32{1, 2, 3}, nil) + values := bldr.NewArray() + defer values.Release() + sc := scalar.NewLargeListScalar(values) + defer sc.Release() + + got, err := scalar.MakeArrayFromScalar(sc, 3, mem) + require.NoError(t, err) + defer got.Release() + require.NoError(t, array.ValidateFull(got)) + + listBldr := array.NewLargeListBuilder(mem, arrow.PrimitiveTypes.Int32) + defer listBldr.Release() + for range 3 { + listBldr.Append(true) + listBldr.ValueBuilder().(*array.Int32Builder).AppendValues([]int32{1, 2, 3}, nil) + } + want := listBldr.NewArray() + defer want.Release() + assert.Truef(t, array.Equal(want, got), "want %s, got %s", want, got) +} + +func TestMakeArrayFromSparseUnionScalar(t *testing.T) { + mem := memory.NewCheckedAllocator(memory.NewGoAllocator()) + defer mem.AssertSize(t, 0) + + dt := arrow.SparseUnionOf([]arrow.Field{ + {Name: "i", Type: arrow.PrimitiveTypes.Int32, Nullable: true}, + {Name: "s", Type: arrow.BinaryTypes.String, Nullable: true}, + }, []arrow.UnionTypeCode{5, 7}) + staleChildID := scalar.NewSparseUnionScalar( + []scalar.Scalar{scalar.NewInt32Scalar(10), scalar.NewStringScalar("hi")}, 7, dt) + staleChildID.ChildID = 0 + + tests := []struct { + name string + sc scalar.Scalar + // nonNull is the index of the child that holds the string "hi" in every + // slot, or -1 when every child is null. + nonNull int + }{ + {name: "valid with other children set", sc: scalar.NewSparseUnionScalar( + []scalar.Scalar{scalar.NewInt32Scalar(10), scalar.NewStringScalar("hi")}, 7, dt), nonNull: 1}, + {name: "child id disagrees with the type code", sc: staleChildID, nonNull: 1}, + {name: "null with a later type code", sc: scalar.NewSparseUnionScalarFromValue( + scalar.MakeNullScalar(arrow.BinaryTypes.String), 1, dt), nonNull: -1}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + const length = 3 + got, err := scalar.MakeArrayFromScalar(tc.sc, length, mem) + require.NoError(t, err) + defer got.Release() + require.NoError(t, array.ValidateFull(got)) + require.Equal(t, length, got.Len()) + + union := got.(*array.SparseUnion) + for i := range length { + assert.Equal(t, arrow.UnionTypeCode(7), union.TypeCode(i), "slot %d", i) + } + for child := range dt.NumFields() { + field := union.Field(child) + require.Equal(t, length, field.Len(), "child %d", child) + if child != tc.nonNull { + assert.Equal(t, length, field.NullN(), "child %d", child) + continue + } + assert.Zero(t, field.NullN(), "child %d", child) + for i := range length { + assert.Equal(t, "hi", field.(*array.String).Value(i), "slot %d", i) + } + } + }) + } +} + +func TestMakeArrayFromDenseUnionScalar(t *testing.T) { + mem := memory.NewCheckedAllocator(memory.NewGoAllocator()) + defer mem.AssertSize(t, 0) + + dt := arrow.DenseUnionOf([]arrow.Field{ + {Name: "i", Type: arrow.PrimitiveTypes.Int32, Nullable: true}, + {Name: "s", Type: arrow.BinaryTypes.String, Nullable: true}, + }, []arrow.UnionTypeCode{5, 7}) + + tests := []struct { + name string + sc scalar.Scalar + valid bool + }{ + {name: "valid", sc: scalar.NewDenseUnionScalar(scalar.NewStringScalar("hi"), 7, dt), valid: true}, + {name: "null with a later type code", sc: scalar.NewDenseUnionScalar( + scalar.MakeNullScalar(arrow.BinaryTypes.String), 7, dt)}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + const length = 3 + got, err := scalar.MakeArrayFromScalar(tc.sc, length, mem) + require.NoError(t, err) + defer got.Release() + require.NoError(t, array.ValidateFull(got)) + require.Equal(t, length, got.Len()) + + union := got.(*array.DenseUnion) + for i := range length { + assert.Equal(t, arrow.UnionTypeCode(7), union.TypeCode(i), "slot %d", i) + assert.Zero(t, union.ValueOffset(i), "slot %d", i) + } + assert.Zero(t, union.Field(0).Len()) + str := union.Field(1) + require.Equal(t, 1, str.Len()) + if tc.valid { + assert.Equal(t, "hi", str.(*array.String).Value(0)) + } else { + assert.True(t, str.IsNull(0)) + } + }) + } +} + +func TestMakeArrayFromExtensionScalar(t *testing.T) { + mem := memory.NewCheckedAllocator(memory.NewGoAllocator()) + defer mem.AssertSize(t, 0) + + dt := extensions.NewUUIDType() + id := [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16} + storage := scalar.NewFixedSizeBinaryScalar(memory.NewBufferBytes(id[:]), dt.StorageType()) + + const length = 3 + got, err := scalar.MakeArrayFromScalar(scalar.NewExtensionScalar(storage, dt), length, mem) Review Comment: The test creates `storage` and an `ExtensionScalar` without ever calling `Release()` on them. If these scalars/buffers are reference-counted (as is common in Arrow), this can leak resources across tests. Prefer binding the extension scalar to a variable and `defer sc.Release()`. If ownership of `storage` is not transferred to the extension scalar, it should also be released explicitly. -- 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]
