-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
arrowbatchconverter_test.go
291 lines (268 loc) · 9.78 KB
/
arrowbatchconverter_test.go
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright 2019 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package colserde_test
import (
"bytes"
"fmt"
"testing"
"github.com/apache/arrow/go/arrow/array"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
"github.com/cockroachdb/cockroach/pkg/col/colserde"
"github.com/cockroachdb/cockroach/pkg/col/coltypes"
"github.com/cockroachdb/cockroach/pkg/sql/colexec"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/randutil"
"github.com/stretchr/testify/require"
)
func randomBatch(allocator *colexec.Allocator) ([]coltypes.T, coldata.Batch) {
const maxTyps = 16
rng, _ := randutil.NewPseudoRand()
typs := make([]coltypes.T, rng.Intn(maxTyps)+1)
for i := range typs {
typs[i] = coltypes.AllTypes[rng.Intn(len(coltypes.AllTypes))]
}
capacity := rng.Intn(int(coldata.BatchSize())) + 1
length := rng.Intn(capacity)
b := colexec.RandomBatch(allocator, rng, typs, capacity, length, rng.Float64())
return typs, b
}
// copyBatch copies the original batch. However, to increase test coverage, only
// use the returned batch to assert equality, not as an input to a testing
// function, since Copy simplifies the internals (e.g. if there are zero
// elements to copy, copyBatch returns a zero-capacity batch, which is less
// interesting than testing a batch with a different capacity of BatchSize() but
// zero elements).
func copyBatch(original coldata.Batch) coldata.Batch {
typs := make([]coltypes.T, original.Width())
for i, vec := range original.ColVecs() {
typs[i] = vec.Type()
}
b := coldata.NewMemBatchWithSize(typs, int(original.Length()))
b.SetLength(original.Length())
for colIdx, col := range original.ColVecs() {
b.ColVec(colIdx).Copy(coldata.CopySliceArgs{
SliceArgs: coldata.SliceArgs{
ColType: typs[colIdx],
Src: col,
SrcEndIdx: uint64(original.Length()),
},
})
}
return b
}
func assertEqualBatches(t *testing.T, expected, actual coldata.Batch) {
t.Helper()
if actual.Selection() != nil {
t.Fatal("violated invariant that batches have no selection vectors")
}
require.Equal(t, expected.Length(), actual.Length())
require.Equal(t, expected.Width(), actual.Width())
for colIdx := 0; colIdx < expected.Width(); colIdx++ {
// Verify equality of ColVecs (this includes nulls). Since the coldata.Vec
// backing array is always of coldata.BatchSize() due to the scratch batch
// that the converter keeps around, the coldata.Vec needs to be sliced to
// the first length elements to match on length, otherwise the check will
// fail.
expectedVec := expected.ColVec(colIdx)
actualVec := actual.ColVec(colIdx)
typ := expectedVec.Type()
require.Equal(t, typ, actualVec.Type())
require.Equal(
t,
expectedVec.Nulls().Slice(0, uint64(expected.Length())),
actualVec.Nulls().Slice(0, uint64(actual.Length())),
)
if typ == coltypes.Bytes {
// Cannot use require.Equal for this type.
// TODO(asubiotto): Again, why not?
expectedBytes := expectedVec.Bytes().Window(0, int(expected.Length()))
resultBytes := actualVec.Bytes().Window(0, int(actual.Length()))
require.Equal(t, expectedBytes.Len(), resultBytes.Len())
for i := 0; i < expectedBytes.Len(); i++ {
if !bytes.Equal(expectedBytes.Get(i), resultBytes.Get(i)) {
t.Fatalf("bytes mismatch at index %d:\nexpected:\n%sactual:\n%s", i, expectedBytes, resultBytes)
}
}
} else if typ == coltypes.Timestamp {
// Cannot use require.Equal for this type.
// TODO(yuzefovich): Again, why not?
expectedTimestamp := expectedVec.Timestamp()[0:expected.Length()]
resultTimestamp := actualVec.Timestamp()[0:actual.Length()]
require.Equal(t, len(expectedTimestamp), len(resultTimestamp))
for i := range expectedTimestamp {
if !expectedTimestamp[i].Equal(resultTimestamp[i]) {
t.Fatalf("Timestamp mismatch at index %d:\nexpected:\n%sactual:\n%s", i, expectedTimestamp[i], resultTimestamp[i])
}
}
} else {
require.Equal(
t,
expectedVec.Window(expectedVec.Type(), 0, uint64(expected.Length())),
actualVec.Window(actualVec.Type(), 0, uint64(actual.Length())),
)
}
}
}
func TestArrowBatchConverterRandom(t *testing.T) {
defer leaktest.AfterTest(t)()
typs, b := randomBatch(testAllocator)
c, err := colserde.NewArrowBatchConverter(typs)
require.NoError(t, err)
// Make a copy of the original batch because the converter modifies and casts
// data without copying for performance reasons.
expected := copyBatch(b)
arrowData, err := c.BatchToArrow(b)
require.NoError(t, err)
actual := coldata.NewMemBatchWithSize(nil, 0)
require.NoError(t, c.ArrowToBatch(arrowData, actual))
assertEqualBatches(t, expected, actual)
}
// roundTripBatch is a helper function that round trips a batch through the
// ArrowBatchConverter and RecordBatchSerializer and asserts that the output
// batch is equal to the input batch. Make sure to copy the input batch before
// passing it to this function to assert equality.
func roundTripBatch(
b coldata.Batch, c *colserde.ArrowBatchConverter, r *colserde.RecordBatchSerializer,
) (coldata.Batch, error) {
var buf bytes.Buffer
arrowDataIn, err := c.BatchToArrow(b)
if err != nil {
return nil, err
}
_, _, err = r.Serialize(&buf, arrowDataIn)
if err != nil {
return nil, err
}
var arrowDataOut []*array.Data
if err := r.Deserialize(&arrowDataOut, buf.Bytes()); err != nil {
return nil, err
}
actual := coldata.NewMemBatchWithSize(nil, 0)
if err := c.ArrowToBatch(arrowDataOut, actual); err != nil {
return nil, err
}
return actual, nil
}
func TestRecordBatchRoundtripThroughBytes(t *testing.T) {
defer leaktest.AfterTest(t)()
for run := 0; run < 10; run++ {
typs, b := randomBatch(testAllocator)
c, err := colserde.NewArrowBatchConverter(typs)
require.NoError(t, err)
r, err := colserde.NewRecordBatchSerializer(typs)
require.NoError(t, err)
// Make a copy of the original batch because the converter modifies and
// casts data without copying for performance reasons.
expected := copyBatch(b)
actual, err := roundTripBatch(b, c, r)
require.NoError(t, err)
assertEqualBatches(t, expected, actual)
}
}
func BenchmarkArrowBatchConverter(b *testing.B) {
// fixedLen specifies how many bytes we should fit variable length data types
// to in order to reduce benchmark noise.
const fixedLen = 64
rng, _ := randutil.NewPseudoRand()
typs := []coltypes.T{
coltypes.Bool,
coltypes.Bytes,
coltypes.Decimal,
coltypes.Int64,
coltypes.Timestamp,
}
// numBytes corresponds 1:1 to typs and specifies how many bytes we are
// converting on one iteration of the benchmark for the corresponding type in
// typs.
numBytes := []int64{
int64(coldata.BatchSize()),
fixedLen * int64(coldata.BatchSize()),
8 * int64(coldata.BatchSize()),
3 * 8 * int64(coldata.BatchSize()),
}
// Run a benchmark on every type we care about.
for typIdx, typ := range typs {
batch := colexec.RandomBatch(testAllocator, rng, []coltypes.T{typ}, int(coldata.BatchSize()), 0 /* length */, 0 /* nullProbability */)
if batch.Width() != 1 {
b.Fatalf("unexpected batch width: %d", batch.Width())
}
if typ == coltypes.Bytes {
// This type has variable length elements, fit all of them to be fixedLen
// bytes long so that we can compare results of one benchmark with
// another. Since we can't overwrite elements in a Bytes, create a new
// one.
// TODO(asubiotto): We should probably create some random spec struct that
// we pass in to RandomBatch.
bytes := batch.ColVec(0).Bytes()
newBytes := coldata.NewBytes(bytes.Len())
for i := 0; i < bytes.Len(); i++ {
diff := len(bytes.Get(i)) - fixedLen
if diff < 0 {
newBytes.Set(i, append(bytes.Get(i), make([]byte, -diff)...))
} else if diff >= 0 {
newBytes.Set(i, bytes.Get(i)[:fixedLen])
}
}
batch.ColVec(0).SetCol(newBytes)
}
c, err := colserde.NewArrowBatchConverter([]coltypes.T{typ})
require.NoError(b, err)
nullFractions := []float64{0, 0.25, 0.5}
setNullFraction := func(batch coldata.Batch, nullFraction float64) {
vec := batch.ColVec(0)
vec.Nulls().UnsetNulls()
numNulls := uint16(int(nullFraction * float64(batch.Length())))
// Set the first numNulls elements to null.
for i := uint16(0); i < batch.Length() && i < numNulls; i++ {
vec.Nulls().SetNull(i)
}
}
for _, nullFraction := range nullFractions {
setNullFraction(batch, nullFraction)
testPrefix := fmt.Sprintf("%s/nullFraction=%0.2f", typ.String(), nullFraction)
var data []*array.Data
b.Run(testPrefix+"/BatchToArrow", func(b *testing.B) {
b.SetBytes(numBytes[typIdx])
for i := 0; i < b.N; i++ {
data, _ = c.BatchToArrow(batch)
if len(data) != 1 {
b.Fatal("expected arrow batch of length 1")
}
if data[0].Len() != int(coldata.BatchSize()) {
b.Fatal("unexpected number of elements")
}
}
})
}
for _, nullFraction := range nullFractions {
setNullFraction(batch, nullFraction)
data, err := c.BatchToArrow(batch)
require.NoError(b, err)
testPrefix := fmt.Sprintf("%s/nullFraction=%0.2f", typ.String(), nullFraction)
result := coldata.NewMemBatch(typs)
b.Run(testPrefix+"/ArrowToBatch", func(b *testing.B) {
b.SetBytes(numBytes[typIdx])
for i := 0; i < b.N; i++ {
// Using require.NoError here causes large enough allocations to
// affect the result.
if err := c.ArrowToBatch(data, result); err != nil {
b.Fatal(err)
}
if result.Width() != 1 {
b.Fatal("expected one column")
}
if result.Length() != coldata.BatchSize() {
b.Fatal("unexpected number of elements")
}
}
})
}
}
}