Skip to content

Commit

Permalink
Hive: Return null if Hive inspects a null struct record (#4283)
Browse files Browse the repository at this point in the history
Fix #4282
  • Loading branch information
tprelle committed Jul 6, 2022
1 parent 56c1993 commit 3959e2f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,19 @@ public StructField getStructFieldRef(String name) {

@Override
public Object getStructFieldData(Object o, StructField structField) {
if (o == null) {
return null;
}

return ((Record) o).get(((IcebergRecordStructField) structField).position());
}

@Override
public List<Object> getStructFieldsDataAsList(Object o) {
if (o == null) {
return null;
}

Record record = (Record) o;
return structFields
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.iceberg.mr.InputFormatConfig;
import org.apache.iceberg.mr.TestHelper;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.types.Type;
import org.apache.iceberg.types.Types;
Expand Down Expand Up @@ -707,6 +708,34 @@ public void testMultiTableInsert() throws IOException {
HiveIcebergTestUtils.validateData(target2, target2Records, 1);
}

/**
* Fix vectorized parquet
* <a href="https://github.com/apache/iceberg/issues/4403">issue-4403</a>.
**/
@Test
public void testStructMapWithNull() throws IOException {
Assume.assumeTrue("Vectorized parquet throw class cast exception see : issue 4403",
!("PARQUET".equals(fileFormat.name()) && isVectorized));
Schema schema = new Schema(required(1, "id", Types.LongType.get()),
required(2, "mapofstructs", Types.MapType.ofRequired(3, 4, Types.StringType.get(),
Types.StructType.of(required(5, "something", Types.StringType.get()),
required(6, "someone", Types.StringType.get()),
required(7, "somewhere", Types.StringType.get())
))
)
);

List<Record> records = TestHelper.RecordsBuilder.newInstance(schema)
.add(0L, ImmutableMap.of())
.build();

testTables.createTable(shell, "mapwithnull", schema, fileFormat, records);

List<Object[]> results = shell.executeStatement("select mapofstructs['context'].someone FROM mapwithnull");
Assert.assertEquals(1, results.size());
Assert.assertNull(results.get(0)[0]);
}

@Test
public void testWriteWithDefaultWriteFormat() {
Assume.assumeTrue("Testing the default file format is enough for a single scenario.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,17 @@ public void testIcebergRecordObjectInspector() {
Assert.assertEquals(innerRecord.get(0), innerSoi.getStructFieldData(innerData, stringField));
}

@Test
public void testIcebergRecordObjectInspectorWithRowNull() {
Schema schema = new Schema(
required(1, "integer_field", Types.IntegerType.get()),
required(2, "struct_field", Types.StructType.of(
Types.NestedField.required(3, "string_field", Types.StringType.get())))
);
StructObjectInspector soi = (StructObjectInspector) IcebergObjectInspector.create(schema);
Assert.assertNull(soi.getStructFieldsDataAsList(null));
StructField integerField = soi.getStructFieldRef("integer_field");
Assert.assertNull(soi.getStructFieldData(null, integerField));
}

}

0 comments on commit 3959e2f

Please sign in to comment.