-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
tree.go
388 lines (319 loc) · 10.9 KB
/
tree.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
/*
* Minio Cloud Storage, (C) 2019 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package schema
import (
"fmt"
"strings"
"github.com/minio/minio/pkg/s3select/internal/parquet-go/gen-go/parquet"
)
func updateMaxDLRL(schemaMap map[string]*Element, maxDL, maxRL int64) {
for _, element := range schemaMap {
element.MaxDefinitionLevel = maxDL
element.MaxRepetitionLevel = maxRL
if *element.RepetitionType != parquet.FieldRepetitionType_REQUIRED {
element.MaxDefinitionLevel++
if *element.RepetitionType == parquet.FieldRepetitionType_REPEATED {
element.MaxRepetitionLevel++
}
}
if element.Children != nil {
updateMaxDLRL(element.Children.schemaMap, element.MaxDefinitionLevel, element.MaxRepetitionLevel)
}
}
}
func toParquetSchema(tree *Tree, treePrefix string, schemaPrefix string, schemaList *[]*parquet.SchemaElement, valueElements *[]*Element) (err error) {
tree.Range(func(name string, element *Element) bool {
pathInTree := name
if treePrefix != "" {
pathInTree = treePrefix + "." + name
}
if element.Type == nil && element.ConvertedType == nil && element.Children == nil {
err = fmt.Errorf("%v: group element must have children", pathInTree)
return false
}
if element.ConvertedType != nil {
switch *element.ConvertedType {
case parquet.ConvertedType_LIST:
// Supported structure.
// <REQUIRED|OPTIONAL> group <name> (LIST) {
// REPEATED group list {
// <REQUIRED|OPTIONAL> <element-type> element;
// }
// }
if element.Type != nil {
err = fmt.Errorf("%v: type must be nil for LIST ConvertedType", pathInTree)
return false
}
if element.Children == nil || element.Children.Length() != 1 {
err = fmt.Errorf("%v: children must have one element only for LIST ConvertedType", pathInTree)
return false
}
listElement, ok := element.Children.Get("list")
if !ok {
err = fmt.Errorf("%v: missing group element 'list' for LIST ConvertedType", pathInTree)
return false
}
if listElement.Name != "list" {
err = fmt.Errorf("%v.list: name must be 'list'", pathInTree)
return false
}
if *listElement.RepetitionType != parquet.FieldRepetitionType_REPEATED {
err = fmt.Errorf("%v.list: repetition type must be REPEATED type", pathInTree)
return false
}
if listElement.Type != nil || listElement.ConvertedType != nil {
err = fmt.Errorf("%v.list: type and converted type must be nil", pathInTree)
return false
}
if listElement.Children == nil || listElement.Children.Length() != 1 {
err = fmt.Errorf("%v.list.element: not found", pathInTree)
return false
}
valueElement, ok := listElement.Children.Get("element")
if !ok {
err = fmt.Errorf("%v.list.element: not found", pathInTree)
return false
}
if valueElement.Name != "element" {
err = fmt.Errorf("%v.list.element: name must be 'element'", pathInTree)
return false
}
case parquet.ConvertedType_MAP:
// Supported structure:
// <REQUIRED|OPTIONAL> group <name> (MAP) {
// REPEATED group key_value {
// REQUIRED <key-type> key;
// <REQUIRED|OPTIONAL> <value-type> value;
// }
// }
if element.Type != nil {
err = fmt.Errorf("%v: type must be nil for MAP ConvertedType", pathInTree)
return false
}
if element.Children == nil || element.Children.Length() != 1 {
err = fmt.Errorf("%v: children must have one element only for MAP ConvertedType", pathInTree)
return false
}
keyValueElement, ok := element.Children.Get("key_value")
if !ok {
err = fmt.Errorf("%v: missing group element 'key_value' for MAP ConvertedType", pathInTree)
return false
}
if keyValueElement.Name != "key_value" {
err = fmt.Errorf("%v.key_value: name must be 'key_value'", pathInTree)
return false
}
if *keyValueElement.RepetitionType != parquet.FieldRepetitionType_REPEATED {
err = fmt.Errorf("%v.key_value: repetition type must be REPEATED type", pathInTree)
return false
}
if keyValueElement.Children == nil || keyValueElement.Children.Length() < 1 || keyValueElement.Children.Length() > 2 {
err = fmt.Errorf("%v.key_value: children must have 'key' and optionally 'value' elements for MAP ConvertedType", pathInTree)
return false
}
keyElement, ok := keyValueElement.Children.Get("key")
if !ok {
err = fmt.Errorf("%v.key_value: missing 'key' element for MAP ConvertedType", pathInTree)
return false
}
if keyElement.Name != "key" {
err = fmt.Errorf("%v.key_value.key: name must be 'key'", pathInTree)
return false
}
if *keyElement.RepetitionType != parquet.FieldRepetitionType_REQUIRED {
err = fmt.Errorf("%v.key_value: repetition type must be REQUIRED type", pathInTree)
return false
}
if keyValueElement.Children.Length() == 2 {
valueElement, ok := keyValueElement.Children.Get("value")
if !ok {
err = fmt.Errorf("%v.key_value: second element must be 'value' element for MAP ConvertedType", pathInTree)
return false
}
if valueElement.Name != "value" {
err = fmt.Errorf("%v.key_value.value: name must be 'value'", pathInTree)
return false
}
}
case parquet.ConvertedType_UTF8, parquet.ConvertedType_UINT_8, parquet.ConvertedType_UINT_16:
fallthrough
case parquet.ConvertedType_UINT_32, parquet.ConvertedType_UINT_64, parquet.ConvertedType_INT_8:
fallthrough
case parquet.ConvertedType_INT_16, parquet.ConvertedType_INT_32, parquet.ConvertedType_INT_64:
if element.Type == nil {
err = fmt.Errorf("%v: ConvertedType %v must have Type value", pathInTree, element.ConvertedType)
return false
}
default:
err = fmt.Errorf("%v: unsupported ConvertedType %v", pathInTree, element.ConvertedType)
return false
}
}
element.PathInTree = pathInTree
element.PathInSchema = element.Name
if schemaPrefix != "" {
element.PathInSchema = schemaPrefix + "." + element.Name
}
if element.Type != nil {
*valueElements = append(*valueElements, element)
}
*schemaList = append(*schemaList, &element.SchemaElement)
if element.Children != nil {
element.numChildren = int32(element.Children.Length())
err = toParquetSchema(element.Children, element.PathInTree, element.PathInSchema, schemaList, valueElements)
}
return (err == nil)
})
return err
}
// Tree - represents tree of schema. Tree preserves order in which elements are added.
type Tree struct {
schemaMap map[string]*Element
keys []string
readOnly bool
}
// String - stringify this tree.
func (tree *Tree) String() string {
var s []string
tree.Range(func(name string, element *Element) bool {
s = append(s, fmt.Sprintf("%v: %v", name, element))
return true
})
return "{" + strings.Join(s, ", ") + "}"
}
// Length - returns length of tree.
func (tree *Tree) Length() int {
return len(tree.keys)
}
func (tree *Tree) travel(pathSegments []string) (pathSegmentIndex int, pathSegment string, currElement *Element, parentTree *Tree, found bool) {
parentTree = tree
for pathSegmentIndex, pathSegment = range pathSegments {
if tree == nil {
found = false
break
}
var tmpCurrElement *Element
if tmpCurrElement, found = tree.schemaMap[pathSegment]; !found {
break
}
currElement = tmpCurrElement
parentTree = tree
tree = currElement.Children
}
return
}
// ReadOnly - returns whether this tree is read only or not.
func (tree *Tree) ReadOnly() bool {
return tree.readOnly
}
// Get - returns the element stored for name.
func (tree *Tree) Get(name string) (element *Element, ok bool) {
pathSegments := strings.Split(name, ".")
for _, pathSegment := range pathSegments {
if tree == nil {
element = nil
ok = false
break
}
if element, ok = tree.schemaMap[pathSegment]; !ok {
break
}
tree = element.Children
}
return element, ok
}
// Set - adds or sets element to name.
func (tree *Tree) Set(name string, element *Element) error {
if tree.readOnly {
return fmt.Errorf("read only tree")
}
pathSegments := strings.Split(name, ".")
if err := validataPathSegments(pathSegments); err != nil {
return err
}
i, pathSegment, currElement, parentTree, found := tree.travel(pathSegments)
if !found {
if i != len(pathSegments)-1 {
return fmt.Errorf("parent %v does not exist", strings.Join(pathSegments[:i+1], "."))
}
if currElement == nil {
parentTree = tree
} else {
if currElement.Type != nil {
return fmt.Errorf("parent %v is not group element", strings.Join(pathSegments[:i], "."))
}
if currElement.Children == nil {
currElement.Children = NewTree()
}
parentTree = currElement.Children
}
parentTree.keys = append(parentTree.keys, pathSegment)
}
parentTree.schemaMap[pathSegment] = element
return nil
}
// Delete - deletes name and its element.
func (tree *Tree) Delete(name string) {
if tree.readOnly {
panic(fmt.Errorf("read only tree"))
}
pathSegments := strings.Split(name, ".")
_, pathSegment, _, parentTree, found := tree.travel(pathSegments)
if found {
for i := range parentTree.keys {
if parentTree.keys[i] == pathSegment {
copy(parentTree.keys[i:], parentTree.keys[i+1:])
parentTree.keys = parentTree.keys[:len(parentTree.keys)-1]
break
}
}
delete(parentTree.schemaMap, pathSegment)
}
}
// Range - calls f sequentially for each name and its element. If f returns false, range stops the iteration.
func (tree *Tree) Range(f func(name string, element *Element) bool) {
for _, name := range tree.keys {
if !f(name, tree.schemaMap[name]) {
break
}
}
}
// ToParquetSchema - returns list of parquet SchemaElement and list of elements those stores values.
func (tree *Tree) ToParquetSchema() (schemaList []*parquet.SchemaElement, valueElements []*Element, err error) {
if tree.readOnly {
return nil, nil, fmt.Errorf("read only tree")
}
updateMaxDLRL(tree.schemaMap, 0, 0)
var schemaElements []*parquet.SchemaElement
if err = toParquetSchema(tree, "", "", &schemaElements, &valueElements); err != nil {
return nil, nil, err
}
tree.readOnly = true
numChildren := int32(len(tree.keys))
schemaList = append(schemaList, &parquet.SchemaElement{
Name: "schema",
RepetitionType: parquet.FieldRepetitionTypePtr(parquet.FieldRepetitionType_REQUIRED),
NumChildren: &numChildren,
})
schemaList = append(schemaList, schemaElements...)
return schemaList, valueElements, nil
}
// NewTree - creates new schema tree.
func NewTree() *Tree {
return &Tree{
schemaMap: make(map[string]*Element),
}
}