Skip to content

ORC-2238: Do not skip row groups for IS_NULL on fields nested in a struct - #2742

Open
mhatipog wants to merge 2 commits into
apache:mainfrom
mhatipog:nested-isnull-sarg
Open

mhatipog wants to merge 2 commits into
apache:mainfrom
mhatipog:nested-isnull-sarg

Conversation

@mhatipog

@mhatipog mhatipog commented Nov 20, 2026 •

Copy link
Copy Markdown

What changes were proposed in this pull request?

SargApplier checks the struct ancestors of an IS_NULL leaf for each row group. When all ancestors are known to have no nulls, the existing leaf statistics can still be used for pruning. Otherwise, or when the path contains a non-struct ancestor, evaluation falls back to YES_NO_NULL. Other operators and top-level columns are unchanged. Thanks to @cxzl25 for suggesting and testing the per-row-group approach.

Why are the changes needed?

ORC writes a struct field only in rows where all of its ancestors are present, so the field's statistics do not count rows where a parent struct is null. When the field itself is never null, hasNull is false and IS_NULL evaluates to NO, which skips row groups that contain matching rows:

// struct<id:int,s:struct<x:int>> with rows (1, NULL) and (2, {x: 5})
SearchArgument sarg = SearchArgumentFactory.newBuilder()
    .isNull("s.x", PredicateLeaf.Type.LONG)
    .build();
reader.rows(reader.options().searchArgument(sarg, null)); // 0 rows before this change

Reproduced on 1.9.8, 2.1.4 and 2.3.1. Spark (SPARK-60362) and Iceberg (apache/iceberg#19064) hit the same case through their ORC readers.

How was this patch tested?

Extended TestRecordReaderImpl#testIsNullOnFieldUnderNullStruct to cover five row groups with nulls at either struct depth or at the leaf. The test fails on the previous patch and passes with the revised implementation, reading exactly the three affected groups (3,000 rows). The nine targeted test classes (TestRecordReaderImpl, TestVectorOrcFile, TestReaderImpl, TestFilters, TestPluginFilters, TestOrcFilterContext, TestRowFilteringIOSkip, TestStringDictionary and TestEncryption) reported 293 passed, 18 skipped, and no failures or errors.

Was this patch authored or co-authored using generative AI tooling?

Yes. Generated-by: an AI coding assistant, used to draft the change and the test; I reviewed and tested both.

mhatipog added a commit to mhatipog/spark that referenced this pull request Nov 20, 2026
@cxzl25
cxzl25 requested a balanced review from Copilot November 30, 2026 10:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified for the nested-struct IS_NULL row-group pruning fix.

No open issues 🎉

What changed in this PR

This PR fixes Java ORC row-group pruning for IS_NULL predicates on fields nested in structs, whose statistics omit rows with null ancestors.

Changes:

  • Track struct ancestry and return YES_NO_NULL for affected predicates so matching row groups are not skipped.
  • Preserve existing behavior for other operators and top-level columns.
  • Add a regression test verifying that a row group containing a null parent struct is read.
File Description
java/​core/​src/​test/​org/​apache/​orc/​impl/​TestRecordReaderImpl.java Adds regression coverage for IS_NULL under a null struct.
java/​core/​src/​java/​org/​apache/​orc/​impl/​RecordReaderImpl.java Prevents statistics-based pruning for nested-struct IS_NULL predicates.

Review effort: Balanced

sargLeaves.get(pred).getOperator() == PredicateLeaf.Operator.IS_NULL) {
// The column statistics only cover rows where every ancestor struct is present,
// so they cannot show that a row group has no nulls for this field.
leafValues[pred] = TruthValue.YES_NO_NULL;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! The root cause makes sense to me: a nested field has no values in rows where an ancestor struct is null, so its hasNull can't rule out IS NULL matches.

One concern is that treating every field under a struct as unsafe is quite broad. Many structs are rarely or never null in practice, but IS NULL on any of their fields would no longer skip any row groups.

Could we check the ancestors' statistics per row group instead? A field's statistics are complete for a row group whenever none of its ancestor structs has a null there. The ancestors' row indexes are already loaded, since all included columns' ROW_INDEX streams are read and the parents of an included column are always included, so this costs no extra I/O. Roughly:

// in the constructor: struct ancestors between the root and the leaf column,
// or null if any of them is not a struct (list/map/union)
leafAncestors[i] = evolution == null || filterColumns[i] <= 0 ? new int[0] :
    findAncestors(evolution.getFileSchema().findSubtype(filterColumns[i]));

// in pickRowGroups
} else if (sargLeaves.get(pred).getOperator() == PredicateLeaf.Operator.IS_NULL &&
    !ancestorsHaveNoNulls(leafAncestors[pred], indexes, rowGroup)) {
  leafValues[pred] = TruthValue.YES_NO_NULL;
}

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

This also covers fields under a list/map/union. There the statistics are counted per element rather than per row, so the current patch, which only checks for struct ancestors, would still use them.

I tried this locally with a test that writes struct<a:struct<b:struct<c:int>>> with 3 row groups of 1000 rows, where a.b is null only in the second one. a.b.c IS NULL reads 1000 rows with the per-row-group check and 3000 with the current patch. The existing TestRecordReaderImpl tests pass, including yours.

Check ancestor row-group statistics before evaluating nested IS_NULL.
Cover nulls at each nesting depth and verify unaffected groups are skipped.

Suggested-by: cxzl25
Generated-by: ChatGPT

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants