@@ -2305,6 +2305,50 @@ mod tests {
23052305 ) ;
23062306 }
23072307
2308+ #[ test]
2309+ fn test_with_fetch_caps_ndv_with_skip ( ) {
2310+ // 1000 rows, NDV=500, OFFSET 5 LIMIT 10
2311+ // with_fetch computes num_rows = min(1000 - 5, 10) = 10
2312+ // NDV should be capped at 10
2313+ let stats = Statistics {
2314+ num_rows : Precision :: Exact ( 1000 ) ,
2315+ total_byte_size : Precision :: Exact ( 8000 ) ,
2316+ column_statistics : vec ! [ ColumnStatistics {
2317+ distinct_count: Precision :: Inexact ( 500 ) ,
2318+ ..Default :: default ( )
2319+ } ] ,
2320+ } ;
2321+
2322+ let result = stats. with_fetch ( Some ( 10 ) , 5 , 1 ) . unwrap ( ) ;
2323+ assert_eq ! ( result. num_rows, Precision :: Exact ( 10 ) ) ;
2324+ assert_eq ! (
2325+ result. column_statistics[ 0 ] . distinct_count,
2326+ Precision :: Inexact ( 10 )
2327+ ) ;
2328+ }
2329+
2330+ #[ test]
2331+ fn test_with_fetch_caps_ndv_with_large_skip ( ) {
2332+ // 1000 rows, NDV=500, OFFSET 995 LIMIT 100
2333+ // with_fetch computes num_rows = min(1000 - 995, 100) = 5
2334+ // NDV should be capped at 5
2335+ let stats = Statistics {
2336+ num_rows : Precision :: Exact ( 1000 ) ,
2337+ total_byte_size : Precision :: Exact ( 8000 ) ,
2338+ column_statistics : vec ! [ ColumnStatistics {
2339+ distinct_count: Precision :: Inexact ( 500 ) ,
2340+ ..Default :: default ( )
2341+ } ] ,
2342+ } ;
2343+
2344+ let result = stats. with_fetch ( Some ( 100 ) , 995 , 1 ) . unwrap ( ) ;
2345+ assert_eq ! ( result. num_rows, Precision :: Exact ( 5 ) ) ;
2346+ assert_eq ! (
2347+ result. column_statistics[ 0 ] . distinct_count,
2348+ Precision :: Inexact ( 5 )
2349+ ) ;
2350+ }
2351+
23082352 #[ test]
23092353 fn test_with_fetch_ndv_below_row_count_unchanged ( ) {
23102354 // NDV=5 and LIMIT 10: NDV should stay at 5
0 commit comments