Skip to content
Merged
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
21 changes: 15 additions & 6 deletions arrow/array/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,22 @@ func (d *Data) Dictionary() arrow.ArrayData { return d.dictionary }

// SetDictionary allows replacing the dictionary for this particular Data object
func (d *Data) SetDictionary(dict arrow.ArrayData) {
if d.dictionary != nil {
d.dictionary.Release()
d.dictionary = nil
var newDict *Data
if dict != nil {
var ok bool
newDict, ok = dict.(*Data)
if !ok {
panic("arrow/array: dictionary data must be *array.Data")
}
}
if dict.(*Data) != nil {
dict.Retain()
d.dictionary = dict.(*Data)

if newDict != nil {
newDict.Retain()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retain-before-release ordering is correct. Please add a SetDictionary(nil) test (clearing is now supported per the PR description but untested), and optionally one asserting the explicit panic for a non-*array.Data implementation.

}
oldDict := d.dictionary
d.dictionary = newDict
if oldDict != nil {
oldDict.Release()
}
}

Expand Down
52 changes: 52 additions & 0 deletions arrow/array/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/apache/arrow-go/v18/arrow/memory"
"github.com/apache/arrow-go/v18/internal/utils/maphash"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDataReset(t *testing.T) {
Expand Down Expand Up @@ -101,6 +102,57 @@ func TestDataResetClearsDictionary(t *testing.T) {
mem.AssertSize(t, 0)
}

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

values := memory.NewResizableBuffer(mem)
values.Resize(4)
copy(values.Bytes(), arrow.Int32Traits.CastToBytes([]int32{42}))
dictData := NewData(arrow.PrimitiveTypes.Int32, 1, []*memory.Buffer{nil, values}, nil, 0, 0)
values.Release()

dictType := &arrow.DictionaryType{
IndexType: arrow.PrimitiveTypes.Int8,
ValueType: arrow.PrimitiveTypes.Int32,
}
data := NewDataWithDictionary(dictType, 1, []*memory.Buffer{nil, memory.NewBufferBytes([]byte{0})}, 0, 0, dictData)
defer data.Release()
dictData.Release()

data.SetDictionary(dictData)
require.Same(t, dictData, data.Dictionary())
require.NotNil(t, dictData.Buffers()[1])

valuesArray := NewInt32Data(dictData)
defer valuesArray.Release()
require.Equal(t, int32(42), valuesArray.Value(0))
}

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

values := memory.NewResizableBuffer(mem)
values.Resize(4)
dictData := NewData(arrow.PrimitiveTypes.Int32, 1, []*memory.Buffer{nil, values}, nil, 0, 0)
values.Release()

dictType := &arrow.DictionaryType{
IndexType: arrow.PrimitiveTypes.Int8,
ValueType: arrow.PrimitiveTypes.Int32,
}
data := NewDataWithDictionary(dictType, 1, []*memory.Buffer{nil, memory.NewBufferBytes([]byte{0})}, 0, 0, dictData)
defer data.Release()
dictData.Release()

require.NotNil(t, data.Dictionary())

data.SetDictionary(nil)

require.Nil(t, data.Dictionary())
}

func TestHashIncludesDataMetadataAndBuffers(t *testing.T) {
seed := maphash.MakeSeed()
hash := func(data arrow.ArrayData) uint64 {
Expand Down
Loading