voonhous commented on code in PR #4164:
URL: https://github.com/apache/flink-cdc/pull/4164#discussion_r2498164366


##########
flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-hudi/src/main/java/org/apache/flink/cdc/connectors/hudi/sink/util/RowDataUtils.java:
##########
@@ -0,0 +1,604 @@
+/*
+ * 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.flink.cdc.connectors.hudi.sink.util;
+
+import org.apache.flink.cdc.common.data.DecimalData;
+import org.apache.flink.cdc.common.data.RecordData;
+import org.apache.flink.cdc.common.data.RecordData.FieldGetter;
+import org.apache.flink.cdc.common.event.ChangeEvent;
+import org.apache.flink.cdc.common.event.DataChangeEvent;
+import org.apache.flink.cdc.common.event.OperationType;
+import org.apache.flink.cdc.common.schema.Column;
+import org.apache.flink.cdc.common.schema.Schema;
+import org.apache.flink.cdc.common.types.DataType;
+import org.apache.flink.cdc.common.types.DataTypeChecks;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.flink.table.types.logical.BigIntType;
+import org.apache.flink.table.types.logical.BinaryType;
+import org.apache.flink.table.types.logical.BooleanType;
+import org.apache.flink.table.types.logical.CharType;
+import org.apache.flink.table.types.logical.DateType;
+import org.apache.flink.table.types.logical.DoubleType;
+import org.apache.flink.table.types.logical.FloatType;
+import org.apache.flink.table.types.logical.IntType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.SmallIntType;
+import org.apache.flink.table.types.logical.TimeType;
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.table.types.logical.TinyIntType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.logical.VarCharType;
+import org.apache.flink.types.RowKind;
+
+import org.apache.hudi.client.model.HoodieFlinkInternalRow;
+import org.apache.hudi.configuration.FlinkOptions;
+import org.apache.hudi.sink.bulk.RowDataKeyGen;
+
+import java.time.ZoneId;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.apache.flink.cdc.common.types.DataTypeChecks.getFieldCount;
+import static org.apache.flink.cdc.common.types.DataTypeChecks.getPrecision;
+
+/** Utils for converting {@link RowData} and {@link DataChangeEvent}. */
+public class RowDataUtils {
+
+    /** Convert {@link DataChangeEvent} to {@link RowData}. */
+    public static RowData convertDataChangeEventToRowData(
+            ChangeEvent changeEvent, List<FieldGetter> fieldGetters) {
+
+        if (!(changeEvent instanceof DataChangeEvent)) {
+            throw new IllegalArgumentException("ChangeEvent must be of type 
DataChangeEvent");
+        }
+
+        DataChangeEvent dataChangeEvent = (DataChangeEvent) changeEvent;
+
+        RecordData recordData;
+        RowKind kind;
+        switch (dataChangeEvent.op()) {
+            case INSERT:
+            case UPDATE:
+            case REPLACE:
+                {
+                    recordData = dataChangeEvent.after();
+                    kind = RowKind.INSERT;
+                    break;
+                }
+            case DELETE:
+                {
+                    recordData = dataChangeEvent.before();
+                    kind = RowKind.DELETE;
+                    break;
+                }
+            default:
+                throw new IllegalArgumentException("don't support type of " + 
dataChangeEvent.op());
+        }
+        GenericRowData genericRowData = new 
GenericRowData(recordData.getArity());
+        genericRowData.setRowKind(kind);
+        for (int i = 0; i < recordData.getArity(); i++) {
+            genericRowData.setField(i, 
fieldGetters.get(i).getFieldOrNull(recordData));
+        }
+        return genericRowData;
+    }
+
+    public static List<FieldGetter> createFieldGetters(Schema schema, ZoneId 
zoneId) {
+        List<Column> columns = schema.getColumns();
+        List<FieldGetter> fieldGetters = new ArrayList<>(columns.size());
+        for (int i = 0; i < columns.size(); i++) {
+            fieldGetters.add(createFieldGetter(columns.get(i).getType(), i, 
zoneId));
+        }
+        return fieldGetters;
+    }
+
+    /** Create a {@link FieldGetter} for the given {@link DataType}. */
+    public static FieldGetter createFieldGetter(DataType fieldType, int 
fieldPos, ZoneId zoneId) {
+        final FieldGetter fieldGetter;
+        // ordered by type root definition
+        switch (fieldType.getTypeRoot()) {
+            case CHAR:
+            case VARCHAR:
+                fieldGetter =
+                        row ->
+                                
org.apache.flink.table.data.StringData.fromString(
+                                        row.getString(fieldPos).toString());
+                break;
+            case BOOLEAN:
+                fieldGetter = row -> row.getBoolean(fieldPos);
+                break;
+            case BINARY:
+            case VARBINARY:
+                fieldGetter = row -> row.getBinary(fieldPos);
+                break;
+            case DECIMAL:
+                final int decimalScale = DataTypeChecks.getScale(fieldType);
+                int precision = getPrecision(fieldType);
+                fieldGetter =
+                        row -> {
+                            DecimalData decimalData =
+                                    row.getDecimal(fieldPos, precision, 
decimalScale);
+                            return 
org.apache.flink.table.data.DecimalData.fromBigDecimal(
+                                    decimalData.toBigDecimal(), precision, 
decimalScale);
+                        };
+                break;
+            case TINYINT:
+                fieldGetter = row -> row.getBoolean(fieldPos);

Review Comment:
   Same underlying implementation with more validation, let's keep it.



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