forked from fraugster/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_dict.go
164 lines (137 loc) · 3.2 KB
/
type_dict.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
package goparquet
import (
"errors"
"fmt"
"io"
"math"
)
type dictDecoder struct {
uniqueValues []interface{}
keys decoder
}
// just for tests
func (d *dictDecoder) setValues(v []interface{}) {
d.uniqueValues = v
}
// the value should be there before the init
func (d *dictDecoder) init(r io.Reader) error {
buf := make([]byte, 1)
if _, err := io.ReadFull(r, buf); err != nil {
return err
}
w := int(buf[0])
if w < 0 || w > 32 {
return fmt.Errorf("invalid bitwidth %d", w)
}
if w >= 0 {
d.keys = newHybridDecoder(w)
err := d.keys.init(r)
return err
}
return errors.New("bit width zero with non-empty dictionary")
}
func (d *dictDecoder) decodeValues(dst []interface{}) (int, error) {
if d.keys == nil {
return 0, errors.New("no value is inside dictionary")
}
size := int32(len(d.uniqueValues))
for i := range dst {
key, err := d.keys.next()
if err != nil {
return i, err
}
if key < 0 || key >= size {
return 0, fmt.Errorf("dict: invalid index %d, values count are %d", key, size)
}
dst[i] = d.uniqueValues[key]
}
return len(dst), nil
}
type dictStore struct {
valueList []interface{}
uniqueValues map[interface{}]struct{}
uniqueValuesSize int64
allValuesSize int64
readPos int
nullCount int32
useDict bool
alloc *allocTracker
}
func (d *dictStore) getValues() []interface{} {
return d.valueList
}
func (d *dictStore) init() {
d.uniqueValues = make(map[interface{}]struct{})
d.valueList = nil
d.reset()
}
func (d *dictStore) reset() {
d.nullCount = 0
d.readPos = 0
d.uniqueValuesSize = 0
d.allValuesSize = 0
}
func (d *dictStore) addValue(v interface{}, size int) {
if v == nil {
d.nullCount++
return
}
if d.useDict {
k := mapKey(v)
if _, found := d.uniqueValues[k]; !found {
d.uniqueValues[k] = struct{}{}
d.uniqueValuesSize += int64(size)
if len(d.uniqueValues) > math.MaxInt16 {
d.useDict = false
}
}
}
d.allValuesSize += int64(size)
d.valueList = append(d.valueList, v)
d.alloc.register(v, uint64(size))
}
func (d *dictStore) getNextValue() (interface{}, error) {
if d.readPos >= len(d.valueList) {
return nil, errors.New("out of range")
}
d.readPos++
return d.valueList[d.readPos-1], nil
}
func (d *dictStore) numValues() int32 {
return int32(len(d.valueList))
}
func (d *dictStore) nullValueCount() int32 {
return d.nullCount
}
func (d *dictStore) distinctValueCount() int64 {
return int64(len(d.uniqueValues))
}
func (d *dictStore) sizes() (dictLen int64, noDictLen int64) {
return d.uniqueValuesSize + int64(4*len(d.valueList)), d.allValuesSize
}
type dictEncoder struct {
w io.Writer
bitWidth int
indices []int32
}
func newDictEncoder(w io.Writer, bitWidth int) *dictEncoder {
return &dictEncoder{w: w, bitWidth: bitWidth}
}
func (d *dictEncoder) Close() error {
// first write the bitLength in a byte
if err := writeFull(d.w, []byte{byte(d.bitWidth)}); err != nil {
return err
}
enc := newHybridEncoder(d.bitWidth)
if err := enc.init(d.w); err != nil {
return err
}
if err := enc.encode(d.indices); err != nil {
return err
}
return enc.Close()
}
func (d *dictEncoder) encodeIndices(indices []int32) error {
d.indices = append(d.indices, indices...)
return nil
}