zeroshade commented on a change in pull request #10071: URL: https://github.com/apache/arrow/pull/10071#discussion_r623235858
########## File path: go/parquet/schema/reflection_test.go ########## @@ -0,0 +1,397 @@ +// 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_test + +import ( + "log" + "os" + "reflect" + "testing" + + "github.com/apache/arrow/go/parquet" + "github.com/apache/arrow/go/parquet/schema" + "github.com/stretchr/testify/assert" +) + +func ExampleNewSchemaFromStruct_primitives() { + type Schema struct { + Bool bool + Int8 int8 + Uint16 uint16 + Int32 int32 + Int64 int64 + Int96 parquet.Int96 + Float float32 + Double float64 + ByteArray string + FixedLenByteArray [10]byte + } + + sc, err := schema.NewSchemaFromStruct(Schema{}) + if err != nil { + log.Fatal(err) + } + + schema.PrintSchema(sc.Root(), os.Stdout, 2) + + // Output: + // repeated group field_id=-1 Schema { + // required boolean field_id=-1 Bool; + // required int32 field_id=-1 Int8 (Int(bitWidth=8, isSigned=true)); + // required int32 field_id=-1 Uint16 (Int(bitWidth=16, isSigned=false)); + // required int32 field_id=-1 Int32 (Int(bitWidth=32, isSigned=true)); + // required int64 field_id=-1 Int64 (Int(bitWidth=64, isSigned=true)); + // required int96 field_id=-1 Int96; + // required float field_id=-1 Float; + // required double field_id=-1 Double; + // required byte_array field_id=-1 ByteArray; + // required fixed_len_byte_array field_id=-1 FixedLenByteArray; + // } +} + +func ExampleNewSchemaFromStruct_convertedtypes() { + type ConvertedSchema struct { + Utf8 string `parquet:"name=utf8, converted=UTF8"` + Uint32 uint32 `parquet:"converted=INT_32"` + Date int32 `parquet:"name=date, converted=date"` + TimeMilli int32 `parquet:"name=timemilli, converted=TIME_MILLIS"` + TimeMicro int64 `parquet:"name=timemicro, converted=time_micros"` + TimeStampMilli int64 `parquet:"converted=timestamp_millis"` + TimeStampMicro int64 `parquet:"converted=timestamp_micros"` + Interval parquet.Int96 `parquet:"converted=INTERVAL"` + Decimal1 int32 `parquet:"converted=decimal, scale=2, precision=9"` + Decimal2 int64 `parquet:"converted=decimal, scale=2, precision=18"` + Decimal3 [12]byte `parquet:"converted=decimal, scale=2, precision=10"` + Decimal4 string `parquet:"converted=decimal, scale=2, precision=20"` + } + + sc, err := schema.NewSchemaFromStruct(&ConvertedSchema{}) + if err != nil { + log.Fatal(err) + } + + schema.PrintSchema(sc.Root(), os.Stdout, 2) + + // Output: + // repeated group field_id=-1 ConvertedSchema { + // required byte_array field_id=-1 utf8 (String); + // required int32 field_id=-1 Uint32 (Int(bitWidth=32, isSigned=true)); + // required int32 field_id=-1 date (Date); + // required int32 field_id=-1 timemilli (Time(isAdjustedToUTC=true, timeUnit=milliseconds)); + // required int64 field_id=-1 timemicro (Time(isAdjustedToUTC=true, timeUnit=microseconds)); + // required int64 field_id=-1 TimeStampMilli (Timestamp(isAdjustedToUTC=true, timeUnit=milliseconds, is_from_converted_type=true, force_set_converted_type=false)); + // required int64 field_id=-1 TimeStampMicro (Timestamp(isAdjustedToUTC=true, timeUnit=microseconds, is_from_converted_type=true, force_set_converted_type=false)); + // required int96 field_id=-1 Interval; + // required int32 field_id=-1 Decimal1 (Decimal(precision=9, scale=2)); + // required int64 field_id=-1 Decimal2 (Decimal(precision=18, scale=2)); + // required fixed_len_byte_array field_id=-1 Decimal3 (Decimal(precision=10, scale=2)); + // required byte_array field_id=-1 Decimal4 (Decimal(precision=20, scale=2)); + // } +} + +func ExampleNewSchemaFromStruct_repetition() { + type RepetitionSchema struct { + List []int64 `parquet:"fieldid=1"` + Repeated []int64 `parquet:"repetition=repeated, fieldid=2"` + Optional *int64 `parquet:"fieldid=3"` + Required *int64 `parquet:"repetition=REQUIRED, fieldid=4"` + Opt int64 `parquet:"repetition=OPTIONAL, fieldid=5"` + } + + sc, err := schema.NewSchemaFromStruct(RepetitionSchema{}) + if err != nil { + log.Fatal(err) + } + + schema.PrintSchema(sc.Root(), os.Stdout, 2) + + // Output: + // repeated group field_id=-1 RepetitionSchema { + // required group field_id=1 List (List) { + // repeated group field_id=-1 list { + // required int64 field_id=-1 element (Int(bitWidth=64, isSigned=true)); + // } + // } + // repeated int64 field_id=2 Repeated (Int(bitWidth=64, isSigned=true)); + // optional int64 field_id=3 Optional (Int(bitWidth=64, isSigned=true)); + // required int64 field_id=4 Required (Int(bitWidth=64, isSigned=true)); + // optional int64 field_id=5 Opt (Int(bitWidth=64, isSigned=true)); + // } +} + +func ExampleNewSchemaFromStruct_logicaltypes() { + type LogicalTypes struct { + String []byte `parquet:"logical=String"` + Enum string `parquet:"logical=enum"` + Date int32 `parquet:"logical=date"` + Decimal1 int32 `parquet:"logical=decimal, precision=9, scale=2"` + Decimal2 int32 `parquet:"logical=decimal, logical.precision=9, scale=2"` + Decimal3 int32 `parquet:"logical=decimal, precision=5, logical.precision=9, scale=1, logical.scale=3"` + TimeMilliUTC int32 `parquet:"logical=TIME, logical.unit=millis"` + TimeMilli int32 `parquet:"logical=Time, logical.unit=millis, logical.isadjustedutc=false"` + TimeMicros int64 `parquet:"logical=time, logical.unit=micros, logical.isadjustedutc=false"` + TimeMicrosUTC int64 `parquet:"logical=time, logical.unit=micros, logical.isadjustedutc=true"` + TimeNanos int64 `parquet:"logical=time, logical.unit=nanos"` + TimestampMilli int64 `parquet:"logical=timestamp, logical.unit=millis"` + TimestampMicrosNotUTC int64 `parquet:"logical=timestamp, logical.unit=micros, logical.isadjustedutc=false"` + TimestampNanos int64 `parquet:"logical=timestamp, logical.unit=nanos"` + JSON string `parquet:"logical=json"` + BSON []byte `parquet:"logical=BSON"` + UUID [16]byte `parquet:"logical=uuid"` + } + + sc, err := schema.NewSchemaFromStruct(LogicalTypes{}) + if err != nil { + log.Fatal(err) + } + + schema.PrintSchema(sc.Root(), os.Stdout, 2) + + // Output: + // repeated group field_id=-1 LogicalTypes { + // required byte_array field_id=-1 String (String); + // required byte_array field_id=-1 Enum (Enum); + // required int32 field_id=-1 Date (Date); + // required int32 field_id=-1 Decimal1 (Decimal(precision=9, scale=2)); + // required int32 field_id=-1 Decimal2 (Decimal(precision=9, scale=2)); + // required int32 field_id=-1 Decimal3 (Decimal(precision=9, scale=3)); + // required int32 field_id=-1 TimeMilliUTC (Time(isAdjustedToUTC=true, timeUnit=milliseconds)); + // required int32 field_id=-1 TimeMilli (Time(isAdjustedToUTC=false, timeUnit=milliseconds)); + // required int64 field_id=-1 TimeMicros (Time(isAdjustedToUTC=false, timeUnit=microseconds)); + // required int64 field_id=-1 TimeMicrosUTC (Time(isAdjustedToUTC=true, timeUnit=microseconds)); + // required int64 field_id=-1 TimeNanos (Time(isAdjustedToUTC=true, timeUnit=nanoseconds)); + // required int64 field_id=-1 TimestampMilli (Timestamp(isAdjustedToUTC=true, timeUnit=milliseconds, is_from_converted_type=false, force_set_converted_type=false)); + // required int64 field_id=-1 TimestampMicrosNotUTC (Timestamp(isAdjustedToUTC=false, timeUnit=microseconds, is_from_converted_type=false, force_set_converted_type=false)); + // required int64 field_id=-1 TimestampNanos (Timestamp(isAdjustedToUTC=true, timeUnit=nanoseconds, is_from_converted_type=false, force_set_converted_type=false)); + // required byte_array field_id=-1 JSON (JSON); + // required byte_array field_id=-1 BSON (BSON); + // required fixed_len_byte_array field_id=-1 UUID (UUID); + // } +} + +func ExampleNewSchemaFromStruct_physicaltype() { + type ChangeTypes struct { + Int32 int64 `parquet:"type=int32"` + FixedLen string `parquet:"type=fixed_len_byte_array, length=10"` + SliceAsFixed []byte `parquet:"type=fixed_len_byte_array, length=12"` + Int int `parquet:"type=int32"` + } + + sc, err := schema.NewSchemaFromStruct(ChangeTypes{}) + if err != nil { + log.Fatal(err) + } + + schema.PrintSchema(sc.Root(), os.Stdout, 2) + + // Output: + // repeated group field_id=-1 ChangeTypes { + // required int32 field_id=-1 Int32 (Int(bitWidth=32, isSigned=true)); + // required fixed_len_byte_array field_id=-1 FixedLen; + // required fixed_len_byte_array field_id=-1 SliceAsFixed; + // required int32 field_id=-1 Int (Int(bitWidth=32, isSigned=true)); + // } +} + +func ExampleNewSchemaFromStruct_nestedtypes() { + type Other struct { + OptionalMap *map[string]*string `parquet:"valuerepetition=required, keylogical=String, valueconverted=BSON"` + } + + type MyMap map[int32]string + + type Nested struct { + SimpleMap map[int32]string + FixedLenMap map[string][]byte `parquet:"keytype=fixed_len_byte_array, keyfieldid=10, valuefieldid=11, keylength=10"` + DecimalMap map[int32]string `parquet:"logical=map, keyconverted=DECIMAL, keyscale=3, keyprecision=7, valuetype=fixed_len_byte_array, valuelength=4, valuelogical=decimal, valuelogical.precision=9, valuescale=2"` + OtherList []*Other + OtherRepeated []Other `parquet:"repetition=repeated"` + DateArray [5]int32 `parquet:"valuelogical=date, logical=list"` + DateMap MyMap `parquet:"keylogical=TIME, keylogical.unit=MILLIS, keylogical.isadjustedutc=false, valuelogical=enum"` + } + + sc, err := schema.NewSchemaFromStruct(Nested{}) + if err != nil { + log.Fatal(err) + } + + schema.PrintSchema(sc.Root(), os.Stdout, 2) + + // Output: + // repeated group field_id=-1 Nested { Review comment: Ah gotcha. That makes sense. -- 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