This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
forked from fraugster/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_float.go
144 lines (117 loc) · 2.78 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
135
136
137
138
139
140
141
142
143
144
package goparquet
import (
"encoding/binary"
"io"
"math"
"github.com/pkg/errors"
"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
min, max float32
*ColumnParameters
}
func (f *floatStore) params() *ColumnParameters {
if f.ColumnParameters == nil {
panic("ColumnParameters is nil")
}
return f.ColumnParameters
}
func (*floatStore) sizeOf(v interface{}) int {
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.min = math.MaxFloat32
f.max = -math.MaxFloat32
}
func (f *floatStore) maxValue() []byte {
if f.max == -math.MaxFloat32 {
return nil
}
ret := make([]byte, 4)
binary.LittleEndian.PutUint32(ret, math.Float32bits(f.max))
return ret
}
func (f *floatStore) minValue() []byte {
if f.min == math.MaxFloat32 {
return nil
}
ret := make([]byte, 4)
binary.LittleEndian.PutUint32(ret, math.Float32bits(f.min))
return ret
}
func (f *floatStore) setMinMax(j float32) {
if j < f.min {
f.min = j
}
if j > f.max {
f.max = 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, errors.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, errors.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))
}