Skip to content

Commit be142d5

Browse files
committed
Safeguard against potential inexact row count being smaller than exact null count
1 parent 8a4bad4 commit be142d5

File tree

1 file changed

+8
-1
lines changed
  • datafusion/physical-plan/src/joins

1 file changed

+8
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,8 @@ pub(crate) fn estimate_join_statistics(
783783
join_type: &JoinType,
784784
schema: &Schema,
785785
) -> Result<Statistics> {
786+
let l = format!("{left:#?}");
787+
let r = format!("{right:#?}");
786788
let left_stats = left.statistics()?;
787789
let right_stats = right.statistics()?;
788790

@@ -955,7 +957,12 @@ fn max_distinct_count(
955957
let result = match num_rows {
956958
Precision::Absent => Precision::Absent,
957959
Precision::Inexact(count) => {
958-
Precision::Inexact(count - stats.null_count.get_value().unwrap_or(&0))
960+
// To safeguard against inexact number of rows (e.g. 0) being smaller than
961+
// an exact null count we need to do a checked subtraction.
962+
match count.checked_sub(*stats.null_count.get_value().unwrap_or(&0)) {
963+
None => Precision::Inexact(0),
964+
Some(non_null_count) => Precision::Inexact(non_null_count),
965+
}
959966
}
960967
Precision::Exact(count) => {
961968
let count = count - stats.null_count.get_value().unwrap_or(&0);

0 commit comments

Comments
 (0)