Repository navigation
Labels
area:scanParquet scan / data readingbugSomething isn't workingpriority:criticalData corruption, silent wrong results, security issues
Description
Describe the bug
CometIcebergNativeScan reads INT96 timestamps incorrectly, resulting in ~1170 year offset.
Example:
- Correct (Spark/Java):
3332-12-14 11:33:10.965 - Comet/iceberg-rust:
2163-11-05 13:24:03.545896
What's Affected?
Column values: WRONG - read via iceberg-rust's ArrowReader with wrong TimeUnit
Partition values: CORRECT - stored in manifest files, computed by Iceberg-Java's ParquetUtil.extractTimestampInt96() which correctly converts to microseconds:
// iceberg/parquet/src/main/java/org/apache/iceberg/parquet/ParquetUtil.java:184-185
return TimeUnit.DAYS.toMicros(julianDay - UNIX_EPOCH_JULIAN)
+ TimeUnit.NANOSECONDS.toMicros(timeOfDayNanos);The Bug (Column Values Only)
- arrow-rs defaults INT96 to
Timestamp(Nanosecond, None)(parquet/src/arrow/schema/primitive.rs:122) - arrow-rs PR #7285 fixed arrow-rs to respect schema hints - if you pass
Timestamp(Microsecond), it converts correctly - iceberg-rust passes the Parquet-derived schema to
ArrowReaderOptions.with_schema()(reader.rs:366) - This schema has
Timestamp(Nanosecond)(from Parquet default), NOTTimestamp(Microsecond)(from Iceberg spec) - So arrow-rs reads INT96 as nanoseconds, but Iceberg expects microseconds
Steps to reproduce
test("migration - INT96 timestamp") {
assume(icebergAvailable, "Iceberg not available in classpath")
withTempIcebergDir { warehouseDir =>
withSQLConf(
"spark.sql.catalog.test_cat" -> "org.apache.iceberg.spark.SparkCatalog",
"spark.sql.catalog.test_cat.type" -> "hadoop",
"spark.sql.catalog.test_cat.warehouse" -> warehouseDir.getAbsolutePath,
CometConf.COMET_ENABLED.key -> "true",
CometConf.COMET_EXEC_ENABLED.key -> "true",
CometConf.COMET_ICEBERG_NATIVE_ENABLED.key -> "true") {
import org.apache.comet.testing.{DataGenOptions, FuzzDataGenerator}
import org.apache.spark.sql.functions.monotonically_increasing_id
import org.apache.spark.sql.types._
val dataPath = s"${warehouseDir.getAbsolutePath}/int96_data"
val numRows = 50
val r = new scala.util.Random(42)
val fuzzSchema = StructType(
Seq(
StructField("outputTimestamp", TimestampType, nullable = true),
StructField("value", DoubleType, nullable = true)))
// Default FuzzDataGenerator baseDate is year 3333, outside the i64 nanosecond
// range (~1677-2262). This triggers the INT96 overflow bug if coercion is missing.
val dataGenOptions = DataGenOptions(allowNull = false)
val fuzzDf =
FuzzDataGenerator.generateDataFrame(r, spark, fuzzSchema, numRows, dataGenOptions)
val df = fuzzDf.withColumn("id", monotonically_increasing_id())
// Write Parquet with INT96 timestamps
withSQLConf(SQLConf.PARQUET_OUTPUT_TIMESTAMP_TYPE.key -> "INT96") {
df.write.mode("overwrite").parquet(dataPath)
}
// Verify the Parquet files actually contain INT96 timestamps
val parquetFiles = new java.io.File(dataPath)
.listFiles()
.filter(f => f.getName.endsWith(".parquet"))
assert(parquetFiles.nonEmpty, "Expected at least one Parquet file")
val parquetFile = parquetFiles.head
val reader = org.apache.parquet.hadoop.ParquetFileReader.open(
org.apache.parquet.hadoop.util.HadoopInputFile.fromPath(
new org.apache.hadoop.fs.Path(parquetFile.getAbsolutePath),
spark.sessionState.newHadoopConf()))
try {
val parquetSchema = reader.getFooter.getFileMetaData.getSchema
val timestampColumn = parquetSchema.getColumns.asScala
.find(_.getPath.mkString(".") == "outputTimestamp")
assert(timestampColumn.isDefined, "Expected outputTimestamp column in Parquet schema")
assert(
timestampColumn.get.getPrimitiveType.getPrimitiveTypeName ==
org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName.INT96,
s"Expected INT96 type for outputTimestamp but got ${timestampColumn.get.getPrimitiveType.getPrimitiveTypeName}")
} finally {
reader.close()
}
// Create an unpartitioned Iceberg table and import the Parquet files
spark.sql("CREATE NAMESPACE IF NOT EXISTS test_cat.db")
spark.sql("""
CREATE TABLE test_cat.db.int96_test (
outputTimestamp TIMESTAMP,
value DOUBLE,
id BIGINT
) USING iceberg
""")
try {
val tableUtilClass = Class.forName("org.apache.iceberg.spark.SparkTableUtil")
val sparkCatalog = spark.sessionState.catalogManager
.catalog("test_cat")
.asInstanceOf[org.apache.iceberg.spark.SparkCatalog]
val ident =
org.apache.spark.sql.connector.catalog.Identifier.of(Array("db"), "int96_test")
val sparkTable = sparkCatalog
.loadTable(ident)
.asInstanceOf[org.apache.iceberg.spark.source.SparkTable]
val table = sparkTable.table()
val stagingDir = s"${warehouseDir.getAbsolutePath}/staging"
spark.sql(s"""CREATE TABLE parquet_temp USING parquet LOCATION '$dataPath'""")
val sourceIdent = new org.apache.spark.sql.catalyst.TableIdentifier("parquet_temp")
val importMethod = tableUtilClass.getMethod(
"importSparkTable",
classOf[org.apache.spark.sql.SparkSession],
classOf[org.apache.spark.sql.catalyst.TableIdentifier],
classOf[org.apache.iceberg.Table],
classOf[String])
importMethod.invoke(null, spark, sourceIdent, table, stagingDir)
val distinctCount = spark
.sql("SELECT COUNT(DISTINCT id) FROM test_cat.db.int96_test")
.collect()(0)
.getLong(0)
assert(
distinctCount == numRows,
s"Expected $numRows distinct IDs but got $distinctCount")
checkIcebergNativeScan("SELECT * FROM test_cat.db.int96_test ORDER BY id")
checkIcebergNativeScan(
"SELECT id, outputTimestamp FROM test_cat.db.int96_test WHERE id < 50 ORDER BY id")
spark.sql("DROP TABLE test_cat.db.int96_test")
spark.sql("DROP TABLE parquet_temp")
} catch {
case _: ClassNotFoundException =>
cancel("SparkTableUtil not available")
}
}
}
}Expected behavior
We should get the same results back as reading through Iceberg Java.
Additional context
Fix Location
The fix needs to be in iceberg-rust, not Comet:
- Modify
reader.rsto overlay Iceberg schema types onto the Parquet-derived schema before passing toArrowReaderOptions - For timestamp columns, ensure
Timestamp(Microsecond, ...)is passed, triggering arrow-rs's INT96 conversion logic from arrow-rs PR #7285
Files to Modify
crates/iceberg/src/arrow/reader.rs- After building
arrow_schemafrom Parquet, overlay Iceberg schema's timestamp types - For columns where Iceberg type is Timestamp/Timestamptz, ensure Arrow schema has
Timestamp(Microsecond, ...)
- After building
Related PRs
- arrow-rs #7285: Support different TimeUnits and timezones when reading Timestamps from INT96
- datafusion #15537: INT96 handling in DataFusion
- comet #1652: INT96 handling in Comet
Metadata
Metadata
Assignees
Labels
area:scanParquet scan / data readingbugSomething isn't workingpriority:criticalData corruption, silent wrong results, security issues
Activity
[-]bug: Native Iceberg reader can return wrong results for migrated Parquet files with INT96 timestamps[/-][+]bug: native Iceberg reader can return wrong results for migrated Parquet files with INT96 timestamps[/+]fix: incorrect Parquet INT96 timestamp values from ArrowReader (#2301)
bump iceberg-rust to latest after picking up fixes for apache#3856 and …
deps: bump iceberg-rust to latest after picking up fixes for #3856 and …
fix: incorrect Parquet INT96 timestamp values from ArrowReader (#2301)
fix: incorrect Parquet INT96 timestamp values from ArrowReader (apach…
fix: incorrect Parquet INT96 timestamp values from ArrowReader (#2301)