Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions java/core/src/java/org/apache/orc/impl/RecordReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,8 @@ public static class SargApplier {
private final SearchArgument sarg;
private final List<PredicateLeaf> sargLeaves;
private final int[] filterColumns;
// File column ids of struct ancestors, or null for a non-struct path.
private final int[][] leafAncestors;
private final long rowIndexStride;
// same as the above array, but indices are set to true
private final SchemaEvolution evolution;
Expand Down Expand Up @@ -1149,12 +1151,58 @@ public SargApplier(SearchArgument sarg,
this.convertToProlepticGregorian = convertToProlepticGregorian;
filterColumns = mapSargColumnsToOrcInternalColIdx(sargLeaves,
evolution);
leafAncestors = new int[filterColumns.length][];
for (int i = 0; i < filterColumns.length; ++i) {
if (sargLeaves.get(i).getOperator() == PredicateLeaf.Operator.IS_NULL) {
leafAncestors[i] = evolution == null || filterColumns[i] <= 0 ? new int[0] :
findStructAncestors(evolution.getFileSchema().findSubtype(filterColumns[i]));
}
}
this.rowIndexStride = rowIndexStride;
this.evolution = evolution;
exceptionCount = new long[sargLeaves.size()];
this.useUTCTimestamp = useUTCTimestamp;
}

private static int[] findStructAncestors(TypeDescription type) {
List<Integer> ancestors = new ArrayList<>();
for (TypeDescription parent = type.getParent(); parent != null;
parent = parent.getParent()) {
if (parent.getCategory() != TypeDescription.Category.STRUCT) {
// Collection and union child statistics are not row-aligned.
return null;
}
if (parent.getParent() != null) {
// The root struct is never null.
ancestors.add(parent.getId());
}
}
return ancestors.stream().mapToInt(Integer::intValue).toArray();
}

private static boolean ancestorsHaveNoNulls(int[] ancestors,
OrcProto.RowIndex[] indexes,
int rowGroup) {
if (ancestors == null) {
return false;
}
for (int column : ancestors) {
OrcProto.RowIndex index = indexes[column];
if (index == null || rowGroup >= index.getEntryCount()) {
return false;
}
OrcProto.RowIndexEntry entry = index.getEntry(rowGroup);
if (!entry.hasStatistics()) {
return false;
}
OrcProto.ColumnStatistics stats = entry.getStatistics();
if (!stats.hasHasNull() || stats.getHasNull()) {
return false;
}
}
return true;
}

public void setRowIndexCols(boolean[] rowIndexCols) {
// included will not be null, row options will fill the array with
// trues if null
Expand Down Expand Up @@ -1193,6 +1241,10 @@ public boolean[] pickRowGroups(StripeInformation stripe,
if (columnIx == -1) {
// the column is a virtual column
leafValues[pred] = TruthValue.YES_NO_NULL;
} else if (sargLeaves.get(pred).getOperator() == PredicateLeaf.Operator.IS_NULL &&
!ancestorsHaveNoNulls(leafAncestors[pred], indexes, rowGroup)) {
// Leaf statistics omit rows where an ancestor struct is null.
leafValues[pred] = TruthValue.YES_NO_NULL;
} else if (exceptionAnswer[pred] != null) {
leafValues[pred] = exceptionAnswer[pred];
} else {
Expand Down
53 changes: 53 additions & 0 deletions java/core/src/test/org/apache/orc/impl/TestRecordReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1971,6 +1971,59 @@ public void testCloseWithException() throws Exception {
Path workDir = new Path(System.getProperty("test.tmp.dir",
"target" + File.separator + "test" + File.separator + "tmp"));

@Test
public void testIsNullOnFieldUnderNullStruct() throws Exception {
Configuration conf = new Configuration();
Path path = new Path(workDir, "isNullUnderNullStruct.orc");
TypeDescription schema = TypeDescription.fromString("struct<id:int,a:struct<b:struct<c:int>>>");
int stride = 1000;
try (Writer writer = OrcFile.createWriter(path,
OrcFile.writerOptions(conf).setSchema(schema).rowIndexStride(stride).overwrite(true))) {
for (int group = 0; group < 5; ++group) {
VectorizedRowBatch batch = schema.createRowBatch(stride);
LongColumnVector id = (LongColumnVector) batch.cols[0];
StructColumnVector a = (StructColumnVector) batch.cols[1];
StructColumnVector b = (StructColumnVector) a.fields[0];
LongColumnVector c = (LongColumnVector) b.fields[0];
for (int row = 0; row < stride; ++row) {
id.vector[row] = (long) group * stride + row;
c.vector[row] = 5;
}
// Groups 0 and 4 have no nulls; the others have one null at a different depth.
if (group == 1) {
a.noNulls = false;
a.isNull[0] = true;
} else if (group == 2) {
b.noNulls = false;
b.isNull[0] = true;
} else if (group == 3) {
c.noNulls = false;
c.isNull[0] = true;
}
batch.size = stride;
writer.addRowBatch(batch);
}
}

Reader reader = OrcFile.createReader(path, OrcFile.readerOptions(conf));
SearchArgument sarg = SearchArgumentFactory.newBuilder()
.isNull("a.b.c", PredicateLeaf.Type.LONG)
.build();
VectorizedRowBatch batch = reader.getSchema().createRowBatch();
long rowCount = 0;
try (RecordReader rows = reader.rows(reader.options().searchArgument(sarg, null))) {
while (rows.nextBatch(batch)) {
LongColumnVector id = (LongColumnVector) batch.cols[0];
for (int row = 0; row < batch.size; ++row) {
assertEquals(stride + rowCount, id.vector[row]);
++rowCount;
}
}
}
// Keep exactly the three groups with nulls, including those at either ancestor depth.
assertEquals(3L * stride, rowCount);
}

private void closeMockedRecordReader(DataReader mockedDataReader) throws IOException {
Path path = new Path(workDir, "empty.orc");
FileSystem.get(conf).delete(path, true);
Expand Down
Loading