← Back to Pull requests

feat(spark): add Spark-compatible AtLeastNNonNulls physical expression #417

Open
unknown opened this pull request with 474 lines diff across 5 files
View on GitHub
unknown unknown commented on Nov 25, 2026 09:34 UTC
Author

Which issue does this PR close?

  • Closes #44.
  • Rationale for this change

    Spark's DataFrame.na.drop filters rows with the internal AtLeastNNonNulls predicate. Comet added native support in apache/datafusion-comet#6180, and having it here lets other engines reuse it. This replaces apache/datafusion#27078, which was opened before the crate moved.

    What changes are included in this PR?

    SparkAtLeastNNonNullsExpr, a PhysicalExpr that callers build directly from a threshold and already-planned children, like SparkFromUtcTimestampExpr. It returns a non-null Boolean, doesn't count NULL or NaN values, and skips a row's remaining computed children once the row reaches the threshold. It isn't a UDF because a UDF gets its arguments already evaluated, so it couldn't skip them.

    When n is neither 1 nor the number of children, the per-row counts are kept in bit planes, one bitmap per binary digit, so each validity word updates 64 rows at once. Updating a Vec<u32> of counts from valid.set_indices() is easier to read but slower. To measure the difference, I changed only the counter on b4efef4 and ran the new Criterion benchmark on both versions. The Vec<u32> version is not part of this PR. The table shows median times from three runs on an Apple M3 Pro, with 8192 rows and n set to half the column count. Both versions stop reading columns once every row reaches n, so they read only half the columns for inputs without NULLs.

    | Input | Bit planes | Vec<u32> |
    | --- | --- | --- |
    | 4 Float64 columns, no NULLs | 2.6 µs | 15.2 µs |
    | 4 Float64 columns, 50% NULL | 4.9 µs | 18.6 µs |
    | 32 Float64 columns, no NULLs | 20.8 µs | 111.5 µs |
    | 32 Float64 columns, 50% NULL | 43.6 µs | 138.9 µs |
    | 4 Utf8 columns, no NULLs | 0.7 µs | 13.3 µs |
    | 32 Utf8 columns, 50% NULL | 11.2 µs | 106.1 µs |

    With 65 rows and 32 Float64 columns (5% NULL, 5% NaN), the bit planes take 3.5 µs and the Vec<u32> takes 4.2 µs.

    Once some rows have reached n, children other than columns and literals are evaluated with evaluate_selection, which skips those rows. SparkFromUtcTimestampExpr uses the same approach to evaluate its time zone child only for rows with a non-NULL timestamp. evaluate_selection filters every column of the batch before it evaluates the child, so this costs much more than reading a column. In a separate run of the benchmark with 50% NULL values and n set to half the number of children, 32 Float64 children took 2.1 ms with each child wrapped in a cast and 46 µs as plain columns. For 32 Utf8 children, the times were 9.2 ms and 11 µs. The benchmark covers these cases too.

    Two fixes compared with apache/datafusion#27078:

  • The bitmap paths for n = 1 and n = number of children repeated a scalar child for every row, so a large string literal could overflow the i32 offsets and panic. Scalars are now checked once.

  • A NaN behind nested dictionaries, which get_field on a dictionary-encoded struct can produce, is no longer counted.
  • Are these changes tested?

    The predicate has no SQL function name, so the .slt tests and the PySpark runner can't call it. The unit tests and the doctest in at_least_n_non_nulls.rs cover NULL/NaN counting, complex values, dictionary and scalar inputs, child skipping, empty batches, and counts on both sides of 64-row word boundaries. The tests for the two fixes fail without them.

    I checked the mixed-type, dictionary/scalar and word-boundary cases with df.na.drop(thresh=n, subset=...) in PySpark 3.5.8 and 4.2.0, and the results matched. Child skipping I only checked against the Spark source: AtLeastNNonNulls.eval stops after n qualifying values.

    Are there any user-facing changes?

    A new public Rust PhysicalExpr. There is no SQL function, since Spark doesn't have one.

    Diff: 474 lines (+355 / -119) across 5 files Reviewer: rich7420 Review duration: 7967m
    All Vouch review confidence checks passed
    Residual risk 0.24 = Change Risk (0.46) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.46 · 474 lines diff across 5 files
    0.46
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.369
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #417"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.7% (MEDIUM) · Higher = larger blast radius
    45.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 24.4% · Safe to merge
    24.4%