mixermt commented on code in PR #7834:
URL: https://github.com/apache/datafusion-comet/pull/7834#discussion_r4789869976


##########
docs/source/user-guide/latest/iceberg.md:
##########
@@ -1012,6 +1019,11 @@ Which entries apply depends on which native paths are 
enabled:
   returns a struct of NULL fields, and an empty list for a list of structs, 
where the native scan
   returns the list with each element's fields NULL
   ([#6794](https://github.com/apache/datafusion-comet/issues/6794)).
+- **Equality deletes on a column that a data file stores in a narrower type, 
with nothing in the
+  table's metadata to show it.** An example is a file appended with field ids 
that carry `INT` for
+  a `BIGINT` column, on a table with one schema and no name mapping. A delete 
file that holds both
+  a value that fits the narrower type and one that does not drops a row that 
no delete value
+  matches ([#7842](https://github.com/apache/datafusion-comet/issues/7842)).

Review Comment:
   A value that doesn't fit is enough on its own. With only `3000000000` in the 
delete file, the native scan drops all three rows of the appended file (`[1,a] 
[2,b] [3,c]`). Your updated test comment already says so. Could #7842 say the 
same?
   
   ```suggestion
     a `BIGINT` column, on a table with one schema and no name mapping. A 
delete file with a value
     that does not fit the narrower type can drop rows that no delete value 
matches
     ([#7842](https://github.com/apache/datafusion-comet/issues/7842)).
   ```



##########
spark/src/test/scala/org/apache/comet/CometIcebergNativeSuite.scala:
##########
@@ -1745,6 +1745,141 @@ class CometIcebergNativeSuite
     }
   }
 
+  private val promotedKeyFallbackReason = "which data files may store in a 
narrower type"
+
+  // The fallback does not depend on the delete values: iceberg-rust compares 
them with a file's
+  // narrower type incorrectly (apache/iceberg-rust#3122), so a delete file 
that mixes values that
+  // fit that type with values that do not drops a row no delete value 
matches, and a file written
+  // with narrower types can lose rows to a delete of values that do not fit.
+  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 =>
+      withNarrowFileCatalog { dir =>
+        val table = "narrow_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(
+          "narrow_cat",
+          "db",
+          "promoted_eq_delete_key",
+          "id",
+          deleteValues,
+          dir)
+        checkSparkAnswerAndFallbackReason(
+          s"SELECT * FROM $table ORDER BY id",
+          promotedKeyFallbackReason)
+        spark.sql(s"DROP TABLE $table")
+      }
+    }
+  }
+
+  // Expiring the snapshots that used the pre-promotion schema removes that 
schema, so the table
+  // shows no type change, only a gap in its schema ids.
+  test("equality delete keyed on a column promoted before expired schemas 
falls back to Spark") {
+    assume(icebergAvailable, "Iceberg not available in classpath")

Review Comment:
   This fails on Spark 3.5 (Iceberg 1.8.1) at the `schemas().size() == 1` 
assert: the table keeps schemas 0 and 1. In 1.8.1, `cleanExpiredMetadata` only 
removes unused partition specs. Schema removal came in 1.9.0 
(apache/iceberg#12089). The PR tier runs only 4.1, so the nightly 3.5 job would 
hit this after merge.
   
   ```suggestion
       assume(icebergAvailable, "Iceberg not available in classpath")
       assume(icebergVersionAtLeast(1, 9), "Removing expired schemas requires 
Iceberg 1.9+")
   ```



##########
spark/src/test/scala/org/apache/comet/CometIcebergNativeSuite.scala:
##########
@@ -1745,6 +1745,141 @@ class CometIcebergNativeSuite
     }
   }
 
