Java Code Examples for org.apache.avro.generic.GenericData.Record#get()

The following examples show how to use org.apache.avro.generic.GenericData.Record#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestHelpers.java    From iceberg with Apache License 2.0 6 votes vote down vote up
public static void assertEqualsBatch(Types.StructType struct, Iterator<Record> expected, ColumnarBatch batch,
                                     boolean checkArrowValidityVector) {
  for (int rowId = 0; rowId < batch.numRows(); rowId++) {
    List<Types.NestedField> fields = struct.fields();
    InternalRow row = batch.getRow(rowId);
    Record rec = expected.next();
    for (int i = 0; i < fields.size(); i += 1) {
      Type fieldType = fields.get(i).type();
      Object expectedValue = rec.get(i);
      Object actualValue = row.isNullAt(i) ? null : row.get(i, convert(fieldType));
      assertEqualsUnsafe(fieldType, expectedValue, actualValue);

      if (checkArrowValidityVector) {
        ColumnVector columnVector = batch.column(i);
        ValueVector arrowVector = ((IcebergArrowColumnVector) columnVector).vectorAccessor().getVector();
        Assert.assertEquals("Nullability doesn't match", expectedValue == null, arrowVector.isNull(rowId));
      }
    }
  }
}
 
Example 2
Source File: AvroConverterTest.java    From bunsen with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiReferenceTypes() {

  Record practitioner = (Record) ((List) avroPatient.get("generalPractitioner")).get(0);

  String organizationId = (String) practitioner.get("OrganizationId");
  String practitionerId = (String) practitioner.get("PractitionerId");

  // The reference is not of this type, so the field should be null.
  Assert.assertNull(organizationId);

  // The field with the expected prefix should match the original data.
  Assert.assertEquals(testPatient.getGeneralPractitionerFirstRep().getReference(),
      "Practitioner/" + practitionerId);

  Assert.assertEquals(testCondition.getSubject().getReference(),
      testConditionDecoded.getSubject().getReference());
}
 
Example 3
Source File: TestReadProjection.java    From iceberg with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmptyProjection() throws Exception {
  Schema schema = new Schema(
      Types.NestedField.required(0, "id", Types.LongType.get()),
      Types.NestedField.optional(1, "data", Types.StringType.get())
  );

  Record record = new Record(AvroSchemaUtil.convert(schema, "table"));
  record.put("id", 34L);
  record.put("data", "test");

  Record projected = writeAndRead("empty_projection", schema, schema.select(), record);

  Assert.assertNotNull("Should read a non-null record", projected);
  try {
    projected.get(0);
    Assert.fail("Should not retrieve value with ordinal 0");
  } catch (ArrayIndexOutOfBoundsException e) {
    // this is expected because there are no values
  }
}
 
Example 4
Source File: TestConvertAvroSchema.java    From nifi with Apache License 2.0 6 votes vote down vote up
private Record convertBasic(Record inputRecord, Locale locale) {
    Record result = new Record(OUTPUT_SCHEMA);
    result.put("id", Long.parseLong(inputRecord.get("id").toString()));
    result.put("color", inputRecord.get("primaryColor").toString());
    if (inputRecord.get("price") == null) {
        result.put("price", null);
    } else {
        final NumberFormat format = NumberFormat.getInstance(locale);
        double price;
        try {
            price = format.parse(inputRecord.get("price").toString()).doubleValue();
        } catch (ParseException e) {
            // Shouldn't happen
            throw new RuntimeException(e);
        }
        result.put("price", price);
    }
    return result;
}
 
Example 5
Source File: TestReadProjection.java    From iceberg with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmptyProjection() throws Exception {
  Schema schema = new Schema(
      Types.NestedField.required(0, "id", Types.LongType.get()),
      Types.NestedField.optional(1, "data", Types.StringType.get())
  );

  Record record = new Record(AvroSchemaUtil.convert(schema, "table"));
  record.put("id", 34L);
  record.put("data", "test");

  Record projected = writeAndRead("empty_projection", schema, schema.select(), record);

  Assert.assertNotNull("Should read a non-null record", projected);
  try {
    projected.get(0);
    Assert.fail("Should not retrieve value with ordinal 0");
  } catch (ArrayIndexOutOfBoundsException e) {
    // this is expected because there are no values
  }
}
 
