kosiew commented on code in PR #27189:
URL: https://github.com/apache/datafusion/pull/27189#discussion_r4753447045


##########
datafusion/common/src/stats.rs:
##########
@@ -773,8 +780,22 @@ impl Statistics {
                     ),
                     _ => Precision::Absent,
                 };
-                precision_min(&mut col_stats.min_value, &item_cs.min_value);
-                precision_max(&mut col_stats.max_value, &item_cs.max_value);
+
+                // Skip min/max from all-null or empty inputs so their
+                // Absent bounds don't wipe out known bounds from other files.
+                match (acc_all_null, item_all_null) {
+                    (false, false) => {
+                        precision_min(&mut col_stats.min_value, 
&item_cs.min_value);
+                        precision_max(&mut col_stats.max_value, 
&item_cs.max_value);
+                    }
+                    (true, false) => {
+                        col_stats.min_value = item_cs.min_value.clone();
+                        col_stats.max_value = item_cs.max_value.clone();
+                    }
+                    // (false, true) and (true, true): keep accumulator as-is

Review Comment:
   The (true, true) branch can retain exact date bounds from an empty Hive 
partition, returning dates instead of NULL for MIN/MAX. Please clear both 
bounds in this branch and add a SQL regression combining an empty dated 
partition with a populated NULL partition.



##########
datafusion/common/src/stats.rs:
##########
@@ -760,6 +763,10 @@ impl Statistics {
             for (col_stats, item_cs) in
                 column_statistics.iter_mut().zip(&stat.column_statistics)
             {
+                // Check before null_count is accumulated so it pairs with 
acc_num_rows.
+                let acc_all_null = is_all_null_or_empty(col_stats, 
&acc_num_rows);
+                let item_all_null = is_all_null_or_empty(item_cs, 
&stat.num_rows);
+
                 col_stats.null_count = 
col_stats.null_count.add(&item_cs.null_count);

Review Comment:
   Non-blocking: Empty inputs with absent null counts cause order-dependent 
scan elimination. Consider treating exact-zero-row inputs as contributing 
Exact(0) nulls and testing both merge orders.



##########
datafusion/datasource/src/file_groups.rs:
##########
@@ -358,10 +358,10 @@ impl FileGroupPartitioner {
 /// in the group for the **full table schema** (file columns + partition 
columns).
 ///
 /// Partition column statistics are derived from the individual file partition 
values:
-/// - `min` = minimum partition value across all files in the group, or absent 
if any
-///   file in the group has a null partition value (see #26748)
-/// - `max` = maximum partition value across all files in the group, or absent 
if any
-///   file in the group has a null partition value
+/// - `min` = minimum partition value across all files in the group
+///   (all-null partitions are skipped; see #26748)

Review Comment:
   Please clarify that extrema exclude only provably empty or all-null inputs. 
Bounds may remain absent when counts cannot establish the absence of non-null 
values.



##########
datafusion/sqllogictest/test_files/listing_table_partitions.slt:
##########
@@ -258,6 +258,60 @@ select a, p from null_part_write order by a;
 statement ok
 set datafusion.execution.listing_table_hive_default_partition_name = 
'__HIVE_DEFAULT_PARTITION__';
 
+
+# Test: a NULL (__HIVE_DEFAULT_PARTITION__) partition must not wipe out the
+# merged min/max statistics of the partition column. See #26748.
+
+query I
+copy (values(1, 100))
+to 
'test_files/scratch/listing_table_partitions/null_part_stats/d=2024-01-01/data.parquet';
+----
+1
+
+query I
+copy (values(2, 200))
+to 
'test_files/scratch/listing_table_partitions/null_part_stats/d=2024-06-15/data.parquet';
+----
+1
+
+query I
+copy (values(3, 300))
+to 
'test_files/scratch/listing_table_partitions/null_part_stats/d=2024-12-31/data.parquet';
+----
+1
+
+query I
+copy (values(4, 400))
+to 
'test_files/scratch/listing_table_partitions/null_part_stats/d=__HIVE_DEFAULT_PARTITION__/data.parquet';
+----
+1
+
+statement count 0
+create external table null_part_stats (column1 int, column2 int, d date)
+stored as parquet
+partitioned by (d)
+location 'test_files/scratch/listing_table_partitions/null_part_stats/';
+
+# min/max on the partition column is answered from statistics (no scan)
+# despite the NULL partition.
+query TT
+explain select min(d), max(d) from null_part_stats;
+----
+logical_plan
+01)Aggregate: groupBy=[[]], aggr=[[min(null_part_stats.d), 
max(null_part_stats.d)]]
+02)--TableScan: null_part_stats projection=[d]
+physical_plan
+01)ProjectionExec: expr=[2024-01-01 as min(null_part_stats.d), 2024-12-31 as 
max(null_part_stats.d)]
+02)--PlaceholderRowExec
+
+query DD
+select min(d), max(d) from null_part_stats;

Review Comment:
   Optional: Assert that SELECT COUNT(*), COUNT(d) FROM null_part_stats returns 
4, 3 to verify that the NULL partition contributes rows but no non-null dates.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to