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


##########
hudi-common/src/main/java/org/apache/hudi/common/model/debezium/PostgresDebeziumAvroPayload.java:
##########
@@ -96,22 +95,31 @@ public Option<IndexedRecord> 
combineAndGetUpdateValue(IndexedRecord currentValue
     Option<IndexedRecord> insertOrDeleteRecord = 
super.combineAndGetUpdateValue(currentValue, schema);
 
     if (insertOrDeleteRecord.isPresent()) {
-      mergeToastedValuesIfPresent(insertOrDeleteRecord.get(), currentValue);
+      return Option.of(mergeToastedValuesIfPresent(insertOrDeleteRecord.get(), 
currentValue));
     }
     return insertOrDeleteRecord;
   }
 
-  private void mergeToastedValuesIfPresent(IndexedRecord incomingRecord, 
IndexedRecord currentRecord) {
-    List<Schema.Field> fields = incomingRecord.getSchema().getFields();
-
-    fields.forEach(field -> {
+  /**
+   * Fills any TOASTed sentinel columns in {@code incomingRecord} with the 
value from
+   * {@code currentRecord}. Returns a NEW record when a fill occurs, so a 
caller (e.g. the record
+   * merger) does not mistake the result for the unchanged incoming record and 
discard the fill;
+   * returns {@code incomingRecord} unchanged when there is nothing to fill.
+   */
+  private IndexedRecord mergeToastedValuesIfPresent(IndexedRecord 
incomingRecord, IndexedRecord currentRecord) {
+    GenericData.Record filled = null;
+    for (Schema.Field field : incomingRecord.getSchema().getFields()) {
       // There are only four avro data types that have unconstrained sizes, 
which are
       // NON-NULLABLE STRING, NULLABLE STRING, NON-NULLABLE BYTES, NULLABLE 
BYTES
       if (((GenericRecord) incomingRecord).get(field.name()) != null
           && (containsStringToastedValues(incomingRecord, field) || 
containsBytesToastedValues(incomingRecord, field))) {
-        ((GenericRecord) incomingRecord).put(field.name(), 
((GenericData.Record) currentRecord).get(field.name()));
+        if (filled == null) {
+          filled = new GenericData.Record((GenericData.Record) incomingRecord, 
false);

Review Comment:
   🤖 Could `incomingRecord` here ever be a `SerializableIndexedRecord` rather 
than a `GenericData.Record`? `BaseAvroPayload.getRecord()` returns a 
`SerializableIndexedRecord` when the stored record's schema isn't 
reference-equal to the requested schema, and in the avro file-group-reader path 
`AvroRecordContext.convertToAvroRecord` just casts `newer.getRecord()`. If so, 
`new GenericData.Record((GenericData.Record) incomingRecord, false)` would 
throw a ClassCastException on a toasted fill. The old in-place code used only 
the `GenericRecord` interface for STRING columns, so it was immune — building 
the copy from `incomingRecord.getSchema()` and copying via the interface might 
be safer.
   
   <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,116 @@
+/*
+ * 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")
+      .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(

Review Comment:
   🤖 nit: `val update` is a singular/verb form for what is actually a sequence 
of records — could you rename it to `val updates` or `val logRecords` to make 
it read as a collection?
   
   <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