Example 6
Source File: TestConvertAvroSchema.java    From nifi with Apache License 2.0 5 votes vote down vote up
private Record convertNested(Record inputRecord) {
    Record result = new Record(
            TestAvroRecordConverter.UNNESTED_OUTPUT_SCHEMA);
    result.put("l1", inputRecord.get("l1"));
    result.put("s1", Long.parseLong(inputRecord.get("s1").toString()));
    if (inputRecord.get("parent") != null) {
        // output schema doesn't have parent name.
        result.put("parentId",
                ((Record) inputRecord.get("parent")).get("id"));
    }
    return result;
}
 
Example 7
Source File: AvroTestHelpers.java    From iceberg with Apache License 2.0 5 votes vote down vote up
static void assertEquals(Types.StructType struct, Record expected, Record actual) {
  List<Types.NestedField> fields = struct.fields();
  for (int i = 0; i < fields.size(); i += 1) {
    Type fieldType = fields.get(i).type();

    Object expectedValue = expected.get(i);
    Object actualValue = actual.get(i);

    assertEquals(fieldType, expectedValue, actualValue);
  }
}
 
Example 8
Source File: TestAvroTypeUtil.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * The issue consists on having an Avro's schema with a default value in an
 * array. See
 * <a href="https://issues.apache.org/jira/browse/NIFI-4893">NIFI-4893</a>.
 * @throws IOException
 *             schema not found.
 */
@Test
public void testDefaultArrayValue1() throws IOException {
    Schema avroSchema = new Schema.Parser().parse(getClass().getResourceAsStream("defaultArrayValue1.json"));
    GenericRecordBuilder builder = new GenericRecordBuilder(avroSchema);
    Record r = builder.build();
    @SuppressWarnings("unchecked")
    GenericData.Array<Integer> values = (GenericData.Array<Integer>) r.get("listOfInt");
    assertEquals(values.size(), 0);
    RecordSchema record = AvroTypeUtil.createSchema(avroSchema);
    RecordField field = record.getField("listOfInt").get();
    assertEquals(RecordFieldType.ARRAY, field.getDataType().getFieldType());
    assertTrue(field.getDefaultValue() instanceof Object[]);
    assertEquals(0, ((Object[]) field.getDefaultValue()).length);
}
 
Example 9
Source File: TestHelpers.java    From iceberg with Apache License 2.0 5 votes vote down vote up
public static void assertEqualsUnsafe(Types.StructType struct, Record rec, InternalRow row) {
  List<Types.NestedField> fields = struct.fields();
  for (int i = 0; i < fields.size(); i += 1) {
    Type fieldType = fields.get(i).type();

    Object expectedValue = rec.get(i);
    Object actualValue = row.get(i, convert(fieldType));

    assertEqualsUnsafe(fieldType, expectedValue, actualValue);
  }
}
 
Example 10
Source File: TestHelpers.java    From iceberg with Apache License 2.0 5 votes vote down vote up
public static void assertEqualsSafe(Types.StructType struct, Record rec, Row row) {
  List<Types.NestedField> fields = struct.fields();
  for (int i = 0; i < fields.size(); i += 1) {
    Type fieldType = fields.get(i).type();

    Object expectedValue = rec.get(i);
    Object actualValue = row.get(i);

    assertEqualsSafe(fieldType, expectedValue, actualValue);
  }
}
 
Example 11
Source File: TestHelpers.java    From iceberg with Apache License 2.0 5 votes vote down vote up
public static void assertEqualsSafe(Types.StructType struct, Record rec, Row row) {
  List<Types.NestedField> fields = struct.fields();
  for (int i = 0; i < fields.size(); i += 1) {
    Type fieldType = fields.get(i).type();

    Object expectedValue = rec.get(i);
    Object actualValue = row.get(i);

    assertEqualsSafe(fieldType, expectedValue, actualValue);
  }
}
 
Example 12
Source File: AvroTestHelpers.java    From iceberg with Apache License 2.0 5 votes vote down vote up
static void assertEquals(Types.StructType struct, Record expected, Record actual) {
  List<Types.NestedField> fields = struct.fields();
  for (int i = 0; i < fields.size(); i += 1) {
    Type fieldType = fields.get(i).type();

    Object expectedValue = expected.get(i);
    Object actualValue = actual.get(i);

    assertEquals(fieldType, expectedValue, actualValue);
  }
}
 
