wombatu-kun commented on code in PR #19110: URL: https://github.com/apache/hudi/pull/19110#discussion_r3497796495
########## hudi-utilities/src/main/java/org/apache/hudi/utilities/transform/debezium/AbstractDebeziumTransformer.java: ########## @@ -0,0 +1,242 @@ +/* + * 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.utilities.transform.debezium; + +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.common.model.debezium.DebeziumConstants; +import org.apache.hudi.common.util.ConfigUtils; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.utilities.config.DebeziumTransformerConfig; +import org.apache.hudi.utilities.transform.Transformer; + +import org.apache.spark.api.java.JavaSparkContext; +import org.apache.spark.sql.Column; +import org.apache.spark.sql.Dataset; +import org.apache.spark.sql.Row; +import org.apache.spark.sql.SparkSession; +import org.apache.spark.sql.functions; +import org.apache.spark.sql.types.StructField; +import org.apache.spark.sql.types.StructType; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; + +import static org.apache.hudi.config.HoodieErrorTableConfig.ERROR_TABLE_ENABLED; +import static org.apache.hudi.utilities.streamer.BaseErrorTableWriter.ERROR_TABLE_CURRUPT_RECORD_COL_NAME; + +/** + * Base {@link Transformer} that flattens a Debezium change-event envelope into a Hudi row. + * + * <p>A Debezium change event is a nested record of the form + * {@code {op, ts_ms, before:{...}, after:{...}, source:{...}}}. This transformer: + * <ul> + * <li>selects the {@code before} image for deletes and the {@code after} image otherwise, + * and explodes it to the row's top level;</li> + * <li>surfaces the common Debezium metadata columns (operation type, processing/origin + * timestamps, shard) along with any database-specific metadata columns supplied by the + * subclass;</li> + * <li>optionally nests the metadata columns under a single {@code _debezium_metadata} struct + * (see {@link DebeziumTransformerConfig#ENABLE_NESTED_FIELDS});</li> + * <li>optionally preserves the error-table corrupt-record column when the error table is + * enabled;</li> + * <li>applies an optional database-specific post-processing step (e.g. ordering/sequence + * columns, LSN defaulting);</li> + * <li>normalizes column nullability (see + * {@link DebeziumTransformerConfig#SCHEMA_AS_NULLABLE}).</li> + * </ul> + * + * <p>The flattened column names are defined in {@link DebeziumConstants}; the matching + * {@code DebeziumAvroPayload} implementations rely on these names for merge/ordering semantics. + * + * <p>Subclasses configure the database-specific behavior purely through the constructor; there is + * no abstract method to implement. + */ +public class AbstractDebeziumTransformer implements Transformer { + + public static final String DEBEZIUM_METADATA_FIELD = "_debezium_metadata"; + private static final String DATA_FIELD = "__data"; + + private static final List<Column> DEFAULT_ROOT_LEVEL_METADATA_COLUMNS = Arrays.asList( + new Column(DebeziumConstants.INCOMING_OP_FIELD).alias(DebeziumConstants.FLATTENED_OP_COL_NAME)); + + private static final List<Column> DEFAULT_NESTED_METADATA_COLUMNS = Arrays.asList( + new Column(DebeziumConstants.INCOMING_TS_MS_FIELD).alias(DebeziumConstants.UPSTREAM_PROCESSING_TS_COL_NAME), + new Column(DebeziumConstants.INCOMING_SOURCE_NAME_FIELD).alias(DebeziumConstants.FLATTENED_SHARD_NAME), + new Column(DebeziumConstants.INCOMING_SOURCE_TS_MS_FIELD).alias(DebeziumConstants.FLATTENED_TS_COL_NAME)); + + private final List<Column> typeSpecificMetadataColumns; + private final Option<Function<Dataset<Row>, Dataset<Row>>> postProcessingOption; + private final boolean nestedFieldsEnabledByDefault; + + protected AbstractDebeziumTransformer( + List<Column> typeSpecificMetadataColumns, + Option<Function<Dataset<Row>, Dataset<Row>>> postProcessingOption) { + this(typeSpecificMetadataColumns, postProcessingOption, false); + } + + /** + * @param typeSpecificMetadataColumns database-specific metadata columns (already aliased to their + * flattened output names). + * @param postProcessingOption optional post-flatten transformation applied to the result. + * @param nestedFieldsEnabledByDefault default used for + * {@link DebeziumTransformerConfig#ENABLE_NESTED_FIELDS} when + * the property is not set explicitly. Lets a subclass (e.g. + * Postgres) opt into nested metadata by default. + */ + protected AbstractDebeziumTransformer( + List<Column> typeSpecificMetadataColumns, + Option<Function<Dataset<Row>, Dataset<Row>>> postProcessingOption, + boolean nestedFieldsEnabledByDefault) { + this.typeSpecificMetadataColumns = typeSpecificMetadataColumns; + this.postProcessingOption = postProcessingOption; + this.nestedFieldsEnabledByDefault = nestedFieldsEnabledByDefault; + } + + @Override + public Dataset<Row> apply(JavaSparkContext javaSparkContext, SparkSession sparkSession, Dataset<Row> rowDataset, TypedProperties props) { + if (rowDataset.columns().length == 0) { + return rowDataset; + } + // Pick selective debezium meta fields: pick the row values from before field for delete record + // and row values from after field for insert or update records. + rowDataset = rowDataset + .withColumn(DATA_FIELD, + functions.when(new Column(DebeziumConstants.INCOMING_OP_FIELD).equalTo(DebeziumConstants.DELETE_OP), + new Column(DebeziumConstants.INCOMING_BEFORE_FIELD)) + .otherwise(new Column(DebeziumConstants.INCOMING_AFTER_FIELD))) + .drop(DebeziumConstants.INCOMING_AFTER_FIELD, DebeziumConstants.INCOMING_BEFORE_FIELD); + + List<Column> allColumns = new ArrayList<>(); + boolean enableNestedFields = isNestedFieldsEnabled(props); + + // When nested fields are enabled, only _change_operation_type and root-level metadata columns should be at root level + if (enableNestedFields) { + Column lsnColumn = null; + List<Column> otherMetadata = new ArrayList<>(); + + // Extract LSN column to root level, keep other metadata nested + for (Column col : typeSpecificMetadataColumns) { + String colStr = col.toString(); + if (colStr.contains(DebeziumConstants.FLATTENED_LSN_COL_NAME)) { Review Comment: This works today only because Spark's `Alias.toString()` embeds the alias name (it renders as `source.lsn AS _event_lsn`); any change to how the column is constructed or rendered would silently break LSN-to-root routing with no test catching it. Passing the LSN column (or its output name) explicitly from the subclass, as suggested, removes the string match entirely. -- 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]
