Skip to content

Commit 0670964

Browse files
Dandandanclaude
andcommitted
Use BooleanArray::has_true/has_false for short-circuit checks
Replace `true_count() == 0`, `true_count() > 0`, `false_count() == 0`, and similar patterns with `has_true()`/`has_false()` where the actual count is not needed. These methods can short-circuit on the first matching value instead of counting all bits. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1e93a67 commit 0670964

6 files changed

Lines changed: 11 additions & 15 deletions

File tree

datafusion/datasource-parquet/src/metadata.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -495,12 +495,10 @@ fn summarize_column_statistics(
495495

496496
// handle the common special case when all row groups have exact statistics
497497
let exactness = &is_max_value_exact_stat;
498-
if !exactness.is_empty()
499-
&& exactness.null_count() == 0
500-
&& exactness.true_count() == exactness.len()
498+
if !exactness.is_empty() && exactness.null_count() == 0 && !exactness.has_false()
501499
{
502500
accumulators.is_max_value_exact[logical_schema_index] = Some(true);
503-
} else if exactness.true_count() == 0 {
501+
} else if !exactness.has_true() {
504502
accumulators.is_max_value_exact[logical_schema_index] = Some(false);
505503
} else {
506504
let val = max_acc.evaluate()?;
@@ -514,12 +512,10 @@ fn summarize_column_statistics(
514512

515513
// handle the common special case when all row groups have exact statistics
516514
let exactness = &is_min_value_exact_stat;
517-
if !exactness.is_empty()
518-
&& exactness.null_count() == 0
519-
&& exactness.true_count() == exactness.len()
515+
if !exactness.is_empty() && exactness.null_count() == 0 && !exactness.has_false()
520516
{
521517
accumulators.is_min_value_exact[logical_schema_index] = Some(true);
522-
} else if exactness.true_count() == 0 {
518+
} else if !exactness.has_true() {
523519
accumulators.is_min_value_exact[logical_schema_index] = Some(false);
524520
} else {
525521
let val = min_acc.evaluate()?;
@@ -650,7 +646,7 @@ fn has_any_exact_match(
650646
let scalar_array = value.to_scalar().ok()?;
651647
let eq_mask = eq(&scalar_array, &array).ok()?;
652648
let combined_mask = and(&eq_mask, exactness).ok()?;
653-
Some(combined_mask.true_count() > 0)
649+
Some(combined_mask.has_true())
654650
}
655651

656652
/// Wrapper to implement [`FileMetadata`] for [`ParquetMetaData`].

datafusion/functions-nested/src/array_has.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ fn array_has_dispatch_for_array<'a>(
337337
let is_nested = arr.data_type().is_nested();
338338
let needle_row = Scalar::new(needle.slice(i, 1));
339339
let eq_array = compare_with_eq(&arr, &needle_row, is_nested)?;
340-
boolean_builder.append_value(eq_array.true_count() > 0);
340+
boolean_builder.append_value(eq_array.has_true());
341341
}
342342

343343
Ok(Arc::new(boolean_builder.finish()))

datafusion/functions-nested/src/replace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ fn general_replace<O: OffsetSizeTrait>(
347347
let mut counter = 0;
348348

349349
// All elements are false, no need to replace, just copy original data
350-
if eq_array.false_count() == eq_array.len() {
350+
if !eq_array.has_true() {
351351
mutable.extend(
352352
original_idx.to_usize().unwrap(),
353353
start.to_usize().unwrap(),

datafusion/physical-plan/src/joins/nested_loop_join.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,7 +1672,7 @@ impl NestedLoopJoinStream {
16721672
return Ok(None);
16731673
}
16741674

1675-
if cur_right_bitmap.true_count() == 0 {
1675+
if !cur_right_bitmap.has_true() {
16761676
// If none of the pairs has passed the join predicate/filter
16771677
Ok(None)
16781678
} else {
@@ -2259,7 +2259,7 @@ fn build_unmatched_batch(
22592259
not(&batch_bitmap)?
22602260
};
22612261

2262-
if bitmap.true_count() == 0 {
2262+
if !bitmap.has_true() {
22632263
return Ok(None);
22642264
}
22652265

datafusion/physical-plan/src/joins/sort_merge_join/filter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ pub fn filter_record_batch_by_join_type(
331331
.unwrap();
332332

333333
// All rows passed the filter — no null-joining needed
334-
if kept_corrected.true_count() == kept_corrected.len() {
334+
if !kept_corrected.has_false() {
335335
return Ok(kept_batch);
336336
}
337337

datafusion/spark/src/function/array/array_contains.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ fn apply_spark_null_semantics(
8484
haystack_arg: &ColumnarValue,
8585
) -> Result<BooleanArray> {
8686
// happy path
87-
if result.false_count() == 0 || haystack_arg.data_type() == DataType::Null {
87+
if !result.has_false() || haystack_arg.data_type() == DataType::Null {
8888
return Ok(result.clone());
8989
}
9090

0 commit comments

Comments
 (0)