Skip to content

Apache DataFusion SQL Query Engine

fix: set distinct_count to Absent when merging statistics (#17385)

Adrian Garcia Badaracco71d3d2971973
diff --git a/datafusion/common/src/stats.rs b/datafusion/common/src/stats.rs
index a6d132ef51..e461274abd 100644
--- a/datafusion/common/src/stats.rs
+++ b/datafusion/common/src/stats.rs
@@ -538,6 +538,7 @@ impl Statistics {
col_stats.max_value = col_stats.max_value.max(&item_col_stats.max_value);
col_stats.min_value = col_stats.min_value.min(&item_col_stats.min_value);
col_stats.sum_value = col_stats.sum_value.add(&item_col_stats.sum_value);
+ col_stats.distinct_count = Precision::Absent;
}
Ok(Statistics {
@@ -1152,4 +1153,50 @@ mod tests {
let e = Statistics::try_merge_iter(&items, &schema).unwrap_err();
assert_contains!(e.to_string(), "Error during planning: Cannot merge statistics with different number of columns: 0 vs 1");
}
+
+ #[test]
+ fn test_try_merge_distinct_count_absent() {
+ // Create statistics with known distinct counts
+ let stats1 = Statistics::default()
+ .with_num_rows(Precision::Exact(10))
+ .with_total_byte_size(Precision::Exact(100))
+ .add_column_statistics(
+ ColumnStatistics::new_unknown()
+ .with_null_count(Precision::Exact(0))
+ .with_min_value(Precision::Exact(ScalarValue::Int32(Some(1))))
+ .with_max_value(Precision::Exact(ScalarValue::Int32(Some(10))))
+ .with_distinct_count(Precision::Exact(5)),
+ );
+
+ let stats2 = Statistics::default()
+ .with_num_rows(Precision::Exact(15))
+ .with_total_byte_size(Precision::Exact(150))
+ .add_column_statistics(
+ ColumnStatistics::new_unknown()
+ .with_null_count(Precision::Exact(0))
+ .with_min_value(Precision::Exact(ScalarValue::Int32(Some(5))))
+ .with_max_value(Precision::Exact(ScalarValue::Int32(Some(20))))
+ .with_distinct_count(Precision::Exact(7)),
+ );
+
+ // Merge statistics
+ let merged_stats = stats1.try_merge(&stats2).unwrap();
+
+ // Verify the results
+ assert_eq!(merged_stats.num_rows, Precision::Exact(25));
+ assert_eq!(merged_stats.total_byte_size, Precision::Exact(250));
+
+ let col_stats = &merged_stats.column_statistics[0];
+ assert_eq!(col_stats.null_count, Precision::Exact(0));
+ assert_eq!(
+ col_stats.min_value,
+ Precision::Exact(ScalarValue::Int32(Some(1)))
+ );
+ assert_eq!(
+ col_stats.max_value,
+ Precision::Exact(ScalarValue::Int32(Some(20)))
+ );
+ // Distinct count should be Absent after merge
+ assert_eq!(col_stats.distinct_count, Precision::Absent);
+ }
}