forked from segmentio/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_go18_test.go
268 lines (235 loc) · 6.23 KB
/
buffer_go18_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
//go:build go1.18
package parquet_test
import (
"encoding/binary"
"errors"
"fmt"
"io"
"math/rand"
"reflect"
"sort"
"testing"
"github.com/segmentio/parquet-go"
)
func TestGenericBuffer(t *testing.T) {
testGenericBuffer[booleanColumn](t)
testGenericBuffer[int16Column](t)
testGenericBuffer[int32Column](t)
testGenericBuffer[int64Column](t)
testGenericBuffer[int96Column](t)
testGenericBuffer[floatColumn](t)
testGenericBuffer[doubleColumn](t)
testGenericBuffer[byteArrayColumn](t)
testGenericBuffer[fixedLenByteArrayColumn](t)
testGenericBuffer[stringColumn](t)
testGenericBuffer[indexedStringColumn](t)
testGenericBuffer[uuidColumn](t)
testGenericBuffer[timeColumn](t)
testGenericBuffer[timeInMillisColumn](t)
testGenericBuffer[mapColumn](t)
testGenericBuffer[decimalColumn](t)
testGenericBuffer[addressBook](t)
testGenericBuffer[contact](t)
testGenericBuffer[listColumn2](t)
testGenericBuffer[listColumn1](t)
testGenericBuffer[listColumn0](t)
testGenericBuffer[nestedListColumn1](t)
testGenericBuffer[nestedListColumn](t)
testGenericBuffer[*contact](t)
testGenericBuffer[paddedBooleanColumn](t)
testGenericBuffer[optionalInt32Column](t)
testGenericBuffer[repeatedInt32Column](t)
}
func testGenericBuffer[Row any](t *testing.T) {
var model Row
t.Run(reflect.TypeOf(model).Name(), func(t *testing.T) {
err := quickCheck(func(rows []Row) bool {
if len(rows) == 0 {
return true // TODO: fix support for parquet files with zero rows
}
if err := testGenericBufferRows(rows); err != nil {
t.Error(err)
return false
}
return true
})
if err != nil {
t.Error(err)
}
})
}
func testGenericBufferRows[Row any](rows []Row) error {
setNullPointers(rows)
buffer := parquet.NewGenericBuffer[Row]()
_, err := buffer.Write(rows)
if err != nil {
return err
}
reader := parquet.NewGenericRowGroupReader[Row](buffer)
result := make([]Row, len(rows))
n, err := reader.Read(result)
if err != nil && !errors.Is(err, io.EOF) {
return err
}
if n < len(rows) {
return fmt.Errorf("not enough values were read: want=%d got=%d", len(rows), n)
}
if !reflect.DeepEqual(rows, result) {
return fmt.Errorf("rows mismatch:\nwant: %#v\ngot: %#v", rows, result)
}
return nil
}
func setNullPointers[Row any](rows []Row) {
if len(rows) > 0 && reflect.TypeOf(rows[0]).Kind() == reflect.Pointer {
for i := range rows {
v := reflect.ValueOf(&rows[i]).Elem()
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
}
}
}
type generator[T any] interface {
generate(*rand.Rand) T
}
func BenchmarkGenericBuffer(b *testing.B) {
benchmarkGenericBuffer[benchmarkRowType](b)
benchmarkGenericBuffer[booleanColumn](b)
benchmarkGenericBuffer[int16Column](b)
benchmarkGenericBuffer[int32Column](b)
benchmarkGenericBuffer[int64Column](b)
benchmarkGenericBuffer[floatColumn](b)
benchmarkGenericBuffer[doubleColumn](b)
benchmarkGenericBuffer[byteArrayColumn](b)
benchmarkGenericBuffer[fixedLenByteArrayColumn](b)
benchmarkGenericBuffer[stringColumn](b)
benchmarkGenericBuffer[indexedStringColumn](b)
benchmarkGenericBuffer[uuidColumn](b)
benchmarkGenericBuffer[timeColumn](b)
benchmarkGenericBuffer[timeInMillisColumn](b)
benchmarkGenericBuffer[mapColumn](b)
benchmarkGenericBuffer[decimalColumn](b)
benchmarkGenericBuffer[contact](b)
benchmarkGenericBuffer[paddedBooleanColumn](b)
benchmarkGenericBuffer[optionalInt32Column](b)
benchmarkGenericBuffer[repeatedInt32Column](b)
}
func benchmarkGenericBuffer[Row generator[Row]](b *testing.B) {
var model Row
b.Run(reflect.TypeOf(model).Name(), func(b *testing.B) {
prng := rand.New(rand.NewSource(0))
rows := make([]Row, benchmarkNumRows)
for i := range rows {
rows[i] = rows[i].generate(prng)
}
b.Run("go1.17", func(b *testing.B) {
buffer := parquet.NewBuffer(parquet.SchemaOf(rows[0]))
i := 0
benchmarkRowsPerSecond(b, func() int {
for j := 0; j < benchmarkRowsPerStep; j++ {
if err := buffer.Write(&rows[i]); err != nil {
b.Fatal(err)
}
}
i += benchmarkRowsPerStep
i %= benchmarkNumRows
if i == 0 {
buffer.Reset()
}
return benchmarkRowsPerStep
})
})
b.Run("go1.18", func(b *testing.B) {
buffer := parquet.NewGenericBuffer[Row]()
i := 0
benchmarkRowsPerSecond(b, func() int {
n, err := buffer.Write(rows[i : i+benchmarkRowsPerStep])
if err != nil {
b.Fatal(err)
}
i += benchmarkRowsPerStep
i %= benchmarkNumRows
if i == 0 {
buffer.Reset()
}
return n
})
})
})
}
func TestIssue327(t *testing.T) {
t.Run("untagged nested lists should panic", func(t *testing.T) {
type testType struct {
ListOfLists [][]int
}
defer func() {
if r := recover(); r == nil {
t.Errorf("Nested lists without the list tag should panic")
}
}()
_ = parquet.NewGenericBuffer[testType]()
})
}
func TestIssue346(t *testing.T) {
type TestType struct {
Key int
}
schema := parquet.SchemaOf(TestType{})
buffer := parquet.NewGenericBuffer[any](schema)
data := make([]any, 1)
data[0] = TestType{Key: 0}
_, _ = buffer.Write(data)
}
func TestIssue347(t *testing.T) {
type TestType struct {
Key int
}
// instantiating with concrete type shouldn't panic
_ = parquet.NewGenericBuffer[TestType]()
// instantiating with schema and interface type parameter shouldn't panic
schema := parquet.SchemaOf(TestType{})
_ = parquet.NewGenericBuffer[any](schema)
defer func() {
if r := recover(); r == nil {
t.Errorf("instantiating generic buffer without schema and with interface " +
"type parameter should panic")
}
}()
_ = parquet.NewGenericBuffer[any]()
}
func BenchmarkSortGenericBuffer(b *testing.B) {
type Row struct {
I0 int64
I1 int64
I2 int64
I3 int64
I4 int64
I5 int64
I6 int64
I7 int64
I8 int64
I9 int64
ID [16]byte
}
buf := parquet.NewGenericBuffer[Row](
parquet.SortingRowGroupConfig(
parquet.SortingColumns(
parquet.Ascending("ID"),
),
),
)
rows := make([]Row, 10e3)
prng := rand.New(rand.NewSource(0))
for i := range rows {
binary.LittleEndian.PutUint64(rows[i].ID[:8], uint64(i))
binary.LittleEndian.PutUint64(rows[i].ID[8:], ^uint64(i))
}
buf.Write(rows)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < 10; j++ {
buf.Swap(prng.Intn(len(rows)), prng.Intn(len(rows)))
}
sort.Sort(buf)
}
}