Example 13
Source File: KafkaAvroMessageDecoder.java    From HiveKa with Apache License 2.0 5 votes vote down vote up
public CamusAvroWrapper(Record record) {
    super(record);
    Record header = (Record) super.getRecord().get("header");
    if (header != null) {
       if (header.get("server") != null) {
           put(new Text("server"), new Text(header.get("server").toString()));
       }
       if (header.get("service") != null) {
           put(new Text("service"), new Text(header.get("service").toString()));
       }
    }
}
 
Example 14
Source File: AvroConverterTest.java    From bunsen with Apache License 2.0 4 votes vote down vote up
@Test
public void testNestedExtension() {

  Extension testEthnicity = testPatient
      .getExtensionsByUrl(TestData.US_CORE_ETHNICITY)
      .get(0);

  Coding testOmbCategory = (Coding) testEthnicity
      .getExtensionsByUrl("ombCategory")
      .get(0)
      .getValue();

  Coding testDetailed1 = (Coding) testEthnicity
      .getExtensionsByUrl("detailed")
      .get(0)
      .getValue();

  Coding testDetailed2 = (Coding) testEthnicity
      .getExtensionsByUrl("detailed")
      .get(1)
      .getValue();

  String testText = testEthnicity
      .getExtensionsByUrl("text")
      .get(0)
      .getValueAsPrimitive()
      .getValueAsString();

  Extension decodedEthnicity = testPatientDecoded
      .getExtensionsByUrl(TestData.US_CORE_ETHNICITY)
      .get(0);

  Coding decodedOmbCategory = (Coding) decodedEthnicity
      .getExtensionsByUrl("ombCategory")
      .get(0)
      .getValue();

  Coding decodedDetailed1 = (Coding) decodedEthnicity
      .getExtensionsByUrl("detailed")
      .get(0)
      .getValue();

  Coding decodedDetailed2 = (Coding) decodedEthnicity
      .getExtensionsByUrl("detailed")
      .get(1)
      .getValue();

  String decodedText = decodedEthnicity
      .getExtensionsByUrl("text")
      .get(0)
      .getValueAsPrimitive()
      .getValueAsString();

  Assert.assertTrue(testOmbCategory.equalsDeep(decodedOmbCategory));
  Assert.assertTrue(testDetailed1.equalsDeep(decodedDetailed1));
  Assert.assertTrue(testDetailed2.equalsDeep(decodedDetailed2));
  Assert.assertEquals(testText, decodedText);

  Record ethnicityRecord = (Record) avroPatient.get("ethnicity");

  Record ombCategoryRecord =  (Record) ethnicityRecord.get("ombCategory");

  List<Record> detailedRecord =  (List<Record>) ethnicityRecord.get("detailed");

  Assert.assertEquals(testOmbCategory.getSystem(), ombCategoryRecord.get("system"));
  Assert.assertEquals(testOmbCategory.getCode(), ombCategoryRecord.get("code"));
  Assert.assertEquals(testOmbCategory.getDisplay(), ombCategoryRecord.get("display"));

  Assert.assertEquals(testDetailed1.getSystem(), detailedRecord.get(0).get("system"));
  Assert.assertEquals(testDetailed1.getCode(), detailedRecord.get(0).get("code"));
  Assert.assertEquals(testDetailed1.getDisplay(), detailedRecord.get(0).get("display"));

  Assert.assertEquals(testDetailed2.getSystem(), detailedRecord.get(1).get("system"));
  Assert.assertEquals(testDetailed2.getCode(), detailedRecord.get(1).get("code"));
  Assert.assertEquals(testDetailed2.getDisplay(), detailedRecord.get(1).get("display"));

  Assert.assertEquals(testText, ethnicityRecord.get("text"));
}
 
