Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pqarrow/arrowutils: Support sorting structs within lists #949

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

metalmatze
Copy link
Member

No description provided.

Comment on lines +279 to +328
case *array.StructBuilder:
defer valueBuilder.Release()

structArray := a.ListValues().(*array.Struct)

// expand the indices from the list to each row in the struct.
structIndicesBuilder := array.NewInt32Builder(mem)
structIndicesBuilder.Reserve(structArray.Len())
for _, i := range indices.Int32Values() {
start, end := a.ValueOffsets(int(i))
for j := start; j < end; j++ {
structIndicesBuilder.Append(int32(j))
}
}
structIndices := structIndicesBuilder.NewInt32Array()

r.Append(true)
start, end := a.ValueOffsets(int(i))
for j := start; j < end; j++ {
idxBuilder.Append(listValues.GetValueIndex(int(j)))
arrays := []arrow.Array{structArray}
err := TakeStructColumn(ctx, structArray, 0, arrays, structIndices)
if err != nil {
return err
}
// Resize is necessary here for the correct offsets to be appended to
// the list builder. Otherwise length will remain at 0.
valueBuilder.Resize(idxBuilder.Len())
}

arr[idx] = r.NewArray()
return nil
newOffsetBuilder := array.NewInt32Builder(mem)
newOffsetBuilder.Append(0)
newOffsetPrevious := int32(0)
for _, i := range indices.Int32Values() {
if a.IsNull(int(i)) {
newOffsetBuilder.AppendNull()
continue
}

start, end := a.ValueOffsets(int(i))
// calculate the length of the current list element and add it to the offsets
newOffsetPrevious += int32(end - start)
newOffsetBuilder.Append(newOffsetPrevious)
}
newOffsets := newOffsetBuilder.NewInt32Array()

data := array.NewData(
arrow.ListOf(structArray.DataType()),
a.Len(),
[]*memory.Buffer{nil, newOffsets.Data().Buffers()[1]},
[]arrow.ArrayData{arrays[0].Data()},
newOffsets.NullN(),
0,
)
defer data.Release()
arr[idx] = array.NewListData(data)

return nil
Copy link
Member Author

Choose a reason for hiding this comment

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

This part is the important change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant