voonhous commented on code in PR #19163:
URL: https://github.com/apache/hudi/pull/19163#discussion_r3870791600


##########
hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/dml/insert/TestBulkInsertRowWriterCommitCoverage.scala:
##########
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.spark.sql.hudi.dml.insert
+
+import org.apache.hudi.HoodieCLIUtils
+import org.apache.hudi.common.util.{Option => HOption}
+
+import org.apache.spark.sql.hudi.common.HoodieSparkSqlTestBase
+
+/**
+ * Focused coverage for the row-writer bulk-insert commit executors in
+ * `org.apache.hudi.commit`: 
[[org.apache.hudi.commit.BaseDatasetBulkInsertCommitActionExecutor]]
+ * and 
[[org.apache.hudi.commit.DatasetBulkInsertOverwriteCommitActionExecutor]].
+ *
+ * All tests set `hoodie.spark.sql.insert.into.operation = bulk_insert` and 
keep the default
+ * row-writer path enabled, so INSERT / INSERT OVERWRITE flow through the 
Dataset-based executors.
+ */
+class TestBulkInsertRowWriterCommitCoverage extends HoodieSparkSqlTestBase {
+
+  test("Test row-writer bulk_insert into partitioned table") {
+    withSQLConf(
+      "hoodie.metadata.enable" -> "false",
+      "hoodie.spark.sql.insert.into.operation" -> "bulk_insert",
+      "hoodie.bulkinsert.shuffle.parallelism" -> "1") {
+      Seq("cow", "mor").foreach { tableType =>
+        withTempDir { tmp =>
+          val tableName = generateTableName
+          spark.sql(
+            s"""
+               |create table $tableName (
+               |  id int,
+               |  name string,
+               |  price double,
+               |  dt string
+               |) using hudi
+               | partitioned by (dt)
+               | location '${tmp.getCanonicalPath}'
+               | tblproperties (type = '$tableType', primaryKey = 'id')
+             """.stripMargin)
+
+          spark.sql(
+            s"""insert into $tableName values
+               | (1, 'a1', 10.0, '2024-01-01'),
+               | (2, 'a2', 20.0, '2024-01-01'),
+               | (3, 'a3', 30.0, '2024-01-02')
+             """.stripMargin)
+
+          checkAnswer(s"select id, name, price, dt from $tableName order by 
id")(
+            Seq(1, "a1", 10.0, "2024-01-01"),
+            Seq(2, "a2", 20.0, "2024-01-01"),
+            Seq(3, "a3", 30.0, "2024-01-02")
+          )
+        }
+      }
+    }
+  }
+
+  test("Test row-writer insert overwrite with dynamic partitions") {
+    // Dynamic overwrite mode is required so that only the partitions present 
in the
+    // incoming data are replaced; the default (static) mode overwrites the 
whole table.
+    withSQLConf(
+      "hoodie.metadata.enable" -> "false",
+      "hoodie.spark.sql.insert.into.operation" -> "bulk_insert",
+      "hoodie.datasource.overwrite.mode" -> "dynamic") {
+      withTempDir { tmp =>
+        val tableName = generateTableName
+        spark.sql(
+          s"""
+             |create table $tableName (
+             |  id int,
+             |  name string,
+             |  dt string
+             |) using hudi
+             | partitioned by (dt)
+             | location '${tmp.getCanonicalPath}'
+             | tblproperties (type = 'cow', primaryKey = 'id')
+           """.stripMargin)
+
+        spark.sql(
+          s"""insert into $tableName values
+             | (1, 'a1', '2024-01-01'),
+             | (2, 'a2', '2024-01-02')
+           """.stripMargin)
+
+        // Dynamic insert overwrite: only the partitions present in the 
incoming data
+        // (2024-01-01) are replaced; 2024-01-02 must survive.
+        spark.sql(
+          s"""insert overwrite table $tableName partition (dt)
+             | select 1 as id, 'a1_new' as name, '2024-01-01' as dt union all
+             | select 3 as id, 'a3' as name, '2024-01-01' as dt
+           """.stripMargin)
+
+        checkAnswer(s"select id, name, dt from $tableName order by id")(
+          Seq(1, "a1_new", "2024-01-01"),
+          Seq(2, "a2", "2024-01-02"),
+          Seq(3, "a3", "2024-01-01")
+        )
+      }
+    }
+  }
+
+  test("Test row-writer insert overwrite with static partition") {
+    withSQLConf(
+      "hoodie.metadata.enable" -> "false",
+      "hoodie.spark.sql.insert.into.operation" -> "bulk_insert") {
+      withTempDir { tmp =>
+        val tableName = generateTableName
+        spark.sql(
+          s"""
+             |create table $tableName (
+             |  id int,
+             |  name string,
+             |  dt string
+             |) using hudi
+             | partitioned by (dt)
+             | location '${tmp.getCanonicalPath}'
+             | tblproperties (type = 'cow', primaryKey = 'id')
+           """.stripMargin)
+
+        spark.sql(
+          s"""insert into $tableName values
+             | (1, 'a1', '2024-01-01'),
+             | (2, 'a2', '2024-01-01'),
+             | (3, 'a3', '2024-01-02')
+           """.stripMargin)
+
+        // Static partition spec -> STATIC_OVERWRITE_PARTITION_PATHS drives 
the static
+        // branch of getPartitionToReplacedFileIds. Only 2024-01-01 is 
replaced.
+        spark.sql(
+          s"""insert overwrite table $tableName partition (dt = '2024-01-01')
+             | select 9 as id, 'a9' as name
+           """.stripMargin)
+
+        checkAnswer(s"select id, name, dt from $tableName order by id")(
+          Seq(3, "a3", "2024-01-02"),
+          Seq(9, "a9", "2024-01-01")
+        )
+      }
+    }
+  }
+
+  test("Test row-writer insert overwrite rejected when overlapping pending 
clustering") {

Review Comment:
   Reworked and moved to `TestInsertTable2` as `Test bulk insert with insert 
overwrite against pending clustering`: two partitions, plan scheduled on 
`dt=2021-07-18` via `run_clustering(selected_partitions => ...)`, parameterized 
over `hoodie.datasource.overwrite.mode` static/dynamic (static uses `partition 
(dt = ...)`, dynamic `partition (dt)` with the value in the rows, so each mode 
resolves through its own arm of `resolveTargetPartitions`). Overwriting 
`dt=2021-07-19` succeeds, asserts `INSERT_OVERWRITE`, 
`partitionToReplaceFileIds == {dt=2021-07-19}` and that the plan is still 
pending; overwriting `dt=2021-07-18` is rejected with rows unchanged.
   
   The unpartitioned arm is a third test that forces 
`hoodie.datasource.write.operation=insert_overwrite`, the only way SQL reaches 
the base-class `resolveTargetPartitions` on an unpartitioned table.
   
   The deduced `INSERT_OVERWRITE_TABLE` leg is deliberately absent: on the 
row-writer path it runs under `SaveMode.Overwrite`, `handleSaveModes` exempts 
only `operation == INSERT_OVERWRITE_TABLE` from `fs.delete(tablePath)`, and 
`ProvidesHoodieConfig` has rewritten the operation to `BULK_INSERT`, so the 
table is deleted and re-initialized before the executor runs and the pending 
plan goes with it. The existing "insert overwrite table" tests in this file 
pass for the same reason (the deletion warning fires in both). Tracked under 
#15984.
   
   The dynamic-mode overwrite is the other survivor, `Test bulk insert with 
insert overwrite partition in dynamic mode`, next to the static one at `:644`.
   



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

Reply via email to