DG47 commented on code in PR #4131:
URL: https://github.com/apache/iceberg-python/pull/4131#discussion_r4744209002


##########
pyiceberg/io/pyarrow.py:
##########
@@ -2833,36 +2833,52 @@ def parquet_path_to_id_mapping(
     return result
 
 
-def _nan_value_counts_from_arrow(table: pa.Table, stats_columns: dict[int, 
StatisticsCollector]) -> dict[int, int]:
-    """Count the non-null NaN values of the top-level float and double columns.
+def _nan_value_counts_from_arrow(
+    table: pa.Table, schema: Schema, stats_columns: dict[int, 
StatisticsCollector]
+) -> dict[int, int]:
+    """Count the non-null NaN values of the float and double columns, 
including nested fields.
 
     PyArrow does not expose the NaN count of a Parquet column statistic, so 
the counts are taken
-    from the data before it is written. Columns that cannot be counted are 
left out, which only
-    means the file cannot be pruned on NaN counts.
+    from the data before it is written. Float and double fields inside 
structs, list elements,
+    and map keys and values are counted as well. Columns that cannot be 
counted are left out,
+    which only means the file cannot be pruned on NaN counts.
 
     Args:
         table (pyarrow.Table): The data that is written to the file.
+        schema (Schema): The Iceberg schema of the file.
         stats_columns (dict[int, StatisticsCollector]): The statistics 
gathering plan.
 
     Returns:
         A mapping of Iceberg field ID to the number of non-null NaN values.
     """
+    nan_field_ids = {
+        field_id
+        for field_id, stats_col in stats_columns.items()
+        if isinstance(stats_col.iceberg_type, (FloatType, DoubleType)) and 
stats_col.mode != MetricsMode(MetricModeTypes.NONE)
+    }
     nan_value_counts: dict[int, int] = {}
-    for pos, arrow_field in enumerate(table.schema):
-        if (field_id := _get_field_id(arrow_field)) is None:
-            continue
-
-        stats_col = stats_columns.get(field_id)
-        if stats_col is None:
-            continue
-
-        if stats_col.mode == MetricsMode(MetricModeTypes.NONE):
-            continue
-
-        if not isinstance(stats_col.iceberg_type, (FloatType, DoubleType)):
-            continue
-
-        nan_value_counts[field_id] = pc.sum(pc.is_nan(table.column(pos)), 
min_count=0).as_py()
+    if not nan_field_ids:
+        return nan_value_counts
+
+    def count(field_id: int, field_type: IcebergType, array: pa.Array) -> None:

Review Comment:
   Done in 8dee48b. The counting is now a `SchemaWithPartnerVisitor` driven by 
`visit_with_partner`, the same way `_to_requested_schema` walks the data. I 
couldn't use `ArrowAccessor` as it is: `ListArray.values` and `MapArray.items` 
return the whole child array, so for a sliced table they count NaNs from rows 
outside the slice, and `StructArray.field` keeps the values under a null 
struct. `_ArrowVisibleValuesAccessor` overrides those lookups with `flatten()` 
and `pc.struct_field`. The second `write()` of a slice in 
`test_nan_value_counts` already fails with the plain accessor, and I added a 
test for a NaN under a null struct.
   



-- 
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