tkaymak commented on code in PR #41331:
URL: https://github.com/apache/beam/pull/41331#discussion_r4706450403


##########
sdks/java/io/debezium/src/main/java/org/apache/beam/io/debezium/DebeziumIO.java:
##########
@@ -327,24 +331,45 @@ public Read<T> withFullMetadata(boolean fullMetadata) {
     }
 
     /**
-     * Returns the format function to use, taking {@link 
#withFullMetadata(boolean)} into account.
+     * Adds the schema change payload ({@code databaseName}, {@code 
schemaName}, {@code ddl}, {@code
+     * ts_ms} and {@code tableChanges}) to schema change events when reading 
as JSON.
+     *
+     * <p>Only applies to {@link DebeziumIO#readAsJson()} / {@link
+     * SourceRecordJson.SourceRecordJsonMapper}; combining it with a custom 
{@link
+     * #withFormatFunction(SourceRecordMapper)} fails with an {@link 
IllegalArgumentException} when
+     * the transform is expanded. Default false.
+     *
+     * @param schemaChangeEvents Whether to emit the schema change payload.
+     * @return PTransform {@link #read}
+     */
+    public Read<T> withSchemaChangeEvents(boolean schemaChangeEvents) {
+      return toBuilder().setSchemaChangeEvents(schemaChangeEvents).build();
+    }
+
+    /**
+     * Returns the format function to use, taking {@link 
#withFullMetadata(boolean)} and {@link
+     * #withSchemaChangeEvents(boolean)} into account.
      *
-     * @throws IllegalArgumentException if full metadata is requested but the 
format function is not
-     *     a {@link SourceRecordJson.SourceRecordJsonMapper}.
+     * @throws IllegalArgumentException if full metadata or schema change 
events are requested but
+     *     the format function is not a {@link 
SourceRecordJson.SourceRecordJsonMapper}.
      */
     SourceRecordMapper<T> resolveFormatFunction() {
       SourceRecordMapper<T> formatFunction = getFormatFunction();
-      if (!Boolean.TRUE.equals(getFullMetadata())) {
+      boolean fullMetadata = Boolean.TRUE.equals(getFullMetadata());
+      boolean schemaChangeEvents = 
Boolean.TRUE.equals(getSchemaChangeEvents());
+      if (!fullMetadata && !schemaChangeEvents) {
         return formatFunction;
       }
       if (formatFunction instanceof SourceRecordJson.SourceRecordJsonMapper) {
         @SuppressWarnings("unchecked")
-        SourceRecordMapper<T> fullMetadataMapper =
-            (SourceRecordMapper<T>) new 
SourceRecordJson.SourceRecordJsonMapper(true);
-        return fullMetadataMapper;
+        SourceRecordMapper<T> jsonMapper =
+            (SourceRecordMapper<T>)
+                new SourceRecordJson.SourceRecordJsonMapper(fullMetadata, 
schemaChangeEvents);

Review Comment:
   This builds the mapper from the two flags on `Read` only. With 
`withFormatFunction(new 
SourceRecordJsonMapper(true)).withSchemaChangeEvents(true)` full metadata is 
lost without an error, and the reverse case loses the schema change payload. 
Falling back to the flags of the existing mapper when the `Read` flag is null 
would avoid that.



##########
sdks/java/io/debezium/src/main/java/org/apache/beam/io/debezium/DebeziumIO.java:
##########
@@ -327,24 +331,45 @@ public Read<T> withFullMetadata(boolean fullMetadata) {
     }
 
     /**
-     * Returns the format function to use, taking {@link 
#withFullMetadata(boolean)} into account.
+     * Adds the schema change payload ({@code databaseName}, {@code 
schemaName}, {@code ddl}, {@code

Review Comment:
   Please mention that this only changes the JSON mapping, and that Debezium 
drops these events when `include.schema.changes=false`. The class Javadoc 
example above sets that property to false.



##########
sdks/java/io/debezium/src/main/java/org/apache/beam/io/debezium/SourceRecordJson.java:
##########
@@ -584,6 +715,92 @@ public DataCollection(@Nullable String dataCollection, 
@Nullable Long eventCount
     }
   }
 
+  /**
+   * Depicts the payload of a schema change SourceRecord, added to its JSON 
after {@code metadata},
+   * {@code before} and {@code after}.
+   */
+  @SuppressWarnings("unused") // Unused params are validated as JSON
+  static class SchemaChangeEvent implements Serializable {
+
+    private final @Nullable String databaseName;
+    private final @Nullable String schemaName;
+    private final @Nullable String ddl;
+
+    @SerializedName("ts_ms")
+    private final @Nullable Long tsMs;
+
+    private final @Nullable List<TableChange> tableChanges;
+
+    /**
+     * SchemaChangeEvent Initializer.
+     *
+     * @param databaseName Database the DDL statement was applied to
+     * @param schemaName Schema the DDL statement was applied to, null for 
MySQL
+     * @param ddl DDL statement that changed the schema

Review Comment:
   For SQL Server and Db2, Debezium passes the literal `"N/A"` here, see 
`SqlServerSchemaChangeEventEmitter` and `Db2SchemaChangeEventEmitter`. Worth a 
few words, since CHANGES.md promises the DDL statement for all four connectors.



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