lvyanquan commented on PR #4610:
URL: https://github.com/apache/flink-cdc/pull/4610#issuecomment-6600174599

   Thanks for fixing this! The NPE issue is clear and the fix works correctly.
   
   However, I'd like to suggest a different approach that separates `REPLACE` 
into its own branch/method instead of merging it with `UPDATE` and adding a 
null check. This is how we handle it internally:
   
   **CanalJsonSerializationSchema** — give `REPLACE` its own `case` block:
   
   ```java
   case REPLACE:
       node.set("old", mapper.getNodeFactory().nullNode());
       node.set("data", mapper.createArrayNode()
               .add(getJsonNode(tableSchemaInfo, dataChangeEvent.after())));
       node.set("type", mapper.getNodeFactory().textNode(OP_UPDATE));
       break;
   ```
   
   **DebeziumJsonSerializationSchema** — give `REPLACE` its own converter 
method:
   
   ```java
   // in the switch:
   case REPLACE:
       converter = this::convertReplaceEventToRowData;
       break;
   
   // new method:
   private void convertReplaceEventToRowData(
           DataChangeEvent dataChangeEvent, GenericRowData genericRowData) {
       genericRowData.setField(BEFORE.getPosition(), null);
       genericRowData.setField(AFTER.getPosition(),
               jsonSerializers.get(dataChangeEvent.tableId())
                       .getRowDataFromRecordData(dataChangeEvent.after(), 
false));
       genericRowData.setField(OPERATION.getPosition(), OP_UPDATE);
   }
   ```
   
   **Why this is preferable:**
   
   1. **Clearer responsibility** — `REPLACE` has its own dedicated logic, not 
coupled with `UPDATE`
   2. **Safer** — doesn't rely on the implicit contract that `before() == null` 
for `REPLACE`. If `DataChangeEvent.updateEvent()` ever allows `before == null` 
in the future, the null-check approach would silently skip before-image 
serialization for `UPDATE` events too
   3. **Easier to maintain** — if the output format for `REPLACE` needs 
adjustment (e.g., emitting as `INSERT` / `c`), only the dedicated branch needs 
to change
   4. **Better readability** — the `switch` statement clearly shows how each 
operation type is handled without needing to read into the method body


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