-
Notifications
You must be signed in to change notification settings - Fork 53
/
type_int96.go
118 lines (97 loc) · 2.31 KB
/
type_int96.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
package goparquet
import (
"errors"
"fmt"
"io"
"github.com/fraugster/parquet-go/parquet"
)
type int96PlainDecoder struct {
r io.Reader
}
func (i *int96PlainDecoder) init(r io.Reader) error {
i.r = r
return nil
}
func (i *int96PlainDecoder) decodeValues(dst []interface{}) (int, error) {
idx := 0
for range dst {
var data [12]byte
// this one is a little tricky do not use ReadFull here
n, err := i.r.Read(data[:])
// make sure we handle the read data first then handle the error
if n == 12 {
dst[idx] = data
idx++
}
if err != nil && (n == 0 || n == 12) {
return idx, err
}
if err != nil {
return idx, fmt.Errorf("not enough byte to read Int96: %w", err)
}
}
return len(dst), nil
}
type int96PlainEncoder struct {
w io.Writer
}
func (i *int96PlainEncoder) Close() error {
return nil
}
func (i *int96PlainEncoder) init(w io.Writer) error {
i.w = w
return nil
}
func (i *int96PlainEncoder) encodeValues(values []interface{}) error {
data := make([]byte, len(values)*12)
for j := range values {
i96 := values[j].([12]byte)
copy(data[j*12:], i96[:])
}
return writeFull(i.w, data)
}
type int96Store struct {
byteArrayStore
}
func (*int96Store) sizeOf(v interface{}) int {
if vv, ok := v.([][12]byte); ok {
return 12 * len(vv)
}
return 12
}
func (is *int96Store) parquetType() parquet.Type {
return parquet.Type_INT96
}
func (is *int96Store) repetitionType() parquet.FieldRepetitionType {
return is.repTyp
}
func (is *int96Store) getValues(v interface{}) ([]interface{}, error) {
var vals []interface{}
switch typed := v.(type) {
case [12]byte:
if err := is.setMinMax(typed[:]); err != nil {
return nil, err
}
vals = []interface{}{typed}
case [][12]byte:
if is.repTyp != parquet.FieldRepetitionType_REPEATED {
return nil, errors.New("the value is not repeated but it is an array")
}
vals = make([]interface{}, len(typed))
for j := range typed {
if err := is.setMinMax(typed[j][:]); err != nil {
return nil, err
}
vals[j] = typed[j]
}
default:
return nil, fmt.Errorf("unsupported type for storing in Int96 column: %T => %+v", v, v)
}
return vals, nil
}
func (*int96Store) append(arrayIn interface{}, value interface{}) interface{} {
if arrayIn == nil {
arrayIn = make([][12]byte, 0, 1)
}
return append(arrayIn.([][12]byte), value.([12]byte))
}