karubian opened a new issue, #19236:
URL: https://github.com/apache/iceberg/issues/19236

   ### Apache Iceberg version
   
   1.12.0 (latest release)
   
   ### Query engine
   
   Spark
   
   ### Please describe the bug 🐞
   
   Iceberg's Parquet footer metrics build a column's lower and upper bounds 
from the row groups whose statistics hold a usable min and max. A row group 
whose statistics hold a null count but no usable min or max contributes its 
counts only, and the bounds of the other row groups are kept. When that row 
group holds a value outside those bounds, scan planning skips the file for a 
filter that matches the value. Filtered reads then miss the row, and a filtered 
`DELETE` leaves it in place. `add_files`, `migrate` and `snapshot` register 
files through these metrics.
   
   These files read that way:
   
   - **parquet-java:** Spark's Parquet writer, parquet-mr 1.13.1, and Iceberg's 
own writer, on parquet-java 1.17.1, give a float or double row group holding 
NaN and 100.0 the statistics `min 100.0, max NaN`. For floating point columns 
in type order, the format tells readers to ignore a NaN min or max, 
[parquet.thrift](https://github.com/apache/parquet-format/blob/5baa8669703fb887f8a0127678f66582800e25bc/src/main/thrift/parquet.thrift#L1174-L1177).
 parquet-java's reader does, and keeps the null count, 
[Statistics](https://github.com/apache/parquet-java/blob/apache-parquet-1.17.1/parquet-column/src/main/java/org/apache/parquet/column/statistics/Statistics.java#L67-L140).
   - **DuckDB 1.5.3 and 2.0.1:** a float or double row group holding a NaN gets 
a null count and a NaN count but no min or max, 
[primitive_column_writer.cpp](https://github.com/duckdb/duckdb/blob/v2.0.1/extension/parquet/writer/primitive_column_writer.cpp#L419-L445).
   - **PyArrow 25.0.1 and 26.0.1:** a string or binary row group whose largest 
value is longer than the default `max_statistics_size` of 4,096 bytes gets a 
null count and no min or max; 4,096 bytes keeps them, 
[properties.h](https://github.com/apache/arrow/blob/apache-arrow-26.0.1/cpp/src/parquet/properties.h#L182).
   
   On Spark 3.5.10 with PyArrow 26.0.1:
   
   ```python
   import tempfile
   
   import pyarrow as pa
   import pyarrow.parquet as pq
   from pyspark.sql import SparkSession
   
   work = tempfile.mkdtemp()
   spark = (SparkSession.builder.master("local[1]")
            .config("spark.jars.packages", 
"org.apache.iceberg:iceberg-spark-runtime-3.5_2.12:1.12.0")
            .config("spark.sql.extensions", 
"org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions")
            .config("spark.sql.catalog.local", 
"org.apache.iceberg.spark.SparkCatalog")
            .config("spark.sql.catalog.local.type", "hadoop")
            .config("spark.sql.catalog.local.warehouse", 
f"file://{work}/warehouse")
            .config("spark.hadoop.parquet.block.size", "1")  # a row group 
every two rows
            .config("spark.hadoop.parquet.page.size.row.check.min", "2")
            .config("spark.hadoop.parquet.page.size.row.check.max", "2")
            .getOrCreate())
   
   # A Parquet file written by Spark, whose second row group holds a NaN
   rows = [(1.0,), (2.0,), (float("nan"),), (100.0,)]
   spark.createDataFrame(spark.sparkContext.parallelize(rows, 1), "d 
double").write.parquet(f"file://{work}/doubles")
   spark.sql("CREATE TABLE local.db.doubles (d double) USING iceberg")
   spark.sql(f"CALL local.system.add_files(table => 'db.doubles', source_table 
=> '`parquet`.`file://{work}/doubles`')")
   spark.sql("SELECT readable_metrics.d.lower_bound, 
readable_metrics.d.upper_bound FROM local.db.doubles.files").show()
   spark.sql("SELECT count(*) FROM local.db.doubles WHERE d = 100.0").show()
   spark.sql("DELETE FROM local.db.doubles WHERE d = 100.0")
   spark.sql("SELECT count(*) FROM local.db.doubles").show()
   
   # A Parquet file written by PyArrow, whose second row group holds a value 
longer than 4,096 bytes
   long_value = "z" * 5000
   pq.write_table(pa.table({"v": ["a", "b", "c", long_value]}), 
f"{work}/strings.parquet", row_group_size=2)
   spark.sql("CREATE TABLE local.db.strings (v string) USING iceberg "
             "TBLPROPERTIES ('format-version' = '2', 'write.delete.mode' = 
'merge-on-read')")
   spark.sql(f"CALL local.system.add_files(table => 'db.strings', source_table 
=> '`parquet`.`file://{work}/strings.parquet`')")
   spark.sql("SELECT readable_metrics.v.upper_bound FROM 
local.db.strings.files").show()
   spark.sql(f"SELECT count(*) FROM local.db.strings WHERE v = 
'{long_value}'").show()
   spark.sql(f"DELETE FROM local.db.strings WHERE v = '{long_value}'")
   spark.sql("SELECT count(*) FROM local.db.strings").show()
   ```
   
   | Table | Recorded bounds | Rows matching the filter | Rows after the 
`DELETE` |
   | --- | --- | --- | --- |
   | `doubles` | 1.0 to 2.0 | 0, expected 1 | 4, expected 3 |
   | `strings` | upper bound `b` | 0, expected 1 | 4, expected 3 |
   
   Both `DELETE` statements succeed and leave all 4 rows. Files of the same 
kinds miss rows the same way on 1.4.3, 1.10.0 and a build of main at 65d832d, 
and `snapshot` misses the same rows on 1.12.0.
   
   All three procedures reach the footer through `SparkTableUtil`:
   
   - **Callers:** `add_files` takes `importSparkPartitions` for a file path, as 
above, and `importSparkTable` for a catalog table, 
[AddFilesProcedure](https://github.com/apache/iceberg/blob/apache-iceberg-1.12.0/spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/procedures/AddFilesProcedure.java#L161-L169)
 and 
[L227-L254](https://github.com/apache/iceberg/blob/apache-iceberg-1.12.0/spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/procedures/AddFilesProcedure.java#L227-L254).
 `migrate` and `snapshot` take `importSparkTable`, 
[MigrateTableSparkAction](https://github.com/apache/iceberg/blob/apache-iceberg-1.12.0/spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/actions/MigrateTableSparkAction.java#L156)
 and 
[SnapshotTableSparkAction](https://github.com/apache/iceberg/blob/apache-iceberg-1.12.0/spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/actions/SnapshotTableSparkAction.java#L154).
   - **The footer read:** both go through 
[TableMigrationUtil](https://github.com/apache/iceberg/blob/apache-iceberg-1.12.0/data/src/main/java/org/apache/iceberg/data/TableMigrationUtil.java#L224-L232)
 to `ParquetUtil.fileMetrics`.
   - **Delta Lake:** its snapshot action calls `ParquetUtil.fileMetrics` too, 
[BaseSnapshotDeltaLakeTableAction](https://github.com/apache/iceberg/blob/apache-iceberg-1.12.0/delta-lake/src/main/java/org/apache/iceberg/delta/BaseSnapshotDeltaLakeTableAction.java#L414).
   
   The loop over a column's row groups is in 
[ParquetMetrics](https://github.com/apache/iceberg/blob/apache-iceberg-1.12.0/parquet/src/main/java/org/apache/iceberg/parquet/ParquetMetrics.java#L355-L377).
   
   - It drops the column's metrics only when a row group's statistics are null 
or empty; #8559 added the empty case.
   - Otherwise it takes min and max only `if (stats.hasNonNullValue())`. The 
variant loop at 
[L623-L643](https://github.com/apache/iceberg/blob/apache-iceberg-1.12.0/parquet/src/main/java/org/apache/iceberg/parquet/ParquetMetrics.java#L623-L643)
 does the same.
   - The row-group filter already treats such statistics as undefined, 
[ParquetMetricsRowGroupFilter](https://github.com/apache/iceberg/blob/apache-iceberg-1.12.0/parquet/src/main/java/org/apache/iceberg/parquet/ParquetMetricsRowGroupFilter.java#L621-L627),
 since #6517 fixed the same NaN case there.
   
   Iceberg's own commits record the right bounds here: `ParquetMetrics` prefers 
the metrics a writer supplies for a field, 
[ParquetMetrics](https://github.com/apache/iceberg/blob/apache-iceberg-1.12.0/parquet/src/main/java/org/apache/iceberg/parquet/ParquetMetrics.java#L250-L262),
 and a Spark append of the same four rows into an Iceberg table records 1.0 to 
100.0. That append wrote one row group, so `add_files` of its file records no 
bounds and the filter still finds the row. A different file that Iceberg wrote 
with two row groups, one of them holding a NaN, does get the short bound when 
it is registered again through `add_files`.
   
   Bounds must hold every non-null, non-NaN value in the file, [Field-level 
Metrics and 
Statistics](https://iceberg.apache.org/spec/#field-level-metrics-and-statistics),
 and readers may skip a file by them, [Scan 
Planning](https://iceberg.apache.org/spec/#scan-planning). I'd propose that a 
column keep its counts and get no bounds when one of its row groups holds 
non-null values but no usable min or max: `minMaxUndefined` in the row-group 
filter, and not `allNulls`. apache/iceberg-python#4313 made the same change in 
PyIceberg.
   
   This report was drafted with an AI assistant, which also ran the 
reproduction on 1.12.0 and checked the linked lines. Not run: `migrate`, which 
takes the same path as `snapshot`.
   
   ### Willingness to contribute
   
   - [x] I can contribute a fix for this bug independently
   - [ ] I would be willing to contribute a fix for this bug with guidance from 
the Iceberg community
   - [ ] I cannot contribute a fix for this bug at this time
   


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