rich7420 commented on code in PR #7155:
URL: https://github.com/apache/datafusion-comet/pull/7155#discussion_r4759579068


##########
docs/source/user-guide/latest/iceberg.md:
##########
@@ -519,7 +519,34 @@ output. A write that runs natively shows 
`CometIcebergWrite` under `IcebergCommi
 plan; an ineligible write keeps `IcebergWrite`.
 
 The native writer reads its input as Arrow batches from a Comet operator, so 
the write's input
-must itself run in Comet. A write whose input is a local relation, such as 
`INSERT ... VALUES` or
+must supply Arrow data. When Spark inserts `AssertNotNull` checks for nullable 
input written to
+required fields, those checks remain in Spark. Eligible output is converted to 
Arrow at the writer
+boundary, allowing native writes without a shuffle.
+
+For a partitioned clustered writer, this new conversion also requires a Spark 
sort to fully consume
+each writer partition before writing; only local or global limits may wrap 
that sort. Unsorted or
+otherwise deferred row input keeps the iceberg-java writer. For example, a row 
that violates
+partition order must fail before a later row's NOT NULL assertion; filling an 
Arrow batch first
+could reverse those errors. Unpartitioned and fanout writers do not require 
this additional
+condition.
+
+Writes with `fixed(N)` fields, including nested fields, keep iceberg-java when 
this row conversion
+would be needed, even with a sort. A row with the wrong byte length must still 
report the Java
+writer's validation failure before a later assertion or Arrow conversion 
error. These restrictions
+preserve the exception type and meaningful message for this newly enabled row 
conversion. They do
+not guarantee identical errors for every existing native write: writes that 
already supply Arrow
+batches retain their eligibility and can report an Arrow cast error instead of 
iceberg-java's
+Parquet error.
+
+This conversion requires proof that string values, including nested strings, 
contain valid UTF-8.
+For example, it accepts input from Comet operators through recognized Spark 
projections that
+preserve those strings or use validated literals. Extracting existing map keys 
and rebuilding a map
+from arrays whose strings are already proven valid also qualify. Unproven 
sources or
+string-producing expressions, such as Spark text input, Spark binary-to-string 
casts, or unproven
+string-bearing collection transforms, keep the iceberg-java writer to preserve 
the original bytes.
+The same table and schema eligibility checks apply.

Review Comment:
   These three declines aren't classified under "Plans for each fallback" yet. 
Worth adding them there?



##########
spark/src/test/scala/org/apache/comet/CometIcebergWriteActionSuite.scala:
##########
@@ -104,6 +111,702 @@ class CometIcebergWriteActionSuite
     }
   }
 
+  Seq(false, true).foreach { adaptive =>
+    Seq(
+      "id INT NOT NULL, s STRUCT<a: INT, b: STRING>",
+      "id INT NOT NULL, s STRUCT<a: INT NOT NULL, b: STRING>",
+      "id INT, s STRUCT<a: INT, b: STRING> NOT NULL").zipWithIndex.foreach {
+      case (schema, index) =>
+        test(s"native Iceberg writer: Spark validates nullable input $index, 
AQE=$adaptive") {

Review Comment:
   12 of the 15 `AQE=true` tests showed no `AdaptiveSparkPlanExec` for me. 
Maybe force AQE or drop those copies?



##########
spark/src/test/scala/org/apache/comet/CometIcebergWriteActionSuite.scala:
##########
@@ -104,6 +111,702 @@ class CometIcebergWriteActionSuite
     }
   }
 
+  Seq(false, true).foreach { adaptive =>
+    Seq(
+      "id INT NOT NULL, s STRUCT<a: INT, b: STRING>",
+      "id INT NOT NULL, s STRUCT<a: INT NOT NULL, b: STRING>",
+      "id INT, s STRUCT<a: INT, b: STRING> NOT NULL").zipWithIndex.foreach {
+      case (schema, index) =>
+        test(s"native Iceberg writer: Spark validates nullable input $index, 
AQE=$adaptive") {
+          assume(icebergAvailable)
+          withSQLConf(SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> 
adaptive.toString) {
+            withIcebergCatalog { _ =>
+              withTempPath { path =>
+                sql("SELECT 1 AS id, named_struct('a', 2, 'b', 'value') AS 
s").write.parquet(
+                  path.toString)
+                val input = spark.read.parquet(path.toString)
+                withTempView("required_write_source") {
+                  input.createOrReplaceTempView("required_write_source")
+                  Seq("", "PARTITIONED BY (bucket(4, 
id))").zipWithIndex.foreach {
+                    case (partition, partIndex) =>
+                      val table = 
s"required_input_${index}_${partIndex}_$adaptive"
+                      val reference = s"${table}_jvm"
+                      Seq(table, reference).foreach { name =>
+                        sql(s"""CREATE TABLE cat.db.$name ($schema) USING 
iceberg $partition
+                          |TBLPROPERTIES 
('write.distribution-mode'='none')""".stripMargin)
+                      }
+                      def appendBoth(name: String): Unit = {
+                        sql(s"INSERT INTO cat.db.$name SELECT * FROM 
required_write_source")
+                        input.writeTo(s"cat.db.$name").append()
+                      }
+                      val snapshot = withNativeEnabled {
+                        captureWrite(table)(appendBoth(table))
+                      }
+                      assert(snapshot.snapshotDelta == 2)
+                      assertSparkRequiredWrite(snapshot.plans)
+                      assert(
+                        snapshot.plans.count(p =>
+                          collectWithSubqueries(p) { case w: 
CometIcebergWriteExec =>
+                            w
+                          }.nonEmpty) == 2,
+                        snapshot.plans.mkString("\n"))
+                      withSQLConf(CometConf.COMET_ENABLED.key -> "false") {
+                        appendBoth(reference)
+                      }
+                      checkAnswer(
+                        sql(s"SELECT * FROM cat.db.$table"),
+                        sql(s"SELECT * FROM cat.db.$reference"))
+                  }
+                }
+              }
+            }
+          }
+        }
+    }
+
+    test(s"native Iceberg writer: required input keeps the default shuffle, 
AQE=$adaptive") {
+      assume(icebergAvailable)
+      withSQLConf(SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> adaptive.toString) 
{
+        withIcebergCatalog { _ =>
+          withTempPath { path =>
+            sql("SELECT 1 AS id UNION ALL SELECT 
2").write.parquet(path.toString)
+            val table = s"required_shuffle_$adaptive"
+            sql(s"""CREATE TABLE cat.db.$table (id INT NOT NULL)
+              |USING iceberg PARTITIONED BY (bucket(4, id))""".stripMargin)
+            val snapshot = withNativeEnabled {
+              captureWrite(table) {
+                
spark.read.parquet(path.toString).writeTo(s"cat.db.$table").append()
+              }
+            }
+            assert(snapshot.snapshotDelta == 1)
+            assertSparkRequiredWrite(snapshot.plans, requireBridge = false)

Review Comment:
   All 40 tests still passed for me with the `CometNativeExec` check in 
`needsRowConversion` removed. Maybe assert no bridge here?



##########
spark/src/main/scala/org/apache/spark/sql/comet/CometSparkToColumnarExec.scala:
##########
@@ -132,6 +135,73 @@ case class CometSparkToColumnarExec(child: SparkPlan)
 
 object CometSparkToColumnarExec extends CometSink[SparkPlan] with 
DataTypeSupport {
 
+  private object RowTypeSupport extends DataTypeSupport with CometTypeShim
+
+  // RowArrowReader recursively writes the collection types supported by 
DataTypeSupport.
+  def isRowSchemaSupported(schema: StructType, fallbackReasons: 
ListBuffer[String]): Boolean =
+    RowTypeSupport.isSchemaSupported(schema, fallbackReasons)

Review Comment:
   #6726 is on main now. Only `CalendarIntervalType` differs. All 41 tests 
passed for me using `isSchemaSupported`. Still needed?



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