zeroshade commented on a change in pull request #10071:
URL: https://github.com/apache/arrow/pull/10071#discussion_r633676933



##########
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:
       Should I modify this to no longer auto-generate the fieldIDs? is there a 
specific reason why the C++ code won't generate them anymore?




-- 
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:
us...@infra.apache.org


Reply via email to