Example 15
Source File: TestReadProjection.java    From iceberg with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testListOfStructsProjection() throws IOException {
  Schema writeSchema = new Schema(
      Types.NestedField.required(0, "id", Types.LongType.get()),
      Types.NestedField.optional(22, "points",
          Types.ListType.ofOptional(21, Types.StructType.of(
              Types.NestedField.required(19, "x", Types.IntegerType.get()),
              Types.NestedField.optional(18, "y", Types.IntegerType.get())
          ))
      )
  );

  Record record = new Record(AvroSchemaUtil.convert(writeSchema, "table"));
  record.put("id", 34L);
  Record p1 = new Record(AvroSchemaUtil.fromOption(
      AvroSchemaUtil.fromOption(record.getSchema().getField("points").schema())
          .getElementType()));
  p1.put("x", 1);
  p1.put("y", 2);
  Record p2 = new Record(p1.getSchema());
  p2.put("x", 3);
  p2.put("y", null);
  record.put("points", ImmutableList.of(p1, p2));

  Schema idOnly = new Schema(
      Types.NestedField.required(0, "id", Types.LongType.get())
  );

  Record projected = writeAndRead("id_only", writeSchema, idOnly, record);
  Assert.assertEquals("Should contain the correct id value", 34L, (long) projected.get("id"));
  Assert.assertNull("Should not project points list", projected.get("points"));

  projected = writeAndRead("all_points", writeSchema, writeSchema.select("points"), record);
  Assert.assertNull("Should not project id", projected.get("id"));
  Assert.assertEquals("Should project points list",
      record.get("points"), projected.get("points"));

  projected = writeAndRead("x_only", writeSchema, writeSchema.select("points.x"), record);
  Assert.assertNull("Should not project id", projected.get("id"));
  Assert.assertNotNull("Should project points list", projected.get("points"));
  List<Record> points = (List<Record>) projected.get("points");
  Assert.assertEquals("Should read 2 points", 2, points.size());
  Record projectedP1 = points.get(0);
  Assert.assertEquals("Should project x", 1, (int) projectedP1.get("x"));
  Assert.assertNull("Should not project y", projectedP1.get("y"));
  Record projectedP2 = points.get(1);
  Assert.assertEquals("Should project x", 3, (int) projectedP2.get("x"));
  Assert.assertNull("Should not project y", projectedP2.get("y"));

  projected = writeAndRead("y_only", writeSchema, writeSchema.select("points.y"), record);
  Assert.assertNull("Should not project id", projected.get("id"));
  Assert.assertNotNull("Should project points list", projected.get("points"));
  points = (List<Record>) projected.get("points");
  Assert.assertEquals("Should read 2 points", 2, points.size());
  projectedP1 = points.get(0);
  Assert.assertNull("Should not project x", projectedP1.get("x"));
  Assert.assertEquals("Should project y", 2, (int) projectedP1.get("y"));
  projectedP2 = points.get(1);
  Assert.assertNull("Should not project x", projectedP2.get("x"));
  Assert.assertEquals("Should project null y", null, projectedP2.get("y"));

  Schema yRenamed = new Schema(
      Types.NestedField.optional(22, "points",
          Types.ListType.ofOptional(21, Types.StructType.of(
              Types.NestedField.optional(18, "z", Types.IntegerType.get())
          ))
      )
  );

  projected = writeAndRead("y_renamed", writeSchema, yRenamed, record);
  Assert.assertNull("Should not project id", projected.get("id"));
  Assert.assertNotNull("Should project points list", projected.get("points"));
  points = (List<Record>) projected.get("points");
  Assert.assertEquals("Should read 2 points", 2, points.size());
  projectedP1 = points.get(0);
  Assert.assertNull("Should not project x", projectedP1.get("x"));
  Assert.assertNull("Should not project y", projectedP1.get("y"));
  Assert.assertEquals("Should project z", 2, (int) projectedP1.get("z"));
  projectedP2 = points.get(1);
  Assert.assertNull("Should not project x", projectedP2.get("x"));
  Assert.assertNull("Should not project y", projectedP2.get("y"));
  Assert.assertEquals("Should project null z", null, projectedP2.get("z"));
}
 
