/
encoder.go
412 lines (369 loc) · 12.9 KB
/
encoder.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
// 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 encoding
import (
"fmt"
"math/bits"
"reflect"
"github.com/apache/arrow/go/v17/arrow"
"github.com/apache/arrow/go/v17/arrow/bitutil"
"github.com/apache/arrow/go/v17/arrow/memory"
"github.com/apache/arrow/go/v17/internal/bitutils"
"github.com/apache/arrow/go/v17/parquet"
format "github.com/apache/arrow/go/v17/parquet/internal/gen-go/parquet"
"github.com/apache/arrow/go/v17/parquet/internal/utils"
"github.com/apache/arrow/go/v17/parquet/schema"
)
//go:generate go run ../../../arrow/_tools/tmpl/main.go -i -data=physical_types.tmpldata plain_encoder_types.gen.go.tmpl typed_encoder.gen.go.tmpl
// EncoderTraits is an interface for the different types to make it more
// convenient to construct encoders for specific types.
type EncoderTraits interface {
Encoder(format.Encoding, bool, *schema.Column, memory.Allocator) TypedEncoder
}
// NewEncoder will return the appropriately typed encoder for the requested physical type
// and encoding.
//
// If mem is nil, memory.DefaultAllocator will be used.
func NewEncoder(t parquet.Type, e parquet.Encoding, useDict bool, descr *schema.Column, mem memory.Allocator) TypedEncoder {
traits := getEncodingTraits(t)
if traits == nil {
return nil
}
if mem == nil {
mem = memory.DefaultAllocator
}
return traits.Encoder(format.Encoding(e), useDict, descr, mem)
}
type encoder struct {
descr *schema.Column
encoding format.Encoding
typeLen int
mem memory.Allocator
sink *PooledBufferWriter
}
// newEncoderBase constructs a new base encoder for embedding on the typed encoders
// encapsulating the common functionality.
func newEncoderBase(e format.Encoding, descr *schema.Column, mem memory.Allocator) encoder {
typelen := -1
if descr != nil && descr.PhysicalType() == parquet.Types.FixedLenByteArray {
typelen = int(descr.TypeLength())
}
return encoder{
descr: descr,
encoding: e,
mem: mem,
typeLen: typelen,
sink: NewPooledBufferWriter(1024),
}
}
func (e *encoder) Release() {
poolbuf := e.sink.buf
memory.Set(poolbuf.Buf(), 0)
poolbuf.ResizeNoShrink(0)
bufferPool.Put(poolbuf)
e.sink = nil
}
// ReserveForWrite allocates n bytes so that the next n bytes written do not require new allocations.
func (e *encoder) ReserveForWrite(n int) { e.sink.Reserve(n) }
func (e *encoder) EstimatedDataEncodedSize() int64 { return int64(e.sink.Len()) }
func (e *encoder) Encoding() parquet.Encoding { return parquet.Encoding(e.encoding) }
func (e *encoder) Allocator() memory.Allocator { return e.mem }
func (e *encoder) append(data []byte) { e.sink.Write(data) }
// FlushValues flushes any unwritten data to the buffer and returns the finished encoded buffer of data.
// This also clears the encoder, ownership of the data belongs to whomever called FlushValues, Release
// should be called on the resulting Buffer when done.
func (e *encoder) FlushValues() (Buffer, error) { return e.sink.Finish(), nil }
// Bytes returns the current bytes that have been written to the encoder's buffer but doesn't transfer ownership.
func (e *encoder) Bytes() []byte { return e.sink.Bytes() }
// Reset drops the data currently in the encoder and resets for new use.
func (e *encoder) Reset() { e.sink.Reset(0) }
type dictEncoder struct {
encoder
dictEncodedSize int
idxBuffer *memory.Buffer
idxValues []int32
memo MemoTable
preservedDict arrow.Array
}
// newDictEncoderBase constructs and returns a dictionary encoder for the appropriate type using the passed
// in memo table for constructing the index.
func newDictEncoderBase(descr *schema.Column, memo MemoTable, mem memory.Allocator) dictEncoder {
return dictEncoder{
encoder: newEncoderBase(format.Encoding_PLAIN_DICTIONARY, descr, mem),
idxBuffer: memory.NewResizableBuffer(mem),
memo: memo,
}
}
// Reset drops all the currently encoded values from the index and indexes from the data to allow
// restarting the encoding process.
func (d *dictEncoder) Reset() {
d.encoder.Reset()
d.dictEncodedSize = 0
d.idxValues = d.idxValues[:0]
d.idxBuffer.ResizeNoShrink(0)
d.memo.Reset()
if d.preservedDict != nil {
d.preservedDict.Release()
d.preservedDict = nil
}
}
func (d *dictEncoder) Release() {
d.encoder.Release()
d.idxBuffer.Release()
if m, ok := d.memo.(BinaryMemoTable); ok {
m.Release()
} else {
d.memo.Reset()
}
if d.preservedDict != nil {
d.preservedDict.Release()
d.preservedDict = nil
}
}
func (d *dictEncoder) expandBuffer(newCap int) {
if cap(d.idxValues) >= newCap {
return
}
curLen := len(d.idxValues)
d.idxBuffer.ResizeNoShrink(arrow.Int32Traits.BytesRequired(bitutil.NextPowerOf2(newCap)))
d.idxValues = arrow.Int32Traits.CastFromBytes(d.idxBuffer.Buf())[: curLen : d.idxBuffer.Len()/arrow.Int32SizeBytes]
}
func (d *dictEncoder) PutIndices(data arrow.Array) error {
newValues := data.Len() - data.NullN()
curPos := len(d.idxValues)
newLen := newValues + curPos
d.expandBuffer(newLen)
d.idxValues = d.idxValues[:newLen:cap(d.idxValues)]
switch data.DataType().ID() {
case arrow.UINT8, arrow.INT8:
values := arrow.Uint8Traits.CastFromBytes(data.Data().Buffers()[1].Bytes())[data.Data().Offset():]
bitutils.VisitSetBitRunsNoErr(data.NullBitmapBytes(),
int64(data.Data().Offset()), int64(data.Len()),
func(pos, length int64) {
for i := int64(0); i < length; i++ {
d.idxValues[curPos] = int32(values[i+pos])
curPos++
}
})
case arrow.UINT16, arrow.INT16:
values := arrow.Uint16Traits.CastFromBytes(data.Data().Buffers()[1].Bytes())[data.Data().Offset():]
bitutils.VisitSetBitRunsNoErr(data.NullBitmapBytes(),
int64(data.Data().Offset()), int64(data.Len()),
func(pos, length int64) {
for i := int64(0); i < length; i++ {
d.idxValues[curPos] = int32(values[i+pos])
curPos++
}
})
case arrow.UINT32, arrow.INT32:
values := arrow.Uint32Traits.CastFromBytes(data.Data().Buffers()[1].Bytes())[data.Data().Offset():]
bitutils.VisitSetBitRunsNoErr(data.NullBitmapBytes(),
int64(data.Data().Offset()), int64(data.Len()),
func(pos, length int64) {
for i := int64(0); i < length; i++ {
d.idxValues[curPos] = int32(values[i+pos])
curPos++
}
})
case arrow.UINT64, arrow.INT64:
values := arrow.Uint64Traits.CastFromBytes(data.Data().Buffers()[1].Bytes())[data.Data().Offset():]
bitutils.VisitSetBitRunsNoErr(data.NullBitmapBytes(),
int64(data.Data().Offset()), int64(data.Len()),
func(pos, length int64) {
for i := int64(0); i < length; i++ {
d.idxValues[curPos] = int32(values[i+pos])
curPos++
}
})
default:
return fmt.Errorf("%w: passed non-integer array to PutIndices", arrow.ErrInvalid)
}
return nil
}
// append the passed index to the indexbuffer
func (d *dictEncoder) addIndex(idx int) {
curLen := len(d.idxValues)
d.expandBuffer(curLen + 1)
d.idxValues = append(d.idxValues, int32(idx))
}
// FlushValues dumps all the currently buffered indexes that would become the data page to a buffer and
// returns it or returns nil and any error encountered.
func (d *dictEncoder) FlushValues() (Buffer, error) {
buf := bufferPool.Get().(*memory.Buffer)
buf.Reserve(int(d.EstimatedDataEncodedSize()))
size, err := d.WriteIndices(buf.Buf())
if err != nil {
poolBuffer{buf}.Release()
return nil, err
}
buf.ResizeNoShrink(size)
return poolBuffer{buf}, nil
}
// EstimatedDataEncodedSize returns the maximum number of bytes needed to store the RLE encoded indexes, not including the
// dictionary index in the computation.
func (d *dictEncoder) EstimatedDataEncodedSize() int64 {
return 1 + int64(utils.MaxRLEBufferSize(d.BitWidth(), len(d.idxValues))+utils.MinRLEBufferSize(d.BitWidth()))
}
// NumEntries returns the number of entires in the dictionary index for this encoder.
func (d *dictEncoder) NumEntries() int {
return d.memo.Size()
}
// BitWidth returns the max bitwidth that would be necessary for encoding the index values currently
// in the dictionary based on the size of the dictionary index.
func (d *dictEncoder) BitWidth() int {
switch d.NumEntries() {
case 0:
return 0
case 1:
return 1
default:
return bits.Len32(uint32(d.NumEntries() - 1))
}
}
// WriteDict writes the dictionary index to the given byte slice.
func (d *dictEncoder) WriteDict(out []byte) {
d.memo.WriteOut(out)
}
// WriteIndices performs Run Length encoding on the indexes and the writes the encoded
// index value data to the provided byte slice, returning the number of bytes actually written.
// If any error is encountered, it will return -1 and the error.
func (d *dictEncoder) WriteIndices(out []byte) (int, error) {
out[0] = byte(d.BitWidth())
enc := utils.NewRleEncoder(utils.NewWriterAtBuffer(out[1:]), d.BitWidth())
for _, idx := range d.idxValues {
if err := enc.Put(uint64(idx)); err != nil {
return -1, err
}
}
nbytes := enc.Flush()
d.idxValues = d.idxValues[:0]
return nbytes + 1, nil
}
// Put adds a value to the dictionary data column, inserting the value if it
// didn't already exist in the dictionary.
func (d *dictEncoder) Put(v interface{}) {
memoIdx, found, err := d.memo.GetOrInsert(v)
if err != nil {
panic(err)
}
if !found {
d.dictEncodedSize += int(reflect.TypeOf(v).Size())
}
d.addIndex(memoIdx)
}
// DictEncodedSize returns the current size of the encoded dictionary
func (d *dictEncoder) DictEncodedSize() int {
return d.dictEncodedSize
}
func (d *dictEncoder) canPutDictionary(values arrow.Array) error {
switch {
case values.NullN() > 0:
return fmt.Errorf("%w: inserted dictionary cannot contain nulls",
arrow.ErrInvalid)
case d.NumEntries() > 0:
return fmt.Errorf("%w: can only call PutDictionary on an empty DictEncoder",
arrow.ErrInvalid)
}
return nil
}
func (d *dictEncoder) PreservedDictionary() arrow.Array { return d.preservedDict }
// spacedCompress is a helper function for encoders to remove the slots in the slices passed in according
// to the bitmap which are null into an output slice that is no longer spaced out with slots for nulls.
func spacedCompress(src, out interface{}, validBits []byte, validBitsOffset int64) int {
nvalid := 0
// for efficiency we use a type switch because the copy runs significantly faster when typed
// than calling reflect.Copy
switch s := src.(type) {
case []int32:
o := out.([]int32)
reader := bitutils.NewSetBitRunReader(validBits, validBitsOffset, int64(len(s)))
for {
run := reader.NextRun()
if run.Length == 0 {
break
}
copy(o[nvalid:], s[int(run.Pos):int(run.Pos+run.Length)])
nvalid += int(run.Length)
}
case []int64:
o := out.([]int64)
reader := bitutils.NewSetBitRunReader(validBits, validBitsOffset, int64(len(s)))
for {
run := reader.NextRun()
if run.Length == 0 {
break
}
copy(o[nvalid:], s[int(run.Pos):int(run.Pos+run.Length)])
nvalid += int(run.Length)
}
case []float32:
o := out.([]float32)
reader := bitutils.NewSetBitRunReader(validBits, validBitsOffset, int64(len(s)))
for {
run := reader.NextRun()
if run.Length == 0 {
break
}
copy(o[nvalid:], s[int(run.Pos):int(run.Pos+run.Length)])
nvalid += int(run.Length)
}
case []float64:
o := out.([]float64)
reader := bitutils.NewSetBitRunReader(validBits, validBitsOffset, int64(len(s)))
for {
run := reader.NextRun()
if run.Length == 0 {
break
}
copy(o[nvalid:], s[int(run.Pos):int(run.Pos+run.Length)])
nvalid += int(run.Length)
}
case []parquet.ByteArray:
o := out.([]parquet.ByteArray)
reader := bitutils.NewSetBitRunReader(validBits, validBitsOffset, int64(len(s)))
for {
run := reader.NextRun()
if run.Length == 0 {
break
}
copy(o[nvalid:], s[int(run.Pos):int(run.Pos+run.Length)])
nvalid += int(run.Length)
}
case []parquet.FixedLenByteArray:
o := out.([]parquet.FixedLenByteArray)
reader := bitutils.NewSetBitRunReader(validBits, validBitsOffset, int64(len(s)))
for {
run := reader.NextRun()
if run.Length == 0 {
break
}
copy(o[nvalid:], s[int(run.Pos):int(run.Pos+run.Length)])
nvalid += int(run.Length)
}
case []bool:
o := out.([]bool)
reader := bitutils.NewSetBitRunReader(validBits, validBitsOffset, int64(len(s)))
for {
run := reader.NextRun()
if run.Length == 0 {
break
}
copy(o[nvalid:], s[int(run.Pos):int(run.Pos+run.Length)])
nvalid += int(run.Length)
}
}
return nvalid
}