andygrove commented on code in PR #7834:
URL: https://github.com/apache/datafusion-comet/pull/7834#discussion_r4788870288
##########
spark/src/main/scala/org/apache/comet/rules/CometScanRule.scala:
##########
@@ -1236,6 +1241,12 @@ case class CometScanRule(session: SparkSession)
s"Equality delete on nested field '$fieldName' (id
$fid) is not yet " +
"supported by iceberg-rust, which resolves
equality field ids " +
"among top-level columns only."
+ case Some((fieldName, _)) if
promotedKeyIds.contains(fid) =>
+ hasUnsupportedDeletes = true
+ fallbackReasons +=
+ s"Equality delete on column '$fieldName', which data
files may store " +
+ "in a narrower type, is not supported by
iceberg-rust " +
+ "(apache/iceberg-rust#3122)."
Review Comment:
Thanks for rechecking. The upstream fixes are in (apache/iceberg-rust#3507
and #3540), but Comet's pin does not have them yet, so the native scan still
drops the row without this fallback. I'm keeping it until the pin bump in #7798
lands, and the removal PR will drop it together with #7712's check.
##########
spark/src/main/scala/org/apache/comet/iceberg/IcebergReflection.scala:
##########
@@ -1399,6 +1399,45 @@ object IcebergReflection extends Logging {
}
}
+ /**
+ * Field ids of the top-level columns that some data file may store in a
narrower type than the
+ * table does, which iceberg-rust mishandles in an equality delete
(apache/iceberg-rust#3122).
+ * That is every column whose type changed in the schema history. Where the
history cannot show
+ * it, every column of a type a promotion can produce counts as well: after
expired schemas (a
+ * gap in the schema ids), and when `filesMayBeNarrower` because a name
mapping, which
+ * `add_files`, `migrate` and `snapshot` set, means imported files, which
can hold `int` as Hive
+ * TINYINT or SMALLINT.
+ *
+ * Throws on reflection failure.
+ */
+ def promotedFieldIds(table: Any, filesMayBeNarrower: Boolean): Set[Int] = {
+ import scala.jdk.CollectionConverters._
+ val schemasById = getMethod(table.getClass, "schemas")
+ .invoke(table)
+ .asInstanceOf[java.util.Map[_, _]]
+ .asScala
+ val schemaIds =
schemasById.keys.map(_.asInstanceOf[Integer].intValue).toSet
+ val assumeNarrower = filesMayBeNarrower || schemaIds != (0 until
schemaIds.size).toSet
+ val promotable = Set("long", "double", "timestamp", "timestamp_ns")
+ def mayBeNarrower(tpe: String): Boolean =
+ tpe.startsWith("decimal(") || promotable(tpe) || (filesMayBeNarrower &&
tpe == "int")
+
+ schemasById.values.toSeq
+ .flatMap { schema =>
+ getMethod(schema.getClass, "columns")
+ .invoke(schema)
+ .asInstanceOf[java.util.List[_]]
+ .asScala
+ }
+ .groupMap(fieldIdOf)(column => fieldTypeOf(column).toString)
Review Comment:
Applied, thank you. `promotedFieldIds` now uses `groupBy` and `map` on a
`java.util.List[Any]`, as in your suggestion.
##########
spark/src/test/scala/org/apache/comet/CometIcebergNativeSuite.scala:
##########
@@ -1745,6 +1745,45 @@ class CometIcebergNativeSuite
}
}
+ // A delete file that mixes values that fit the pre-promotion type with
values that do not drops a
+ // row that no delete value matches (apache/iceberg-rust#3122).
+ test("equality delete keyed on a promoted column falls back to Spark") {
+ assume(icebergAvailable, "Iceberg not available in classpath")
+
+ Seq(Seq(3000000000L), Seq(2L, 3000000000L), Seq(3000000000L, 2L)).foreach
{ deleteValues =>
+ 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") {
+ val table = "test_cat.db.promoted_eq_delete_key"
+ spark.sql(
+ s"CREATE TABLE $table (id INT, v STRING) USING iceberg " +
+ "TBLPROPERTIES ('format-version' = '2')")
+ withSQLConf(CometConf.COMET_ENABLED.key -> "false") {
+ spark.sql(s"INSERT INTO $table VALUES (1, 'a'), (2, 'b'), (3,
'c')")
+ spark.sql(s"ALTER TABLE $table ALTER COLUMN id TYPE BIGINT")
+ spark.sql(s"INSERT INTO $table VALUES (4, 'd'), (3000000000, 'e')")
+ }
+ commitEqualityDeletes(
+ "test_cat",
+ "db",
+ "promoted_eq_delete_key",
+ "id",
+ deleteValues,
+ warehouseDir)
+ checkIcebergNativeScanFallback(
+ s"SELECT * FROM $table ORDER BY id",
+ s"an equality delete of $deleteValues keyed on a promoted column")
Review Comment:
Done in 9f4b3ccfa5. The test now asserts the fallback reason, and the
comment says the fallback does not depend on the delete values. I added a test
for expired schemas (`expireSnapshots` with `retainLast(1)` and
`cleanExpiredMetadata`, plus an `expireOlderThan`, since `retainLast` alone
kept both schemas) and one for a name-mapped table with an `INT32` file for a
`BIGINT` key. A third test checks that a `BIGINT` key that was never promoted
stays native.
##########
docs/source/user-guide/latest/iceberg.md:
##########
@@ -267,6 +267,13 @@ The following scenarios will fall back to the JVM Iceberg
reader:
- Encrypted tables with 192-bit data keys (no AES-192-GCM in the underlying
crypto)
- Delete files in a format other than Parquet or Puffin (Avro or ORC
positional/equality deletes)
- Equality deletes keyed on a field nested in a struct
+- Equality deletes keyed on a column whose data files may store a narrower
type than the table,
Review Comment:
Agreed that no planning rule can catch it. I filed #7842 and added it under
"Reads that differ from Spark" in the user guide, noting that the pin bump in
#7798 fixes it.
--
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]