Example 16
Source File: AvroConverterTest.java    From bunsen with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultiNestedExtension() {

  final Extension nestedExtension1 = testBunsenTestProfilePatient
      .getExtensionsByUrl(TestData.BUNSEN_TEST_NESTED_EXT_FIELD)
      .get(0);

  final Extension nestedExtension2 = testBunsenTestProfilePatient
      .getExtensionsByUrl(TestData.BUNSEN_TEST_NESTED_EXT_FIELD)
      .get(1);

  String text1 = nestedExtension1.getExtensionsByUrl("text")
      .get(0).getValueAsPrimitive().getValueAsString();

  String text2 = nestedExtension1.getExtensionsByUrl("text")
      .get(1).getValueAsPrimitive().getValueAsString();

  String text3 = nestedExtension2.getExtensionsByUrl("text")
      .get(0).getValueAsPrimitive().getValueAsString();

  CodeableConcept codeableConcept1 = (CodeableConcept) nestedExtension1
      .getExtensionsByUrl(TestData.BUNSEN_TEST_CODEABLE_CONCEPT_EXT_FIELD)
      .get(0).getValue();

  CodeableConcept codeableConcept2 = (CodeableConcept) nestedExtension1
      .getExtensionsByUrl(TestData.BUNSEN_TEST_CODEABLE_CONCEPT_EXT_FIELD)
      .get(1).getValue();

  CodeableConcept codeableConcept3 = (CodeableConcept) nestedExtension2
      .getExtensionsByUrl(TestData.BUNSEN_TEST_CODEABLE_CONCEPT_EXT_FIELD)
      .get(0).getValue();

  final Extension decodedNestedExtension1 = testBunsenTestProfilePatientDecoded
      .getExtensionsByUrl(TestData.BUNSEN_TEST_NESTED_EXT_FIELD)
      .get(0);

  final Extension decodedNestedExtension2 = testBunsenTestProfilePatientDecoded
      .getExtensionsByUrl(TestData.BUNSEN_TEST_NESTED_EXT_FIELD)
      .get(1);

  String decodedText1 = decodedNestedExtension1.getExtensionsByUrl("text")
      .get(0).getValueAsPrimitive().getValueAsString();

  String decodedText2 = decodedNestedExtension1.getExtensionsByUrl("text")
      .get(1).getValueAsPrimitive().getValueAsString();

  String decodedText3 = decodedNestedExtension2.getExtensionsByUrl("text")
      .get(0).getValueAsPrimitive().getValueAsString();

  CodeableConcept decodedCodeableConcept1 = (CodeableConcept) decodedNestedExtension1
      .getExtensionsByUrl(TestData.BUNSEN_TEST_CODEABLE_CONCEPT_EXT_FIELD)
      .get(0).getValue();

  CodeableConcept decodedCodeableConcept2 = (CodeableConcept) decodedNestedExtension1
      .getExtensionsByUrl(TestData.BUNSEN_TEST_CODEABLE_CONCEPT_EXT_FIELD)
      .get(1).getValue();

  CodeableConcept decodedCodeableConcept3 = (CodeableConcept) decodedNestedExtension2
      .getExtensionsByUrl(TestData.BUNSEN_TEST_CODEABLE_CONCEPT_EXT_FIELD)
      .get(0).getValue();

  Assert.assertEquals(text1, decodedText1);
  Assert.assertEquals(text2, decodedText2);
  Assert.assertEquals(text3, decodedText3);

  Assert.assertTrue(codeableConcept1.equalsDeep(decodedCodeableConcept1));
  Assert.assertTrue(codeableConcept2.equalsDeep(decodedCodeableConcept2));
  Assert.assertTrue(codeableConcept3.equalsDeep(decodedCodeableConcept3));

  final List<Record> nestedExtList = (List<Record>) avroBunsenTestProfilePatient
      .get("nestedExt");

  final Record nestedExt1 = nestedExtList.get(0);
  final Record nestedExt2 = nestedExtList.get(1);

  final List<Record> textList1 = (List<Record>) nestedExt1.get("text");
  final List<Record> textList2 = (List<Record>) nestedExt2.get("text");

  final List<Record> codeableConceptsList1 = (List<Record>) nestedExt1.get("codeableConceptExt");
  final List<Record> codeableConceptsList2 = (List<Record>) nestedExt2.get("codeableConceptExt");

  Assert.assertEquals(text1, textList1.get(0));
  Assert.assertEquals(text2, textList1.get(1));
  Assert.assertEquals(text3, textList2.get(0));

  Assert.assertEquals(codeableConcept1.getCoding().get(0).getCode(),
      ((List<Record>)codeableConceptsList1.get(0).get("coding")).get(0).get("code"));

  Assert.assertEquals(codeableConcept2.getCoding().get(0).getCode(),
      ((List<Record>)codeableConceptsList1.get(1).get("coding")).get(0).get("code"));

  Assert.assertEquals(codeableConcept3.getCoding().get(0).getCode(),
      ((List<Record>)codeableConceptsList2.get(0).get("coding")).get(0).get("code"));
}
 
