hudi-agent commented on code in PR #19280:
URL: https://github.com/apache/hudi/pull/19280#discussion_r3574143515


##########
hudi-common/src/main/java/org/apache/hudi/common/model/HoodieAvroRecordMerger.java:
##########
@@ -71,10 +71,12 @@ public <T> BufferedRecord<T> merge(BufferedRecord<T> older, 
BufferedRecord<T> ne
         if (updatedRecord == previousAvroData || updatedRecord == 
HoodieRecord.SENTINEL) {
           return older;
         }
-        if (updatedRecord == newerAvroRecord) {
-          // simply return the newer record instead of creating a new record
-          return newer;
-        }
+        // Do NOT short-circuit `updatedRecord == newerAvroRecord` here. A 
payload may mutate the
+        // incoming avro record IN PLACE and return that same reference (e.g.
+        // PostgresDebeziumAvroPayload fills the __debezium_unavailable_value 
sentinel for TOASTed
+        // columns from the base value). Returning the original engine-backed 
`newer` in that case
+        // would drop the mutation, because `newerAvroRecord` is a separate 
avro conversion of
+        // `newer.getRecord()`. Always rebuild from `updatedRecord` so an 
in-place mutation survives.
         // Construct a new BufferedRecord with updated value
         T resultRecord = recordContext.convertAvroRecord(updatedRecord);
         return BufferedRecords.fromEngineRecord(resultRecord, updatedValue

Review Comment:
   🤖 With the short-circuit gone, this path now rebuilds via 
`fromEngineRecord(..., updatedValue.isEmpty())`, which sets the result's 
`HoodieOperation` to null (isEmpty() is always false here). The old `return 
newer` preserved `newer`'s operation. Could that matter if `newer` carries an 
UPDATE_BEFORE/-U operation (isDelete() would flip from true to false)? For the 
Debezium/OverwriteWithLatest case it looks harmless, but I wanted to confirm no 
CDC merge path relies on the operation being carried through.
   
   <sub><i>⚠️ AI-generated; verify before applying. React 👍/👎 to flag 
quality.</i></sub>



##########
hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/hudi/functional/TestDebeziumToastedValue.scala:
##########
@@ -0,0 +1,117 @@
+/*
+ * 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.DataSourceWriteOptions.{OPERATION, ORDERING_FIELDS, 
RECORDKEY_FIELD, TABLE_TYPE}
+import org.apache.hudi.common.config.HoodieMetadataConfig
+import org.apache.hudi.common.model.HoodieTableType
+import org.apache.hudi.common.model.debezium.{DebeziumConstants, 
PostgresDebeziumAvroPayload}
+import org.apache.hudi.common.table.HoodieTableMetaClient
+import org.apache.hudi.config.{HoodieCompactionConfig, HoodieWriteConfig}
+import org.apache.hudi.testutils.SparkClientFunctionalTestHarness
+
+import org.apache.spark.sql.SaveMode
+import org.junit.jupiter.api.Assertions.assertEquals
+import org.junit.jupiter.params.ParameterizedTest
+import org.junit.jupiter.params.provider.ValueSource
+
+/**
+ * Functional test for [[PostgresDebeziumAvroPayload]]'s toasted / 
unavailable-value handling on a
+ * Merge-on-Read table.
+ *
+ * Postgres TOAST semantics: when a Debezium UPDATE cannot capture a large 
STRING/BYTES column that
+ * did not change, it ships the sentinel 
[[PostgresDebeziumAvroPayload.DEBEZIUM_TOASTED_VALUE]]
+ * (`__debezium_unavailable_value`) for that column. When the base file and 
delta log are merged at
+ * read time, such columns must KEEP the prior (base) value rather than be 
overwritten with the
+ * sentinel.
+ *
+ * Covers table versions 6, 8 and 9.
+ */
+class TestDebeziumToastedValue extends SparkClientFunctionalTestHarness {
+
+  private val sentinel = PostgresDebeziumAvroPayload.DEBEZIUM_TOASTED_VALUE
+  private val columns = Seq(
+    "id",
+    "name",
+    "city",
+    DebeziumConstants.FLATTENED_OP_COL_NAME,
+    DebeziumConstants.FLATTENED_LSN_COL_NAME)
+
+  @ParameterizedTest
+  @ValueSource(strings = Array("6", "8", "9"))
+  def testToastedColumnKeepsPriorValueOnRead(tableVersion: String): Unit = {
+    val opts = Map(
+      HoodieWriteConfig.WRITE_PAYLOAD_CLASS_NAME.key() -> 
classOf[PostgresDebeziumAvroPayload].getName,
+      HoodieMetadataConfig.ENABLE.key() -> "false",
+      // Preserve the table version across writes: disable auto-upgrade and 
pin the write version so
+      // a later write cannot migrate the table to a newer version mid-test.
+      HoodieWriteConfig.AUTO_UPGRADE_VERSION.key() -> "false",
+      HoodieWriteConfig.WRITE_TABLE_VERSION.key() -> tableVersion)
+
+    // 1. Base records at LSN 10.
+    val base = Seq(
+      (1, "alice", "NYC", "c", 10L),
+      (2, "bob", "LA", "c", 10L))
+    spark.createDataFrame(base).toDF(columns: _*).write.format("hudi")
+      .option(RECORDKEY_FIELD.key(), "id")
+      .option(ORDERING_FIELDS.key(), DebeziumConstants.FLATTENED_LSN_COL_NAME)
+      .option(TABLE_TYPE.key(), HoodieTableType.MERGE_ON_READ.name())
+      .option(DataSourceWriteOptions.TABLE_NAME.key(), "toasted_table")
+      .option(OPERATION.key(), 
DataSourceWriteOptions.BULK_INSERT_OPERATION_OPT_VAL)
+      .option(HoodieCompactionConfig.INLINE_COMPACT.key(), "false")
+      .option(HoodieWriteConfig.WRITE_TABLE_VERSION.key(), tableVersion)

Review Comment:
   🤖 nit: `WRITE_TABLE_VERSION` is already included in `opts` (line 66) and 
applied by `.options(opts)` on the next line — could you drop this redundant 
`.option(...)` call?
   
   <sub><i>⚠️ AI-generated; verify before applying. React 👍/👎 to flag 
quality.</i></sub>



##########
hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/hudi/functional/TestDebeziumToastedValue.scala:
##########
@@ -0,0 +1,117 @@
+/*
+ * 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.DataSourceWriteOptions.{OPERATION, ORDERING_FIELDS, 
RECORDKEY_FIELD, TABLE_TYPE}
+import org.apache.hudi.common.config.HoodieMetadataConfig
+import org.apache.hudi.common.model.HoodieTableType
+import org.apache.hudi.common.model.debezium.{DebeziumConstants, 
PostgresDebeziumAvroPayload}
+import org.apache.hudi.common.table.HoodieTableMetaClient
+import org.apache.hudi.config.{HoodieCompactionConfig, HoodieWriteConfig}
+import org.apache.hudi.testutils.SparkClientFunctionalTestHarness
+
+import org.apache.spark.sql.SaveMode
+import org.junit.jupiter.api.Assertions.assertEquals
+import org.junit.jupiter.params.ParameterizedTest
+import org.junit.jupiter.params.provider.ValueSource
+
+/**
+ * Functional test for [[PostgresDebeziumAvroPayload]]'s toasted / 
unavailable-value handling on a
+ * Merge-on-Read table.
+ *
+ * Postgres TOAST semantics: when a Debezium UPDATE cannot capture a large 
STRING/BYTES column that
+ * did not change, it ships the sentinel 
[[PostgresDebeziumAvroPayload.DEBEZIUM_TOASTED_VALUE]]
+ * (`__debezium_unavailable_value`) for that column. When the base file and 
delta log are merged at
+ * read time, such columns must KEEP the prior (base) value rather than be 
overwritten with the
+ * sentinel.
+ *
+ * Covers table versions 6, 8 and 9.
+ */
+class TestDebeziumToastedValue extends SparkClientFunctionalTestHarness {
+
+  private val sentinel = PostgresDebeziumAvroPayload.DEBEZIUM_TOASTED_VALUE
+  private val columns = Seq(
+    "id",
+    "name",
+    "city",
+    DebeziumConstants.FLATTENED_OP_COL_NAME,
+    DebeziumConstants.FLATTENED_LSN_COL_NAME)
+
+  @ParameterizedTest
+  @ValueSource(strings = Array("6", "8", "9"))
+  def testToastedColumnKeepsPriorValueOnRead(tableVersion: String): Unit = {
+    val opts = Map(
+      HoodieWriteConfig.WRITE_PAYLOAD_CLASS_NAME.key() -> 
classOf[PostgresDebeziumAvroPayload].getName,
+      HoodieMetadataConfig.ENABLE.key() -> "false",
+      // Preserve the table version across writes: disable auto-upgrade and 
pin the write version so
+      // a later write cannot migrate the table to a newer version mid-test.
+      HoodieWriteConfig.AUTO_UPGRADE_VERSION.key() -> "false",
+      HoodieWriteConfig.WRITE_TABLE_VERSION.key() -> tableVersion)
+
+    // 1. Base records at LSN 10.
+    val base = Seq(
+      (1, "alice", "NYC", "c", 10L),
+      (2, "bob", "LA", "c", 10L))
+    spark.createDataFrame(base).toDF(columns: _*).write.format("hudi")
+      .option(RECORDKEY_FIELD.key(), "id")
+      .option(ORDERING_FIELDS.key(), DebeziumConstants.FLATTENED_LSN_COL_NAME)
+      .option(TABLE_TYPE.key(), HoodieTableType.MERGE_ON_READ.name())
+      .option(DataSourceWriteOptions.TABLE_NAME.key(), "toasted_table")
+      .option(OPERATION.key(), 
DataSourceWriteOptions.BULK_INSERT_OPERATION_OPT_VAL)
+      .option(HoodieCompactionConfig.INLINE_COMPACT.key(), "false")
+      .option(HoodieWriteConfig.WRITE_TABLE_VERSION.key(), tableVersion)
+      .options(opts)
+      .mode(SaveMode.Overwrite)
+      .save(basePath)
+
+    // 2. Newer log records at LSN 20, each carrying the toasted sentinel in 
one column:
+    //    id=1 -> name updated, `city` toasted (must keep base "NYC")
+    //    id=2 -> name toasted (must keep base "bob"), `city` updated
+    val update = Seq(
+      (1, "alice2", sentinel, "u", 20L),
+      (2, sentinel, "SF", "u", 20L))
+    spark.createDataFrame(update).toDF(columns: _*).write.format("hudi")
+      .option(OPERATION.key(), "upsert")

Review Comment:
   🤖 nit: the base write on line 77 uses 
`DataSourceWriteOptions.BULK_INSERT_OPERATION_OPT_VAL` — could you use 
`DataSourceWriteOptions.UPSERT_OPERATION_OPT_VAL` here instead of the bare 
string `"upsert"` for consistency?
   
   <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]

Reply via email to