/** * Used by Avro reflection to instantiate this class when reading manifest files. */ public GenericDataFile(org.apache.avro.Schema avroSchema) { this.avroSchema = avroSchema; Types.StructType schema = AvroSchemaUtil.convert(avroSchema).asNestedType().asStructType(); // partition type may be null if the field was not projected Type partType = schema.fieldType("partition"); if (partType != null) { this.partitionType = partType.asNestedType().asStructType(); } else { this.partitionType = EMPTY_STRUCT_TYPE; } List<Types.NestedField> fields = schema.fields(); List<Types.NestedField> allFields = DataFile.getType(partitionType).fields(); this.fromProjectionPos = new int[fields.size()]; for (int i = 0; i < fromProjectionPos.length; i += 1) { boolean found = false; for (int j = 0; j < allFields.size(); j += 1) { if (fields.get(i).fieldId() == allFields.get(j).fieldId()) { found = true; fromProjectionPos[i] = j; } } if (!found) { throw new IllegalArgumentException("Cannot find projected field: " + fields.get(i)); } } this.partitionData = new PartitionData(partitionType); }
@Test public void testStructs() throws Exception { Types.StructType struct = Types.StructType.of( Types.NestedField.required(34, "Name!", Types.StringType.get()), Types.NestedField.optional(35, "col", Types.DecimalType.of(38, 2))); Type copy = TestHelpers.roundTripSerialize(struct); Assert.assertEquals("Struct serialization should be equal to starting type", struct, copy); Type stringType = copy.asNestedType().asStructType().fieldType("Name!"); Assert.assertSame("Struct serialization should preserve identity type", Types.StringType.get(), stringType); Type decimalType = copy.asNestedType().asStructType().field(35).type(); Assert.assertEquals("Struct serialization should support id lookup", Types.DecimalType.of(38, 2), decimalType); }