Example 17
Source File: ParquetAvroValueReaders.java    From iceberg with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected Object getField(Record intermediate, int pos) {
  return intermediate.get(pos);
}
 
Example 18
Source File: TestReadProjection.java    From iceberg with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testListOfStructsProjection() throws IOException {
  Schema writeSchema = new Schema(
      Types.NestedField.required(0, "id", Types.LongType.get()),
      Types.NestedField.optional(22, "points",
          Types.ListType.ofOptional(21, Types.StructType.of(
              Types.NestedField.required(19, "x", Types.IntegerType.get()),
              Types.NestedField.optional(18, "y", Types.IntegerType.get())
          ))
      )
  );

  Record record = new Record(AvroSchemaUtil.convert(writeSchema, "table"));
  record.put("id", 34L);
  Record p1 = new Record(AvroSchemaUtil.fromOption(
      AvroSchemaUtil.fromOption(record.getSchema().getField("points").schema())
          .getElementType()));
  p1.put("x", 1);
  p1.put("y", 2);
  Record p2 = new Record(p1.getSchema());
  p2.put("x", 3);
  p2.put("y", null);
  record.put("points", ImmutableList.of(p1, p2));

  Schema idOnly = new Schema(
      Types.NestedField.required(0, "id", Types.LongType.get())
  );

  Record projected = writeAndRead("id_only", writeSchema, idOnly, record);
  Assert.assertEquals("Should contain the correct id value", 34L, (long) projected.get("id"));
  Assert.assertNull("Should not project points list", projected.get("points"));

  projected = writeAndRead("all_points", writeSchema, writeSchema.select("points"), record);
  Assert.assertNull("Should not project id", projected.get("id"));
  Assert.assertEquals("Should project points list",
      record.get("points"), projected.get("points"));

  projected = writeAndRead("x_only", writeSchema, writeSchema.select("points.x"), record);
  Assert.assertNull("Should not project id", projected.get("id"));
  Assert.assertNotNull("Should project points list", projected.get("points"));
  List<Record> points = (List<Record>) projected.get("points");
  Assert.assertEquals("Should read 2 points", 2, points.size());
  Record projectedP1 = points.get(0);
  Assert.assertEquals("Should project x", 1, (int) projectedP1.get("x"));
  Assert.assertNull("Should not project y", projectedP1.get("y"));
  Record projectedP2 = points.get(1);
  Assert.assertEquals("Should project x", 3, (int) projectedP2.get("x"));
  Assert.assertNull("Should not project y", projectedP2.get("y"));

  projected = writeAndRead("y_only", writeSchema, writeSchema.select("points.y"), record);
  Assert.assertNull("Should not project id", projected.get("id"));
  Assert.assertNotNull("Should project points list", projected.get("points"));
  points = (List<Record>) projected.get("points");
  Assert.assertEquals("Should read 2 points", 2, points.size());
  projectedP1 = points.get(0);
  Assert.assertNull("Should not project x", projectedP1.get("x"));
  Assert.assertEquals("Should project y", 2, (int) projectedP1.get("y"));
  projectedP2 = points.get(1);
  Assert.assertNull("Should not project x", projectedP2.get("x"));
  Assert.assertEquals("Should project null y", null, projectedP2.get("y"));

  Schema yRenamed = new Schema(
      Types.NestedField.optional(22, "points",
          Types.ListType.ofOptional(21, Types.StructType.of(
              Types.NestedField.optional(18, "z", Types.IntegerType.get())
          ))
      )
  );

  projected = writeAndRead("y_renamed", writeSchema, yRenamed, record);
  Assert.assertNull("Should not project id", projected.get("id"));
  Assert.assertNotNull("Should project points list", projected.get("points"));
  points = (List<Record>) projected.get("points");
  Assert.assertEquals("Should read 2 points", 2, points.size());
  projectedP1 = points.get(0);
  Assert.assertNull("Should not project x", projectedP1.get("x"));
  Assert.assertNull("Should not project y", projectedP1.get("y"));
  Assert.assertEquals("Should project z", 2, (int) projectedP1.get("z"));
  projectedP2 = points.get(1);
  Assert.assertNull("Should not project x", projectedP2.get("x"));
  Assert.assertNull("Should not project y", projectedP2.get("y"));
  Assert.assertEquals("Should project null z", null, projectedP2.get("z"));
}
 
