Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions arrow/scalar/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,13 +574,13 @@ func init() {
// GetScalar creates a scalar object from the value at a given index in the
// passed in array, returns an error if unable to do so.
func GetScalar(arr arrow.Array, idx int) (Scalar, error) {
if arr.DataType().ID() != arrow.DICTIONARY && arr.IsNull(idx) {
return MakeNullScalar(arr.DataType()), nil
if idx < 0 || idx >= arr.Len() {
return nil, fmt.Errorf("%w: called GetScalar with index out of range",
arrow.ErrIndex)
}

if idx >= arr.Len() {
return nil, fmt.Errorf("%w: called GetScalar with index larger than array len",
arrow.ErrIndex)
if arr.DataType().ID() != arrow.DICTIONARY && arr.IsNull(idx) {
return MakeNullScalar(arr.DataType()), nil
}

switch arr := arr.(type) {
Expand Down
13 changes: 13 additions & 0 deletions arrow/scalar/scalar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,19 @@ func TestDictionaryScalarBasics(t *testing.T) {
}
}

func TestGetScalarIndexOutOfRange(t *testing.T) {
mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
defer mem.AssertSize(t, 0)

arr, _, _ := array.FromJSON(mem, arrow.BinaryTypes.String, strings.NewReader(`["alpha", "beta"]`))
defer arr.Release()

_, err := scalar.GetScalar(arr, -1)
assert.ErrorIs(t, err, arrow.ErrIndex)
_, err = scalar.GetScalar(arr, 2)
assert.ErrorIs(t, err, arrow.ErrIndex)
}

func TestDictionaryScalarValidateErrors(t *testing.T) {
mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
defer mem.AssertSize(t, 0)
Expand Down