feat(spark): add Spark-compatible AtLeastNNonNulls physical expression #417
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
rich7420
Which issue does this PR close?
Rationale for this change
Spark's
DataFrame.na.dropfilters rows with the internalAtLeastNNonNullspredicate. 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, aPhysicalExprthat callers build directly from a threshold and already-planned children, likeSparkFromUtcTimestampExpr. 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
nis 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 aVec<u32>of counts fromvalid.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. TheVec<u32>version is not part of this PR. The table shows median times from three runs on an Apple M3 Pro, with 8192 rows andnset to half the column count. Both versions stop reading columns once every row reachesn, 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 withevaluate_selection, which skips those rows.SparkFromUtcTimestampExpruses the same approach to evaluate its time zone child only for rows with a non-NULL timestamp.evaluate_selectionfilters 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 andnset 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:
n = 1andn= number of children repeated a scalar child for every row, so a large string literal could overflow thei32offsets and panic. Scalars are now checked once.get_fieldon a dictionary-encoded struct can produce, is no longer counted.Are these changes tested?
The predicate has no SQL function name, so the
.slttests and the PySpark runner can't call it. The unit tests and the doctest inat_least_n_non_nulls.rscover 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.evalstops afternqualifying values.Are there any user-facing changes?
A new public Rust
PhysicalExpr. There is no SQL function, since Spark doesn't have one.