1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
// 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 ipc
import (
"errors"
"fmt"
"math/bits"
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/array"
"github.com/apache/arrow-go/v18/arrow/memory"
)
// swap the endianness of the array's buffers as needed in-place to save
// the cost of reallocation.
//
// assumes that nested data buffers are never re-used, if an *array.Data
// child is re-used among the children or the dictionary then this might
// end up double-swapping (putting it back into the original endianness).
// if it is needed to support re-using the buffers, then this can be
// re-factored to instead return a NEW array.Data object with newly
// allocated buffers, rather than doing it in place.
//
// For now this is intended to be used by the IPC readers after loading
// arrays from an IPC message which currently is guaranteed to not re-use
// buffers between arrays.
func swapEndianArrayData(data *array.Data) error {
if data.Offset() != 0 {
return errors.New("unsupported data format: data.offset != 0")
}
if err := swapType(data.DataType(), data); err != nil {
return err
}
return swapChildren(data.Children())
}
func swapChildren(children []arrow.ArrayData) (err error) {
for i := range children {
if err = swapEndianArrayData(children[i].(*array.Data)); err != nil {
break
}
}
return
}
func swapType(dt arrow.DataType, data *array.Data) (err error) {
switch dt.ID() {
case arrow.BINARY, arrow.STRING:
swapOffsets(1, 32, data)
return
case arrow.LARGE_BINARY, arrow.LARGE_STRING:
swapOffsets(1, 64, data)
return
case arrow.NULL, arrow.BOOL, arrow.INT8, arrow.UINT8,
arrow.FIXED_SIZE_BINARY, arrow.FIXED_SIZE_LIST, arrow.STRUCT:
return
}
switch dt := dt.(type) {
case *arrow.Decimal128Type:
rawdata := arrow.Uint64Traits.CastFromBytes(data.Buffers()[1].Bytes())
length := data.Buffers()[1].Len() / arrow.Decimal128SizeBytes
for i := 0; i < length; i++ {
idx := i * 2
tmp := bits.ReverseBytes64(rawdata[idx])
rawdata[idx] = bits.ReverseBytes64(rawdata[idx+1])
rawdata[idx+1] = tmp
}
case *arrow.Decimal256Type:
rawdata := arrow.Uint64Traits.CastFromBytes(data.Buffers()[1].Bytes())
length := data.Buffers()[1].Len() / arrow.Decimal256SizeBytes
for i := 0; i < length; i++ {
idx := i * 4
tmp0 := bits.ReverseBytes64(rawdata[idx])
tmp1 := bits.ReverseBytes64(rawdata[idx+1])
tmp2 := bits.ReverseBytes64(rawdata[idx+2])
rawdata[idx] = bits.ReverseBytes64(rawdata[idx+3])
rawdata[idx+1] = tmp2
rawdata[idx+2] = tmp1
rawdata[idx+3] = tmp0
}
case arrow.UnionType:
if dt.Mode() == arrow.DenseMode {
swapOffsets(2, 32, data)
}
case *arrow.ListType:
swapOffsets(1, 32, data)
case *arrow.LargeListType:
swapOffsets(1, 64, data)
case *arrow.MapType:
swapOffsets(1, 32, data)
case *arrow.DayTimeIntervalType:
byteSwapBuffer(32, data.Buffers()[1])
case *arrow.MonthDayNanoIntervalType:
rawdata := arrow.MonthDayNanoIntervalTraits.CastFromBytes(data.Buffers()[1].Bytes())
for i, tmp := range rawdata {
rawdata[i].Days = int32(bits.ReverseBytes32(uint32(tmp.Days)))
rawdata[i].Months = int32(bits.ReverseBytes32(uint32(tmp.Months)))
rawdata[i].Nanoseconds = int64(bits.ReverseBytes64(uint64(tmp.Nanoseconds)))
}
case arrow.ExtensionType:
return swapType(dt.StorageType(), data)
case *arrow.DictionaryType:
// dictionary itself was already swapped in ReadDictionary calls
return swapType(dt.IndexType, data)
case arrow.FixedWidthDataType:
byteSwapBuffer(dt.BitWidth(), data.Buffers()[1])
default:
err = fmt.Errorf("%w: swapping endianness of %s", arrow.ErrNotImplemented, dt)
}
return
}
// this can get called on an invalid Array Data object by the IPC reader,
// so we won't rely on the data.length and will instead rely on the buffer's
// own size instead.
func byteSwapBuffer(bw int, buf *memory.Buffer) {
if bw == 1 || buf == nil {
// if byte width == 1, no need to swap anything
return
}
switch bw {
case 16:
data := arrow.Uint16Traits.CastFromBytes(buf.Bytes())
for i := range data {
data[i] = bits.ReverseBytes16(data[i])
}
case 32:
data := arrow.Uint32Traits.CastFromBytes(buf.Bytes())
for i := range data {
data[i] = bits.ReverseBytes32(data[i])
}
case 64:
data := arrow.Uint64Traits.CastFromBytes(buf.Bytes())
for i := range data {
data[i] = bits.ReverseBytes64(data[i])
}
}
}
func swapOffsets(index int, bitWidth int, data *array.Data) {
if data.Buffers()[index] == nil || data.Buffers()[index].Len() == 0 {
return
}
// other than unions, offset has one more element than the data.length
// don't yet implement large types, so hardcode 32bit offsets for now
byteSwapBuffer(bitWidth, data.Buffers()[index])
}
|