forked from segmentio/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.go
488 lines (430 loc) · 13.4 KB
/
buffer.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package parquet
import (
"math/bits"
"sort"
"sync"
"sync/atomic"
)
// Buffer represents an in-memory group of parquet rows.
//
// The main purpose of the Buffer type is to provide a way to sort rows before
// writing them to a parquet file. Buffer implements sort.Interface as a way
// to support reordering the rows that have been written to it.
type Buffer struct {
config *RowGroupConfig
schema *Schema
rowbuf []Row
colbuf [][]Value
chunks []ColumnChunk
columns []ColumnBuffer
sorted []ColumnBuffer
}
// NewBuffer constructs a new buffer, using the given list of buffer options
// to configure the buffer returned by the function.
//
// The function panics if the buffer configuration is invalid. Programs that
// cannot guarantee the validity of the options passed to NewBuffer should
// construct the buffer configuration independently prior to calling this
// function:
//
// config, err := parquet.NewRowGroupConfig(options...)
// if err != nil {
// // handle the configuration error
// ...
// } else {
// // this call to create a buffer is guaranteed not to panic
// buffer := parquet.NewBuffer(config)
// ...
// }
func NewBuffer(options ...RowGroupOption) *Buffer {
config, err := NewRowGroupConfig(options...)
if err != nil {
panic(err)
}
buf := &Buffer{
config: config,
}
if config.Schema != nil {
buf.configure(config.Schema)
}
return buf
}
func (buf *Buffer) configure(schema *Schema) {
if schema == nil {
return
}
sortingColumns := buf.config.SortingColumns
buf.sorted = make([]ColumnBuffer, len(sortingColumns))
forEachLeafColumnOf(schema, func(leaf leafColumn) {
nullOrdering := nullsGoLast
columnIndex := int(leaf.columnIndex)
columnType := leaf.node.Type()
bufferCap := buf.config.ColumnBufferCapacity
dictionary := (Dictionary)(nil)
encoding := encodingOf(leaf.node)
if isDictionaryEncoding(encoding) {
estimatedDictBufferSize := columnType.EstimateSize(bufferCap)
dictBuffer := columnType.NewValues(
make([]byte, 0, estimatedDictBufferSize),
nil,
)
dictionary = columnType.NewDictionary(columnIndex, 0, dictBuffer)
columnType = dictionary.Type()
}
column := columnType.NewColumnBuffer(columnIndex, bufferCap)
switch {
case leaf.maxRepetitionLevel > 0:
column = newRepeatedColumnBuffer(column, leaf.maxRepetitionLevel, leaf.maxDefinitionLevel, nullOrdering)
case leaf.maxDefinitionLevel > 0:
column = newOptionalColumnBuffer(column, leaf.maxDefinitionLevel, nullOrdering)
}
buf.columns = append(buf.columns, column)
if sortingIndex := searchSortingColumn(sortingColumns, leaf.path); sortingIndex < len(sortingColumns) {
if sortingColumns[sortingIndex].Descending() {
column = &reversedColumnBuffer{column}
}
if sortingColumns[sortingIndex].NullsFirst() {
nullOrdering = nullsGoFirst
}
buf.sorted[sortingIndex] = column
}
})
buf.schema = schema
buf.rowbuf = make([]Row, 0, 1)
buf.colbuf = make([][]Value, len(buf.columns))
buf.chunks = make([]ColumnChunk, len(buf.columns))
for i, column := range buf.columns {
buf.chunks[i] = column
}
}
// Size returns the estimated size of the buffer in memory (in bytes).
func (buf *Buffer) Size() int64 {
size := int64(0)
for _, col := range buf.columns {
size += col.Size()
}
return size
}
// NumRows returns the number of rows written to the buffer.
func (buf *Buffer) NumRows() int64 { return int64(buf.Len()) }
// ColumnChunks returns the buffer columns.
func (buf *Buffer) ColumnChunks() []ColumnChunk { return buf.chunks }
// ColumnBuffer returns the buffer columns.
//
// This method is similar to ColumnChunks, but returns a list of ColumnBuffer
// instead of a ColumnChunk values (the latter being read-only); calling
// ColumnBuffers or ColumnChunks with the same index returns the same underlying
// objects, but with different types, which removes the need for making a type
// assertion if the program needed to write directly to the column buffers.
// The presence of the ColumnChunks method is still required to satisfy the
// RowGroup interface.
func (buf *Buffer) ColumnBuffers() []ColumnBuffer { return buf.columns }
// Schema returns the schema of the buffer.
//
// The schema is either configured by passing a Schema in the option list when
// constructing the buffer, or lazily discovered when the first row is written.
func (buf *Buffer) Schema() *Schema { return buf.schema }
// SortingColumns returns the list of columns by which the buffer will be
// sorted.
//
// The sorting order is configured by passing a SortingColumns option when
// constructing the buffer.
func (buf *Buffer) SortingColumns() []SortingColumn { return buf.config.SortingColumns }
// Len returns the number of rows written to the buffer.
func (buf *Buffer) Len() int {
if len(buf.columns) == 0 {
return 0
} else {
// All columns have the same number of rows.
return buf.columns[0].Len()
}
}
// Less returns true if row[i] < row[j] in the buffer.
func (buf *Buffer) Less(i, j int) bool {
for _, col := range buf.sorted {
switch {
case col.Less(i, j):
return true
case col.Less(j, i):
return false
}
}
return false
}
// Swap exchanges the rows at indexes i and j.
func (buf *Buffer) Swap(i, j int) {
for _, col := range buf.columns {
col.Swap(i, j)
}
}
// Reset clears the content of the buffer, allowing it to be reused.
func (buf *Buffer) Reset() {
for _, col := range buf.columns {
col.Reset()
}
}
// Write writes a row held in a Go value to the buffer.
func (buf *Buffer) Write(row interface{}) error {
if buf.schema == nil {
buf.configure(SchemaOf(row))
}
buf.rowbuf = buf.rowbuf[:1]
defer clearRows(buf.rowbuf)
buf.rowbuf[0] = buf.schema.Deconstruct(buf.rowbuf[0], row)
_, err := buf.WriteRows(buf.rowbuf)
return err
}
// WriteRows writes parquet rows to the buffer.
func (buf *Buffer) WriteRows(rows []Row) (int, error) {
defer func() {
for i, colbuf := range buf.colbuf {
clearValues(colbuf)
buf.colbuf[i] = colbuf[:0]
}
}()
if buf.schema == nil {
return 0, ErrRowGroupSchemaMissing
}
for _, row := range rows {
for _, value := range row {
columnIndex := value.Column()
buf.colbuf[columnIndex] = append(buf.colbuf[columnIndex], value)
}
}
for columnIndex, values := range buf.colbuf {
if _, err := buf.columns[columnIndex].WriteValues(values); err != nil {
// TODO: an error at this stage will leave the buffer in an invalid
// state since the row was partially written. Applications are not
// expected to continue using the buffer after getting an error,
// maybe we can enforce it?
return 0, err
}
}
return len(rows), nil
}
// WriteRowGroup satisfies the RowGroupWriter interface.
func (buf *Buffer) WriteRowGroup(rowGroup RowGroup) (int64, error) {
rowGroupSchema := rowGroup.Schema()
switch {
case rowGroupSchema == nil:
return 0, ErrRowGroupSchemaMissing
case buf.schema == nil:
buf.configure(rowGroupSchema)
case !nodesAreEqual(buf.schema, rowGroupSchema):
return 0, ErrRowGroupSchemaMismatch
}
if !sortingColumnsHavePrefix(rowGroup.SortingColumns(), buf.SortingColumns()) {
return 0, ErrRowGroupSortingColumnsMismatch
}
n := buf.NumRows()
r := rowGroup.Rows()
defer r.Close()
_, err := CopyRows(bufferWriter{buf}, r)
return buf.NumRows() - n, err
}
// Rows returns a reader exposing the current content of the buffer.
//
// The buffer and the returned reader share memory. Mutating the buffer
// concurrently to reading rows may result in non-deterministic behavior.
func (buf *Buffer) Rows() Rows { return &rowGroupRows{rowGroup: buf} }
// bufferWriter is an adapter for Buffer which implements both RowWriter and
// PageWriter to enable optimizations in CopyRows for types that support writing
// rows by copying whole pages instead of calling WriteRow repeatedly.
type bufferWriter struct{ buf *Buffer }
func (w bufferWriter) WriteRows(rows []Row) (int, error) {
return w.buf.WriteRows(rows)
}
func (w bufferWriter) WriteValues(values []Value) (int, error) {
return w.buf.columns[values[0].Column()].WriteValues(values)
}
func (w bufferWriter) WritePage(page Page) (int64, error) {
return CopyValues(w.buf.columns[page.Column()], page.Values())
}
var (
_ RowGroup = (*Buffer)(nil)
_ RowGroupWriter = (*Buffer)(nil)
_ sort.Interface = (*Buffer)(nil)
_ RowWriter = (*bufferWriter)(nil)
_ PageWriter = (*bufferWriter)(nil)
_ ValueWriter = (*bufferWriter)(nil)
)
type buffer struct {
data []byte
refc uintptr
pool *bufferPool
}
func newBuffer(data []byte) *buffer {
return &buffer{data: data, refc: 1}
}
func (b *buffer) ref() {
atomic.AddUintptr(&b.refc, +1)
}
func (b *buffer) unref() {
if atomic.AddUintptr(&b.refc, ^uintptr(0)) == 0 {
if b.pool != nil {
b.pool.put(b)
}
}
}
// bufferPool holds a slice of sync.pools used for levelled buffering.
// the table below shows the pools used for different buffer sizes when both getting
// and putting a buffer. when allocating a new buffer from a given pool we always choose the
// min of the put range to guarantee that all gets will have an adequately sized buffer.
//
// [pool] : <get range> : <put range> : <alloc size>
// [0] : 0 -> 1023 : 1024 -> 2047 : 1024
// [1] : 1024 -> 2047 : 2048 -> 4095 : 2048
// [2] : 2048 -> 4095 : 4096 -> 8191 : 4096
// ...
const numPoolBuckets = 16
const basePoolIncrement = 1024
type bufferPool struct {
pool [numPoolBuckets]sync.Pool
}
// get returns a buffer from the levelled buffer pool. sz is used to choose the appropriate pool
func (p *bufferPool) get(sz int) *buffer {
i := levelledPoolIndex(sz)
b, _ := p.pool[i].Get().(*buffer)
if b == nil {
// align size to the pool
poolSize := basePoolIncrement << i
if sz > poolSize { // this can occur when the buffer requested is larger than the largest pool
poolSize = sz
}
b = &buffer{
data: make([]byte, 0, poolSize),
pool: p,
}
}
// if the buffer comes from the largest pool it may not be big enough
if cap(b.data) < sz {
p.pool[i].Put(b)
b = &buffer{
data: make([]byte, 0, sz),
pool: p,
}
}
b.data = b.data[:sz]
b.ref()
return b
}
func (p *bufferPool) put(b *buffer) {
if b.pool != p {
panic("BUG: buffer returned to a different pool than the one it was allocated from")
}
// if this slice is somehow less then our min pool size, just drop it
sz := cap(b.data)
if sz < basePoolIncrement {
return
}
i := levelledPoolIndex(sz / 2) // divide by 2 to put the buffer in the level below so it will always be large enough
p.pool[i].Put(b)
}
// levelledPoolIndex returns the index of the pool to use for a buffer of size sz. it never returns
// an index that will panic
func levelledPoolIndex(sz int) int {
i := sz / basePoolIncrement
i = 32 - bits.LeadingZeros32(uint32(i)) // log2
if i >= numPoolBuckets {
i = numPoolBuckets - 1
}
if i < 0 {
i = 0
}
return i
}
var (
buffers bufferPool
)
type bufferedPage struct {
Page
values *buffer
offsets *buffer
repetitionLevels *buffer
definitionLevels *buffer
}
func (p *bufferedPage) Slice(i, j int64) Page {
bufferRef(p.values)
bufferRef(p.offsets)
bufferRef(p.definitionLevels)
bufferRef(p.repetitionLevels)
return &bufferedPage{
values: p.values,
offsets: p.offsets,
definitionLevels: p.definitionLevels,
repetitionLevels: p.repetitionLevels,
Page: p.Page.Slice(i, j),
}
}
func (p *bufferedPage) Retain() {
bufferRef(p.values)
bufferRef(p.offsets)
bufferRef(p.definitionLevels)
bufferRef(p.repetitionLevels)
}
func (p *bufferedPage) Release() {
bufferUnref(p.values)
bufferUnref(p.offsets)
bufferUnref(p.definitionLevels)
bufferUnref(p.repetitionLevels)
}
func bufferRef(buf *buffer) {
if buf != nil {
buf.ref()
}
}
func bufferUnref(buf *buffer) {
if buf != nil {
buf.unref()
}
}
// Retain is a helper function to increment the reference counter of pages
// backed by memory which can be granularly managed by the application.
//
// Usage of this function is optional and with Release, is intended to allow
// finer grain memory management in the application. Most programs should be
// able to rely on automated memory management provided by the Go garbage
// collector instead.
//
// The function should be called when a page lifetime is about to be shared
// between multiple goroutines or layers of an application, and the program
// wants to express "sharing ownership" of the page.
//
// Calling this function on pages that do not embed a reference counter does
// nothing.
func Retain(page Page) {
if p, _ := page.(retainable); p != nil {
p.Retain()
}
}
// Release is a helper function to decrement the reference counter of pages
// backed by memory which can be granularly managed by the application.
//
// Usage of this is optional and with Retain, is intended to allow finer grained
// memory management in the application, at the expense of potentially causing
// panics if the page is used after its reference count has reached zero. Most
// programs should be able to rely on automated memory management provided by
// the Go garbage collector instead.
//
// The function should be called to return a page to the internal buffer pool,
// when a goroutine "releases ownership" it acquired either by being the single
// owner (e.g. capturing the return value from a ReadPage call) or having gotten
// shared ownership by calling Retain.
//
// Calling this function on pages that do not embed a reference counter does
// nothing.
func Release(page Page) {
if p, _ := page.(releasable); p != nil {
p.Release()
}
}
type retainable interface {
Retain()
}
type releasable interface {
Release()
}
var (
_ retainable = (*bufferedPage)(nil)
_ releasable = (*bufferedPage)(nil)
)