-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
binarybuilder.go
704 lines (601 loc) · 16.8 KB
/
binarybuilder.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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
// 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 array
import (
"bytes"
"encoding/base64"
"fmt"
"math"
"reflect"
"sync/atomic"
"unsafe"
"github.com/apache/arrow/go/v16/arrow"
"github.com/apache/arrow/go/v16/arrow/internal/debug"
"github.com/apache/arrow/go/v16/arrow/memory"
"github.com/apache/arrow/go/v16/internal/json"
)
// A BinaryBuilder is used to build a Binary array using the Append methods.
type BinaryBuilder struct {
builder
dtype arrow.BinaryDataType
offsets bufBuilder
values *byteBufferBuilder
appendOffsetVal func(int)
getOffsetVal func(int) int
maxCapacity uint64
offsetByteWidth int
}
// NewBinaryBuilder can be used for any of the variable length binary types,
// Binary, LargeBinary, String, LargeString by passing the appropriate data type
func NewBinaryBuilder(mem memory.Allocator, dtype arrow.BinaryDataType) *BinaryBuilder {
var (
offsets bufBuilder
offsetValFn func(int)
maxCapacity uint64
offsetByteWidth int
getOffsetVal func(int) int
)
switch dtype.Layout().Buffers[1].ByteWidth {
case 4:
b := newInt32BufferBuilder(mem)
offsetValFn = func(v int) { b.AppendValue(int32(v)) }
getOffsetVal = func(i int) int { return int(b.Value(i)) }
offsets = b
maxCapacity = math.MaxInt32
offsetByteWidth = arrow.Int32SizeBytes
case 8:
b := newInt64BufferBuilder(mem)
offsetValFn = func(v int) { b.AppendValue(int64(v)) }
getOffsetVal = func(i int) int { return int(b.Value(i)) }
offsets = b
maxCapacity = math.MaxInt64
offsetByteWidth = arrow.Int64SizeBytes
}
b := &BinaryBuilder{
builder: builder{refCount: 1, mem: mem},
dtype: dtype,
offsets: offsets,
values: newByteBufferBuilder(mem),
appendOffsetVal: offsetValFn,
maxCapacity: maxCapacity,
offsetByteWidth: offsetByteWidth,
getOffsetVal: getOffsetVal,
}
return b
}
func (b *BinaryBuilder) Type() arrow.DataType { return b.dtype }
// Release decreases the reference count by 1.
// When the reference count goes to zero, the memory is freed.
// Release may be called simultaneously from multiple goroutines.
func (b *BinaryBuilder) Release() {
debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases")
if atomic.AddInt64(&b.refCount, -1) == 0 {
if b.nullBitmap != nil {
b.nullBitmap.Release()
b.nullBitmap = nil
}
if b.offsets != nil {
b.offsets.Release()
b.offsets = nil
}
if b.values != nil {
b.values.Release()
b.values = nil
}
}
}
func (b *BinaryBuilder) Append(v []byte) {
b.Reserve(1)
b.appendNextOffset()
b.values.Append(v)
b.UnsafeAppendBoolToBitmap(true)
}
func (b *BinaryBuilder) AppendString(v string) {
b.Append([]byte(v))
}
func (b *BinaryBuilder) AppendNull() {
b.Reserve(1)
b.appendNextOffset()
b.UnsafeAppendBoolToBitmap(false)
}
func (b *BinaryBuilder) AppendNulls(n int) {
for i := 0; i < n; i++ {
b.AppendNull()
}
}
func (b *BinaryBuilder) AppendEmptyValue() {
b.Reserve(1)
b.appendNextOffset()
b.UnsafeAppendBoolToBitmap(true)
}
func (b *BinaryBuilder) AppendEmptyValues(n int) {
for i := 0; i < n; i++ {
b.AppendEmptyValue()
}
}
// AppendValues will append the values in the v slice. The valid slice determines which values
// in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty,
// all values in v are appended and considered valid.
func (b *BinaryBuilder) AppendValues(v [][]byte, valid []bool) {
if len(v) != len(valid) && len(valid) != 0 {
panic("len(v) != len(valid) && len(valid) != 0")
}
if len(v) == 0 {
return
}
b.Reserve(len(v))
for _, vv := range v {
b.appendNextOffset()
b.values.Append(vv)
}
b.builder.unsafeAppendBoolsToBitmap(valid, len(v))
}
// AppendStringValues will append the values in the v slice. The valid slice determines which values
// in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty,
// all values in v are appended and considered valid.
func (b *BinaryBuilder) AppendStringValues(v []string, valid []bool) {
if len(v) != len(valid) && len(valid) != 0 {
panic("len(v) != len(valid) && len(valid) != 0")
}
if len(v) == 0 {
return
}
b.Reserve(len(v))
for _, vv := range v {
b.appendNextOffset()
b.values.Append([]byte(vv))
}
b.builder.unsafeAppendBoolsToBitmap(valid, len(v))
}
func (b *BinaryBuilder) UnsafeAppend(v []byte) {
b.appendNextOffset()
b.values.unsafeAppend(v)
b.UnsafeAppendBoolToBitmap(true)
}
func (b *BinaryBuilder) Value(i int) []byte {
start := b.getOffsetVal(i)
var end int
if i == (b.length - 1) {
end = b.values.Len()
} else {
end = b.getOffsetVal(i + 1)
}
return b.values.Bytes()[start:end]
}
func (b *BinaryBuilder) init(capacity int) {
b.builder.init(capacity)
b.offsets.resize((capacity + 1) * b.offsetByteWidth)
}
// DataLen returns the number of bytes in the data array.
func (b *BinaryBuilder) DataLen() int { return b.values.length }
// DataCap returns the total number of bytes that can be stored
// without allocating additional memory.
func (b *BinaryBuilder) DataCap() int { return b.values.capacity }
// Reserve ensures there is enough space for appending n elements
// by checking the capacity and calling Resize if necessary.
func (b *BinaryBuilder) Reserve(n int) {
b.builder.reserve(n, b.Resize)
}
// ReserveData ensures there is enough space for appending n bytes
// by checking the capacity and resizing the data buffer if necessary.
func (b *BinaryBuilder) ReserveData(n int) {
if b.values.capacity < b.values.length+n {
b.values.resize(b.values.Len() + n)
}
}
// Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(),
// additional memory will be allocated. If n is smaller, the allocated memory may be reduced.
func (b *BinaryBuilder) Resize(n int) {
b.offsets.resize((n + 1) * b.offsetByteWidth)
if (n * b.offsetByteWidth) < b.offsets.Len() {
b.offsets.SetLength(n * b.offsetByteWidth)
}
b.builder.resize(n, b.init)
}
func (b *BinaryBuilder) ResizeData(n int) {
b.values.length = n
}
// NewArray creates a Binary array from the memory buffers used by the builder and resets the BinaryBuilder
// so it can be used to build a new array.
//
// Builds the appropriate Binary or LargeBinary array based on the datatype
// it was initialized with.
func (b *BinaryBuilder) NewArray() arrow.Array {
if b.offsetByteWidth == arrow.Int32SizeBytes {
return b.NewBinaryArray()
}
return b.NewLargeBinaryArray()
}
// NewBinaryArray creates a Binary array from the memory buffers used by the builder and resets the BinaryBuilder
// so it can be used to build a new array.
func (b *BinaryBuilder) NewBinaryArray() (a *Binary) {
if b.offsetByteWidth != arrow.Int32SizeBytes {
panic("arrow/array: invalid call to NewBinaryArray when building a LargeBinary array")
}
data := b.newData()
a = NewBinaryData(data)
data.Release()
return
}
func (b *BinaryBuilder) NewLargeBinaryArray() (a *LargeBinary) {
if b.offsetByteWidth != arrow.Int64SizeBytes {
panic("arrow/array: invalid call to NewLargeBinaryArray when building a Binary array")
}
data := b.newData()
a = NewLargeBinaryData(data)
data.Release()
return
}
func (b *BinaryBuilder) newData() (data *Data) {
b.appendNextOffset()
offsets, values := b.offsets.Finish(), b.values.Finish()
data = NewData(b.dtype, b.length, []*memory.Buffer{b.nullBitmap, offsets, values}, nil, b.nulls, 0)
if offsets != nil {
offsets.Release()
}
if values != nil {
values.Release()
}
b.builder.reset()
return
}
func (b *BinaryBuilder) appendNextOffset() {
numBytes := b.values.Len()
debug.Assert(uint64(numBytes) <= b.maxCapacity, "exceeded maximum capacity of binary array")
b.appendOffsetVal(numBytes)
}
func (b *BinaryBuilder) AppendValueFromString(s string) error {
if s == NullValueStr {
b.AppendNull()
return nil
}
if b.dtype.IsUtf8() {
b.Append([]byte(s))
return nil
}
decodedVal, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return fmt.Errorf("could not decode base64 string: %w", err)
}
b.Append(decodedVal)
return nil
}
func (b *BinaryBuilder) UnmarshalOne(dec *json.Decoder) error {
t, err := dec.Token()
if err != nil {
return err
}
switch v := t.(type) {
case string:
data, err := base64.StdEncoding.DecodeString(v)
if err != nil {
return err
}
b.Append(data)
case []byte:
b.Append(v)
case nil:
b.AppendNull()
default:
return &json.UnmarshalTypeError{
Value: fmt.Sprint(t),
Type: reflect.TypeOf([]byte{}),
Offset: dec.InputOffset(),
}
}
return nil
}
func (b *BinaryBuilder) Unmarshal(dec *json.Decoder) error {
for dec.More() {
if err := b.UnmarshalOne(dec); err != nil {
return err
}
}
return nil
}
func (b *BinaryBuilder) UnmarshalJSON(data []byte) error {
dec := json.NewDecoder(bytes.NewReader(data))
t, err := dec.Token()
if err != nil {
return err
}
if delim, ok := t.(json.Delim); !ok || delim != '[' {
return fmt.Errorf("binary builder must unpack from json array, found %s", delim)
}
return b.Unmarshal(dec)
}
const (
dfltBlockSize = 32 << 10 // 32 KB
viewValueSizeLimit int32 = math.MaxInt32
)
type BinaryViewBuilder struct {
builder
dtype arrow.BinaryDataType
data *memory.Buffer
rawData []arrow.ViewHeader
blockBuilder multiBufferBuilder
}
func NewBinaryViewBuilder(mem memory.Allocator) *BinaryViewBuilder {
return &BinaryViewBuilder{
dtype: arrow.BinaryTypes.BinaryView,
builder: builder{
refCount: 1,
mem: mem,
},
blockBuilder: multiBufferBuilder{
refCount: 1,
blockSize: dfltBlockSize,
mem: mem,
},
}
}
func (b *BinaryViewBuilder) SetBlockSize(sz uint) {
b.blockBuilder.blockSize = int(sz)
}
func (b *BinaryViewBuilder) Type() arrow.DataType { return b.dtype }
func (b *BinaryViewBuilder) Release() {
debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases")
if atomic.AddInt64(&b.refCount, -1) != 0 {
return
}
if b.nullBitmap != nil {
b.nullBitmap.Release()
b.nullBitmap = nil
}
if b.data != nil {
b.data.Release()
b.data = nil
b.rawData = nil
}
}
func (b *BinaryViewBuilder) init(capacity int) {
b.builder.init(capacity)
b.data = memory.NewResizableBuffer(b.mem)
bytesN := arrow.ViewHeaderTraits.BytesRequired(capacity)
b.data.Resize(bytesN)
b.rawData = arrow.ViewHeaderTraits.CastFromBytes(b.data.Bytes())
}
func (b *BinaryViewBuilder) Resize(n int) {
nbuild := n
if n < minBuilderCapacity {
n = minBuilderCapacity
}
if b.capacity == 0 {
b.init(n)
return
}
b.builder.resize(nbuild, b.init)
b.data.Resize(arrow.ViewHeaderTraits.BytesRequired(n))
b.rawData = arrow.ViewHeaderTraits.CastFromBytes(b.data.Bytes())
}
func (b *BinaryViewBuilder) ReserveData(length int) {
if int32(length) > viewValueSizeLimit {
panic(fmt.Errorf("%w: BinaryView or StringView elements cannot reference strings larger than 2GB",
arrow.ErrInvalid))
}
b.blockBuilder.Reserve(int(length))
}
func (b *BinaryViewBuilder) Reserve(n int) {
b.builder.reserve(n, b.Resize)
}
func (b *BinaryViewBuilder) Append(v []byte) {
if int32(len(v)) > viewValueSizeLimit {
panic(fmt.Errorf("%w: BinaryView or StringView elements cannot reference strings larger than 2GB", arrow.ErrInvalid))
}
if !arrow.IsViewInline(len(v)) {
b.ReserveData(len(v))
}
b.Reserve(1)
b.UnsafeAppend(v)
}
// AppendString is identical to Append, only accepting a string instead
// of a byte slice, avoiding the extra copy that would occur if you simply
// did []byte(v).
//
// This is different than AppendValueFromString which exists for the
// Builder interface, in that this expects raw binary data which is
// appended unmodified. AppendValueFromString expects base64 encoded binary
// data instead.
func (b *BinaryViewBuilder) AppendString(v string) {
// create a []byte without copying the bytes
// in go1.20 this would be unsafe.StringData
val := *(*[]byte)(unsafe.Pointer(&struct {
string
int
}{v, len(v)}))
b.Append(val)
}
func (b *BinaryViewBuilder) AppendNull() {
b.Reserve(1)
b.UnsafeAppendBoolToBitmap(false)
}
func (b *BinaryViewBuilder) AppendNulls(n int) {
b.Reserve(n)
for i := 0; i < n; i++ {
b.UnsafeAppendBoolToBitmap(false)
}
}
func (b *BinaryViewBuilder) AppendEmptyValue() {
b.Reserve(1)
b.UnsafeAppendBoolToBitmap(true)
}
func (b *BinaryViewBuilder) AppendEmptyValues(n int) {
b.Reserve(n)
b.unsafeAppendBoolsToBitmap(nil, n)
}
func (b *BinaryViewBuilder) UnsafeAppend(v []byte) {
hdr := &b.rawData[b.length]
hdr.SetBytes(v)
if !hdr.IsInline() {
b.blockBuilder.UnsafeAppend(hdr, v)
}
b.UnsafeAppendBoolToBitmap(true)
}
func (b *BinaryViewBuilder) AppendValues(v [][]byte, valid []bool) {
if len(v) != len(valid) && len(valid) != 0 {
panic("len(v) != len(valid) && len(valid) != 0")
}
if len(v) == 0 {
return
}
b.Reserve(len(v))
outOfLineTotal := 0
for i, vv := range v {
if len(valid) == 0 || valid[i] {
if !arrow.IsViewInline(len(vv)) {
outOfLineTotal += len(vv)
}
}
}
b.ReserveData(outOfLineTotal)
for i, vv := range v {
if len(valid) == 0 || valid[i] {
hdr := &b.rawData[b.length+i]
hdr.SetBytes(vv)
if !hdr.IsInline() {
b.blockBuilder.UnsafeAppend(hdr, vv)
}
}
}
b.builder.unsafeAppendBoolsToBitmap(valid, len(v))
}
func (b *BinaryViewBuilder) AppendStringValues(v []string, valid []bool) {
if len(v) != len(valid) && len(valid) != 0 {
panic("len(v) != len(valid) && len(valid) != 0")
}
if len(v) == 0 {
return
}
b.Reserve(len(v))
outOfLineTotal := 0
for i, vv := range v {
if len(valid) == 0 || valid[i] {
if !arrow.IsViewInline(len(vv)) {
outOfLineTotal += len(vv)
}
}
}
b.ReserveData(outOfLineTotal)
for i, vv := range v {
if len(valid) == 0 || valid[i] {
hdr := &b.rawData[b.length+i]
hdr.SetString(vv)
if !hdr.IsInline() {
b.blockBuilder.UnsafeAppendString(hdr, vv)
}
}
}
b.builder.unsafeAppendBoolsToBitmap(valid, len(v))
}
// AppendValueFromString is paired with ValueStr for fulfilling the
// base Builder interface. This is intended to read in a human-readable
// string such as from CSV or JSON and append it to the array.
//
// For Binary values are expected to be base64 encoded (and will be
// decoded as such before being appended).
func (b *BinaryViewBuilder) AppendValueFromString(s string) error {
if s == NullValueStr {
b.AppendNull()
return nil
}
if b.dtype.IsUtf8() {
b.Append([]byte(s))
return nil
}
decodedVal, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return fmt.Errorf("could not decode base64 string: %w", err)
}
b.Append(decodedVal)
return nil
}
func (b *BinaryViewBuilder) UnmarshalOne(dec *json.Decoder) error {
t, err := dec.Token()
if err != nil {
return err
}
switch v := t.(type) {
case string:
data, err := base64.StdEncoding.DecodeString(v)
if err != nil {
return err
}
b.Append(data)
case []byte:
b.Append(v)
case nil:
b.AppendNull()
default:
return &json.UnmarshalTypeError{
Value: fmt.Sprint(t),
Type: reflect.TypeOf([]byte{}),
Offset: dec.InputOffset(),
}
}
return nil
}
func (b *BinaryViewBuilder) Unmarshal(dec *json.Decoder) error {
for dec.More() {
if err := b.UnmarshalOne(dec); err != nil {
return err
}
}
return nil
}
func (b *BinaryViewBuilder) UnmarshalJSON(data []byte) error {
dec := json.NewDecoder(bytes.NewReader(data))
t, err := dec.Token()
if err != nil {
return err
}
if delim, ok := t.(json.Delim); !ok || delim != '[' {
return fmt.Errorf("binary view builder must unpack from json array, found %s", delim)
}
return b.Unmarshal(dec)
}
func (b *BinaryViewBuilder) newData() (data *Data) {
bytesRequired := arrow.ViewHeaderTraits.BytesRequired(b.length)
if bytesRequired > 0 && bytesRequired < b.data.Len() {
// trim buffers
b.data.Resize(bytesRequired)
}
dataBuffers := b.blockBuilder.Finish()
data = NewData(b.dtype, b.length, append([]*memory.Buffer{
b.nullBitmap, b.data}, dataBuffers...), nil, b.nulls, 0)
b.reset()
if b.data != nil {
b.data.Release()
b.data = nil
b.rawData = nil
for _, buf := range dataBuffers {
buf.Release()
}
}
return
}
func (b *BinaryViewBuilder) NewBinaryViewArray() (a *BinaryView) {
data := b.newData()
a = NewBinaryViewData(data)
data.Release()
return
}
func (b *BinaryViewBuilder) NewArray() arrow.Array {
return b.NewBinaryViewArray()
}
var (
_ Builder = (*BinaryBuilder)(nil)
_ Builder = (*BinaryViewBuilder)(nil)
)