forked from xitongsys/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParquetType.go
284 lines (250 loc) · 8.54 KB
/
ParquetType.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
package ParquetType
import (
"fmt"
"math/big"
"reflect"
"github.com/krehermann/parquet-go/parquet"
)
func TypeNameToParquetType(name string, baseName string) (*parquet.Type, *parquet.ConvertedType) {
if name == "BOOLEAN" {
return parquet.TypePtr(parquet.Type_BOOLEAN), nil
} else if name == "INT32" {
return parquet.TypePtr(parquet.Type_INT32), nil
} else if name == "INT64" {
return parquet.TypePtr(parquet.Type_INT64), nil
} else if name == "INT96" {
return parquet.TypePtr(parquet.Type_INT96), nil
} else if name == "FLOAT" {
return parquet.TypePtr(parquet.Type_FLOAT), nil
} else if name == "DOUBLE" {
return parquet.TypePtr(parquet.Type_DOUBLE), nil
} else if name == "BYTE_ARRAY" {
return parquet.TypePtr(parquet.Type_BYTE_ARRAY), nil
} else if name == "FIXED_LEN_BYTE_ARRAY" {
return parquet.TypePtr(parquet.Type_FIXED_LEN_BYTE_ARRAY), nil
} else if name == "UTF8" {
return parquet.TypePtr(parquet.Type_BYTE_ARRAY), parquet.ConvertedTypePtr(parquet.ConvertedType_UTF8)
} else if name == "INT_8" {
return parquet.TypePtr(parquet.Type_INT32), parquet.ConvertedTypePtr(parquet.ConvertedType_INT_8)
} else if name == "INT_16" {
return parquet.TypePtr(parquet.Type_INT32), parquet.ConvertedTypePtr(parquet.ConvertedType_INT_16)
} else if name == "INT_32" {
return parquet.TypePtr(parquet.Type_INT32), parquet.ConvertedTypePtr(parquet.ConvertedType_INT_32)
} else if name == "INT_64" {
return parquet.TypePtr(parquet.Type_INT64), parquet.ConvertedTypePtr(parquet.ConvertedType_INT_64)
} else if name == "UINT_8" {
return parquet.TypePtr(parquet.Type_INT32), parquet.ConvertedTypePtr(parquet.ConvertedType_UINT_8)
} else if name == "UINT_16" {
return parquet.TypePtr(parquet.Type_INT32), parquet.ConvertedTypePtr(parquet.ConvertedType_UINT_16)
} else if name == "UINT_32" {
return parquet.TypePtr(parquet.Type_INT32), parquet.ConvertedTypePtr(parquet.ConvertedType_UINT_32)
} else if name == "UINT_64" {
return parquet.TypePtr(parquet.Type_INT64), parquet.ConvertedTypePtr(parquet.ConvertedType_UINT_64)
} else if name == "DATE" {
return parquet.TypePtr(parquet.Type_INT32), parquet.ConvertedTypePtr(parquet.ConvertedType_DATE)
} else if name == "TIME_MILLIS" {
return parquet.TypePtr(parquet.Type_INT32), parquet.ConvertedTypePtr(parquet.ConvertedType_TIME_MILLIS)
} else if name == "TIME_MICROS" {
return parquet.TypePtr(parquet.Type_INT64), parquet.ConvertedTypePtr(parquet.ConvertedType_TIME_MICROS)
} else if name == "TIMESTAMP_MILLIS" {
return parquet.TypePtr(parquet.Type_INT64), parquet.ConvertedTypePtr(parquet.ConvertedType_TIMESTAMP_MILLIS)
} else if name == "TIMESTAMP_MICROS" {
return parquet.TypePtr(parquet.Type_INT64), parquet.ConvertedTypePtr(parquet.ConvertedType_TIMESTAMP_MICROS)
} else if name == "INTERVAL" {
return parquet.TypePtr(parquet.Type_FIXED_LEN_BYTE_ARRAY), parquet.ConvertedTypePtr(parquet.ConvertedType_INTERVAL)
} else if name == "DECIMAL" {
if baseName == "INT32" {
return parquet.TypePtr(parquet.Type_INT32), parquet.ConvertedTypePtr(parquet.ConvertedType_DECIMAL)
} else if baseName == "INT64" {
return parquet.TypePtr(parquet.Type_INT64), parquet.ConvertedTypePtr(parquet.ConvertedType_DECIMAL)
} else if baseName == "FIXED_LEN_BYTE_ARRAY" {
return parquet.TypePtr(parquet.Type_FIXED_LEN_BYTE_ARRAY), parquet.ConvertedTypePtr(parquet.ConvertedType_DECIMAL)
} else {
return parquet.TypePtr(parquet.Type_BYTE_ARRAY), parquet.ConvertedTypePtr(parquet.ConvertedType_DECIMAL)
}
}
return nil, nil
}
func ParquetTypeToGoType(src interface{}, pT *parquet.Type, cT *parquet.ConvertedType) interface{} {
if src == nil {
return nil
}
if cT == nil {
return src
}
if *cT == parquet.ConvertedType_UINT_8 {
return uint32(src.(int32))
} else if *cT == parquet.ConvertedType_UINT_16 {
return uint32(src.(int32))
} else if *cT == parquet.ConvertedType_UINT_32 {
return uint32(src.(int32))
} else if *cT == parquet.ConvertedType_UINT_64 {
return uint64(src.(int64))
} else {
return src
}
}
func GoTypeToParquetType(src interface{}, pT *parquet.Type, cT *parquet.ConvertedType) interface{} {
if cT == nil {
return src
}
if *cT == parquet.ConvertedType_UINT_8 {
return int32(src.(uint32))
} else if *cT == parquet.ConvertedType_UINT_16 {
return int32(src.(uint32))
} else if *cT == parquet.ConvertedType_UINT_32 {
return int32(src.(uint32))
} else if *cT == parquet.ConvertedType_UINT_64 {
return int64(src.(uint64))
} else {
return src
}
}
//Scan a string to parquet value; length and scale just for decimal
func StrToParquetType(s string, pT *parquet.Type, cT *parquet.ConvertedType, length int, scale int) interface{} {
if cT == nil {
if *pT == parquet.Type_BOOLEAN {
var v bool
fmt.Sscanf(s, "%t", &v)
return v
} else if *pT == parquet.Type_INT32 {
var v int32
fmt.Sscanf(s, "%d", &v)
return v
} else if *pT == parquet.Type_INT64 {
var v int64
fmt.Sscanf(s, "%d", &v)
return v
} else if *pT == parquet.Type_INT96 {
res := StrIntToBinary(s, "LittleEndian", 12, true)
return res
} else if *pT == parquet.Type_FLOAT {
var v float32
fmt.Sscanf(s, "%f", &v)
return v
} else if *pT == parquet.Type_DOUBLE {
var v float64
fmt.Sscanf(s, "%f", &v)
return v
} else if *pT == parquet.Type_BYTE_ARRAY {
return s
} else if *pT == parquet.Type_FIXED_LEN_BYTE_ARRAY {
return s
}
return nil
}
if *cT == parquet.ConvertedType_UTF8 {
return s
} else if *cT == parquet.ConvertedType_INT_8 || *cT == parquet.ConvertedType_INT_16 || *cT == parquet.ConvertedType_INT_32 ||
*cT == parquet.ConvertedType_DATE || *cT == parquet.ConvertedType_TIME_MILLIS {
var v int32
fmt.Sscanf(s, "%d", &v)
return v
} else if *cT == parquet.ConvertedType_UINT_8 || *cT == parquet.ConvertedType_UINT_16 || *cT == parquet.ConvertedType_UINT_32 {
var vt uint32
fmt.Sscanf(s, "%d", &vt)
return int32(vt)
} else if *cT == parquet.ConvertedType_UINT_64 {
var vt uint64
fmt.Sscanf(s, "%d", &vt)
return int64(vt)
} else if *cT == parquet.ConvertedType_INT_64 ||
*cT == parquet.ConvertedType_TIME_MICROS || *cT == parquet.ConvertedType_TIMESTAMP_MICROS || *cT == parquet.ConvertedType_TIMESTAMP_MILLIS {
var v int64
fmt.Sscanf(s, "%d", &v)
return v
} else if *cT == parquet.ConvertedType_INTERVAL {
res := StrIntToBinary(s, "LittleEndian", 12, false)
return res
} else if *cT == parquet.ConvertedType_DECIMAL {
numSca := big.NewFloat(1.0)
for i := 0; i < scale; i++ {
numSca.Mul(numSca, big.NewFloat(10))
}
num := new(big.Float)
num.SetString(s)
num.Mul(num, numSca)
if *pT == parquet.Type_INT32 {
tmp, _ := num.Float64()
return int32(tmp)
} else if *pT == parquet.Type_INT64 {
tmp, _ := num.Float64()
return int64(tmp)
} else if *pT == parquet.Type_FIXED_LEN_BYTE_ARRAY {
s = num.String()
res := StrIntToBinary(s, "BigEndian", length, true)
return res
} else {
s = num.String()
res := StrIntToBinary(s, "BigEndian", 0, true)
return res
}
} else {
return nil
}
}
//order=LittleEndian or BigEndian; length is byte num
func StrIntToBinary(num string, order string, length int, signed bool) string {
bigNum := new(big.Int)
bigNum.SetString(num, 10)
if !signed {
res := bigNum.Bytes()
if len(res) < length {
res = append(make([]byte, length-len(res)), res...)
}
if order == "LittleEndian" {
for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 {
res[i], res[j] = res[j], res[i]
}
}
if length > 0 {
res = res[len(res)-length:]
}
return string(res)
}
flag := bigNum.Cmp(big.NewInt(0))
if flag == 0 {
if length <= 0 {
length = 1
}
return string(make([]byte, length))
}
bigNum = bigNum.SetBytes(bigNum.Bytes())
bs := bigNum.Bytes()
if len(bs) < length {
bs = append(make([]byte, length-len(bs)), bs...)
}
upperBs := make([]byte, len(bs))
upperBs[0] = byte(0x80)
upper := new(big.Int)
upper.SetBytes(upperBs)
if flag > 0 {
upper = upper.Sub(upper, big.NewInt(1))
}
if bigNum.Cmp(upper) > 0 {
bs = append(make([]byte, 1), bs...)
}
if flag < 0 {
modBs := make([]byte, len(bs)+1)
modBs[0] = byte(0x01)
mod := new(big.Int)
mod.SetBytes(modBs)
bs = mod.Sub(mod, bigNum).Bytes()
}
if length > 0 {
bs = bs[len(bs)-length:]
}
if order == "LittleEndian" {
for i, j := 0, len(bs)-1; i < j; i, j = i+1, j-1 {
bs[i], bs[j] = bs[j], bs[i]
}
}
return string(bs)
}
func JSONTypeToParquetType(val reflect.Value, pT *parquet.Type, cT *parquet.ConvertedType, length int, scale int) interface{} {
if val.Type().Kind() == reflect.Interface && val.IsNil() {
return nil
}
s := fmt.Sprintf("%v", val)
return StrToParquetType(s, pT, cT, length, scale)
}