comphead commented on code in PR #7233: URL: https://github.com/apache/datafusion-comet/pull/7233#discussion_r4669999929
########## docs/source/user-guide/latest/iceberg.md: ########## @@ -125,6 +125,23 @@ results, but are not pushed into iceberg-rust, which binds accessors only for pr These residuals provide no native row-group pruning, nor do conjunctions containing them; safe partial pruning is tracked in [#5883](https://github.com/apache/datafusion-comet/issues/5883). +Some predicates are left to the filter above the scan because iceberg-rust would return +different rows than Spark: + +- Range comparisons on `FLOAT` and `DOUBLE` columns, and comparisons with `0.0`, `-0.0` or NaN. +- `NOT` over equality on a nullable column, as in `NOT (id <=> 1)`, unless it is ANDed with an + `IS NOT NULL` check on that column, written in the query or inferred by Spark as for `b = false`. + `!=` reaches iceberg-rust as a separate comparison and is not affected. +- Comparisons other than `=` on a column whose type was promoted. This applies to every `BIGINT`, + `DOUBLE`, `DECIMAL` and `TIMESTAMP_NTZ` column when expiring snapshots removed older schemas, + and to every `INT`, `BIGINT` and `DOUBLE` column of a table with a + `schema.name-mapping.default` property, which `add_files`, `migrate` and `snapshot` set. Review Comment: With `filesMayBeNarrower`, `assumeNarrower` also returns every `timestamp`, `timestamp_ns` and `decimal(...)` column, so a table with a name mapping loses range and `IN` pushdown on `TIMESTAMP_NTZ` columns too. This bullet and the PR description list only `INT`, `BIGINT` and `DOUBLE`. Could both name the incomplete-history types plus `INT`? A `timestamp` column in the new unit test for the flag would also pin that behavior. ########## spark/src/test/scala/org/apache/comet/CometIcebergNativeSuite.scala: ########## @@ -4899,6 +4899,306 @@ class CometIcebergNativeSuite } } + private def checkResidualPredicates(table: String, predicates: Seq[String]): Unit = + for (predicate <- predicates) { + checkIcebergNativeScan(s"SELECT id FROM $table WHERE ($predicate)") + } + + test("floating-point residuals preserve signed zeros in native scans") { + assume(icebergAvailable, "Iceberg not available in classpath") + + withHadoopCatalog("test_cat") { + withSQLConf( + CometConf.COMET_ENABLED.key -> "true", + CometConf.COMET_EXEC_ENABLED.key -> "true", + CometConf.COMET_ICEBERG_NATIVE_ENABLED.key -> "true") { + val table = "test_cat.db.floating_residual_test" + spark.sql(s"CREATE TABLE $table (id INT, f FLOAT, d DOUBLE) USING iceberg") + try { + withSQLConf(CometConf.COMET_ENABLED.key -> "false") { + // One file containing both zeros keeps file pruning from deciding the comparison. + // Distinct ids make a missing signed-zero row observable without comparing floats. + spark + .sql(""" + SELECT id, CAST(v AS FLOAT) AS f, CAST(v AS DOUBLE) AS d + FROM VALUES + (1, '-0.0'), + (2, '0.0'), + (3, '1.0'), + (4, '-1.0'), + (5, NULL), + (6, 'NaN') + AS input(id, v) + """) + .coalesce(1) + .write + .format("iceberg") + .mode("append") + .saveAsTable(table) + assert(spark.sql(s"SELECT file_path FROM $table.files").collect().length == 1) + val rows = spark.sql(s"SELECT id, f, d FROM $table").collect().sortBy(_.getInt(0)) + assert(java.lang.Float.floatToRawIntBits(rows(0).getFloat(1)) == Int.MinValue) + assert(java.lang.Double.doubleToRawLongBits(rows(0).getDouble(2)) == Long.MinValue) + assert(java.lang.Float.floatToRawIntBits(rows(1).getFloat(1)) == 0) + assert(java.lang.Double.doubleToRawLongBits(rows(1).getDouble(2)) == 0L) + } + + for ((column, dataType) <- Seq("f" -> "FLOAT", "d" -> "DOUBLE")) { + val zero = s"CAST(0 AS $dataType)" + val negativeZero = s"CAST('-0.0' AS $dataType)" + val one = s"CAST(1 AS $dataType)" + val minusOne = s"CAST(-1 AS $dataType)" + val predicates = Seq( + s"$column = $zero", + s"$column = $negativeZero", + s"$column >= $zero", + s"$column <= $negativeZero", + s"$column IN ($zero, $one)", + s"NOT ($column < $zero)", + // Nonzero literals push equality and IN; range comparisons stay out. + s"$column = $one", + s"$column IN ($one, $minusOne)", + s"NOT ($column IN ($one, $minusOne) AND id > 1)", + s"$column < $one", + s"$column > $minusOne", + s"NOT ($column = $one)", + s"$column = $zero OR id = 3", + s"NOT ($column = $zero AND id > 1)", + s"$column IS NULL", + s"$column IS NOT NULL") + checkResidualPredicates(table, predicates) + } + } finally { + spark.sql(s"DROP TABLE $table") + } + } + } + } + + test("negated null-safe equality keeps NULL rows in native scans") { + assume(icebergAvailable, "Iceberg not available in classpath") + + withHadoopCatalog("test_cat") { + withSQLConf( + CometConf.COMET_ENABLED.key -> "true", + CometConf.COMET_EXEC_ENABLED.key -> "true", + CometConf.COMET_ICEBERG_NATIVE_ENABLED.key -> "true") { + val table = "test_cat.db.null_safe_residual_test" + spark.sql(s"CREATE TABLE $table (id INT, b BOOLEAN) USING iceberg") + try { + withSQLConf(CometConf.COMET_ENABLED.key -> "false") { + // One file keeps file pruning from deciding the comparison. + spark + .sql(""" + SELECT * FROM VALUES + (NULL, NULL), (1, true), (2, false) + AS t(id, b) + """) + .coalesce(1) + .write + .format("iceberg") + .mode("append") + .saveAsTable(table) + assert(spark.sql(s"SELECT file_path FROM $table.files").collect().length == 1) + } + // Iceberg converts `id <=> 1` to an equality, which is false for a NULL id in Spark but + // NULL in the native reader, so only its negation can drop the NULL row. Spark also + // writes that negation as IS DISTINCT FROM and, for booleans, IS NOT TRUE. A null + // check ANDed with it rejects the NULL row either way. + val predicates = Seq( + "NOT (id <=> 1)", + "NOT (id <=> 1 AND id < 5)", + "NOT (id <=> 1) OR id = 2", + "id IS DISTINCT FROM 1", + "b IS NOT TRUE", + "NOT b", + "b = false", + "id IS NOT NULL AND NOT (id <=> 1)", + "NOT (id = 1)", + "id != 1") + checkResidualPredicates(table, predicates) + // `id != 1` reaches Iceberg as a not-equal, and `b = false` as `b IS NOT NULL AND NOT b`, + // whose null check covers the negated equality, so both are still pushed. A negated + // `<=>` has no such check and stays out. + // Planning commonData leaks manifest streams on Iceberg versions before 1.8.0. + if (icebergVersionAtLeast(1, 8)) { + for ((predicate, pushed) <- Seq( + "b = false" -> true, + "id != 1" -> true, + "id IS NOT NULL AND NOT (id <=> 1)" -> true, + "NOT (id <=> 1)" -> false, + "b IS NOT TRUE" -> false)) { + val (_, cometPlan) = checkSparkAnswer(s"SELECT id FROM $table WHERE $predicate") + val scans = collectIcebergNativeScans(cometPlan) + assert(scans.length == 1, s"$cometPlan") + val common = OperatorOuterClass.IcebergScanCommon.parseFrom(scans.head.commonData) + assert(common.getResidualPoolCount == (if (pushed) 1 else 0), s"$predicate") + } + } + } finally { + spark.sql(s"DROP TABLE $table") + } + } + } + } + + test("residuals on tables with a name mapping keep rows from imported files") { + assume(icebergAvailable, "Iceberg not available in classpath") + + withHadoopCatalog("test_cat") { + withSQLConf( + CometConf.COMET_ENABLED.key -> "true", + CometConf.COMET_EXEC_ENABLED.key -> "true", + CometConf.COMET_ICEBERG_NATIVE_ENABLED.key -> "true") { + val sourceName = "imported_residual_source" + val table = "test_cat.db.imported_residual_test" + withTempDir { dir => + // The file stores id as INT, t as TINYINT and d as FLOAT, while the source table below + // declares them as BIGINT, INT and DOUBLE. snapshotTable imports the file as it is, sets + // the table's name mapping and records no schema change. + val dataPath = new File(dir, "source").getAbsolutePath + withSQLConf(CometConf.COMET_ENABLED.key -> "false") { + spark + .sql("SELECT CAST(id AS INT) AS id, CAST(id AS TINYINT) AS t, " + + "CAST(id AS FLOAT) AS d FROM range(1, 4)") + .coalesce(1) + .write + .parquet(dataPath) + } + withTable(sourceName) { + spark.sql(s"CREATE TABLE $sourceName (id BIGINT, t INT, d DOUBLE) " + + s"USING parquet LOCATION '$dataPath'") + val actions = Class + .forName("org.apache.iceberg.spark.actions.SparkActions") + .getMethod("get") + .invoke(null) + val snapshot = actions.getClass + .getMethod("snapshotTable", classOf[String]) + .invoke(actions, sourceName) + val destination = snapshot.getClass + .getMethod("as", classOf[String]) + .invoke(snapshot, table) + destination.getClass.getMethod("execute").invoke(destination) + try { + val icebergTable = Spark3Util.loadIcebergTable(spark, table) + assert(icebergTable.properties().get("schema.name-mapping.default") != null) + assert(icebergTable.schemas().size() == 1) + + // iceberg-rust skips the imported file's pages for these, or casts 200 and + // 3000000000 to a NULL TINYINT and INT. Range predicates on d are not pushed to the + // native scan with or without a name mapping, so d is covered by IN only. + val predicates = Seq( + "id > 0", + "id < 3000000000", + "id IN (1, 2)", + "t < 200", + "t IN (1, 200)", + "d IN (1.0D, 2.0D)", + "id = 2", + "t = 2", + "d = 2.0D") + for (predicate <- predicates) { + checkIcebergNativeScan(s"SELECT id FROM $table WHERE $predicate") + } + + // Equality stays pushed: it is correct for the imported file. + val df = spark.sql(s"SELECT id FROM $table WHERE id = 2 AND t = 2") + df.collect() + val scans = collectIcebergNativeScans(df.queryExecution.executedPlan) + assert(scans.length == 1) + val common = OperatorOuterClass.IcebergScanCommon.parseFrom(scans.head.commonData) + assert(common.getResidualPoolCount == 1) Review Comment: Since https://github.com/apache/datafusion-comet/pull/7491, which this branch now includes, the common block ships no residual pool entries, so `getResidualPoolCount` is 0 here even when the residual is pushed, and the assertion fails. `assert(distinctResiduals(scans.head).size == 1)` counts the per-partition pools, as `d5bf49ecd6` on https://github.com/apache/datafusion-comet/pull/7156 does for the null-safe test. ########## spark/src/test/scala/org/apache/comet/CometIcebergNativeSuite.scala: ########## @@ -4899,6 +4899,306 @@ class CometIcebergNativeSuite } } + private def checkResidualPredicates(table: String, predicates: Seq[String]): Unit = + for (predicate <- predicates) { + checkIcebergNativeScan(s"SELECT id FROM $table WHERE ($predicate)") + } + + test("floating-point residuals preserve signed zeros in native scans") { + assume(icebergAvailable, "Iceberg not available in classpath") + + withHadoopCatalog("test_cat") { + withSQLConf( + CometConf.COMET_ENABLED.key -> "true", + CometConf.COMET_EXEC_ENABLED.key -> "true", + CometConf.COMET_ICEBERG_NATIVE_ENABLED.key -> "true") { + val table = "test_cat.db.floating_residual_test" + spark.sql(s"CREATE TABLE $table (id INT, f FLOAT, d DOUBLE) USING iceberg") + try { + withSQLConf(CometConf.COMET_ENABLED.key -> "false") { + // One file containing both zeros keeps file pruning from deciding the comparison. + // Distinct ids make a missing signed-zero row observable without comparing floats. + spark + .sql(""" + SELECT id, CAST(v AS FLOAT) AS f, CAST(v AS DOUBLE) AS d + FROM VALUES + (1, '-0.0'), + (2, '0.0'), + (3, '1.0'), + (4, '-1.0'), + (5, NULL), + (6, 'NaN') + AS input(id, v) + """) + .coalesce(1) + .write + .format("iceberg") + .mode("append") + .saveAsTable(table) + assert(spark.sql(s"SELECT file_path FROM $table.files").collect().length == 1) + val rows = spark.sql(s"SELECT id, f, d FROM $table").collect().sortBy(_.getInt(0)) + assert(java.lang.Float.floatToRawIntBits(rows(0).getFloat(1)) == Int.MinValue) + assert(java.lang.Double.doubleToRawLongBits(rows(0).getDouble(2)) == Long.MinValue) + assert(java.lang.Float.floatToRawIntBits(rows(1).getFloat(1)) == 0) + assert(java.lang.Double.doubleToRawLongBits(rows(1).getDouble(2)) == 0L) + } + + for ((column, dataType) <- Seq("f" -> "FLOAT", "d" -> "DOUBLE")) { + val zero = s"CAST(0 AS $dataType)" + val negativeZero = s"CAST('-0.0' AS $dataType)" + val one = s"CAST(1 AS $dataType)" + val minusOne = s"CAST(-1 AS $dataType)" + val predicates = Seq( + s"$column = $zero", + s"$column = $negativeZero", + s"$column >= $zero", + s"$column <= $negativeZero", + s"$column IN ($zero, $one)", + s"NOT ($column < $zero)", + // Nonzero literals push equality and IN; range comparisons stay out. + s"$column = $one", + s"$column IN ($one, $minusOne)", + s"NOT ($column IN ($one, $minusOne) AND id > 1)", + s"$column < $one", + s"$column > $minusOne", + s"NOT ($column = $one)", + s"$column = $zero OR id = 3", + s"NOT ($column = $zero AND id > 1)", + s"$column IS NULL", + s"$column IS NOT NULL") + checkResidualPredicates(table, predicates) + } + } finally { + spark.sql(s"DROP TABLE $table") + } + } + } + } + + test("negated null-safe equality keeps NULL rows in native scans") { + assume(icebergAvailable, "Iceberg not available in classpath") + + withHadoopCatalog("test_cat") { + withSQLConf( + CometConf.COMET_ENABLED.key -> "true", + CometConf.COMET_EXEC_ENABLED.key -> "true", + CometConf.COMET_ICEBERG_NATIVE_ENABLED.key -> "true") { + val table = "test_cat.db.null_safe_residual_test" + spark.sql(s"CREATE TABLE $table (id INT, b BOOLEAN) USING iceberg") + try { + withSQLConf(CometConf.COMET_ENABLED.key -> "false") { + // One file keeps file pruning from deciding the comparison. + spark + .sql(""" + SELECT * FROM VALUES + (NULL, NULL), (1, true), (2, false) + AS t(id, b) + """) + .coalesce(1) + .write + .format("iceberg") + .mode("append") + .saveAsTable(table) + assert(spark.sql(s"SELECT file_path FROM $table.files").collect().length == 1) + } + // Iceberg converts `id <=> 1` to an equality, which is false for a NULL id in Spark but + // NULL in the native reader, so only its negation can drop the NULL row. Spark also + // writes that negation as IS DISTINCT FROM and, for booleans, IS NOT TRUE. A null + // check ANDed with it rejects the NULL row either way. + val predicates = Seq( + "NOT (id <=> 1)", + "NOT (id <=> 1 AND id < 5)", + "NOT (id <=> 1) OR id = 2", + "id IS DISTINCT FROM 1", + "b IS NOT TRUE", + "NOT b", + "b = false", + "id IS NOT NULL AND NOT (id <=> 1)", + "NOT (id = 1)", + "id != 1") + checkResidualPredicates(table, predicates) + // `id != 1` reaches Iceberg as a not-equal, and `b = false` as `b IS NOT NULL AND NOT b`, + // whose null check covers the negated equality, so both are still pushed. A negated + // `<=>` has no such check and stays out. + // Planning commonData leaks manifest streams on Iceberg versions before 1.8.0. + if (icebergVersionAtLeast(1, 8)) { + for ((predicate, pushed) <- Seq( + "b = false" -> true, + "id != 1" -> true, + "id IS NOT NULL AND NOT (id <=> 1)" -> true, + "NOT (id <=> 1)" -> false, + "b IS NOT TRUE" -> false)) { + val (_, cometPlan) = checkSparkAnswer(s"SELECT id FROM $table WHERE $predicate") + val scans = collectIcebergNativeScans(cometPlan) + assert(scans.length == 1, s"$cometPlan") + val common = OperatorOuterClass.IcebergScanCommon.parseFrom(scans.head.commonData) + assert(common.getResidualPoolCount == (if (pushed) 1 else 0), s"$predicate") + } + } + } finally { + spark.sql(s"DROP TABLE $table") + } + } + } + } + + test("residuals on tables with a name mapping keep rows from imported files") { + assume(icebergAvailable, "Iceberg not available in classpath") + + withHadoopCatalog("test_cat") { + withSQLConf( + CometConf.COMET_ENABLED.key -> "true", + CometConf.COMET_EXEC_ENABLED.key -> "true", + CometConf.COMET_ICEBERG_NATIVE_ENABLED.key -> "true") { + val sourceName = "imported_residual_source" + val table = "test_cat.db.imported_residual_test" + withTempDir { dir => + // The file stores id as INT, t as TINYINT and d as FLOAT, while the source table below + // declares them as BIGINT, INT and DOUBLE. snapshotTable imports the file as it is, sets + // the table's name mapping and records no schema change. + val dataPath = new File(dir, "source").getAbsolutePath + withSQLConf(CometConf.COMET_ENABLED.key -> "false") { + spark + .sql("SELECT CAST(id AS INT) AS id, CAST(id AS TINYINT) AS t, " + + "CAST(id AS FLOAT) AS d FROM range(1, 4)") + .coalesce(1) + .write + .parquet(dataPath) + } + withTable(sourceName) { + spark.sql(s"CREATE TABLE $sourceName (id BIGINT, t INT, d DOUBLE) " + + s"USING parquet LOCATION '$dataPath'") + val actions = Class + .forName("org.apache.iceberg.spark.actions.SparkActions") + .getMethod("get") + .invoke(null) + val snapshot = actions.getClass + .getMethod("snapshotTable", classOf[String]) + .invoke(actions, sourceName) + val destination = snapshot.getClass + .getMethod("as", classOf[String]) + .invoke(snapshot, table) + destination.getClass.getMethod("execute").invoke(destination) + try { + val icebergTable = Spark3Util.loadIcebergTable(spark, table) + assert(icebergTable.properties().get("schema.name-mapping.default") != null) + assert(icebergTable.schemas().size() == 1) + + // iceberg-rust skips the imported file's pages for these, or casts 200 and + // 3000000000 to a NULL TINYINT and INT. Range predicates on d are not pushed to the + // native scan with or without a name mapping, so d is covered by IN only. + val predicates = Seq( + "id > 0", + "id < 3000000000", + "id IN (1, 2)", + "t < 200", + "t IN (1, 200)", + "d IN (1.0D, 2.0D)", + "id = 2", + "t = 2", + "d = 2.0D") + for (predicate <- predicates) { + checkIcebergNativeScan(s"SELECT id FROM $table WHERE $predicate") + } Review Comment: This loop is `checkResidualPredicates(table, predicates)`, the helper the other residual tests in this file use. ########## spark/src/test/scala/org/apache/comet/CometIcebergNativeSuite.scala: ########## @@ -4899,6 +4899,306 @@ class CometIcebergNativeSuite } } + private def checkResidualPredicates(table: String, predicates: Seq[String]): Unit = + for (predicate <- predicates) { + checkIcebergNativeScan(s"SELECT id FROM $table WHERE ($predicate)") + } + + test("floating-point residuals preserve signed zeros in native scans") { + assume(icebergAvailable, "Iceberg not available in classpath") + + withHadoopCatalog("test_cat") { + withSQLConf( + CometConf.COMET_ENABLED.key -> "true", + CometConf.COMET_EXEC_ENABLED.key -> "true", + CometConf.COMET_ICEBERG_NATIVE_ENABLED.key -> "true") { + val table = "test_cat.db.floating_residual_test" + spark.sql(s"CREATE TABLE $table (id INT, f FLOAT, d DOUBLE) USING iceberg") + try { + withSQLConf(CometConf.COMET_ENABLED.key -> "false") { + // One file containing both zeros keeps file pruning from deciding the comparison. + // Distinct ids make a missing signed-zero row observable without comparing floats. + spark + .sql(""" + SELECT id, CAST(v AS FLOAT) AS f, CAST(v AS DOUBLE) AS d + FROM VALUES + (1, '-0.0'), + (2, '0.0'), + (3, '1.0'), + (4, '-1.0'), + (5, NULL), + (6, 'NaN') + AS input(id, v) + """) + .coalesce(1) + .write + .format("iceberg") + .mode("append") + .saveAsTable(table) + assert(spark.sql(s"SELECT file_path FROM $table.files").collect().length == 1) + val rows = spark.sql(s"SELECT id, f, d FROM $table").collect().sortBy(_.getInt(0)) + assert(java.lang.Float.floatToRawIntBits(rows(0).getFloat(1)) == Int.MinValue) + assert(java.lang.Double.doubleToRawLongBits(rows(0).getDouble(2)) == Long.MinValue) + assert(java.lang.Float.floatToRawIntBits(rows(1).getFloat(1)) == 0) + assert(java.lang.Double.doubleToRawLongBits(rows(1).getDouble(2)) == 0L) + } + + for ((column, dataType) <- Seq("f" -> "FLOAT", "d" -> "DOUBLE")) { + val zero = s"CAST(0 AS $dataType)" + val negativeZero = s"CAST('-0.0' AS $dataType)" + val one = s"CAST(1 AS $dataType)" + val minusOne = s"CAST(-1 AS $dataType)" + val predicates = Seq( + s"$column = $zero", + s"$column = $negativeZero", + s"$column >= $zero", + s"$column <= $negativeZero", + s"$column IN ($zero, $one)", + s"NOT ($column < $zero)", + // Nonzero literals push equality and IN; range comparisons stay out. + s"$column = $one", + s"$column IN ($one, $minusOne)", + s"NOT ($column IN ($one, $minusOne) AND id > 1)", + s"$column < $one", + s"$column > $minusOne", + s"NOT ($column = $one)", + s"$column = $zero OR id = 3", + s"NOT ($column = $zero AND id > 1)", + s"$column IS NULL", + s"$column IS NOT NULL") + checkResidualPredicates(table, predicates) + } + } finally { + spark.sql(s"DROP TABLE $table") + } + } + } + } + + test("negated null-safe equality keeps NULL rows in native scans") { + assume(icebergAvailable, "Iceberg not available in classpath") + + withHadoopCatalog("test_cat") { + withSQLConf( + CometConf.COMET_ENABLED.key -> "true", + CometConf.COMET_EXEC_ENABLED.key -> "true", + CometConf.COMET_ICEBERG_NATIVE_ENABLED.key -> "true") { + val table = "test_cat.db.null_safe_residual_test" + spark.sql(s"CREATE TABLE $table (id INT, b BOOLEAN) USING iceberg") + try { + withSQLConf(CometConf.COMET_ENABLED.key -> "false") { + // One file keeps file pruning from deciding the comparison. + spark + .sql(""" + SELECT * FROM VALUES + (NULL, NULL), (1, true), (2, false) + AS t(id, b) + """) + .coalesce(1) + .write + .format("iceberg") + .mode("append") + .saveAsTable(table) + assert(spark.sql(s"SELECT file_path FROM $table.files").collect().length == 1) + } + // Iceberg converts `id <=> 1` to an equality, which is false for a NULL id in Spark but + // NULL in the native reader, so only its negation can drop the NULL row. Spark also + // writes that negation as IS DISTINCT FROM and, for booleans, IS NOT TRUE. A null + // check ANDed with it rejects the NULL row either way. + val predicates = Seq( + "NOT (id <=> 1)", + "NOT (id <=> 1 AND id < 5)", + "NOT (id <=> 1) OR id = 2", + "id IS DISTINCT FROM 1", + "b IS NOT TRUE", + "NOT b", + "b = false", + "id IS NOT NULL AND NOT (id <=> 1)", + "NOT (id = 1)", + "id != 1") + checkResidualPredicates(table, predicates) + // `id != 1` reaches Iceberg as a not-equal, and `b = false` as `b IS NOT NULL AND NOT b`, + // whose null check covers the negated equality, so both are still pushed. A negated + // `<=>` has no such check and stays out. + // Planning commonData leaks manifest streams on Iceberg versions before 1.8.0. + if (icebergVersionAtLeast(1, 8)) { + for ((predicate, pushed) <- Seq( + "b = false" -> true, + "id != 1" -> true, + "id IS NOT NULL AND NOT (id <=> 1)" -> true, + "NOT (id <=> 1)" -> false, + "b IS NOT TRUE" -> false)) { + val (_, cometPlan) = checkSparkAnswer(s"SELECT id FROM $table WHERE $predicate") + val scans = collectIcebergNativeScans(cometPlan) + assert(scans.length == 1, s"$cometPlan") + val common = OperatorOuterClass.IcebergScanCommon.parseFrom(scans.head.commonData) + assert(common.getResidualPoolCount == (if (pushed) 1 else 0), s"$predicate") + } + } + } finally { + spark.sql(s"DROP TABLE $table") + } + } + } + } + + test("residuals on tables with a name mapping keep rows from imported files") { + assume(icebergAvailable, "Iceberg not available in classpath") + + withHadoopCatalog("test_cat") { + withSQLConf( + CometConf.COMET_ENABLED.key -> "true", + CometConf.COMET_EXEC_ENABLED.key -> "true", + CometConf.COMET_ICEBERG_NATIVE_ENABLED.key -> "true") { + val sourceName = "imported_residual_source" + val table = "test_cat.db.imported_residual_test" + withTempDir { dir => + // The file stores id as INT, t as TINYINT and d as FLOAT, while the source table below + // declares them as BIGINT, INT and DOUBLE. snapshotTable imports the file as it is, sets + // the table's name mapping and records no schema change. + val dataPath = new File(dir, "source").getAbsolutePath + withSQLConf(CometConf.COMET_ENABLED.key -> "false") { + spark + .sql("SELECT CAST(id AS INT) AS id, CAST(id AS TINYINT) AS t, " + + "CAST(id AS FLOAT) AS d FROM range(1, 4)") + .coalesce(1) + .write + .parquet(dataPath) + } + withTable(sourceName) { + spark.sql(s"CREATE TABLE $sourceName (id BIGINT, t INT, d DOUBLE) " + + s"USING parquet LOCATION '$dataPath'") + val actions = Class + .forName("org.apache.iceberg.spark.actions.SparkActions") + .getMethod("get") + .invoke(null) + val snapshot = actions.getClass + .getMethod("snapshotTable", classOf[String]) + .invoke(actions, sourceName) + val destination = snapshot.getClass + .getMethod("as", classOf[String]) + .invoke(snapshot, table) + destination.getClass.getMethod("execute").invoke(destination) + try { + val icebergTable = Spark3Util.loadIcebergTable(spark, table) + assert(icebergTable.properties().get("schema.name-mapping.default") != null) + assert(icebergTable.schemas().size() == 1) Review Comment: The reflective `snapshotTable` call and the two assertions after it repeat lines 2698 to 2712 of `migration - nested name mapping falls back to Spark` almost line for line, and this is the fourth such call in the suite. A small helper that snapshots a source table into a destination and returns the loaded Iceberg table would serve both tests. -- 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]
