This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
column_mapping.go
88 lines (76 loc) · 1.95 KB
/
column_mapping.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
package parquet
// LeafColumn is a struct type representing leaf columns of a parquet schema.
type LeafColumn struct {
Node Node
Path []string
ColumnIndex int
MaxRepetitionLevel int
MaxDefinitionLevel int
}
func columnMappingOf(schema Node) (mapping columnMappingGroup, columns [][]string) {
mapping = make(columnMappingGroup)
columns = make([][]string, 0, 16)
forEachLeafColumnOf(schema, func(leaf leafColumn) {
path := make(columnPath, len(leaf.path))
copy(path, leaf.path)
columns = append(columns, path)
group := mapping
for len(path) > 1 {
columnName := path[0]
g, ok := group[columnName].(columnMappingGroup)
if !ok {
g = make(columnMappingGroup)
group[columnName] = g
}
group, path = g, path[1:]
}
leaf.path = path // use the copy
group[path[0]] = &columnMappingLeaf{column: leaf}
})
return mapping, columns
}
type columnMapping interface {
lookup(path columnPath) leafColumn
}
type columnMappingGroup map[string]columnMapping
func (group columnMappingGroup) lookup(path columnPath) leafColumn {
if len(path) > 0 {
c, ok := group[path[0]]
if ok {
return c.lookup(path[1:])
}
}
return leafColumn{columnIndex: -1}
}
func (group columnMappingGroup) lookupClosest(path columnPath) leafColumn {
for len(path) > 0 {
g, ok := group[path[0]].(columnMappingGroup)
if ok {
group, path = g, path[1:]
} else {
firstName := ""
firstLeaf := (*columnMappingLeaf)(nil)
for name, child := range group {
if leaf, ok := child.(*columnMappingLeaf); ok {
if firstLeaf == nil || name < firstName {
firstName, firstLeaf = name, leaf
}
}
}
if firstLeaf != nil {
return firstLeaf.column
}
break
}
}
return leafColumn{columnIndex: -1}
}
type columnMappingLeaf struct {
column leafColumn
}
func (leaf *columnMappingLeaf) lookup(path columnPath) leafColumn {
if len(path) == 0 {
return leaf.column
}
return leafColumn{columnIndex: -1}
}