forked from fraugster/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_float.go
134 lines (107 loc) · 2.58 KB
/
type_float.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
package goparquet
import (
"encoding/binary"
"fmt"
"io"
"math"
"github.com/fraugster/parquet-go/parquet"
)
type floatPlainDecoder struct {
r io.Reader
}
func (f *floatPlainDecoder) init(r io.Reader) error {
f.r = r
return nil
}
func (f *floatPlainDecoder) decodeValues(dst []interface{}) (int, error) {
var data uint32
for i := range dst {
if err := binary.Read(f.r, binary.LittleEndian, &data); err != nil {
return i, err
}
dst[i] = math.Float32frombits(data)
}
return len(dst), nil
}
type floatPlainEncoder struct {
w io.Writer
}
func (d *floatPlainEncoder) Close() error {
return nil
}
func (d *floatPlainEncoder) init(w io.Writer) error {
d.w = w
return nil
}
func (d *floatPlainEncoder) encodeValues(values []interface{}) error {
data := make([]uint32, len(values))
for i := range values {
data[i] = math.Float32bits(values[i].(float32))
}
return binary.Write(d.w, binary.LittleEndian, data)
}
type floatStore struct {
repTyp parquet.FieldRepetitionType
stats *floatStats
pageStats *floatStats
*ColumnParameters
}
func (f *floatStore) getStats() minMaxValues {
return f.stats
}
func (f *floatStore) getPageStats() minMaxValues {
return f.pageStats
}
func (f *floatStore) params() *ColumnParameters {
if f.ColumnParameters == nil {
panic("ColumnParameters is nil")
}
return f.ColumnParameters
}
func (*floatStore) sizeOf(v interface{}) int {
if vv, ok := v.([]float32); ok {
return 4 * len(vv)
}
return 4
}
func (f *floatStore) parquetType() parquet.Type {
return parquet.Type_FLOAT
}
func (f *floatStore) repetitionType() parquet.FieldRepetitionType {
return f.repTyp
}
func (f *floatStore) reset(rep parquet.FieldRepetitionType) {
f.repTyp = rep
f.stats.reset()
f.pageStats.reset()
}
func (f *floatStore) setMinMax(j float32) {
f.stats.setMinMax(j)
f.pageStats.setMinMax(j)
}
func (f *floatStore) getValues(v interface{}) ([]interface{}, error) {
var vals []interface{}
switch typed := v.(type) {
case float32:
f.setMinMax(typed)
vals = []interface{}{typed}
case []float32:
if f.repTyp != parquet.FieldRepetitionType_REPEATED {
return nil, fmt.Errorf("the value is not repeated but it is an array")
}
vals = make([]interface{}, len(typed))
for j := range typed {
f.setMinMax(typed[j])
vals[j] = typed[j]
}
default:
return nil, fmt.Errorf("unsupported type for storing in float32 column: %T => %+v", v, v)
}
return vals, nil
}
func (*floatStore) append(arrayIn interface{}, value interface{}) interface{} {
if arrayIn == nil {
arrayIn = make([]float32, 0, 1)
}
return append(arrayIn.([]float32), value.(float32))
}