forked from xitongsys/parquet-go
/
unmarshal.go
250 lines (209 loc) · 6.13 KB
/
unmarshal.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
package marshal
import (
"errors"
"reflect"
"strings"
"github.com/xitongsys/parquet-go/common"
"github.com/xitongsys/parquet-go/layout"
"github.com/xitongsys/parquet-go/schema"
"github.com/xitongsys/parquet-go/types"
"github.com/xitongsys/parquet-go/parquet"
)
//Record Map KeyValue pair
type KeyValue struct {
Key reflect.Value
Value reflect.Value
}
type MapRecord struct {
KeyValues []KeyValue
Index int
}
type SliceRecord struct {
Values []reflect.Value
Index int
}
//Convert the table map to objects slice. desInterface is a slice of pointers of objects
func Unmarshal(tableMap *map[string]*layout.Table, bgn int, end int, dstInterface interface{}, schemaHandler *schema.SchemaHandler, prefixPath string) (err error) {
defer func() {
if r := recover(); r != nil {
switch x := r.(type) {
case string:
err = errors.New(x)
case error:
err = x
default:
err = errors.New("unknown error")
}
}
}()
tableNeeds := make(map[string]*layout.Table)
tableBgn, tableEnd := make(map[string]int), make(map[string]int)
for name, table := range *tableMap {
if !strings.HasPrefix(name, prefixPath) {
continue
}
tableNeeds[name] = table
ln := len(table.Values)
num := -1
tableBgn[name], tableEnd[name] = -1, -1
for i := 0; i < ln; i++ {
if table.RepetitionLevels[i] == 0 {
num++
if num == bgn {
tableBgn[name] = i
}
if num == end {
tableEnd[name] = i
break
}
}
}
if tableEnd[name] < 0 {
tableEnd[name] = ln
}
if tableBgn[name] < 0 {
return
}
}
mapRecords := make(map[reflect.Value]*MapRecord)
mapRecordsStack := make([]reflect.Value, 0)
sliceRecords := make(map[reflect.Value]*SliceRecord)
sliceRecordsStack := make([]reflect.Value, 0)
root := reflect.ValueOf(dstInterface).Elem()
prefixIndex := common.PathStrIndex(prefixPath) - 1
for name, table := range tableNeeds {
path := table.Path
bgn := tableBgn[name]
end := tableEnd[name]
schemaIndexs := make([]int, len(path))
for i := 0; i< len(path); i++ {
curPathStr := common.PathToStr(path[:i + 1])
schemaIndexs[i] = int(schemaHandler.MapIndex[curPathStr])
}
repetitionLevels, definitionLevels := make([]int32, len(path)), make([]int32, len(path))
for i := 0; i<len(path); i++ {
repetitionLevels[i], _ = schemaHandler.MaxRepetitionLevel(path[:i+1])
definitionLevels[i], _ = schemaHandler.MaxDefinitionLevel(path[:i+1])
}
for _, rc := range sliceRecords {
rc.Index = -1
}
for _, rc := range mapRecords {
rc.Index = -1
}
for i := bgn; i < end; i++ {
rl, dl, val := table.RepetitionLevels[i], table.DefinitionLevels[i], table.Values[i]
po, index := root, prefixIndex
for index < len(path) {
schemaIndex := schemaIndexs[index]
pT, cT := schemaHandler.SchemaElements[schemaIndex].Type, schemaHandler.SchemaElements[schemaIndex].ConvertedType
if po.Type().Kind() == reflect.Slice && (cT == nil || *cT != parquet.ConvertedType_LIST){
if po.IsNil() {
po.Set(reflect.MakeSlice(po.Type(), 0, 0))
}
if _, ok := sliceRecords[po]; !ok {
sliceRecords[po] = &SliceRecord{
Values: []reflect.Value{},
Index: -1,
}
sliceRecordsStack = append(sliceRecordsStack, po)
}
if rl == repetitionLevels[index] || sliceRecords[po].Index < 0 {
sliceRecords[po].Index++
}
if sliceRecords[po].Index >= len(sliceRecords[po].Values) {
sliceRecords[po].Values = append(sliceRecords[po].Values, reflect.New(po.Type().Elem()).Elem())
}
po = sliceRecords[po].Values[sliceRecords[po].Index]
} else if po.Type().Kind() == reflect.Slice && cT != nil && *cT == parquet.ConvertedType_LIST {
if po.IsNil() {
po.Set(reflect.MakeSlice(po.Type(), 0, 0))
}
if _, ok := sliceRecords[po]; !ok {
sliceRecords[po] = &SliceRecord{
Values: []reflect.Value{},
Index: -1,
}
sliceRecordsStack = append(sliceRecordsStack, po)
}
index++
if definitionLevels[index] > dl {
break
}
if rl == repetitionLevels[index] || sliceRecords[po].Index < 0 {
sliceRecords[po].Index++
}
if sliceRecords[po].Index >= len(sliceRecords[po].Values) {
sliceRecords[po].Values = append(sliceRecords[po].Values, reflect.New(po.Type().Elem()).Elem())
}
po = sliceRecords[po].Values[sliceRecords[po].Index]
index++
if definitionLevels[index] > dl {
break
}
} else if po.Type().Kind() == reflect.Map {
if po.IsNil() {
po.Set(reflect.MakeMap(po.Type()))
}
if _, ok := mapRecords[po]; !ok {
mapRecords[po] = &MapRecord{
KeyValues: []KeyValue{},
Index: -1,
}
mapRecordsStack = append(mapRecordsStack, po)
}
index++
if definitionLevels[index] > dl {
break
}
if rl == repetitionLevels[index] || mapRecords[po].Index < 0 {
mapRecords[po].Index++
}
if mapRecords[po].Index >= len(mapRecords[po].KeyValues) {
mapRecords[po].KeyValues = append(mapRecords[po].KeyValues,
KeyValue{
Key: reflect.New(po.Type().Key()).Elem(),
Value: reflect.New(po.Type().Elem()).Elem(),
})
}
if strings.ToLower(path[index + 1]) == "key" {
po = mapRecords[po].KeyValues[mapRecords[po].Index].Key
}else {
po = mapRecords[po].KeyValues[mapRecords[po].Index].Value
}
index++
if definitionLevels[index] > dl {
break
}
} else if po.Type().Kind() == reflect.Ptr {
if po.IsNil() {
po.Set(reflect.New(po.Type().Elem()))
}
po = po.Elem()
} else if po.Type().Kind() == reflect.Struct {
index++
if definitionLevels[index] > dl {
break;
}
po = po.FieldByName(path[index])
} else {
po.Set(reflect.ValueOf(types.ParquetTypeToGoType(val, pT, cT)))
break
}
}
}
}
for i := len(sliceRecordsStack) - 1; i >= 0; i-- {
po := sliceRecordsStack[i]
vs := sliceRecords[po]
potmp := reflect.Append(po, vs.Values...)
po.Set(potmp)
}
for i := len(mapRecordsStack) - 1; i >= 0; i-- {
po := mapRecordsStack[i]
for _, kv := range mapRecords[po].KeyValues {
po.SetMapIndex(kv.Key, kv.Value)
}
}
return nil
}