emkornfield commented on a change in pull request #10071: URL: https://github.com/apache/arrow/pull/10071#discussion_r633656815
########## File path: go/parquet/schema/reflection.go ########## @@ -0,0 +1,827 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 ( + "reflect" + "strconv" + "strings" + + "github.com/apache/arrow/go/parquet" + format "github.com/apache/arrow/go/parquet/internal/gen-go/parquet" + "golang.org/x/xerrors" +) + +type taggedInfo struct { + Name string + + Type parquet.Type + KeyType parquet.Type + ValueType parquet.Type + + Length int32 + KeyLength int32 + ValueLength int32 + + Scale int32 + KeyScale int32 + ValueScale int32 + + Precision int32 + KeyPrecision int32 + ValuePrecision int32 + + FieldID int32 + KeyFieldID int32 + ValueFieldID int32 + + RepetitionType parquet.Repetition + ValueRepetition parquet.Repetition + + Converted ConvertedType + KeyConverted ConvertedType + ValueConverted ConvertedType + + LogicalFields map[string]string + KeyLogicalFields map[string]string + ValueLogicalFields map[string]string + + LogicalType LogicalType + KeyLogicalType LogicalType + ValueLogicalType LogicalType +} + +func (t *taggedInfo) CopyForKey() (ret taggedInfo) { + ret = *t + ret.Type = t.KeyType + ret.Length = t.KeyLength + ret.Scale = t.KeyScale + ret.Precision = t.KeyPrecision + ret.FieldID = t.KeyFieldID + ret.RepetitionType = parquet.Repetitions.Required + ret.Converted = t.KeyConverted + ret.LogicalType = t.KeyLogicalType + return +} + +func (t *taggedInfo) CopyForValue() (ret taggedInfo) { + ret = *t + ret.Type = t.ValueType + ret.Length = t.ValueLength + ret.Scale = t.ValueScale + ret.Precision = t.ValuePrecision + ret.FieldID = t.ValueFieldID + ret.RepetitionType = t.ValueRepetition + ret.Converted = t.ValueConverted + ret.LogicalType = t.ValueLogicalType + return +} + +func (t *taggedInfo) UpdateLogicalTypes() { + processLogicalType := func(fields map[string]string, precision, scale int32) LogicalType { + t, ok := fields["type"] + if !ok { + return NoLogicalType{} + } + + switch strings.ToLower(t) { + case "string": + return StringLogicalType{} + case "map": + return MapLogicalType{} + case "list": + return ListLogicalType{} + case "enum": + return EnumLogicalType{} + case "decimal": + if v, ok := fields["precision"]; ok { + precision = int32FromType(v) + } + if v, ok := fields["scale"]; ok { + scale = int32FromType(v) + } + return NewDecimalLogicalType(precision, scale) + case "date": + return DateLogicalType{} + case "time": + unit, ok := fields["unit"] + if !ok { + panic("must specify unit for time logical type") + } + adjustedToUtc, ok := fields["isadjustedutc"] + if !ok { + adjustedToUtc = "true" + } + return NewTimeLogicalType(boolFromStr(adjustedToUtc), timeUnitFromString(strings.ToLower(unit))) + case "timestamp": + unit, ok := fields["unit"] + if !ok { + panic("must specify unit for time logical type") + } + adjustedToUtc, ok := fields["isadjustedutc"] + if !ok { + adjustedToUtc = "true" + } + return NewTimestampLogicalType(boolFromStr(adjustedToUtc), timeUnitFromString(unit)) + case "integer": + width, ok := fields["bitwidth"] + if !ok { + panic("must specify bitwidth if explicitly setting integer logical type") + } + signed, ok := fields["signed"] + if !ok { + signed = "true" + } + + return NewIntLogicalType(int8(int32FromType(width)), boolFromStr(signed)) + case "null": + return NullLogicalType{} + case "json": + return JSONLogicalType{} + case "bson": + return BSONLogicalType{} + case "uuid": + return UUIDLogicalType{} + default: + panic(xerrors.Errorf("invalid logical type specified: %s", t)) + } + } + + t.LogicalType = processLogicalType(t.LogicalFields, t.Precision, t.Scale) + t.KeyLogicalType = processLogicalType(t.KeyLogicalFields, t.KeyPrecision, t.KeyScale) + t.ValueLogicalType = processLogicalType(t.ValueLogicalFields, t.ValuePrecision, t.ValueScale) +} + +func newTaggedInfo() taggedInfo { + return taggedInfo{ + Type: parquet.Types.Undefined, + KeyType: parquet.Types.Undefined, + ValueType: parquet.Types.Undefined, + RepetitionType: parquet.Repetitions.Undefined, + ValueRepetition: parquet.Repetitions.Undefined, + Converted: ConvertedTypes.NA, + KeyConverted: ConvertedTypes.NA, + ValueConverted: ConvertedTypes.NA, + FieldID: -1, + KeyFieldID: -1, + ValueFieldID: -1, + LogicalFields: make(map[string]string), + KeyLogicalFields: make(map[string]string), + ValueLogicalFields: make(map[string]string), + LogicalType: NoLogicalType{}, + KeyLogicalType: NoLogicalType{}, + ValueLogicalType: NoLogicalType{}, + } +} + +var int32FromType = func(v string) int32 { + val, err := strconv.Atoi(v) + if err != nil { + panic(err) + } + return int32(val) +} + +var boolFromStr = func(v string) bool { + val, err := strconv.ParseBool(v) + if err != nil { + panic(err) + } + return val +} + +func infoFromTags(f reflect.StructTag) *taggedInfo { + typeFromStr := func(v string) parquet.Type { + t, err := format.TypeFromString(strings.ToUpper(v)) + if err != nil { + panic(xerrors.Errorf("invalid type specified: %s", v)) + } + return parquet.Type(t) + } + + repFromStr := func(v string) parquet.Repetition { + r, err := format.FieldRepetitionTypeFromString(strings.ToUpper(v)) + if err != nil { + panic(err) + } + return parquet.Repetition(r) + } + + convertedFromStr := func(v string) ConvertedType { + c, err := format.ConvertedTypeFromString(strings.ToUpper(v)) + if err != nil { + panic(err) + } + return ConvertedType(c) + } + + if ptags, ok := f.Lookup("parquet"); ok { + info := newTaggedInfo() + for _, tag := range strings.Split(strings.Replace(ptags, "\t", "", -1), ",") { + tag = strings.TrimSpace(tag) + kv := strings.SplitN(tag, "=", 2) + key := strings.TrimSpace(strings.ToLower(kv[0])) + value := strings.TrimSpace(kv[1]) + + switch key { + case "name": + info.Name = value + case "type": + info.Type = typeFromStr(value) + case "keytype": + info.KeyType = typeFromStr(value) + case "valuetype": + info.ValueType = typeFromStr(value) + case "length": + info.Length = int32FromType(value) + case "keylength": + info.KeyLength = int32FromType(value) + case "valuelength": + info.ValueLength = int32FromType(value) + case "scale": + info.Scale = int32FromType(value) + case "keyscale": + info.KeyScale = int32FromType(value) + case "valuescale": + info.ValueScale = int32FromType(value) + case "precision": + info.Precision = int32FromType(value) + case "keyprecision": + info.KeyPrecision = int32FromType(value) + case "valueprecision": + info.ValuePrecision = int32FromType(value) + case "fieldid": + info.FieldID = int32FromType(value) + case "keyfieldid": + info.KeyFieldID = int32FromType(value) + case "valuefieldid": + info.ValueFieldID = int32FromType(value) + case "repetition": + info.RepetitionType = repFromStr(value) + case "valuerepetition": + info.ValueRepetition = repFromStr(value) + case "converted": + info.Converted = convertedFromStr(value) + case "keyconverted": + info.KeyConverted = convertedFromStr(value) + case "valueconverted": + info.ValueConverted = convertedFromStr(value) + case "logical": + info.LogicalFields["type"] = value + case "keylogical": + info.KeyLogicalFields["type"] = value + case "valuelogical": + info.ValueLogicalFields["type"] = value + default: + switch { + case strings.HasPrefix(key, "logical."): + info.LogicalFields[strings.TrimPrefix(key, "logical.")] = value + case strings.HasPrefix(key, "keylogical."): + info.KeyLogicalFields[strings.TrimPrefix(key, "keylogical.")] = value + case strings.HasPrefix(key, "valuelogical."): + info.ValueLogicalFields[strings.TrimPrefix(key, "valuelogical.")] = value + } + } + } + info.UpdateLogicalTypes() + return &info + } + return nil +} + +// typeToNode recurseively converts a physical type and the tag info into parquet Nodes +// +// to avoid having to propagate errors up potentially high numbers of recursive calls +// we use panics and then recover in the public function NewSchemaFromStruct so that a +// failure very far down the stack quickly unwinds. +func typeToNode(name string, typ reflect.Type, repType parquet.Repetition, info *taggedInfo) Node { + // set up our default values for everything + var ( + converted = ConvertedTypes.None + logical LogicalType = NoLogicalType{} + fieldID = int32(-1) + physical = parquet.Types.Undefined + typeLen = 0 + precision = 0 + scale = 0 + ) + if info != nil { // we have struct tag info to process + fieldID = info.FieldID Review comment: just a note there has been a recent issue/PR that is changing the logic around fieldID (c++ code will not generate them any more) ########## File path: go/parquet/schema/node.go ########## @@ -0,0 +1,625 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 ( + "github.com/apache/arrow/go/parquet" + format "github.com/apache/arrow/go/parquet/internal/gen-go/parquet" + "github.com/apache/thrift/lib/go/thrift" + "golang.org/x/xerrors" +) + +// NodeType describes whether the Node is a Primitive or Group node +type NodeType int + +// the available constants for NodeType +const ( + Primitive NodeType = iota + Group +) + +// Node is the interface for both Group and Primitive Nodes. +// A logical schema type has a name, repetition level, and optionally +// a logical type (converted type is the deprecated version of the logical +// type concept, which is maintained for forward compatibility) +type Node interface { + Name() string + Type() NodeType + RepetitionType() parquet.Repetition + ConvertedType() ConvertedType + LogicalType() LogicalType + FieldID() int32 + Parent() Node + SetParent(Node) + Path() string + Equals(Node) bool + Visit(v Visitor) + toThrift() *format.SchemaElement +} + +// Visitor is an interface for creating functionality to walk the schema tree. +// +// A visitor can be passed to the Visit function of a Node in order to walk +// the tree. VisitPre is called the first time a node is encountered. If +// it is a group node, the return is checked and if it is false, the children +// will be skipped. +// +// VisitPost is called after visiting any children +type Visitor interface { + VisitPre(Node) bool + VisitPost(Node) +} + +// ColumnPathFromNode walks the parents of the given node to construct it's +// column path +func ColumnPathFromNode(n Node) parquet.ColumnPath { + if n == nil { + return nil + } + + c := make([]string, 0) + + // build the path in reverse order as we traverse nodes to the top + cursor := n + for cursor.Parent() != nil { + c = append(c, cursor.Name()) + cursor = cursor.Parent() + } + + // reverse the order of the list in place so that our result + // is in the proper, correct order. + for i := len(c)/2 - 1; i >= 0; i-- { Review comment: go doesn't have a built in reverse function? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