+  private val promotedKeyFallbackReason = "which data files may store in a 
narrower type"
+
+  // The fallback does not depend on the delete values: iceberg-rust compares 
them with a file's
+  // narrower type incorrectly (apache/iceberg-rust#3122), so a delete file 
that mixes values that
+  // fit that type with values that do not drops a row no delete value 
matches, and a file written
+  // with narrower types can lose rows to a delete of values that do not fit.
+  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 =>
+      withNarrowFileCatalog { dir =>
+        val table = "narrow_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(
+          "narrow_cat",
+          "db",
+          "promoted_eq_delete_key",
+          "id",
+          deleteValues,
+          dir)
+        checkSparkAnswerAndFallbackReason(
+          s"SELECT * FROM $table ORDER BY id",
+          promotedKeyFallbackReason)
+        spark.sql(s"DROP TABLE $table")
+      }
+    }
+  }
+
+  // Expiring the snapshots that used the pre-promotion schema removes that 
schema, so the table
+  // shows no type change, only a gap in its schema ids.
+  test("equality delete keyed on a column promoted before expired schemas 
falls back to Spark") {
+    assume(icebergAvailable, "Iceberg not available in classpath")
+    withNarrowFileCatalog { dir =>
+      val table = "narrow_cat.db.expired_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')")
+      }
+      Spark3Util
+        .loadIcebergTable(spark, table)
+        .expireSnapshots()
+        .expireOlderThan(System.currentTimeMillis() + 60000)
+        .retainLast(1)
+        .cleanExpiredMetadata(true)
+        .commit()
+      val icebergTable = Spark3Util.loadIcebergTable(spark, table)
+      assert(icebergTable.schemas().size() == 1)
+      assert(icebergTable.schema().schemaId() != 0)
+
+      commitEqualityDeletes(
+        "narrow_cat",
+        "db",
+        "expired_eq_delete_key",
+        "id",
+        Seq(2L, 3000000000L),
+        dir)
+      checkSparkAnswerAndFallbackReason(
+        s"SELECT * FROM $table ORDER BY id",
+        promotedKeyFallbackReason)
+    }
+  }
+
+  // The shape add_files, migrate and snapshot leave: a name mapping, and 
files that store the
+  // column in a narrower type than the table's.
+  test("equality delete keyed on a column of a name-mapped table falls back to 
Spark") {
+    assume(icebergAvailable, "Iceberg not available in classpath")
+    withNarrowFileCatalog { dir =>
+      val table = "narrow_cat.db.mapped_eq_delete_key"
+      spark.sql(
+        s"CREATE TABLE $table (id BIGINT, v STRING) USING iceberg " +
+          "TBLPROPERTIES ('format-version' = '2')")
+      spark.sql(s"INSERT INTO $table VALUES (4, 'd')")
+      val icebergTable = Spark3Util.loadIcebergTable(spark, table)
+      icebergTable
+        .updateProperties()
+        .set(
+          "schema.name-mapping.default",
+          org.apache.iceberg.mapping.NameMappingParser
+            
.toJson(org.apache.iceberg.mapping.MappingUtil.create(icebergTable.schema())))
+        .commit()
+      val fileSchema = appendParquetFile(
+        table,
+        dir,
+        StructType(Seq(StructField("id", IntegerType), StructField("v", 
StringType))),
+        Seq(Row(1, "a"), Row(2, "b"), Row(3, "c")))
+      assert(fileSchema.getFields.asScala.forall(_.getId == null), fileSchema)
+
+      commitEqualityDeletes(
+        "narrow_cat",
+        "db",
+        "mapped_eq_delete_key",
+        "id",
+        Seq(2L, 3000000000L),
+        dir)
+      checkSparkAnswerAndFallbackReason(
+        s"SELECT * FROM $table ORDER BY id",
+        promotedKeyFallbackReason)
+    }

Review Comment:
   No test covers the `int` part of this rule. With `tpe == "int"` removed from 
`mayBeNarrower`, every test still passes, yet an `INT` key on a name-mapped 
table whose file stores `INT8` (Hive `TINYINT`) loses `[1,a]` and `[3,c]` in 
the native scan for deletes `[2, 200]`. This runs the test for both key types. 
Both pass on 3.5 and 4.1, and the `INT` case fails without the `int` check:
   
   ```suggestion
     // column in a narrower type than the table's, such as Hive TINYINT or 
SMALLINT for an INT.
     Seq(
       ("BIGINT", IntegerType, Seq[Any](1, 2, 3), Seq[Any](2L, 3000000000L)),
       ("INT", ByteType, Seq[Any](1.toByte, 2.toByte, 3.toByte), Seq[Any](2, 
200))).foreach {
       case (keyType, fileType, ids, deleteValues) =>
         test(
           s"equality delete keyed on a $keyType column of a name-mapped table 
falls back to Spark") {
           assume(icebergAvailable, "Iceberg not available in classpath")
           withNarrowFileCatalog { dir =>
             val name = s"mapped_${fileType.typeName}_eq_delete_key"
             val table = s"narrow_cat.db.$name"
             spark.sql(
               s"CREATE TABLE $table (id $keyType, v STRING) USING iceberg " +
                 "TBLPROPERTIES ('format-version' = '2')")
             spark.sql(s"INSERT INTO $table VALUES (4, 'd')")
             val icebergTable = Spark3Util.loadIcebergTable(spark, table)
             icebergTable
               .updateProperties()
               .set(
                 "schema.name-mapping.default",
                 org.apache.iceberg.mapping.NameMappingParser
                   
.toJson(org.apache.iceberg.mapping.MappingUtil.create(icebergTable.schema())))
               .commit()
             val fileSchema = appendParquetFile(
               table,
               dir,
               StructType(Seq(StructField("id", fileType), StructField("v", 
StringType))),
               ids.zip(Seq("a", "b", "c")).map { case (id, v) => Row(id, v) })
             assert(fileSchema.getFields.asScala.forall(_.getId == null), 
fileSchema)
   
             commitEqualityDeletes("narrow_cat", "db", name, "id", 
deleteValues, dir)
             checkSparkAnswerAndFallbackReason(
               s"SELECT * FROM $table ORDER BY id",
               promotedKeyFallbackReason)
           }
         }
   ```



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