-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathschemahandler.go
454 lines (403 loc) · 12.6 KB
/
schemahandler.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package schema
import (
"errors"
"fmt"
"reflect"
"github.com/xitongsys/parquet-go/common"
"github.com/xitongsys/parquet-go/parquet"
)
/*
PathMap Example
root(a dummy root) (Path: "root", Children: A)
|
A (Path:"root/A", Childend: B,C)
/ \
B(Path:"root/A/B") C(Path:"root/A/C")
*/
// PathMapType records the path and its children; This is used in Marshal for improve performance.
type PathMapType struct {
Path string
Children map[string]*PathMapType
}
func NewPathMap(path string) *PathMapType {
pathMap := new(PathMapType)
pathMap.Path = path
pathMap.Children = make(map[string]*PathMapType)
return pathMap
}
func (pmt *PathMapType) Add(path []string) {
ln := len(path)
if ln <= 1 {
return
}
c := path[1]
if _, ok := pmt.Children[c]; !ok {
pmt.Children[c] = NewPathMap(pmt.Path + common.PAR_GO_PATH_DELIMITER + c)
}
pmt.Children[c].Add(path[1:])
}
/////////////////pathMap///////////////////////////
// SchemaHandler stores the schema data
type SchemaHandler struct {
SchemaElements []*parquet.SchemaElement
MapIndex map[string]int32
IndexMap map[int32]string
PathMap *PathMapType
Infos []*common.Tag
InPathToExPath map[string]string
ExPathToInPath map[string]string
ValueColumns []string
}
// setValueColumns collects leaf nodes' full path in SchemaHandler.ValueColumns
func (sh *SchemaHandler) setValueColumns() {
for i := 0; i < len(sh.SchemaElements); i++ {
schema := sh.SchemaElements[i]
numChildren := schema.GetNumChildren()
if numChildren == 0 {
pathStr := sh.IndexMap[int32(i)]
sh.ValueColumns = append(sh.ValueColumns, pathStr)
}
}
}
func (sh *SchemaHandler) GetColumnNum() int64 {
return int64(len(sh.ValueColumns))
}
// setPathMap builds the PathMap from leaf SchemaElement
func (sh *SchemaHandler) setPathMap() {
sh.PathMap = NewPathMap(sh.GetRootInName())
for i := 0; i < len(sh.SchemaElements); i++ {
schema := sh.SchemaElements[i]
numChildren := schema.GetNumChildren()
if numChildren == 0 {
pathStr := sh.IndexMap[int32(i)]
sh.PathMap.Add(common.StrToPath(pathStr))
}
}
}
// GetRepetitionType returns the repetition type of a column by it's schema path
func (sh *SchemaHandler) GetRepetitionType(path []string) (parquet.FieldRepetitionType, error) {
pathStr := common.PathToStr(path)
if index, ok := sh.MapIndex[pathStr]; ok {
return sh.SchemaElements[index].GetRepetitionType(), nil
}
return 0, errors.New("Name Not In Schema")
}
// MaxDefinitionLevel returns the max definition level type of a column by it's schema path
func (sh *SchemaHandler) MaxDefinitionLevel(path []string) (int32, error) {
var res int32 = 0
ln := len(path)
for i := 2; i <= ln; i++ {
rt, err := sh.GetRepetitionType(path[:i])
if err != nil {
return 0, err
}
if rt != parquet.FieldRepetitionType_REQUIRED {
res++
}
}
return res, nil
}
// MaxRepetitionLevel returns the max repetition level type of a column by it's schema path
func (sh *SchemaHandler) GetRepetitionLevelIndex(path []string, rl int32) (int32, error) {
var res int32 = 0
ln := len(path)
for i := 2; i <= ln; i++ {
rt, err := sh.GetRepetitionType(path[:i])
if err != nil {
return 0, err
}
if rt == parquet.FieldRepetitionType_REPEATED {
res++
}
if res == rl {
return int32(i - 1), nil
}
}
return res, fmt.Errorf("rl = %d not found in path = %v", rl, path)
}
// MaxRepetitionLevel returns the max repetition level type of a column by it's schema path
func (sh *SchemaHandler) MaxRepetitionLevel(path []string) (int32, error) {
var res int32 = 0
ln := len(path)
for i := 2; i <= ln; i++ {
rt, err := sh.GetRepetitionType(path[:i])
if err != nil {
return 0, err
}
if rt == parquet.FieldRepetitionType_REPEATED {
res++
}
}
return res, nil
}
func (sh *SchemaHandler) GetInName(index int) string {
return sh.Infos[index].InName
}
func (sh *SchemaHandler) GetExName(index int) string {
return sh.Infos[index].ExName
}
func (sh *SchemaHandler) CreateInExMap() {
//use DFS get path of schema
sh.ExPathToInPath, sh.InPathToExPath = map[string]string{}, map[string]string{}
schemas := sh.SchemaElements
ln := int32(len(schemas))
var pos int32 = 0
stack := make([][2]int32, 0) // stack item[0]: index of schemas; item[1]: numChildren
for pos < ln || len(stack) > 0 {
if len(stack) == 0 || stack[len(stack)-1][1] > 0 {
if len(stack) > 0 {
stack[len(stack)-1][1]--
}
item := [2]int32{pos, schemas[pos].GetNumChildren()}
stack = append(stack, item)
pos++
} else { // leaf node
inPath, exPath := make([]string, 0), make([]string, 0)
for i := 0; i < len(stack); i++ {
inPath = append(inPath, sh.Infos[stack[i][0]].InName)
exPath = append(exPath, sh.Infos[stack[i][0]].ExName)
inPathStr, exPathStr := common.PathToStr(inPath), common.PathToStr(exPath)
sh.ExPathToInPath[exPathStr] = inPathStr
sh.InPathToExPath[inPathStr] = exPathStr
}
stack = stack[:len(stack)-1]
}
}
}
//Convert a path to internal path
func (sh *SchemaHandler) ConvertToInPathStr(pathStr string) (string, error) {
if _, ok := sh.InPathToExPath[pathStr]; ok {
return pathStr, nil
}
if res, ok := sh.ExPathToInPath[pathStr]; ok {
return res, nil
}
return "", fmt.Errorf("can't find path %v", pathStr)
}
//Get root name from the schema handler
func (sh *SchemaHandler) GetRootInName() string {
if len(sh.SchemaElements) <= 0 {
return ""
}
return sh.Infos[0].InName
}
func (sh *SchemaHandler) GetRootExName() string {
if len(sh.SchemaElements) <= 0 {
return ""
}
return sh.Infos[0].ExName
}
type Item struct {
GoType reflect.Type
Info *common.Tag
}
func NewItem() *Item {
item := new(Item)
item.Info = common.NewTag()
return item
}
//Create schema handler from a object
func NewSchemaHandlerFromStruct(obj interface{}) (sh *SchemaHandler, 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("error occurred")
}
}
}()
ot := reflect.TypeOf(obj).Elem()
item := NewItem()
item.GoType = ot
item.Info.InName = "Parquet_go_root"
item.Info.ExName = "parquet_go_root"
item.Info.RepetitionType = parquet.FieldRepetitionType_REQUIRED
stack := make([]*Item, 1)
stack[0] = item
schemaElements := make([]*parquet.SchemaElement, 0)
infos := make([]*common.Tag, 0)
for len(stack) > 0 {
ln := len(stack)
item = stack[ln-1]
stack = stack[:ln-1]
var newInfo *common.Tag
if item.GoType.Kind() == reflect.Struct {
schema := parquet.NewSchemaElement()
schema.Name = item.Info.InName
schema.RepetitionType = &item.Info.RepetitionType
numField := int32(item.GoType.NumField())
schema.NumChildren = &numField
schemaElements = append(schemaElements, schema)
newInfo = common.NewTag()
common.DeepCopy(item.Info, newInfo)
infos = append(infos, newInfo)
for i := int(numField - 1); i >= 0; i-- {
f := item.GoType.Field(i)
tagStr := f.Tag.Get("parquet")
//ignore item without parquet tag
if len(tagStr) <= 0 {
numField--
continue
}
newItem := NewItem()
newItem.Info, err = common.StringToTag(tagStr)
if err != nil {
return nil, fmt.Errorf("failed parse tag: %s", err.Error())
}
newItem.Info.InName = f.Name
newItem.GoType = f.Type
if f.Type.Kind() == reflect.Ptr {
newItem.GoType = f.Type.Elem()
newItem.Info.RepetitionType = parquet.FieldRepetitionType_OPTIONAL
}
stack = append(stack, newItem)
}
} else if item.GoType.Kind() == reflect.Slice &&
item.Info.RepetitionType != parquet.FieldRepetitionType_REPEATED {
schema := parquet.NewSchemaElement()
schema.Name = item.Info.InName
rt1 := item.Info.RepetitionType
schema.RepetitionType = &rt1
var numField int32 = 1
schema.NumChildren = &numField
ct1 := parquet.ConvertedType_LIST
schema.ConvertedType = &ct1
schemaElements = append(schemaElements, schema)
newInfo = common.NewTag()
common.DeepCopy(item.Info, newInfo)
infos = append(infos, newInfo)
schema = parquet.NewSchemaElement()
schema.Name = "List"
rt2 := parquet.FieldRepetitionType_REPEATED
schema.RepetitionType = &rt2
schema.NumChildren = &numField
schemaElements = append(schemaElements, schema)
newInfo = common.NewTag()
common.DeepCopy(item.Info, newInfo)
newInfo.InName, newInfo.ExName = "List", "list"
infos = append(infos, newInfo)
newItem := NewItem()
newItem.Info = common.GetValueTagMap(item.Info)
newItem.Info.InName = "Element"
newItem.Info.ExName = "element"
newItem.GoType = item.GoType.Elem()
if newItem.GoType.Kind() == reflect.Ptr {
newItem.Info.RepetitionType = parquet.FieldRepetitionType_OPTIONAL
newItem.GoType = item.GoType.Elem().Elem()
} else {
newItem.Info.RepetitionType = parquet.FieldRepetitionType_REQUIRED
}
stack = append(stack, newItem)
} else if item.GoType.Kind() == reflect.Slice &&
item.Info.RepetitionType == parquet.FieldRepetitionType_REPEATED {
newItem := NewItem()
newItem.Info = item.Info
newItem.GoType = item.GoType.Elem()
stack = append(stack, newItem)
} else if item.GoType.Kind() == reflect.Map {
schema := parquet.NewSchemaElement()
schema.Name = item.Info.InName
rt1 := item.Info.RepetitionType
schema.RepetitionType = &rt1
var numField1 int32 = 1
schema.NumChildren = &numField1
ct1 := parquet.ConvertedType_MAP
schema.ConvertedType = &ct1
schemaElements = append(schemaElements, schema)
newInfo = common.NewTag()
common.DeepCopy(item.Info, newInfo)
infos = append(infos, newInfo)
schema = parquet.NewSchemaElement()
schema.Name = "Key_value"
rt2 := parquet.FieldRepetitionType_REPEATED
schema.RepetitionType = &rt2
var numField2 int32 = 2
schema.NumChildren = &numField2
ct2 := parquet.ConvertedType_MAP_KEY_VALUE
schema.ConvertedType = &ct2
schemaElements = append(schemaElements, schema)
newInfo = common.NewTag()
common.DeepCopy(item.Info, newInfo)
newInfo.InName, newInfo.ExName = "Key_value", "key_value"
infos = append(infos, newInfo)
newItem := NewItem()
newItem.Info = common.GetValueTagMap(item.Info)
newItem.GoType = item.GoType.Elem()
if newItem.GoType.Kind() == reflect.Ptr {
newItem.Info.RepetitionType = parquet.FieldRepetitionType_OPTIONAL
newItem.GoType = item.GoType.Elem().Elem()
} else {
newItem.Info.RepetitionType = parquet.FieldRepetitionType_REQUIRED
}
stack = append(stack, newItem)
newItem = NewItem()
newItem.Info = common.GetKeyTagMap(item.Info)
newItem.GoType = item.GoType.Key()
newItem.Info.RepetitionType = parquet.FieldRepetitionType_REQUIRED
stack = append(stack, newItem)
} else {
schema, err := common.NewSchemaElementFromTagMap(item.Info)
if err != nil {
return nil, fmt.Errorf("failed to create schema from tag map: %s", err.Error())
}
schemaElements = append(schemaElements, schema)
newInfo = common.NewTag()
common.DeepCopy(item.Info, newInfo)
infos = append(infos, newInfo)
}
}
res := NewSchemaHandlerFromSchemaList(schemaElements)
res.Infos = infos
res.CreateInExMap()
return res, nil
}
// NewSchemaHandlerFromSchemaList creates schema handler from schema list
func NewSchemaHandlerFromSchemaList(schemas []*parquet.SchemaElement) *SchemaHandler {
schemaHandler := new(SchemaHandler)
schemaHandler.MapIndex = make(map[string]int32)
schemaHandler.IndexMap = make(map[int32]string)
schemaHandler.InPathToExPath = make(map[string]string)
schemaHandler.ExPathToInPath = make(map[string]string)
schemaHandler.SchemaElements = schemas
schemaHandler.Infos = make([]*common.Tag, len(schemas))
for i := 0; i < len(schemas); i++ {
name := schemas[i].GetName()
InName, ExName := common.StringToVariableName(name), name
schemaHandler.Infos[i] = &common.Tag{
InName: InName,
ExName: ExName,
}
}
schemaHandler.CreateInExMap()
//use DFS get path of schema
ln := int32(len(schemas))
var pos int32 = 0
stack := make([][2]int32, 0) //stack item[0]: index of schemas; item[1]: numChildren
for pos < ln || len(stack) > 0 {
if len(stack) == 0 || stack[len(stack)-1][1] > 0 {
if len(stack) > 0 {
stack[len(stack)-1][1]--
}
item := [2]int32{pos, schemas[pos].GetNumChildren()}
stack = append(stack, item)
pos++
} else {
path := make([]string, 0)
for i := 0; i < len(stack); i++ {
inname := schemaHandler.Infos[stack[i][0]].InName
path = append(path, inname)
}
topPos := stack[len(stack)-1][0]
schemaHandler.MapIndex[common.PathToStr(path)] = topPos
schemaHandler.IndexMap[topPos] = common.PathToStr(path)
stack = stack[:len(stack)-1]
}
}
schemaHandler.setPathMap()
schemaHandler.setValueColumns()
return schemaHandler
}