hudi-agent commented on code in PR #19305: URL: https://github.com/apache/hudi/pull/19305#discussion_r3768569611
########## hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/hudi/functional/TestComplexKeyGenExistingTableUpgrade.scala: ########## @@ -0,0 +1,181 @@ +/* + * 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.hudi.functional + +import org.apache.hudi.DataSourceWriteOptions +import org.apache.hudi.common.testutils.HoodieTestDataGenerator +import org.apache.hudi.common.testutils.RawTripTestPayload.recordsToStrings +import org.apache.hudi.config.HoodieWriteConfig +import org.apache.hudi.keygen.KeyGenUtils +import org.apache.hudi.storage.StoragePath +import org.apache.hudi.testutils.HoodieSparkClientTestBase +import org.apache.spark.sql.{DataFrame, SaveMode} +import org.junit.jupiter.api.Assertions.{assertEquals, assertTrue} +import org.junit.jupiter.api.{AfterEach, BeforeEach, Test} + +import scala.collection.JavaConverters._ + +/** + * Reproduces the existing-table upgrade scenario: + * - existing tables created/written with Hudi 0.14.1 (single-field ComplexKeyGenerator, + * so _hoodie_record_key is stored as the BARE value, e.g. "prod-001"), + * - single record key field, single partition path field, ComplexKeyGenerator passed explicitly, + * - then the upgraded app keeps UPSERTING the SAME records. + * + * Goal: show whether continued writes keep the key format stable (no duplicates) under the + * default (auto-deduce ON) path, and that turning the safety nets OFF reproduces duplicates. + */ +class TestComplexKeyGenExistingTableUpgrade extends HoodieSparkClientTestBase { + + private val recordKeyField = "_row_key" + private val partitionPathField = "partition" + + var commonOpts: Map[String, String] = Map( + "hoodie.insert.shuffle.parallelism" -> "4", + "hoodie.upsert.shuffle.parallelism" -> "4", + DataSourceWriteOptions.PRECOMBINE_FIELD.key -> "timestamp", + DataSourceWriteOptions.RECORDKEY_FIELD.key -> recordKeyField, + DataSourceWriteOptions.PARTITIONPATH_FIELD.key -> partitionPathField, + DataSourceWriteOptions.KEYGENERATOR_CLASS_NAME.key -> "org.apache.hudi.keygen.ComplexKeyGenerator", + HoodieWriteConfig.TBL_NAME.key -> "existing_table_upgrade_test" + ) + + @BeforeEach + override def setUp(): Unit = { + initPath() + initSparkContexts() + initTestDataGenerator() + initHoodieStorage() + } + + @AfterEach + override def tearDown(): Unit = { + cleanupResources() + } + + /** Writes the initial 0.14.1-style commit: bare-value record keys (new.encoding=true), no auto-deduce. */ + private def writeInitial0141Table(dataGen: HoodieTestDataGenerator) = { + val records = dataGen.generateInserts("001", 100) + val inputDF = sparkSession.read.json( + sparkSession.sparkContext.parallelize(recordsToStrings(records).asScala.toList, 2)) + inputDF.write.format("org.apache.hudi") + .options(commonOpts) + .option(HoodieWriteConfig.COMPLEX_KEYGEN_NEW_ENCODING.key, "true") // 0.14.1 stored bare value + .option(HoodieWriteConfig.COMPLEX_KEYGEN_AUTO_DEDUCE_ENCODING.key, "false") + .option(HoodieWriteConfig.ENABLE_COMPLEX_KEYGEN_VALIDATION.key, "false") + .option(DataSourceWriteOptions.OPERATION.key, DataSourceWriteOptions.INSERT_OPERATION_OPT_VAL) + .mode(SaveMode.Overwrite) + .save(basePath) + records + } + + private def readTable(): DataFrame = + sparkSession.read.format("org.apache.hudi").load(basePath) + + /** SAFE PATH: upgrade app keeps defaults (auto-deduce ON) -> bare value preserved, no duplicates. */ + @Test + def testUpgradeWithDefaultsNoDuplicates(): Unit = { + val dataGen = new HoodieTestDataGenerator(0xDEED) + val inserted = writeInitial0141Table(dataGen) + + val (t0, d0, bare0, pref0) = keyStatsRaw() + println(s"[DEFAULTS] after 0.14.1 insert: total=$t0 distinctKeys=$d0 bareKeys=$bare0 prefixedKeys=$pref0") + assertEquals(100L, t0) Review Comment: 🤖 nit: the `println` calls scattered through `testUpgradeWithDefaultsNoDuplicates` and `testUpgradeWithSafetyNetsOffReproducesDuplicates` look like debugging leftovers — they'd clutter CI output. Could you remove them or promote the key assertions to LOG.info statements? <sub><i>⚠️ AI-generated; verify before applying. React 👍/👎 to flag quality.</i></sub> ########## hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/BaseHoodieWriteClient.java: ########## @@ -1427,6 +1437,45 @@ && isComplexKeyGeneratorWithSingleRecordKeyField(tableConfig)) { } } + private void handleComplexKeygenEncoding(HoodieTableMetaClient metaClient) { Review Comment: 🤖 nit: `handleComplexKeygenEncoding` uses the vague verb "handle" — could you rename it to something more precise like `resolveAndApplyComplexKeyEncoding` or `applyAutoDeducedComplexKeyEncoding`? That makes the intent (read cache / deduce / set on config) clearer at the call site in `initTable`. <sub><i>⚠️ AI-generated; verify before applying. React 👍/👎 to flag quality.</i></sub> -- 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]
