voonhous commented on code in PR #19163: URL: https://github.com/apache/hudi/pull/19163#discussion_r3664860637
########## hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/dml/others/TestMergeIntoWriteCoverage.scala: ########## @@ -0,0 +1,242 @@ +/* + * 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.others + +import org.apache.hudi.DataSourceWriteOptions.SPARK_SQL_OPTIMIZED_WRITES + +import org.apache.spark.sql.hudi.common.HoodieSparkSqlTestBase + +/** + * Focused coverage for the MERGE INTO write path implemented by + * [[org.apache.spark.sql.hudi.command.payload.ExpressionPayload]] and + * [[org.apache.spark.sql.hudi.command.MergeIntoKeyGenerator]]. + * + * The scenarios below intentionally combine, within a single statement, all of: + * - conditional matched UPDATE clauses (the update-condition evaluation loop), + * - matched DELETE clauses (both conditional and unconditional delete markers), + * - conditional NOT-MATCHED INSERT clauses (including a not-matched row that + * matches no insert condition and is therefore filtered), + * so both the matched and not-matched evaluators, the delete-marker branch, and the + * record-merge branch are exercised. Running against partitioned COW and MOR tables + * with the row-writer path both enabled and disabled additionally drives the + * record-key / partition-path extraction overloads of MergeIntoKeyGenerator (the + * meta-field-populated branch for matched rows and the key-generator fallback for + * freshly inserted rows). + */ +class TestMergeIntoWriteCoverage extends HoodieSparkSqlTestBase { + + test("Test MergeInto conditional update, delete-marker and insert on partitioned COW") { + Seq(true, false).foreach { optimizedWrites => + withTempDir { tmp => + withSQLConf(SPARK_SQL_OPTIMIZED_WRITES.key() -> optimizedWrites.toString) { + val tableName = generateTableName + spark.sql( + s""" + |create table $tableName ( + | id int, + | name string, + | price double, + | ts int, + | pt string + |) using hudi + | partitioned by (pt) + | location '${tmp.getCanonicalPath}' + | tblproperties ( + | type = 'cow', + | primaryKey = 'id', Review Comment: You're right, thanks. `buildMergeIntoConfig` picks `MergeIntoKeyGenerator` only when `!hasPrimaryKey()`, and all four tests here declare `primaryKey = 'id'`, so every one of them goes through `SqlKeyGenerator`. The claim was wrong. Narrowed rather than added a test: `TestMergeIntoTableWithNonRecordKeyField` already covers the pkless path end to end -- `Test pkless complex merge cond`, `Test pkless multiple source match`, and `Test MergeInto Basic pkless`, between them matched update, not-matched insert, and matched delete. A fifth here would duplicate that. Scaladoc and the PR description now say which generator these tests actually drive and point at that suite. Happy to add the pkless case here instead if you'd rather have it co-located. ########## hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/dml/insert/TestBulkInsertRowWriterCommitCoverage.scala: ########## @@ -0,0 +1,203 @@ +/* + * 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 + * {@code org.apache.hudi.commit}: [[org.apache.hudi.commit.BaseDatasetBulkInsertCommitActionExecutor]] + * and [[org.apache.hudi.commit.DatasetBulkInsertOverwriteCommitActionExecutor]]. + * + * All tests set {@code 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.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") { + withSQLConf("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-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.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") { + withSQLConf( + "hoodie.spark.sql.insert.into.operation" -> "bulk_insert", + "hoodie.compact.inline" -> "false", + "hoodie.compact.schedule.inline" -> "false") { + withTempDir { tmp => + val tableName = generateTableName + val basePath = s"${tmp.getCanonicalPath}/$tableName" + spark.sql( + s""" + |create table $tableName ( + | id int, + | name string, + | dt string + |) using hudi + | partitioned by (dt) + | location '$basePath' + | tblproperties (type = 'cow', primaryKey = 'id') + """.stripMargin) + + // Two separate bulk-insert commits create two file groups in the same partition, + // guaranteeing the size-based planner produces a non-empty clustering plan. + spark.sql(s"insert into $tableName values (1, 'a1', '2024-01-01')") + spark.sql(s"insert into $tableName values (2, 'a2', '2024-01-01')") + + // Schedule (but do not run) a clustering plan so the file groups in 2024-01-01 + // are in pending clustering. + val client = HoodieCLIUtils.createHoodieWriteClient(spark, basePath, Map.empty, Option(tableName)) + try { + assert(client.scheduleClustering(HOption.empty()).isPresent, + "expected a pending clustering plan to be scheduled") + } finally { + client.close() + } + + // An insert overwrite that targets the partition under pending clustering must be + // rejected by the default SparkRejectUpdateStrategy before any write materializes. + checkExceptionContain(new Runnable { Review Comment: Done. It was also the only `new Runnable` left in the spark-datasource tests -- everything else already uses the lambda form. ########## hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/dml/insert/TestBulkInsertRowWriterCommitCoverage.scala: ########## @@ -0,0 +1,207 @@ +/* + * 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 + * {@code org.apache.hudi.commit}: [[org.apache.hudi.commit.BaseDatasetBulkInsertCommitActionExecutor]] Review Comment: Done, backticks for both `{@code ...}` spans in this file. Scaladoc renders the Javadoc form literally, so they were showing up as `{@code ...}` in the generated docs. ########## hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/ddl/TestAlterTableColumnCoverage.scala: ########## @@ -0,0 +1,125 @@ +/* + * 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.ddl + +import org.apache.hudi.HoodieSparkUtils + +import org.apache.spark.sql.catalyst.TableIdentifier +import org.apache.spark.sql.hudi.common.HoodieSparkSqlTestBase + +/** + * Column add / rename / drop / position / comment coverage for + * [[org.apache.spark.sql.hudi.command.AlterTableCommand]] driven through the schema-on-read + * (schema evolution) path. Uses an unpartitioned table so the {@code commitWithSchema} data-schema + * derivation takes the no-partition-column branch, complementing the partitioned coverage in + * {@code TestSpark3DDL}. + */ +class TestAlterTableColumnCoverage extends HoodieSparkSqlTestBase { Review Comment: Done, `[[TestSpark3DDL]]` -- same package, so it resolves. Fixed the `{@code commitWithSchema}` span two lines up while there. ########## hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/dml/others/TestMergeIntoWriteCoverage.scala: ########## @@ -0,0 +1,242 @@ +/* + * 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.others + +import org.apache.hudi.DataSourceWriteOptions.SPARK_SQL_OPTIMIZED_WRITES + +import org.apache.spark.sql.hudi.common.HoodieSparkSqlTestBase + +/** + * Focused coverage for the MERGE INTO write path implemented by + * [[org.apache.spark.sql.hudi.command.payload.ExpressionPayload]] and + * [[org.apache.spark.sql.hudi.command.MergeIntoKeyGenerator]]. + * + * The scenarios below intentionally combine, within a single statement, all of: + * - conditional matched UPDATE clauses (the update-condition evaluation loop), + * - matched DELETE clauses (both conditional and unconditional delete markers), + * - conditional NOT-MATCHED INSERT clauses (including a not-matched row that + * matches no insert condition and is therefore filtered), + * so both the matched and not-matched evaluators, the delete-marker branch, and the + * record-merge branch are exercised. Running against partitioned COW and MOR tables + * with the row-writer path both enabled and disabled additionally drives the + * record-key / partition-path extraction overloads of MergeIntoKeyGenerator (the + * meta-field-populated branch for matched rows and the key-generator fallback for + * freshly inserted rows). + */ +class TestMergeIntoWriteCoverage extends HoodieSparkSqlTestBase { + + test("Test MergeInto conditional update, delete-marker and insert on partitioned COW") { + Seq(true, false).foreach { optimizedWrites => + withTempDir { tmp => + withSQLConf(SPARK_SQL_OPTIMIZED_WRITES.key() -> optimizedWrites.toString) { + val tableName = generateTableName + spark.sql( + s""" + |create table $tableName ( + | id int, + | name string, + | price double, + | ts int, + | pt string + |) using hudi + | partitioned by (pt) + | location '${tmp.getCanonicalPath}' + | tblproperties ( + | type = 'cow', + | primaryKey = 'id', + | preCombineField = 'ts' + | ) + """.stripMargin) + + // Seed: id 1 & 2 in partition p1, id 3 in partition p2. + spark.sql( + s"""insert into $tableName values + | (1, 'a1', 10.0, 1, 'p1'), + | (2, 'a2', 20.0, 1, 'p1'), + | (3, 'a3', 30.0, 1, 'p2') + """.stripMargin) + + // Single MERGE that exercises every clause type at once: + // - id 1 matches "flag = 'u'" -> conditional UPDATE (record merge, higher ts wins) + // - id 2 matches "flag = 'd'" -> conditional DELETE (delete marker) + // - id 3 matches neither cond -> no matched clause fires, record retained + // - id 4 not matched, insert cond -> INSERT into a brand new partition p3 + // - id 5 not matched, no insert cond -> filtered out (not written) + spark.sql( + s""" + | merge into $tableName t + | using ( + | select 1 as id, 'a1_u' as name, 11.0 as price, 2 as ts, 'p1' as pt, 'u' as flag union all + | select 2 as id, 'a2_d' as name, 22.0 as price, 2 as ts, 'p1' as pt, 'd' as flag union all + | select 3 as id, 'a3_x' as name, 33.0 as price, 2 as ts, 'p2' as pt, 'x' as flag union all + | select 4 as id, 'a4_i' as name, 40.0 as price, 2 as ts, 'p3' as pt, 'i' as flag union all + | select 5 as id, 'a5_i' as name, 50.0 as price, 2 as ts, 'p3' as pt, 'n' as flag Review Comment: Done, `a5_n`. Row 5 isn't asserted anywhere, so nothing else changed. -- 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]
