forked from segmentio/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
order_test.go
440 lines (388 loc) · 10.1 KB
/
order_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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package parquet
import (
"bytes"
"sort"
"testing"
"github.com/segmentio/parquet-go/internal/quick"
)
type boolOrder []bool
func (v boolOrder) Len() int { return len(v) }
func (v boolOrder) Less(i, j int) bool { return !v[i] && v[j] }
func (v boolOrder) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
type int32Order []int32
func (v int32Order) Len() int { return len(v) }
func (v int32Order) Less(i, j int) bool { return v[i] < v[j] }
func (v int32Order) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
type int64Order []int64
func (v int64Order) Len() int { return len(v) }
func (v int64Order) Less(i, j int) bool { return v[i] < v[j] }
func (v int64Order) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
type uint32Order []uint32
func (v uint32Order) Len() int { return len(v) }
func (v uint32Order) Less(i, j int) bool { return v[i] < v[j] }
func (v uint32Order) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
type uint64Order []uint64
func (v uint64Order) Len() int { return len(v) }
func (v uint64Order) Less(i, j int) bool { return v[i] < v[j] }
func (v uint64Order) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
type float32Order []float32
func (v float32Order) Len() int { return len(v) }
func (v float32Order) Less(i, j int) bool { return v[i] < v[j] }
func (v float32Order) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
type float64Order []float64
func (v float64Order) Len() int { return len(v) }
func (v float64Order) Less(i, j int) bool { return v[i] < v[j] }
func (v float64Order) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
type bytesOrder [][]byte
func (v bytesOrder) Len() int { return len(v) }
func (v bytesOrder) Less(i, j int) bool { return bytes.Compare(v[i], v[j]) < 0 }
func (v bytesOrder) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
func orderingName(ordering int) string {
switch {
case isAscending(ordering):
return "ascending"
case isDescending(ordering):
return "descending"
default:
return "undefined"
}
}
func isAscending(ordering int) bool {
return ordering > 0
}
func isDescending(ordering int) bool {
return ordering < 0
}
func isUndefined(ordering int) bool {
return ordering == 0
}
func isOrdered(set sort.Interface) bool {
return set.Len() > 1 && sort.IsSorted(set)
}
func checkOrdering(t *testing.T, set sort.Interface, ordering int) bool {
t.Helper()
switch {
case isOrdered(set):
if !isAscending(ordering) {
t.Errorf("got=%s want=ascending", orderingName(ordering))
return false
}
case isOrdered(sort.Reverse(set)):
if !isDescending(ordering) {
t.Errorf("got=%s want=descending", orderingName(ordering))
return false
}
default:
if !isUndefined(ordering) {
t.Errorf("got=%s want=undefined", orderingName(ordering))
return false
}
}
return true
}
func TestOrderOfBool(t *testing.T) {
check := func(values []bool) bool {
return checkOrdering(t, boolOrder(values), orderOfBool(values))
}
err := quick.Check(func(values []bool) bool {
if !check(values) {
return false
}
sort.Sort(boolOrder(values))
if !check(values) {
return false
}
sort.Sort(sort.Reverse(boolOrder(values)))
if !check(values) {
return false
}
return true
})
if err != nil {
t.Error(err)
}
}
func TestOrderOfInt32(t *testing.T) {
check := func(values []int32) bool {
return checkOrdering(t, int32Order(values), orderOfInt32(values))
}
err := quick.Check(func(values []int32) bool {
if !check(values) {
return false
}
sort.Sort(int32Order(values))
if !check(values) {
return false
}
sort.Sort(sort.Reverse(int32Order(values)))
if !check(values) {
return false
}
return true
})
if err != nil {
t.Error(err)
}
// This extra test validates that out-of-order values at 64 byte boundaries
// are properly detected; it tests corner cases of the vectorized code path
// which works on 64 bytes per loop iteration.
values := []int32{
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15,
// 15 > 14, the algorithm must detect that the values are not ordered.
14, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
}
if !check(values) {
t.Error("failed due to not checking the connection between sequences of 16 elements")
}
}
func TestOrderOfInt64(t *testing.T) {
check := func(values []int64) bool {
return checkOrdering(t, int64Order(values), orderOfInt64(values))
}
err := quick.Check(func(values []int64) bool {
if !check(values) {
return false
}
sort.Sort(int64Order(values))
if !check(values) {
return false
}
sort.Sort(sort.Reverse(int64Order(values)))
if !check(values) {
return false
}
return true
})
if err != nil {
t.Error(err)
}
values := []int64{
0, 1, 2, 3, 4, 5, 6, 7,
6, 9, 10, 11, 12, 13, 14, 15,
14, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
}
if !check(values) {
t.Error("failed due to not checking the connection between sequences of 8 elements")
}
}
func TestOrderOfUint32(t *testing.T) {
check := func(values []uint32) bool {
return checkOrdering(t, uint32Order(values), orderOfUint32(values))
}
err := quick.Check(func(values []uint32) bool {
if !check(values) {
return false
}
sort.Sort(uint32Order(values))
if !check(values) {
return false
}
sort.Sort(sort.Reverse(uint32Order(values)))
if !check(values) {
return false
}
return true
})
if err != nil {
t.Error(err)
}
values := []uint32{
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15,
14, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
}
if !check(values) {
t.Error("failed due to not checking the connection between sequences of 16 elements")
}
}
func TestOrderOfUint64(t *testing.T) {
check := func(values []uint64) bool {
return checkOrdering(t, uint64Order(values), orderOfUint64(values))
}
err := quick.Check(func(values []uint64) bool {
if !check(values) {
return false
}
sort.Sort(uint64Order(values))
if !check(values) {
return false
}
sort.Sort(sort.Reverse(uint64Order(values)))
if !check(values) {
return false
}
return true
})
if err != nil {
t.Error(err)
}
values := []uint64{
0, 1, 2, 3, 4, 5, 6, 7,
6, 9, 10, 11, 12, 13, 14, 15,
14, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
}
if !check(values) {
t.Error("failed due to not checking the connection between sequences of 8 elements")
}
}
func TestOrderOfFloat32(t *testing.T) {
check := func(values []float32) bool {
return checkOrdering(t, float32Order(values), orderOfFloat32(values))
}
err := quick.Check(func(values []float32) bool {
if !check(values) {
return false
}
sort.Sort(float32Order(values))
if !check(values) {
return false
}
sort.Sort(sort.Reverse(float32Order(values)))
if !check(values) {
return false
}
return true
})
if err != nil {
t.Error(err)
}
values := []float32{
0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15,
14, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
}
if !check(values) {
t.Error("failed due to not checking the connection between sequences of 16 elements")
}
}
func TestOrderOfFloat64(t *testing.T) {
check := func(values []float64) bool {
return checkOrdering(t, float64Order(values), orderOfFloat64(values))
}
err := quick.Check(func(values []float64) bool {
if !check(values) {
return false
}
sort.Sort(float64Order(values))
if !check(values) {
return false
}
sort.Sort(sort.Reverse(float64Order(values)))
if !check(values) {
return false
}
return true
})
if err != nil {
t.Error(err)
}
values := []float64{
0, 1, 2, 3, 4, 5, 6, 7,
6, 9, 10, 11, 12, 13, 14, 15,
14, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
}
if !check(values) {
t.Error("failed due to not checking the connection between sequences of 8 elements")
}
}
func TestOrderOfBytes(t *testing.T) {
check := func(values [][]byte) bool {
return checkOrdering(t, bytesOrder(values), orderOfBytes(values))
}
err := quick.Check(func(values [][16]byte) bool {
slices := make([][]byte, len(values))
for i := range values {
slices[i] = values[i][:]
}
if !check(slices) {
return false
}
sort.Sort(bytesOrder(slices))
if !check(slices) {
return false
}
sort.Sort(sort.Reverse(bytesOrder(slices)))
if !check(slices) {
return false
}
return true
})
if err != nil {
t.Error(err)
}
}
func BenchmarkOrderOfBool(b *testing.B) {
forEachBenchmarkBufferSize(b, func(b *testing.B, bufferSize int) {
values := make([]bool, bufferSize/1)
for i := 0; i < b.N; i++ {
orderOfBool(values)
}
})
}
func BenchmarkOrderOfInt32(b *testing.B) {
forEachBenchmarkBufferSize(b, func(b *testing.B, bufferSize int) {
values := make([]int32, bufferSize/4)
for i := 0; i < b.N; i++ {
orderOfInt32(values)
}
})
}
func BenchmarkOrderOfInt64(b *testing.B) {
forEachBenchmarkBufferSize(b, func(b *testing.B, bufferSize int) {
values := make([]int64, bufferSize/8)
for i := 0; i < b.N; i++ {
orderOfInt64(values)
}
})
}
func BenchmarkOrderOfUint32(b *testing.B) {
forEachBenchmarkBufferSize(b, func(b *testing.B, bufferSize int) {
values := make([]uint32, bufferSize/4)
for i := 0; i < b.N; i++ {
orderOfUint32(values)
}
})
}
func BenchmarkOrderOfUint64(b *testing.B) {
forEachBenchmarkBufferSize(b, func(b *testing.B, bufferSize int) {
values := make([]uint64, bufferSize/8)
for i := 0; i < b.N; i++ {
orderOfUint64(values)
}
})
}
func BenchmarkOrderOfFloat32(b *testing.B) {
forEachBenchmarkBufferSize(b, func(b *testing.B, bufferSize int) {
values := make([]float32, bufferSize/4)
for i := 0; i < b.N; i++ {
orderOfFloat32(values)
}
})
}
func BenchmarkOrderOfFloat64(b *testing.B) {
forEachBenchmarkBufferSize(b, func(b *testing.B, bufferSize int) {
values := make([]float64, bufferSize/8)
for i := 0; i < b.N; i++ {
orderOfFloat64(values)
}
})
}
func BenchmarkOrderOfBytes(b *testing.B) {
forEachBenchmarkBufferSize(b, func(b *testing.B, bufferSize int) {
data := make([]byte, bufferSize)
values := make([][]byte, len(data)/16)
for i := range values {
values[i] = data[i*16 : (i+1)*16]
}
for i := 0; i < b.N; i++ {
orderOfBytes(values)
}
})
}