forked from fraugster/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_def.go
279 lines (251 loc) · 7.43 KB
/
schema_def.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
package parquetschema
import (
"bytes"
"fmt"
"io"
"github.com/iambus/parquet-go/parquet"
)
// SchemaDefinition represents a valid textual schema definition.
type SchemaDefinition struct {
RootColumn *ColumnDefinition
}
// ColumnDefinition represents the schema definition of a column and optionally its children.
type ColumnDefinition struct {
Children []*ColumnDefinition
SchemaElement *parquet.SchemaElement
}
// SchemaDefinitionFromColumnDefinition creates a new schema definition from the provided root column definition.
func SchemaDefinitionFromColumnDefinition(c *ColumnDefinition) *SchemaDefinition {
if c == nil {
return nil
}
return &SchemaDefinition{RootColumn: c}
}
// ParseSchemaDefinition parses a textual schema definition and returns
// a SchemaDefinition object, or an error if parsing has failed. The textual schema definition
// needs to adhere to the following grammar:
//
// message ::= 'message' <identifier> '{' <message-body> '}'
// message-body ::= <column-definition>*
// column-definition ::= <repetition-type> <column-type-definition>
// repetition-type ::= 'required' | 'repeated' | 'optional'
// column-type-definition ::= <group-definition> | <field-definition>
// group-definition ::= 'group' <identifier> <converted-type-annotation>? '{' <message-body> '}'
// field-definition ::= <type> <identifier> <logical-type-annotation>? <field-id-definition>? ';'
// type ::= 'binary'
// | 'float'
// | 'double'
// | 'boolean'
// | 'int32'
// | 'int64'
// | 'int96'
// | 'fixed_len_byte_array' '(' <number> ')'
// converted-type-annotation ::= '(' <converted-type> ')'
// converted-type ::= 'UTF8'
// | 'MAP'
// | 'MAP_KEY_VALUE'
// | 'LIST'
// | 'ENUM'
// | 'DECIMAL'
// | 'DATE'
// | 'TIME_MILLIS'
// | 'TIME_MICROS'
// | 'TIMESTAMP_MILLIS'
// | 'TIMESTAMP_MICROS'
// | 'UINT_8'
// | 'UINT_16'
// | 'UINT_32'
// | 'UINT_64'
// | 'INT_8'
// | 'INT_16'
// | 'INT_32'
// | 'INT_64'
// | 'JSON'
// | 'BSON'
// | 'INTERVAL'
// logical-type-annotation ::= '(' <logical-type> ')'
// logical-type ::= 'STRING'
// | 'DATE'
// | 'TIMESTAMP' '(' <time-unit> ',' <boolean> ')'
// | 'UUID'
// | 'ENUM'
// | 'JSON'
// | 'BSON'
// | 'INT' '(' <bit-width> ',' <boolean> ')'
// | 'DECIMAL' '(' <precision> ',' <scale> ')'
// field-id-definition ::= '=' <number>
// number ::= <digit>+
// digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
// time-unit ::= 'MILLIS' | 'MICROS' | 'NANOS'
// boolean ::= 'false' | 'true'
// identifier ::= <alpha> <alphanum>*
// alpha ::= 'a'..'z' | 'A'..'Z'
// alphanum ::= <alpha> | <digit>
// bit-width ::= '8' | '16' | '32' | '64'
// precision := <number>
// scale := <number>
// For examples of textual schema definitions, please take a look at schema-files/*.schema.
func ParseSchemaDefinition(schemaText string) (*SchemaDefinition, error) {
p := newSchemaParser(schemaText)
if err := p.parse(); err != nil {
return nil, err
}
return &SchemaDefinition{
RootColumn: p.root,
}, nil
}
// String returns a textual representation of the schema definition. This textual representation
// adheres to the format accepted by the ParseSchemaDefinition function. A textual schema definition
// parsed by ParseSchemaDefinition and turned back into a string by this method repeatedly will
// always remain the same, save for differences in the emitted whitespaces.
func (sd *SchemaDefinition) String() string {
if sd == nil || sd.RootColumn == nil {
return "message empty {\n}\n"
}
buf := new(bytes.Buffer)
fmt.Fprintf(buf, "message %s {\n", sd.RootColumn.SchemaElement.Name)
printCols(buf, sd.RootColumn.Children, 2)
fmt.Fprintf(buf, "}\n")
return buf.String()
}
// SubSchema returns the direct child of the current schema definition
// that matches the provided name. If no such child exists, nil is
// returned.
func (sd *SchemaDefinition) SubSchema(name string) *SchemaDefinition {
if sd == nil {
return nil
}
for _, c := range sd.RootColumn.Children {
if c.SchemaElement.Name == name {
return &SchemaDefinition{
RootColumn: c,
}
}
}
return nil
}
// SchemaElement returns the schema element associated with the current
// schema definition. If no schema element is present, then nil is returned.
func (sd *SchemaDefinition) SchemaElement() *parquet.SchemaElement {
if sd == nil || sd.RootColumn == nil {
return nil
}
return sd.RootColumn.SchemaElement
}
func printCols(w io.Writer, cols []*ColumnDefinition, indent int) {
for _, col := range cols {
printIndent(w, indent)
elem := col.SchemaElement
switch elem.GetRepetitionType() {
case parquet.FieldRepetitionType_REPEATED:
fmt.Fprintf(w, "repeated")
case parquet.FieldRepetitionType_OPTIONAL:
fmt.Fprintf(w, "optional")
case parquet.FieldRepetitionType_REQUIRED:
fmt.Fprintf(w, "required")
}
fmt.Fprintf(w, " ")
if elem.Type == nil {
fmt.Fprintf(w, "group %s", elem.GetName())
if elem.ConvertedType != nil {
fmt.Fprintf(w, " (%s)", elem.GetConvertedType().String())
}
fmt.Fprintf(w, " {\n")
printCols(w, col.Children, indent+2)
printIndent(w, indent)
fmt.Fprintf(w, "}\n")
} else {
typ := getSchemaType(elem)
fmt.Fprintf(w, "%s %s", typ, elem.GetName())
if elem.LogicalType != nil {
fmt.Fprintf(w, " (%s)", getSchemaLogicalType(elem.GetLogicalType()))
} else if elem.ConvertedType != nil {
fmt.Fprintf(w, " (%s)", elem.GetConvertedType().String())
}
if elem.FieldID != nil {
fmt.Fprintf(w, " = %d", elem.GetFieldID())
}
fmt.Fprintf(w, ";\n")
}
}
}
func printIndent(w io.Writer, indent int) {
for i := 0; i < indent; i++ {
fmt.Fprintf(w, " ")
}
}
func getSchemaType(elem *parquet.SchemaElement) string {
switch elem.GetType() {
case parquet.Type_BYTE_ARRAY:
return "binary"
case parquet.Type_FLOAT:
return "float"
case parquet.Type_DOUBLE:
return "double"
case parquet.Type_BOOLEAN:
return "boolean"
case parquet.Type_INT32:
return "int32"
case parquet.Type_INT64:
return "int64"
case parquet.Type_INT96:
return "int96"
case parquet.Type_FIXED_LEN_BYTE_ARRAY:
return fmt.Sprintf("fixed_len_byte_array(%d)", elem.GetTypeLength())
}
return fmt.Sprintf("UT:%s", elem.GetType())
}
func getTimestampLogicalType(t *parquet.LogicalType) string {
unit := ""
switch {
case t.TIMESTAMP.Unit.IsSetNANOS():
unit = "NANOS"
case t.TIMESTAMP.Unit.IsSetMICROS():
unit = "MICROS"
case t.TIMESTAMP.Unit.IsSetMILLIS():
unit = "MILLIS"
default:
unit = "BUG_UNKNOWN_TIMESTAMP_UNIT"
}
return fmt.Sprintf("TIMESTAMP(%s, %t)", unit, t.TIMESTAMP.IsAdjustedToUTC)
}
func getTimeLogicalType(t *parquet.LogicalType) string {
unit := ""
switch {
case t.TIME.Unit.IsSetNANOS():
unit = "NANOS"
case t.TIME.Unit.IsSetMICROS():
unit = "MICROS"
case t.TIME.Unit.IsSetMILLIS():
unit = "MILLIS"
default:
unit = "BUG_UNKNOWN_TIMESTAMP_UNIT"
}
return fmt.Sprintf("TIME(%s, %t)", unit, t.TIME.IsAdjustedToUTC)
}
func getSchemaLogicalType(t *parquet.LogicalType) string {
switch {
case t.IsSetSTRING():
return "STRING"
case t.IsSetDATE():
return "DATE"
case t.IsSetTIMESTAMP():
return getTimestampLogicalType(t)
case t.IsSetTIME():
return getTimeLogicalType(t)
case t.IsSetUUID():
return "UUID"
case t.IsSetENUM():
return "ENUM"
case t.IsSetJSON():
return "JSON"
case t.IsSetBSON():
return "BSON"
case t.IsSetDECIMAL():
return fmt.Sprintf("DECIMAL(%d, %d)", t.DECIMAL.Precision, t.DECIMAL.Scale)
case t.IsSetINTEGER():
return fmt.Sprintf("INT(%d, %t)", t.INTEGER.BitWidth, t.INTEGER.IsSigned)
default:
return "BUG(UNKNOWN)"
}
}