zeroshade commented on a change in pull request #10071: URL: https://github.com/apache/arrow/pull/10071#discussion_r619923938
########## File path: go/parquet/schema/schema_element_test.go ########## @@ -0,0 +1,432 @@ +// 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 ( + "testing" + + "github.com/apache/arrow/go/parquet" + format "github.com/apache/arrow/go/parquet/internal/gen-go/parquet" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" +) + +type schemaElementConstruction struct { + node Node + element *format.SchemaElement + name string + expectConverted bool + converted ConvertedType + expectLogical bool + checkLogical func(*format.SchemaElement) bool +} + +type decimalSchemaElementConstruction struct { + schemaElementConstruction + precision int + scale int +} + +type temporalSchemaElementConstruction struct { + schemaElementConstruction + adjusted bool + unit TimeUnitType + getUnit func(*format.SchemaElement) *format.TimeUnit +} + +type intSchemaElementConstruction struct { + schemaElementConstruction + width int8 + signed bool +} + +type legacySchemaElementConstructArgs struct { + name string + physical parquet.Type + len int + expectConverted bool + converted ConvertedType + expectLogical bool + checkLogical func(*format.SchemaElement) bool +} + +type schemaElementConstructArgs struct { + name string + logical LogicalType + physical parquet.Type + len int + expectConverted bool + converted ConvertedType + expectLogical bool + checkLogical func(*format.SchemaElement) bool +} +type SchemaElementConstructionSuite struct { + suite.Suite +} + +func (s *SchemaElementConstructionSuite) reconstruct(c schemaElementConstructArgs) *schemaElementConstruction { + ret := &schemaElementConstruction{ + node: NewPrimitiveNodeLogical(c.name, parquet.Repetitions.Required, c.logical, c.physical, c.len, -1), + name: c.name, + expectConverted: c.expectConverted, + converted: c.converted, + expectLogical: c.expectLogical, + checkLogical: c.checkLogical, + } + ret.element = ret.node.toThrift() + return ret +} + +func (s *SchemaElementConstructionSuite) legacyReconstruct(c legacySchemaElementConstructArgs) *schemaElementConstruction { + ret := &schemaElementConstruction{ + node: NewPrimitiveNodeConverted(c.name, parquet.Repetitions.Required, c.physical, c.converted, c.len, 0, 0, -1), + name: c.name, + expectConverted: c.expectConverted, + converted: c.converted, + expectLogical: c.expectLogical, + checkLogical: c.checkLogical, + } + ret.element = ret.node.toThrift() + return ret +} + +func (s *SchemaElementConstructionSuite) inspect(c *schemaElementConstruction) { + if c.expectConverted { + s.True(c.element.IsSetConvertedType()) + s.Equal(c.converted, ConvertedType(*c.element.ConvertedType)) + } else { + s.False(c.element.IsSetConvertedType()) + } + if c.expectLogical { + s.True(c.element.IsSetLogicalType()) + s.True(c.checkLogical(c.element)) + } else { + s.False(c.element.IsSetLogicalType()) + } +} + +func (s *SchemaElementConstructionSuite) TestSimple() { + checkNone := func(*format.SchemaElement) bool { return true } + + tests := []struct { + name string + args *schemaElementConstructArgs + legacy *legacySchemaElementConstructArgs + }{ + {"string", &schemaElementConstructArgs{ + "string", StringLogicalType{}, parquet.Types.ByteArray, -1, true, ConvertedTypes.UTF8, true, + func(e *format.SchemaElement) bool { return e.LogicalType.IsSetSTRING() }, + }, nil}, + {"enum", &schemaElementConstructArgs{ + "enum", EnumLogicalType{}, parquet.Types.ByteArray, -1, true, ConvertedTypes.Enum, true, + func(e *format.SchemaElement) bool { return e.LogicalType.IsSetENUM() }, + }, nil}, + {"date", &schemaElementConstructArgs{ + "date", DateLogicalType{}, parquet.Types.Int32, -1, true, ConvertedTypes.Date, true, + func(e *format.SchemaElement) bool { return e.LogicalType.IsSetDATE() }, + }, nil}, + {"interval", &schemaElementConstructArgs{ + "interval", IntervalLogicalType{}, parquet.Types.FixedLenByteArray, 12, true, ConvertedTypes.Interval, false, + checkNone, + }, nil}, + {"null", &schemaElementConstructArgs{ + "null", NullLogicalType{}, parquet.Types.Double, -1, false, ConvertedTypes.NA, true, + func(e *format.SchemaElement) bool { return e.LogicalType.IsSetUNKNOWN() }, + }, nil}, + {"json", &schemaElementConstructArgs{ + "json", JSONLogicalType{}, parquet.Types.ByteArray, -1, true, ConvertedTypes.JSON, true, + func(e *format.SchemaElement) bool { return e.LogicalType.IsSetJSON() }, + }, nil}, + {"bson", &schemaElementConstructArgs{ + "bson", BSONLogicalType{}, parquet.Types.ByteArray, -1, true, ConvertedTypes.BSON, true, + func(e *format.SchemaElement) bool { return e.LogicalType.IsSetBSON() }, + }, nil}, + {"uuid", &schemaElementConstructArgs{ + "uuid", UUIDLogicalType{}, parquet.Types.FixedLenByteArray, 16, false, ConvertedTypes.NA, true, + func(e *format.SchemaElement) bool { return e.LogicalType.IsSetUUID() }, + }, nil}, + {"none", &schemaElementConstructArgs{ + "none", NoLogicalType{}, parquet.Types.Int64, -1, false, ConvertedTypes.NA, false, + checkNone, + }, nil}, + {"unknown", &schemaElementConstructArgs{ + "unknown", UnknownLogicalType{}, parquet.Types.Int64, -1, true, ConvertedTypes.NA, false, + checkNone, + }, nil}, + {"timestamp_ms", nil, &legacySchemaElementConstructArgs{ + "timestamp_ms", parquet.Types.Int64, -1, true, ConvertedTypes.TimestampMillis, false, checkNone}}, + {"timestamp_us", nil, &legacySchemaElementConstructArgs{ + "timestamp_us", parquet.Types.Int64, -1, true, ConvertedTypes.TimestampMicros, false, checkNone}}, + } + for _, tt := range tests { + s.Run(tt.name, func() { + var sc *schemaElementConstruction + if tt.args != nil { + sc = s.reconstruct(*tt.args) + } else { + sc = s.legacyReconstruct(*tt.legacy) + } + s.Equal(tt.name, sc.element.Name) + s.inspect(sc) + }) + } +} + +func (s *SchemaElementConstructionSuite) reconstructDecimal(c schemaElementConstructArgs) *decimalSchemaElementConstruction { + ret := s.reconstruct(c) + dec := c.logical.(*DecimalLogicalType) + return &decimalSchemaElementConstruction{*ret, int(dec.Precision()), int(dec.Scale())} +} + +func (s *SchemaElementConstructionSuite) inspectDecimal(d *decimalSchemaElementConstruction) { + s.inspect(&d.schemaElementConstruction) + s.EqualValues(d.precision, d.element.GetPrecision()) + s.EqualValues(d.scale, d.element.GetScale()) + s.EqualValues(d.precision, d.element.LogicalType.DECIMAL.Precision) + s.EqualValues(d.scale, d.element.LogicalType.DECIMAL.Scale) +} + +func (s *SchemaElementConstructionSuite) TestDecimal() { + checkDecimal := func(p *format.SchemaElement) bool { return p.LogicalType.IsSetDECIMAL() } + + tests := []schemaElementConstructArgs{ + {"decimal16_6", NewDecimalLogicalType(16, 6), parquet.Types.Int64, -1, true, ConvertedTypes.Decimal, true, checkDecimal}, + {"decimal1_0", NewDecimalLogicalType(1, 0), parquet.Types.Int32, -1, true, ConvertedTypes.Decimal, true, checkDecimal}, + {"decimal10", NewDecimalLogicalType(10, 0), parquet.Types.Int64, -1, true, ConvertedTypes.Decimal, true, checkDecimal}, Review comment: added ########## File path: go/parquet/schema/schema_flatten_test.go ########## @@ -0,0 +1,150 @@ +// 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 ( + "testing" + + "github.com/apache/arrow/go/parquet" + format "github.com/apache/arrow/go/parquet/internal/gen-go/parquet" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" +) + +func NewPrimitive(name string, repetition format.FieldRepetitionType, typ format.Type, fieldID int32) *format.SchemaElement { + ret := &format.SchemaElement{ + Name: name, + RepetitionType: format.FieldRepetitionTypePtr(repetition), + Type: format.TypePtr(typ), + } + if fieldID >= 0 { + ret.FieldID = &fieldID + } + return ret +} + +func NewGroup(name string, repetition format.FieldRepetitionType, numChildren, fieldID int32) *format.SchemaElement { + ret := &format.SchemaElement{ + Name: name, + RepetitionType: format.FieldRepetitionTypePtr(repetition), + NumChildren: &numChildren, + } + if fieldID >= 0 { + ret.FieldID = &fieldID + } + return ret +} + +type SchemaFlattenSuite struct { + suite.Suite + + name string +} + +func (s *SchemaFlattenSuite) SetupSuite() { + s.name = "parquet_schema" +} + +func (s *SchemaFlattenSuite) TestDecimalMetadata() { + group := NewGroupNodeConverted("group", parquet.Repetitions.Repeated, FieldList{ + NewPrimitiveNodeConverted("decimal", parquet.Repetitions.Required, parquet.Types.Int64, ConvertedTypes.Decimal, 0, 8, 4, -1), Review comment: done -- 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]
