Skip to content

Commit f43b071

Browse files
committed
Add with_fetch NDV capping tests with skip
1 parent ddfd8f3 commit f43b071

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

datafusion/common/src/stats.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,6 +2100,50 @@ mod tests {
21002100
);
21012101
}
21022102

2103+
#[test]
2104+
fn test_with_fetch_caps_ndv_with_skip() {
2105+
// 1000 rows, NDV=500, OFFSET 5 LIMIT 10
2106+
// with_fetch computes num_rows = min(1000 - 5, 10) = 10
2107+
// NDV should be capped at 10
2108+
let stats = Statistics {
2109+
num_rows: Precision::Exact(1000),
2110+
total_byte_size: Precision::Exact(8000),
2111+
column_statistics: vec![ColumnStatistics {
2112+
distinct_count: Precision::Inexact(500),
2113+
..Default::default()
2114+
}],
2115+
};
2116+
2117+
let result = stats.with_fetch(Some(10), 5, 1).unwrap();
2118+
assert_eq!(result.num_rows, Precision::Exact(10));
2119+
assert_eq!(
2120+
result.column_statistics[0].distinct_count,
2121+
Precision::Inexact(10)
2122+
);
2123+
}
2124+
2125+
#[test]
2126+
fn test_with_fetch_caps_ndv_with_large_skip() {
2127+
// 1000 rows, NDV=500, OFFSET 995 LIMIT 100
2128+
// with_fetch computes num_rows = min(1000 - 995, 100) = 5
2129+
// NDV should be capped at 5
2130+
let stats = Statistics {
2131+
num_rows: Precision::Exact(1000),
2132+
total_byte_size: Precision::Exact(8000),
2133+
column_statistics: vec![ColumnStatistics {
2134+
distinct_count: Precision::Inexact(500),
2135+
..Default::default()
2136+
}],
2137+
};
2138+
2139+
let result = stats.with_fetch(Some(100), 995, 1).unwrap();
2140+
assert_eq!(result.num_rows, Precision::Exact(5));
2141+
assert_eq!(
2142+
result.column_statistics[0].distinct_count,
2143+
Precision::Inexact(5)
2144+
);
2145+
}
2146+
21032147
#[test]
21042148
fn test_with_fetch_ndv_below_row_count_unchanged() {
21052149
// NDV=5 and LIMIT 10: NDV should stay at 5

0 commit comments

Comments
 (0)