diff --git a/java/core/src/java/org/apache/orc/impl/RecordReaderImpl.java b/java/core/src/java/org/apache/orc/impl/RecordReaderImpl.java index 34ffe45204..aebdde0610 100644 --- a/java/core/src/java/org/apache/orc/impl/RecordReaderImpl.java +++ b/java/core/src/java/org/apache/orc/impl/RecordReaderImpl.java @@ -1115,6 +1115,8 @@ public static class SargApplier { private final SearchArgument sarg; private final List 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; @@ -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 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 @@ -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 { diff --git a/java/core/src/test/org/apache/orc/impl/TestRecordReaderImpl.java b/java/core/src/test/org/apache/orc/impl/TestRecordReaderImpl.java index 5c47111db8..158d316bfd 100644 --- a/java/core/src/test/org/apache/orc/impl/TestRecordReaderImpl.java +++ b/java/core/src/test/org/apache/orc/impl/TestRecordReaderImpl.java @@ -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>>"); + 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);