Example 19
Source File: ParquetAvroValueReaders.java    From iceberg with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected Object getField(Record intermediate, int pos) {
  return intermediate.get(pos);
}
 
Example 20
Source File: TestReadProjection.java    From iceberg with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testListOfStructsProjection() throws IOException {
  Schema writeSchema = new Schema(
      Types.NestedField.required(0, "id", Types.LongType.get()),
      Types.NestedField.optional(22, "points",
          Types.ListType.ofOptional(21, Types.StructType.of(
              Types.NestedField.required(19, "x", Types.IntegerType.get()),
              Types.NestedField.optional(18, "y", Types.IntegerType.get())
          ))
      )
  );

  Record record = new Record(AvroSchemaUtil.convert(writeSchema, "table"));
  record.put("id", 34L);
  Record p1 = new Record(AvroSchemaUtil.fromOption(
      AvroSchemaUtil.fromOption(record.getSchema().getField("points").schema())
          .getElementType()));
  p1.put("x", 1);
  p1.put("y", 2);
  Record p2 = new Record(p1.getSchema());
  p2.put("x", 3);
  p2.put("y", null);
  record.put("points", ImmutableList.of(p1, p2));

  Schema idOnly = new Schema(
      Types.NestedField.required(0, "id", Types.LongType.get())
  );

  Record projected = writeAndRead("id_only", writeSchema, idOnly, record);
  Assert.assertEquals("Should contain the correct id value", 34L, (long) projected.get("id"));
  Assert.assertNull("Should not project points list", projected.get("points"));

  projected = writeAndRead("all_points", writeSchema, writeSchema.select("points"), record);
  Assert.assertNull("Should not project id", projected.get("id"));
  Assert.assertEquals("Should project points list",
      record.get("points"), projected.get("points"));

  projected = writeAndRead("x_only", writeSchema, writeSchema.select("points.x"), record);
  Assert.assertNull("Should not project id", projected.get("id"));
  Assert.assertNotNull("Should project points list", projected.get("points"));
  List<Record> points = (List<Record>) projected.get("points");
  Assert.assertEquals("Should read 2 points", 2, points.size());
  Record projectedP1 = points.get(0);
  Assert.assertEquals("Should project x", 1, (int) projectedP1.get("x"));
  Assert.assertNull("Should not project y", projectedP1.get("y"));
  Record projectedP2 = points.get(1);
  Assert.assertEquals("Should project x", 3, (int) projectedP2.get("x"));
  Assert.assertNull("Should not project y", projectedP2.get("y"));

  projected = writeAndRead("y_only", writeSchema, writeSchema.select("points.y"), record);
  Assert.assertNull("Should not project id", projected.get("id"));
  Assert.assertNotNull("Should project points list", projected.get("points"));
  points = (List<Record>) projected.get("points");
  Assert.assertEquals("Should read 2 points", 2, points.size());
  projectedP1 = points.get(0);
  Assert.assertNull("Should not project x", projectedP1.get("x"));
  Assert.assertEquals("Should project y", 2, (int) projectedP1.get("y"));
  projectedP2 = points.get(1);
  Assert.assertNull("Should not project x", projectedP2.get("x"));
  Assert.assertEquals("Should project null y", null, projectedP2.get("y"));

  Schema yRenamed = new Schema(
      Types.NestedField.optional(22, "points",
          Types.ListType.ofOptional(21, Types.StructType.of(
              Types.NestedField.optional(18, "z", Types.IntegerType.get())
          ))
      )
  );

  projected = writeAndRead("y_renamed", writeSchema, yRenamed, record);
  Assert.assertNull("Should not project id", projected.get("id"));
  Assert.assertNotNull("Should project points list", projected.get("points"));
  points = (List<Record>) projected.get("points");
  Assert.assertEquals("Should read 2 points", 2, points.size());
  projectedP1 = points.get(0);
  Assert.assertNull("Should not project x", projectedP1.get("x"));
  Assert.assertNull("Should not project y", projectedP1.get("y"));
  Assert.assertEquals("Should project z", 2, (int) projectedP1.get("z"));
  projectedP2 = points.get(1);
  Assert.assertNull("Should not project x", projectedP2.get("x"));
  Assert.assertNull("Should not project y", projectedP2.get("y"));
  Assert.assertEquals("Should project null z", null, projectedP2.get("z"));
}