JingsongLi commented on code in PR #1226:
URL: https://github.com/apache/incubator-paimon/pull/1226#discussion_r1205549566


##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/action/cdc/mysql/MySqlTypeUtils.java:
##########
@@ -232,4 +270,110 @@ public static DataType toDataType(
                         String.format("Don't support MySQL type '%s' yet.", 
type));
         }
     }
+
+    public static boolean isGeoType(String type) {
+        switch (type.toUpperCase()) {
+            case GEOMETRY:
+            case POINT:
+            case LINESTRING:
+            case POLYGON:
+            case MULTIPOINT:
+            case MULTILINESTRING:
+            case MULTIPOLYGON:
+            case GEOMETRYCOLLECTION:
+                return true;
+            default:
+                return false;
+        }
+    }
+
+    public static String convertWkbArray(byte[] wkb) throws 
JsonProcessingException {
+        String geoJson = 
OGCGeometry.fromBinary(ByteBuffer.wrap(wkb)).asGeoJson();
+        JsonNode originGeoNode = objectMapper.readTree(geoJson);
+
+        Optional<Integer> srid =
+                Optional.ofNullable(
+                        originGeoNode.has("srid") ? 
originGeoNode.get("srid").intValue() : null);
+        Map<String, Object> geometryInfo = new HashMap<>();
+        String geometryType = originGeoNode.get("type").asText();
+        geometryInfo.put("type", geometryType);
+        if (geometryType.equalsIgnoreCase("GeometryCollection")) {
+            geometryInfo.put("geometries", originGeoNode.get("geometries"));
+        } else {
+            geometryInfo.put("coordinates", originGeoNode.get("coordinates"));
+        }
+        geometryInfo.put("srid", srid.orElse(0));
+        ObjectWriter objectWriter = objectMapper.writer();
+        return objectWriter.writeValueAsString(geometryInfo);
+    }
+
+    public static boolean isScaleType(String typeName) {
+        return HAVE_SCALE_LIST.stream()
+                .anyMatch(type -> 
getShortType(typeName).toUpperCase().startsWith(type));
+    }
+
+    public static boolean isEnumType(String typeName) {
+        return typeName.toLowerCase().startsWith(ENUM);
+    }
+
+    public static boolean isSetType(String typeName) {
+        return typeName.toLowerCase().startsWith(SET);
+    }
+
+    /* Get type after the brackets are removed.*/
+    public static String getShortType(String typeName) {
+
+        if (typeName.contains(LEFT_BRACKETS) && 
typeName.contains(RIGHT_BRACKETS)) {
+            String type =
+                    typeName.substring(0, 
typeName.indexOf(LEFT_BRACKETS)).trim()
+                            + 
typeName.substring(typeName.indexOf(RIGHT_BRACKETS) + 1);
+            return type;
+        } else {
+            return typeName;
+        }
+    }
+
+    public static int getPrecision(String typeName) {
+        if (typeName.contains(LEFT_BRACKETS)
+                && typeName.contains(RIGHT_BRACKETS)
+                && isScaleType(typeName)) {
+            int precision =
+                    Integer.parseInt(
+                            typeName.substring(
+                                            typeName.indexOf(LEFT_BRACKETS) + 
1,
+                                            typeName.indexOf(COMMA))
+                                    .trim());
+            return precision;
+        } else if ((typeName.contains(LEFT_BRACKETS)
+                && typeName.contains(RIGHT_BRACKETS)
+                && !isScaleType(typeName)
+                && !isEnumType(typeName)
+                && !isSetType(typeName))) {
+            int precision =
+                    Integer.parseInt(
+                            typeName.substring(
+                                            typeName.indexOf(LEFT_BRACKETS) + 
1,
+                                            typeName.indexOf(RIGHT_BRACKETS))
+                                    .trim());
+            return precision;
+        } else {
+            return 0;
+        }
+    }
+
+    public static int getScale(String typeName) {
+        if (typeName.contains(LEFT_BRACKETS)
+                && typeName.contains(RIGHT_BRACKETS)
+                && isScaleType(typeName)) {
+            int scale =

Review Comment:
   Local variable 'scale' is redundant 



##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/action/cdc/mysql/MySqlTypeUtils.java:
##########
@@ -232,4 +270,110 @@ public static DataType toDataType(
                         String.format("Don't support MySQL type '%s' yet.", 
type));
         }
     }
+
+    public static boolean isGeoType(String type) {
+        switch (type.toUpperCase()) {
+            case GEOMETRY:
+            case POINT:
+            case LINESTRING:
+            case POLYGON:
+            case MULTIPOINT:
+            case MULTILINESTRING:
+            case MULTIPOLYGON:
+            case GEOMETRYCOLLECTION:
+                return true;
+            default:
+                return false;
+        }
+    }
+
+    public static String convertWkbArray(byte[] wkb) throws 
JsonProcessingException {
+        String geoJson = 
OGCGeometry.fromBinary(ByteBuffer.wrap(wkb)).asGeoJson();
+        JsonNode originGeoNode = objectMapper.readTree(geoJson);
+
+        Optional<Integer> srid =
+                Optional.ofNullable(
+                        originGeoNode.has("srid") ? 
originGeoNode.get("srid").intValue() : null);
+        Map<String, Object> geometryInfo = new HashMap<>();
+        String geometryType = originGeoNode.get("type").asText();
+        geometryInfo.put("type", geometryType);
+        if (geometryType.equalsIgnoreCase("GeometryCollection")) {
+            geometryInfo.put("geometries", originGeoNode.get("geometries"));
+        } else {
+            geometryInfo.put("coordinates", originGeoNode.get("coordinates"));
+        }
+        geometryInfo.put("srid", srid.orElse(0));
+        ObjectWriter objectWriter = objectMapper.writer();
+        return objectWriter.writeValueAsString(geometryInfo);
+    }
+
+    public static boolean isScaleType(String typeName) {
+        return HAVE_SCALE_LIST.stream()
+                .anyMatch(type -> 
getShortType(typeName).toUpperCase().startsWith(type));
+    }
+
+    public static boolean isEnumType(String typeName) {
+        return typeName.toLowerCase().startsWith(ENUM);
+    }
+
+    public static boolean isSetType(String typeName) {
+        return typeName.toLowerCase().startsWith(SET);
+    }
+
+    /* Get type after the brackets are removed.*/
+    public static String getShortType(String typeName) {
+
+        if (typeName.contains(LEFT_BRACKETS) && 
typeName.contains(RIGHT_BRACKETS)) {
+            String type =

Review Comment:
   be careful about ide check, here: Local variable 'type' is redundant 



##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/action/cdc/mysql/MySqlTypeUtils.java:
##########
@@ -232,4 +270,110 @@ public static DataType toDataType(
                         String.format("Don't support MySQL type '%s' yet.", 
type));
         }
     }
+
+    public static boolean isGeoType(String type) {
+        switch (type.toUpperCase()) {
+            case GEOMETRY:
+            case POINT:
+            case LINESTRING:
+            case POLYGON:
+            case MULTIPOINT:
+            case MULTILINESTRING:
+            case MULTIPOLYGON:
+            case GEOMETRYCOLLECTION:
+                return true;
+            default:
+                return false;
+        }
+    }
+
+    public static String convertWkbArray(byte[] wkb) throws 
JsonProcessingException {
+        String geoJson = 
OGCGeometry.fromBinary(ByteBuffer.wrap(wkb)).asGeoJson();
+        JsonNode originGeoNode = objectMapper.readTree(geoJson);
+
+        Optional<Integer> srid =
+                Optional.ofNullable(
+                        originGeoNode.has("srid") ? 
originGeoNode.get("srid").intValue() : null);
+        Map<String, Object> geometryInfo = new HashMap<>();
+        String geometryType = originGeoNode.get("type").asText();
+        geometryInfo.put("type", geometryType);
+        if (geometryType.equalsIgnoreCase("GeometryCollection")) {
+            geometryInfo.put("geometries", originGeoNode.get("geometries"));
+        } else {
+            geometryInfo.put("coordinates", originGeoNode.get("coordinates"));
+        }
+        geometryInfo.put("srid", srid.orElse(0));
+        ObjectWriter objectWriter = objectMapper.writer();
+        return objectWriter.writeValueAsString(geometryInfo);
+    }
+
+    public static boolean isScaleType(String typeName) {
+        return HAVE_SCALE_LIST.stream()
+                .anyMatch(type -> 
getShortType(typeName).toUpperCase().startsWith(type));
+    }
+
+    public static boolean isEnumType(String typeName) {
+        return typeName.toLowerCase().startsWith(ENUM);
+    }
+
+    public static boolean isSetType(String typeName) {
+        return typeName.toLowerCase().startsWith(SET);

Review Comment:
   ditto



##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/action/cdc/kafka/canal/CanalJsonEventParser.java:
##########
@@ -0,0 +1,354 @@
+/*
+ * 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.paimon.flink.action.cdc.kafka.canal;
+
+import org.apache.paimon.flink.action.cdc.ComputedColumn;
+import org.apache.paimon.flink.action.cdc.TableNameConverter;
+import org.apache.paimon.flink.action.cdc.mysql.MySqlTypeUtils;
+import org.apache.paimon.flink.sink.cdc.CdcRecord;
+import org.apache.paimon.flink.sink.cdc.EventParser;
+import org.apache.paimon.types.DataField;
+import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.RowKind;
+import org.apache.paimon.utils.Preconditions;
+import org.apache.paimon.utils.StringUtils;
+
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.core.type.TypeReference;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.JsonNode;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.node.ArrayNode;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.node.NullNode;
+
+import com.alibaba.druid.DbType;
+import com.alibaba.druid.sql.SQLUtils;
+import com.alibaba.druid.sql.ast.SQLName;
+import com.alibaba.druid.sql.ast.SQLStatement;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddColumn;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropColumnItem;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableItem;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement;
+import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
+import 
com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn;
+import 
com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+import static org.apache.paimon.utils.Preconditions.checkArgument;
+
+/** {@link EventParser} for Canal-json. */
+public class CanalJsonEventParser implements EventParser<String> {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(CanalJsonEventParser.class);
+
+    private static final String FIELD_DATA = "data";
+    private static final String FIELD_OLD = "old";
+    private static final String TYPE = "type";
+    private static final String OP_INSERT = "INSERT";
+    private static final String OP_UPDATE = "UPDATE";
+    private static final String OP_DELETE = "DELETE";
+    private static final String OP_CREATE = "CREATE";
+
+    private final ObjectMapper objectMapper = new ObjectMapper();
+
+    private JsonNode root;
+    private Map<String, String> mySqlFieldTypes;
+    private Map<String, DataType> paimonFieldTypes;
+
+    private final boolean caseSensitive;
+    private final TableNameConverter tableNameConverter;
+    private final List<ComputedColumn> computedColumns;
+
+    public CanalJsonEventParser(boolean caseSensitive, List<ComputedColumn> 
computedColumns) {
+        this(caseSensitive, new TableNameConverter(caseSensitive), 
computedColumns);
+    }
+
+    public CanalJsonEventParser(boolean caseSensitive, TableNameConverter 
tableNameConverter) {
+        this(caseSensitive, tableNameConverter, Collections.emptyList());
+    }
+
+    public CanalJsonEventParser(
+            boolean caseSensitive,
+            TableNameConverter tableNameConverter,
+            List<ComputedColumn> computedColumns) {
+        this.caseSensitive = caseSensitive;
+        this.tableNameConverter = tableNameConverter;
+        this.computedColumns = computedColumns;
+    }
+
+    @Override
+    public void setRawEvent(String rawEvent) {
+        try {
+            root = objectMapper.readValue(rawEvent, JsonNode.class);
+            Preconditions.checkNotNull(
+                    root.get("mysqlType"),
+                    "CanalJsonEventParser only supports canal-json format,"
+                            + "please make sure that your topic's format is 
accurate.");
+            JsonNode mysqlType = root.get("mysqlType");
+
+            if (!isUpdatedDataFields()) {
+                updateFieldTypes(mysqlType);
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public String tableName() {
+        String tableName = root.get("table").asText();
+        return tableNameConverter.convert(tableName);
+    }
+
+    private void updateFieldTypes(JsonNode schema) {
+        mySqlFieldTypes = new LinkedHashMap<>();
+        paimonFieldTypes = new LinkedHashMap<>();
+        Iterator<String> iterator = schema.fieldNames();
+        while (iterator.hasNext()) {
+            String fieldName = iterator.next();
+            String fieldType = schema.get(fieldName).asText();
+            mySqlFieldTypes.put(fieldName, fieldType);
+            paimonFieldTypes.put(fieldName, 
MySqlTypeUtils.toDataType(fieldType));
+        }
+    }
+
+    @Override
+    public boolean isUpdatedDataFields() {
+        if (root.get("isDdl") == null) {
+            return false;
+        } else {
+            return "true".equals(root.get("isDdl").asText());
+        }
+    }
+
+    @Override
+    public Optional<List<DataField>> getUpdatedDataFields() {
+        String sql = root.get("sql").asText();
+
+        if (StringUtils.isEmpty(sql)) {
+            return Optional.empty();
+        }
+
+        List<DataField> result = new ArrayList<>();
+        int id = 0;
+        for (Map.Entry<String, DataType> fieldType : 
paimonFieldTypes.entrySet()) {
+            result.add(new DataField(id++, fieldType.getKey(), 
fieldType.getValue()));
+        }
+        SQLStatement sqlStatement = SQLUtils.parseSingleStatement(sql, 
DbType.mysql);
+        if (sqlStatement instanceof SQLAlterTableStatement) {
+            SQLAlterTableStatement sqlAlterTableStatement = 
(SQLAlterTableStatement) sqlStatement;
+            for (int i = 0; i < sqlAlterTableStatement.getItems().size(); i++) 
{
+                SQLAlterTableItem sqlAlterTableItem = 
sqlAlterTableStatement.getItems().get(i);
+                if (sqlAlterTableItem instanceof SQLAlterTableAddColumn) {
+                    SQLAlterTableAddColumn sqlAlterTableAddColumn =
+                            (SQLAlterTableAddColumn) sqlAlterTableItem;
+                    List<SQLColumnDefinition> columns = 
sqlAlterTableAddColumn.getColumns();
+                    for (int j = 0; j < columns.size(); j++) {
+                        String columnName = 
columns.get(j).getColumnName().replace("`", "");
+                        String columnType = 
columns.get(j).getDataType().toString();
+                        DataType dataType = 
MySqlTypeUtils.toDataType(columnType);
+                        boolean notNull =
+                                
columns.get(j).toString().toUpperCase().contains("NOT NULL");
+                        dataType = notNull ? dataType.notNull() : 
dataType.nullable();
+                        result =
+                                result.stream()
+                                        .filter(dataField -> 
!dataField.name().equals(columnName))
+                                        .collect(Collectors.toList());
+                        result.add(new DataField(id, columnName, dataType));
+                        mySqlFieldTypes.put(columnName, columnType);
+                        paimonFieldTypes.put(columnName, 
MySqlTypeUtils.toDataType(columnType));
+                    }
+                } else if (sqlAlterTableItem instanceof 
SQLAlterTableDropColumnItem) {
+                    SQLAlterTableDropColumnItem sqlAlterTableDropColumnItem =
+                            (SQLAlterTableDropColumnItem) sqlAlterTableItem;
+                    List<String> columnNames =
+                            sqlAlterTableDropColumnItem.getColumns().stream()
+                                    .map(SQLName::getSimpleName)
+                                    .collect(Collectors.toList());
+                    columnNames.forEach(
+                            columnName -> {
+                                mySqlFieldTypes.remove(columnName);
+                                paimonFieldTypes.remove(columnName);
+                            });
+
+                    result =
+                            result.stream()
+                                    .filter(dataField -> 
!columnNames.contains(dataField.name()))
+                                    .collect(Collectors.toList());
+                } else if (sqlAlterTableItem instanceof 
MySqlAlterTableModifyColumn) {
+                    MySqlAlterTableModifyColumn mySqlAlterTableModifyColumn =
+                            (MySqlAlterTableModifyColumn) sqlAlterTableItem;
+                    SQLColumnDefinition newColumnDefinition =
+                            
mySqlAlterTableModifyColumn.getNewColumnDefinition();
+                    String columnName = 
newColumnDefinition.getColumnName().replace("`", "");
+                    String columnType = 
newColumnDefinition.getDataType().toString();
+                    DataType dataType = MySqlTypeUtils.toDataType(columnType);
+                    boolean notNull =
+                            
newColumnDefinition.toString().toUpperCase().contains("NOT NULL");
+                    dataType = notNull ? dataType.notNull() : 
dataType.nullable();
+                    result =
+                            result.stream()
+                                    .filter(dataField -> 
!dataField.name().equals(columnName))
+                                    .collect(Collectors.toList());
+                    result.add(new DataField(id, columnName, dataType));
+                    mySqlFieldTypes.put(columnName, columnType);
+                    paimonFieldTypes.put(columnName, 
MySqlTypeUtils.toDataType(columnType));
+                } else if (sqlAlterTableItem instanceof 
MySqlAlterTableChangeColumn) {
+                    MySqlAlterTableChangeColumn mySqlAlterTableChangeColumn =
+                            (MySqlAlterTableChangeColumn) sqlAlterTableItem;
+                    SQLColumnDefinition newColumnDefinition =
+                            
mySqlAlterTableChangeColumn.getNewColumnDefinition();
+                    String oldColumnName =
+                            
mySqlAlterTableChangeColumn.getColumnName().getSimpleName();
+                    String columnName = 
newColumnDefinition.getColumnName().replace("`", "");
+                    String columnType = 
newColumnDefinition.getDataType().toString();
+                    DataType dataType = MySqlTypeUtils.toDataType(columnType);
+                    boolean notNull =
+                            
newColumnDefinition.toString().toUpperCase().contains("NOT NULL");
+                    dataType = notNull ? dataType.notNull() : 
dataType.nullable();
+                    result =
+                            result.stream()
+                                    .filter(dataField -> 
!dataField.name().equals(oldColumnName))
+                                    .collect(Collectors.toList());
+                    result.add(new DataField(id, columnName, dataType));
+                    mySqlFieldTypes.remove(oldColumnName);
+                    paimonFieldTypes.remove(oldColumnName);
+                    mySqlFieldTypes.put(columnName, columnType);
+                    paimonFieldTypes.put(columnName, 
MySqlTypeUtils.toDataType(columnType));
+                }
+            }
+        }
+
+        return Optional.of(result);
+    }
+
+    @Override
+    public List<CdcRecord> getRecords() {
+        List<CdcRecord> records = new ArrayList<>();
+        String type = root.get(TYPE).asText();
+        if (OP_UPDATE.equals(type)) {
+            ArrayNode data = (ArrayNode) root.get(FIELD_DATA);
+            ArrayNode old =
+                    root.get(FIELD_OLD) instanceof NullNode
+                            ? null
+                            : (ArrayNode) root.get(FIELD_OLD);
+            for (int i = 0; i < data.size(); i++) {
+                Map<String, String> after = extractRow(data.get(i));
+                if (old != null) {
+                    Map<String, String> before = extractRow(old.get(i));
+                    // fields in "old" (before) means the fields are changed
+                    // fields not in "old" (before) means the fields are not 
changed
+                    // so we just copy the not changed fields into before
+                    for (Map.Entry<String, String> entry : after.entrySet()) {
+                        if (!before.containsKey(entry.getKey())) {
+                            before.put(entry.getKey(), entry.getValue());
+                        }
+                    }
+                    before = caseSensitive ? before : 
keyCaseInsensitive(before);
+                    records.add(new CdcRecord(RowKind.DELETE, before));
+                }
+                after = caseSensitive ? after : keyCaseInsensitive(after);
+                records.add(new CdcRecord(RowKind.INSERT, after));
+            }
+        } else if (OP_INSERT.equals(type)) {
+            ArrayNode data = (ArrayNode) root.get(FIELD_DATA);
+            for (int i = 0; i < data.size(); i++) {
+                Map<String, String> after = extractRow(data.get(i));
+                after = caseSensitive ? after : keyCaseInsensitive(after);
+                records.add(new CdcRecord(RowKind.INSERT, after));
+            }
+        } else if (OP_DELETE.equals(type)) {
+            ArrayNode data = (ArrayNode) root.get(FIELD_DATA);
+            for (int i = 0; i < data.size(); i++) {
+                Map<String, String> after = extractRow(data.get(i));
+                after = caseSensitive ? after : keyCaseInsensitive(after);
+                records.add(new CdcRecord(RowKind.DELETE, after));
+            }
+        }
+
+        return records;
+    }
+
+    private Map<String, String> extractRow(JsonNode recordRow) {
+        Map<String, Object> jsonMap =
+                objectMapper.convertValue(recordRow, new 
TypeReference<Map<String, Object>>() {});
+        if (jsonMap == null) {
+            return new HashMap<>();
+        }
+        Map<String, String> resultMap = new HashMap<>();
+        for (Map.Entry<String, String> field : mySqlFieldTypes.entrySet()) {
+            String fieldName = field.getKey();
+            String mySqlType = field.getValue();
+            Object objectValue = jsonMap.get(fieldName);
+            if (objectValue == null) {
+                continue;
+            }
+
+            String oldValue = objectValue.toString();
+            String newValue = oldValue;
+            if 
(MySqlTypeUtils.isSetType(MySqlTypeUtils.getShortType(mySqlType))) {
+                newValue = CanalFieldParser.convertSet(newValue, mySqlType);
+            } else if 
(MySqlTypeUtils.isEnumType(MySqlTypeUtils.getShortType(mySqlType))) {
+                newValue = CanalFieldParser.convertEnum(newValue, mySqlType);
+            } else if 
(MySqlTypeUtils.isGeoType(MySqlTypeUtils.getShortType(mySqlType))) {
+                try {
+                    byte[] wkb =
+                            CanalFieldParser.convertGeoType2WkbArray(
+                                    
oldValue.getBytes(StandardCharsets.ISO_8859_1));
+                    newValue = MySqlTypeUtils.convertWkbArray(wkb);
+                } catch (Exception e) {
+                    throw new IllegalArgumentException(
+                            String.format("Failed to convert %s to geometry 
JSON.", oldValue), e);
+                }
+            }
+            resultMap.put(fieldName, newValue);
+        }
+        // generate values of computed columns
+        for (ComputedColumn computedColumn : computedColumns) {
+            resultMap.put(

Review Comment:
   No test cover



##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/action/cdc/kafka/canal/CanalJsonEventParser.java:
##########
@@ -0,0 +1,354 @@
+/*
+ * 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.paimon.flink.action.cdc.kafka.canal;
+
+import org.apache.paimon.flink.action.cdc.ComputedColumn;
+import org.apache.paimon.flink.action.cdc.TableNameConverter;
+import org.apache.paimon.flink.action.cdc.mysql.MySqlTypeUtils;
+import org.apache.paimon.flink.sink.cdc.CdcRecord;
+import org.apache.paimon.flink.sink.cdc.EventParser;
+import org.apache.paimon.types.DataField;
+import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.RowKind;
+import org.apache.paimon.utils.Preconditions;
+import org.apache.paimon.utils.StringUtils;
+
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.core.type.TypeReference;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.JsonNode;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.node.ArrayNode;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.node.NullNode;
+
+import com.alibaba.druid.DbType;
+import com.alibaba.druid.sql.SQLUtils;
+import com.alibaba.druid.sql.ast.SQLName;
+import com.alibaba.druid.sql.ast.SQLStatement;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddColumn;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropColumnItem;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableItem;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement;
+import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
+import 
com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn;
+import 
com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+import static org.apache.paimon.utils.Preconditions.checkArgument;
+
+/** {@link EventParser} for Canal-json. */
+public class CanalJsonEventParser implements EventParser<String> {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(CanalJsonEventParser.class);
+
+    private static final String FIELD_DATA = "data";
+    private static final String FIELD_OLD = "old";
+    private static final String TYPE = "type";
+    private static final String OP_INSERT = "INSERT";
+    private static final String OP_UPDATE = "UPDATE";
+    private static final String OP_DELETE = "DELETE";
+    private static final String OP_CREATE = "CREATE";
+
+    private final ObjectMapper objectMapper = new ObjectMapper();
+
+    private JsonNode root;
+    private Map<String, String> mySqlFieldTypes;
+    private Map<String, DataType> paimonFieldTypes;
+
+    private final boolean caseSensitive;
+    private final TableNameConverter tableNameConverter;
+    private final List<ComputedColumn> computedColumns;
+
+    public CanalJsonEventParser(boolean caseSensitive, List<ComputedColumn> 
computedColumns) {
+        this(caseSensitive, new TableNameConverter(caseSensitive), 
computedColumns);
+    }
+
+    public CanalJsonEventParser(boolean caseSensitive, TableNameConverter 
tableNameConverter) {
+        this(caseSensitive, tableNameConverter, Collections.emptyList());
+    }
+
+    public CanalJsonEventParser(
+            boolean caseSensitive,
+            TableNameConverter tableNameConverter,
+            List<ComputedColumn> computedColumns) {
+        this.caseSensitive = caseSensitive;
+        this.tableNameConverter = tableNameConverter;
+        this.computedColumns = computedColumns;
+    }
+
+    @Override
+    public void setRawEvent(String rawEvent) {
+        try {
+            root = objectMapper.readValue(rawEvent, JsonNode.class);
+            Preconditions.checkNotNull(
+                    root.get("mysqlType"),
+                    "CanalJsonEventParser only supports canal-json format,"
+                            + "please make sure that your topic's format is 
accurate.");
+            JsonNode mysqlType = root.get("mysqlType");
+
+            if (!isUpdatedDataFields()) {
+                updateFieldTypes(mysqlType);
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public String tableName() {
+        String tableName = root.get("table").asText();
+        return tableNameConverter.convert(tableName);
+    }
+
+    private void updateFieldTypes(JsonNode schema) {
+        mySqlFieldTypes = new LinkedHashMap<>();
+        paimonFieldTypes = new LinkedHashMap<>();
+        Iterator<String> iterator = schema.fieldNames();
+        while (iterator.hasNext()) {
+            String fieldName = iterator.next();
+            String fieldType = schema.get(fieldName).asText();
+            mySqlFieldTypes.put(fieldName, fieldType);
+            paimonFieldTypes.put(fieldName, 
MySqlTypeUtils.toDataType(fieldType));
+        }
+    }
+
+    @Override
+    public boolean isUpdatedDataFields() {
+        if (root.get("isDdl") == null) {
+            return false;
+        } else {
+            return "true".equals(root.get("isDdl").asText());
+        }
+    }
+
+    @Override
+    public Optional<List<DataField>> getUpdatedDataFields() {
+        String sql = root.get("sql").asText();
+
+        if (StringUtils.isEmpty(sql)) {
+            return Optional.empty();
+        }
+
+        List<DataField> result = new ArrayList<>();
+        int id = 0;
+        for (Map.Entry<String, DataType> fieldType : 
paimonFieldTypes.entrySet()) {
+            result.add(new DataField(id++, fieldType.getKey(), 
fieldType.getValue()));
+        }
+        SQLStatement sqlStatement = SQLUtils.parseSingleStatement(sql, 
DbType.mysql);
+        if (sqlStatement instanceof SQLAlterTableStatement) {
+            SQLAlterTableStatement sqlAlterTableStatement = 
(SQLAlterTableStatement) sqlStatement;
+            for (int i = 0; i < sqlAlterTableStatement.getItems().size(); i++) 
{
+                SQLAlterTableItem sqlAlterTableItem = 
sqlAlterTableStatement.getItems().get(i);
+                if (sqlAlterTableItem instanceof SQLAlterTableAddColumn) {
+                    SQLAlterTableAddColumn sqlAlterTableAddColumn =
+                            (SQLAlterTableAddColumn) sqlAlterTableItem;
+                    List<SQLColumnDefinition> columns = 
sqlAlterTableAddColumn.getColumns();
+                    for (int j = 0; j < columns.size(); j++) {

Review Comment:
   Use `for (SQLColumnDefinition column : columns)`



##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/action/cdc/mysql/MySqlTypeUtils.java:
##########
@@ -232,4 +270,110 @@ public static DataType toDataType(
                         String.format("Don't support MySQL type '%s' yet.", 
type));
         }
     }
+
+    public static boolean isGeoType(String type) {
+        switch (type.toUpperCase()) {
+            case GEOMETRY:
+            case POINT:
+            case LINESTRING:
+            case POLYGON:
+            case MULTIPOINT:
+            case MULTILINESTRING:
+            case MULTIPOLYGON:
+            case GEOMETRYCOLLECTION:
+                return true;
+            default:
+                return false;
+        }
+    }
+
+    public static String convertWkbArray(byte[] wkb) throws 
JsonProcessingException {
+        String geoJson = 
OGCGeometry.fromBinary(ByteBuffer.wrap(wkb)).asGeoJson();
+        JsonNode originGeoNode = objectMapper.readTree(geoJson);
+
+        Optional<Integer> srid =
+                Optional.ofNullable(
+                        originGeoNode.has("srid") ? 
originGeoNode.get("srid").intValue() : null);
+        Map<String, Object> geometryInfo = new HashMap<>();
+        String geometryType = originGeoNode.get("type").asText();
+        geometryInfo.put("type", geometryType);
+        if (geometryType.equalsIgnoreCase("GeometryCollection")) {
+            geometryInfo.put("geometries", originGeoNode.get("geometries"));
+        } else {
+            geometryInfo.put("coordinates", originGeoNode.get("coordinates"));
+        }
+        geometryInfo.put("srid", srid.orElse(0));
+        ObjectWriter objectWriter = objectMapper.writer();
+        return objectWriter.writeValueAsString(geometryInfo);
+    }
+
+    public static boolean isScaleType(String typeName) {
+        return HAVE_SCALE_LIST.stream()
+                .anyMatch(type -> 
getShortType(typeName).toUpperCase().startsWith(type));
+    }
+
+    public static boolean isEnumType(String typeName) {
+        return typeName.toLowerCase().startsWith(ENUM);
+    }
+
+    public static boolean isSetType(String typeName) {
+        return typeName.toLowerCase().startsWith(SET);
+    }
+
+    /* Get type after the brackets are removed.*/
+    public static String getShortType(String typeName) {
+
+        if (typeName.contains(LEFT_BRACKETS) && 
typeName.contains(RIGHT_BRACKETS)) {
+            String type =
+                    typeName.substring(0, 
typeName.indexOf(LEFT_BRACKETS)).trim()
+                            + 
typeName.substring(typeName.indexOf(RIGHT_BRACKETS) + 1);
+            return type;
+        } else {
+            return typeName;
+        }
+    }
+
+    public static int getPrecision(String typeName) {
+        if (typeName.contains(LEFT_BRACKETS)
+                && typeName.contains(RIGHT_BRACKETS)
+                && isScaleType(typeName)) {
+            int precision =

Review Comment:
   Local variable 'precision' is redundant



##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/action/cdc/mysql/MySqlTypeUtils.java:
##########
@@ -232,4 +270,110 @@ public static DataType toDataType(
                         String.format("Don't support MySQL type '%s' yet.", 
type));
         }
     }
+
+    public static boolean isGeoType(String type) {
+        switch (type.toUpperCase()) {
+            case GEOMETRY:
+            case POINT:
+            case LINESTRING:
+            case POLYGON:
+            case MULTIPOINT:
+            case MULTILINESTRING:
+            case MULTIPOLYGON:
+            case GEOMETRYCOLLECTION:
+                return true;
+            default:
+                return false;
+        }
+    }
+
+    public static String convertWkbArray(byte[] wkb) throws 
JsonProcessingException {
+        String geoJson = 
OGCGeometry.fromBinary(ByteBuffer.wrap(wkb)).asGeoJson();
+        JsonNode originGeoNode = objectMapper.readTree(geoJson);
+
+        Optional<Integer> srid =
+                Optional.ofNullable(
+                        originGeoNode.has("srid") ? 
originGeoNode.get("srid").intValue() : null);
+        Map<String, Object> geometryInfo = new HashMap<>();
+        String geometryType = originGeoNode.get("type").asText();
+        geometryInfo.put("type", geometryType);
+        if (geometryType.equalsIgnoreCase("GeometryCollection")) {
+            geometryInfo.put("geometries", originGeoNode.get("geometries"));
+        } else {
+            geometryInfo.put("coordinates", originGeoNode.get("coordinates"));
+        }
+        geometryInfo.put("srid", srid.orElse(0));
+        ObjectWriter objectWriter = objectMapper.writer();
+        return objectWriter.writeValueAsString(geometryInfo);
+    }
+
+    public static boolean isScaleType(String typeName) {
+        return HAVE_SCALE_LIST.stream()
+                .anyMatch(type -> 
getShortType(typeName).toUpperCase().startsWith(type));
+    }
+
+    public static boolean isEnumType(String typeName) {
+        return typeName.toLowerCase().startsWith(ENUM);

Review Comment:
   Method 'startsWith()' always returns false: the argument contains an 
uppercase symbol while the qualifier is lowercase-only.
   
   Please add tests.



##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/action/cdc/mysql/MySqlTypeUtils.java:
##########
@@ -232,4 +270,110 @@ public static DataType toDataType(
                         String.format("Don't support MySQL type '%s' yet.", 
type));
         }
     }
+
+    public static boolean isGeoType(String type) {
+        switch (type.toUpperCase()) {
+            case GEOMETRY:
+            case POINT:
+            case LINESTRING:
+            case POLYGON:
+            case MULTIPOINT:
+            case MULTILINESTRING:
+            case MULTIPOLYGON:
+            case GEOMETRYCOLLECTION:
+                return true;
+            default:
+                return false;
+        }
+    }
+
+    public static String convertWkbArray(byte[] wkb) throws 
JsonProcessingException {
+        String geoJson = 
OGCGeometry.fromBinary(ByteBuffer.wrap(wkb)).asGeoJson();
+        JsonNode originGeoNode = objectMapper.readTree(geoJson);
+
+        Optional<Integer> srid =
+                Optional.ofNullable(
+                        originGeoNode.has("srid") ? 
originGeoNode.get("srid").intValue() : null);
+        Map<String, Object> geometryInfo = new HashMap<>();
+        String geometryType = originGeoNode.get("type").asText();
+        geometryInfo.put("type", geometryType);
+        if (geometryType.equalsIgnoreCase("GeometryCollection")) {
+            geometryInfo.put("geometries", originGeoNode.get("geometries"));
+        } else {
+            geometryInfo.put("coordinates", originGeoNode.get("coordinates"));
+        }
+        geometryInfo.put("srid", srid.orElse(0));
+        ObjectWriter objectWriter = objectMapper.writer();
+        return objectWriter.writeValueAsString(geometryInfo);
+    }
+
+    public static boolean isScaleType(String typeName) {
+        return HAVE_SCALE_LIST.stream()
+                .anyMatch(type -> 
getShortType(typeName).toUpperCase().startsWith(type));
+    }
+
+    public static boolean isEnumType(String typeName) {
+        return typeName.toLowerCase().startsWith(ENUM);
+    }
+
+    public static boolean isSetType(String typeName) {
+        return typeName.toLowerCase().startsWith(SET);
+    }
+
+    /* Get type after the brackets are removed.*/
+    public static String getShortType(String typeName) {
+
+        if (typeName.contains(LEFT_BRACKETS) && 
typeName.contains(RIGHT_BRACKETS)) {
+            String type =
+                    typeName.substring(0, 
typeName.indexOf(LEFT_BRACKETS)).trim()
+                            + 
typeName.substring(typeName.indexOf(RIGHT_BRACKETS) + 1);
+            return type;
+        } else {
+            return typeName;
+        }
+    }
+
+    public static int getPrecision(String typeName) {
+        if (typeName.contains(LEFT_BRACKETS)
+                && typeName.contains(RIGHT_BRACKETS)
+                && isScaleType(typeName)) {
+            int precision =
+                    Integer.parseInt(
+                            typeName.substring(
+                                            typeName.indexOf(LEFT_BRACKETS) + 
1,
+                                            typeName.indexOf(COMMA))
+                                    .trim());
+            return precision;
+        } else if ((typeName.contains(LEFT_BRACKETS)
+                && typeName.contains(RIGHT_BRACKETS)
+                && !isScaleType(typeName)
+                && !isEnumType(typeName)
+                && !isSetType(typeName))) {
+            int precision =

Review Comment:
   Local variable 'precision' is redundant``



##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/action/cdc/kafka/canal/CanalJsonEventParser.java:
##########
@@ -0,0 +1,354 @@
+/*
+ * 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.paimon.flink.action.cdc.kafka.canal;
+
+import org.apache.paimon.flink.action.cdc.ComputedColumn;
+import org.apache.paimon.flink.action.cdc.TableNameConverter;
+import org.apache.paimon.flink.action.cdc.mysql.MySqlTypeUtils;
+import org.apache.paimon.flink.sink.cdc.CdcRecord;
+import org.apache.paimon.flink.sink.cdc.EventParser;
+import org.apache.paimon.types.DataField;
+import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.RowKind;
+import org.apache.paimon.utils.Preconditions;
+import org.apache.paimon.utils.StringUtils;
+
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.core.type.TypeReference;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.JsonNode;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.node.ArrayNode;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.node.NullNode;
+
+import com.alibaba.druid.DbType;
+import com.alibaba.druid.sql.SQLUtils;
+import com.alibaba.druid.sql.ast.SQLName;
+import com.alibaba.druid.sql.ast.SQLStatement;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddColumn;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropColumnItem;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableItem;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement;
+import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
+import 
com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn;
+import 
com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+import static org.apache.paimon.utils.Preconditions.checkArgument;
+
+/** {@link EventParser} for Canal-json. */
+public class CanalJsonEventParser implements EventParser<String> {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(CanalJsonEventParser.class);
+
+    private static final String FIELD_DATA = "data";
+    private static final String FIELD_OLD = "old";
+    private static final String TYPE = "type";
+    private static final String OP_INSERT = "INSERT";
+    private static final String OP_UPDATE = "UPDATE";
+    private static final String OP_DELETE = "DELETE";
+    private static final String OP_CREATE = "CREATE";
+
+    private final ObjectMapper objectMapper = new ObjectMapper();
+
+    private JsonNode root;
+    private Map<String, String> mySqlFieldTypes;
+    private Map<String, DataType> paimonFieldTypes;
+
+    private final boolean caseSensitive;
+    private final TableNameConverter tableNameConverter;
+    private final List<ComputedColumn> computedColumns;
+
+    public CanalJsonEventParser(boolean caseSensitive, List<ComputedColumn> 
computedColumns) {
+        this(caseSensitive, new TableNameConverter(caseSensitive), 
computedColumns);
+    }
+
+    public CanalJsonEventParser(boolean caseSensitive, TableNameConverter 
tableNameConverter) {
+        this(caseSensitive, tableNameConverter, Collections.emptyList());
+    }
+
+    public CanalJsonEventParser(
+            boolean caseSensitive,
+            TableNameConverter tableNameConverter,
+            List<ComputedColumn> computedColumns) {
+        this.caseSensitive = caseSensitive;
+        this.tableNameConverter = tableNameConverter;
+        this.computedColumns = computedColumns;
+    }
+
+    @Override
+    public void setRawEvent(String rawEvent) {
+        try {
+            root = objectMapper.readValue(rawEvent, JsonNode.class);
+            Preconditions.checkNotNull(
+                    root.get("mysqlType"),
+                    "CanalJsonEventParser only supports canal-json format,"
+                            + "please make sure that your topic's format is 
accurate.");
+            JsonNode mysqlType = root.get("mysqlType");
+
+            if (!isUpdatedDataFields()) {
+                updateFieldTypes(mysqlType);
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public String tableName() {
+        String tableName = root.get("table").asText();
+        return tableNameConverter.convert(tableName);
+    }
+
+    private void updateFieldTypes(JsonNode schema) {
+        mySqlFieldTypes = new LinkedHashMap<>();
+        paimonFieldTypes = new LinkedHashMap<>();
+        Iterator<String> iterator = schema.fieldNames();
+        while (iterator.hasNext()) {
+            String fieldName = iterator.next();
+            String fieldType = schema.get(fieldName).asText();
+            mySqlFieldTypes.put(fieldName, fieldType);
+            paimonFieldTypes.put(fieldName, 
MySqlTypeUtils.toDataType(fieldType));
+        }
+    }
+
+    @Override
+    public boolean isUpdatedDataFields() {
+        if (root.get("isDdl") == null) {
+            return false;
+        } else {
+            return "true".equals(root.get("isDdl").asText());
+        }
+    }
+
+    @Override
+    public Optional<List<DataField>> getUpdatedDataFields() {
+        String sql = root.get("sql").asText();
+
+        if (StringUtils.isEmpty(sql)) {
+            return Optional.empty();
+        }
+
+        List<DataField> result = new ArrayList<>();
+        int id = 0;
+        for (Map.Entry<String, DataType> fieldType : 
paimonFieldTypes.entrySet()) {
+            result.add(new DataField(id++, fieldType.getKey(), 
fieldType.getValue()));
+        }
+        SQLStatement sqlStatement = SQLUtils.parseSingleStatement(sql, 
DbType.mysql);
+        if (sqlStatement instanceof SQLAlterTableStatement) {
+            SQLAlterTableStatement sqlAlterTableStatement = 
(SQLAlterTableStatement) sqlStatement;
+            for (int i = 0; i < sqlAlterTableStatement.getItems().size(); i++) 
{
+                SQLAlterTableItem sqlAlterTableItem = 
sqlAlterTableStatement.getItems().get(i);
+                if (sqlAlterTableItem instanceof SQLAlterTableAddColumn) {
+                    SQLAlterTableAddColumn sqlAlterTableAddColumn =
+                            (SQLAlterTableAddColumn) sqlAlterTableItem;
+                    List<SQLColumnDefinition> columns = 
sqlAlterTableAddColumn.getColumns();
+                    for (int j = 0; j < columns.size(); j++) {
+                        String columnName = 
columns.get(j).getColumnName().replace("`", "");
+                        String columnType = 
columns.get(j).getDataType().toString();
+                        DataType dataType = 
MySqlTypeUtils.toDataType(columnType);
+                        boolean notNull =
+                                
columns.get(j).toString().toUpperCase().contains("NOT NULL");
+                        dataType = notNull ? dataType.notNull() : 
dataType.nullable();
+                        result =
+                                result.stream()
+                                        .filter(dataField -> 
!dataField.name().equals(columnName))
+                                        .collect(Collectors.toList());
+                        result.add(new DataField(id, columnName, dataType));
+                        mySqlFieldTypes.put(columnName, columnType);
+                        paimonFieldTypes.put(columnName, 
MySqlTypeUtils.toDataType(columnType));
+                    }
+                } else if (sqlAlterTableItem instanceof 
SQLAlterTableDropColumnItem) {
+                    SQLAlterTableDropColumnItem sqlAlterTableDropColumnItem =

Review Comment:
   No test cover



##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/action/cdc/kafka/canal/CanalJsonEventParser.java:
##########
@@ -0,0 +1,354 @@
+/*
+ * 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.paimon.flink.action.cdc.kafka.canal;
+
+import org.apache.paimon.flink.action.cdc.ComputedColumn;
+import org.apache.paimon.flink.action.cdc.TableNameConverter;
+import org.apache.paimon.flink.action.cdc.mysql.MySqlTypeUtils;
+import org.apache.paimon.flink.sink.cdc.CdcRecord;
+import org.apache.paimon.flink.sink.cdc.EventParser;
+import org.apache.paimon.types.DataField;
+import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.RowKind;
+import org.apache.paimon.utils.Preconditions;
+import org.apache.paimon.utils.StringUtils;
+
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.core.type.TypeReference;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.JsonNode;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.node.ArrayNode;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.node.NullNode;
+
+import com.alibaba.druid.DbType;
+import com.alibaba.druid.sql.SQLUtils;
+import com.alibaba.druid.sql.ast.SQLName;
+import com.alibaba.druid.sql.ast.SQLStatement;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddColumn;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropColumnItem;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableItem;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement;
+import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
+import 
com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn;
+import 
com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+import static org.apache.paimon.utils.Preconditions.checkArgument;
+
+/** {@link EventParser} for Canal-json. */
+public class CanalJsonEventParser implements EventParser<String> {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(CanalJsonEventParser.class);
+
+    private static final String FIELD_DATA = "data";
+    private static final String FIELD_OLD = "old";
+    private static final String TYPE = "type";
+    private static final String OP_INSERT = "INSERT";
+    private static final String OP_UPDATE = "UPDATE";
+    private static final String OP_DELETE = "DELETE";
+    private static final String OP_CREATE = "CREATE";
+
+    private final ObjectMapper objectMapper = new ObjectMapper();
+
+    private JsonNode root;
+    private Map<String, String> mySqlFieldTypes;
+    private Map<String, DataType> paimonFieldTypes;
+
+    private final boolean caseSensitive;
+    private final TableNameConverter tableNameConverter;
+    private final List<ComputedColumn> computedColumns;
+
+    public CanalJsonEventParser(boolean caseSensitive, List<ComputedColumn> 
computedColumns) {
+        this(caseSensitive, new TableNameConverter(caseSensitive), 
computedColumns);
+    }
+
+    public CanalJsonEventParser(boolean caseSensitive, TableNameConverter 
tableNameConverter) {
+        this(caseSensitive, tableNameConverter, Collections.emptyList());
+    }
+
+    public CanalJsonEventParser(
+            boolean caseSensitive,
+            TableNameConverter tableNameConverter,
+            List<ComputedColumn> computedColumns) {
+        this.caseSensitive = caseSensitive;
+        this.tableNameConverter = tableNameConverter;
+        this.computedColumns = computedColumns;
+    }
+
+    @Override
+    public void setRawEvent(String rawEvent) {
+        try {
+            root = objectMapper.readValue(rawEvent, JsonNode.class);
+            Preconditions.checkNotNull(
+                    root.get("mysqlType"),
+                    "CanalJsonEventParser only supports canal-json format,"
+                            + "please make sure that your topic's format is 
accurate.");
+            JsonNode mysqlType = root.get("mysqlType");
+
+            if (!isUpdatedDataFields()) {
+                updateFieldTypes(mysqlType);
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public String tableName() {
+        String tableName = root.get("table").asText();
+        return tableNameConverter.convert(tableName);
+    }
+
+    private void updateFieldTypes(JsonNode schema) {
+        mySqlFieldTypes = new LinkedHashMap<>();
+        paimonFieldTypes = new LinkedHashMap<>();
+        Iterator<String> iterator = schema.fieldNames();
+        while (iterator.hasNext()) {
+            String fieldName = iterator.next();
+            String fieldType = schema.get(fieldName).asText();
+            mySqlFieldTypes.put(fieldName, fieldType);
+            paimonFieldTypes.put(fieldName, 
MySqlTypeUtils.toDataType(fieldType));
+        }
+    }
+
+    @Override
+    public boolean isUpdatedDataFields() {
+        if (root.get("isDdl") == null) {
+            return false;
+        } else {
+            return "true".equals(root.get("isDdl").asText());
+        }
+    }
+
+    @Override
+    public Optional<List<DataField>> getUpdatedDataFields() {
+        String sql = root.get("sql").asText();
+
+        if (StringUtils.isEmpty(sql)) {
+            return Optional.empty();
+        }
+
+        List<DataField> result = new ArrayList<>();
+        int id = 0;
+        for (Map.Entry<String, DataType> fieldType : 
paimonFieldTypes.entrySet()) {
+            result.add(new DataField(id++, fieldType.getKey(), 
fieldType.getValue()));
+        }
+        SQLStatement sqlStatement = SQLUtils.parseSingleStatement(sql, 
DbType.mysql);
+        if (sqlStatement instanceof SQLAlterTableStatement) {
+            SQLAlterTableStatement sqlAlterTableStatement = 
(SQLAlterTableStatement) sqlStatement;
+            for (int i = 0; i < sqlAlterTableStatement.getItems().size(); i++) 
{
+                SQLAlterTableItem sqlAlterTableItem = 
sqlAlterTableStatement.getItems().get(i);
+                if (sqlAlterTableItem instanceof SQLAlterTableAddColumn) {
+                    SQLAlterTableAddColumn sqlAlterTableAddColumn =
+                            (SQLAlterTableAddColumn) sqlAlterTableItem;
+                    List<SQLColumnDefinition> columns = 
sqlAlterTableAddColumn.getColumns();
+                    for (int j = 0; j < columns.size(); j++) {
+                        String columnName = 
columns.get(j).getColumnName().replace("`", "");
+                        String columnType = 
columns.get(j).getDataType().toString();
+                        DataType dataType = 
MySqlTypeUtils.toDataType(columnType);
+                        boolean notNull =
+                                
columns.get(j).toString().toUpperCase().contains("NOT NULL");
+                        dataType = notNull ? dataType.notNull() : 
dataType.nullable();
+                        result =
+                                result.stream()
+                                        .filter(dataField -> 
!dataField.name().equals(columnName))
+                                        .collect(Collectors.toList());
+                        result.add(new DataField(id, columnName, dataType));
+                        mySqlFieldTypes.put(columnName, columnType);
+                        paimonFieldTypes.put(columnName, 
MySqlTypeUtils.toDataType(columnType));
+                    }
+                } else if (sqlAlterTableItem instanceof 
SQLAlterTableDropColumnItem) {
+                    SQLAlterTableDropColumnItem sqlAlterTableDropColumnItem =
+                            (SQLAlterTableDropColumnItem) sqlAlterTableItem;
+                    List<String> columnNames =
+                            sqlAlterTableDropColumnItem.getColumns().stream()
+                                    .map(SQLName::getSimpleName)
+                                    .collect(Collectors.toList());
+                    columnNames.forEach(
+                            columnName -> {
+                                mySqlFieldTypes.remove(columnName);
+                                paimonFieldTypes.remove(columnName);
+                            });
+
+                    result =
+                            result.stream()
+                                    .filter(dataField -> 
!columnNames.contains(dataField.name()))
+                                    .collect(Collectors.toList());
+                } else if (sqlAlterTableItem instanceof 
MySqlAlterTableModifyColumn) {
+                    MySqlAlterTableModifyColumn mySqlAlterTableModifyColumn =
+                            (MySqlAlterTableModifyColumn) sqlAlterTableItem;
+                    SQLColumnDefinition newColumnDefinition =
+                            
mySqlAlterTableModifyColumn.getNewColumnDefinition();
+                    String columnName = 
newColumnDefinition.getColumnName().replace("`", "");
+                    String columnType = 
newColumnDefinition.getDataType().toString();
+                    DataType dataType = MySqlTypeUtils.toDataType(columnType);
+                    boolean notNull =
+                            
newColumnDefinition.toString().toUpperCase().contains("NOT NULL");
+                    dataType = notNull ? dataType.notNull() : 
dataType.nullable();
+                    result =
+                            result.stream()
+                                    .filter(dataField -> 
!dataField.name().equals(columnName))
+                                    .collect(Collectors.toList());
+                    result.add(new DataField(id, columnName, dataType));
+                    mySqlFieldTypes.put(columnName, columnType);
+                    paimonFieldTypes.put(columnName, 
MySqlTypeUtils.toDataType(columnType));
+                } else if (sqlAlterTableItem instanceof 
MySqlAlterTableChangeColumn) {
+                    MySqlAlterTableChangeColumn mySqlAlterTableChangeColumn =
+                            (MySqlAlterTableChangeColumn) sqlAlterTableItem;
+                    SQLColumnDefinition newColumnDefinition =
+                            
mySqlAlterTableChangeColumn.getNewColumnDefinition();
+                    String oldColumnName =
+                            
mySqlAlterTableChangeColumn.getColumnName().getSimpleName();
+                    String columnName = 
newColumnDefinition.getColumnName().replace("`", "");
+                    String columnType = 
newColumnDefinition.getDataType().toString();
+                    DataType dataType = MySqlTypeUtils.toDataType(columnType);
+                    boolean notNull =
+                            
newColumnDefinition.toString().toUpperCase().contains("NOT NULL");
+                    dataType = notNull ? dataType.notNull() : 
dataType.nullable();
+                    result =
+                            result.stream()
+                                    .filter(dataField -> 
!dataField.name().equals(oldColumnName))
+                                    .collect(Collectors.toList());
+                    result.add(new DataField(id, columnName, dataType));
+                    mySqlFieldTypes.remove(oldColumnName);
+                    paimonFieldTypes.remove(oldColumnName);
+                    mySqlFieldTypes.put(columnName, columnType);
+                    paimonFieldTypes.put(columnName, 
MySqlTypeUtils.toDataType(columnType));
+                }
+            }
+        }
+
+        return Optional.of(result);
+    }
+
+    @Override
+    public List<CdcRecord> getRecords() {
+        List<CdcRecord> records = new ArrayList<>();
+        String type = root.get(TYPE).asText();
+        if (OP_UPDATE.equals(type)) {
+            ArrayNode data = (ArrayNode) root.get(FIELD_DATA);
+            ArrayNode old =
+                    root.get(FIELD_OLD) instanceof NullNode
+                            ? null
+                            : (ArrayNode) root.get(FIELD_OLD);
+            for (int i = 0; i < data.size(); i++) {
+                Map<String, String> after = extractRow(data.get(i));
+                if (old != null) {
+                    Map<String, String> before = extractRow(old.get(i));
+                    // fields in "old" (before) means the fields are changed
+                    // fields not in "old" (before) means the fields are not 
changed
+                    // so we just copy the not changed fields into before
+                    for (Map.Entry<String, String> entry : after.entrySet()) {
+                        if (!before.containsKey(entry.getKey())) {
+                            before.put(entry.getKey(), entry.getValue());
+                        }
+                    }
+                    before = caseSensitive ? before : 
keyCaseInsensitive(before);
+                    records.add(new CdcRecord(RowKind.DELETE, before));
+                }
+                after = caseSensitive ? after : keyCaseInsensitive(after);
+                records.add(new CdcRecord(RowKind.INSERT, after));
+            }
+        } else if (OP_INSERT.equals(type)) {
+            ArrayNode data = (ArrayNode) root.get(FIELD_DATA);
+            for (int i = 0; i < data.size(); i++) {
+                Map<String, String> after = extractRow(data.get(i));
+                after = caseSensitive ? after : keyCaseInsensitive(after);
+                records.add(new CdcRecord(RowKind.INSERT, after));
+            }
+        } else if (OP_DELETE.equals(type)) {
+            ArrayNode data = (ArrayNode) root.get(FIELD_DATA);
+            for (int i = 0; i < data.size(); i++) {
+                Map<String, String> after = extractRow(data.get(i));
+                after = caseSensitive ? after : keyCaseInsensitive(after);
+                records.add(new CdcRecord(RowKind.DELETE, after));
+            }
+        }
+
+        return records;
+    }
+
+    private Map<String, String> extractRow(JsonNode recordRow) {
+        Map<String, Object> jsonMap =
+                objectMapper.convertValue(recordRow, new 
TypeReference<Map<String, Object>>() {});
+        if (jsonMap == null) {
+            return new HashMap<>();
+        }
+        Map<String, String> resultMap = new HashMap<>();
+        for (Map.Entry<String, String> field : mySqlFieldTypes.entrySet()) {
+            String fieldName = field.getKey();
+            String mySqlType = field.getValue();
+            Object objectValue = jsonMap.get(fieldName);
+            if (objectValue == null) {
+                continue;
+            }
+
+            String oldValue = objectValue.toString();
+            String newValue = oldValue;
+            if 
(MySqlTypeUtils.isSetType(MySqlTypeUtils.getShortType(mySqlType))) {
+                newValue = CanalFieldParser.convertSet(newValue, mySqlType);
+            } else if 
(MySqlTypeUtils.isEnumType(MySqlTypeUtils.getShortType(mySqlType))) {
+                newValue = CanalFieldParser.convertEnum(newValue, mySqlType);
+            } else if 
(MySqlTypeUtils.isGeoType(MySqlTypeUtils.getShortType(mySqlType))) {
+                try {
+                    byte[] wkb =
+                            CanalFieldParser.convertGeoType2WkbArray(
+                                    
oldValue.getBytes(StandardCharsets.ISO_8859_1));
+                    newValue = MySqlTypeUtils.convertWkbArray(wkb);
+                } catch (Exception e) {
+                    throw new IllegalArgumentException(
+                            String.format("Failed to convert %s to geometry 
JSON.", oldValue), e);
+                }
+            }
+            resultMap.put(fieldName, newValue);
+        }
+        // generate values of computed columns
+        for (ComputedColumn computedColumn : computedColumns) {
+            resultMap.put(
+                    computedColumn.columnName(),
+                    
computedColumn.eval(resultMap.get(computedColumn.fieldReference())));
+        }
+
+        return resultMap;
+    }
+
+    private Map<String, String> keyCaseInsensitive(Map<String, String> origin) 
{
+        Map<String, String> keyCaseInsensitive = new HashMap<>();

Review Comment:
   No test cover



##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/action/cdc/kafka/canal/CanalJsonEventParser.java:
##########
@@ -0,0 +1,354 @@
+/*
+ * 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.paimon.flink.action.cdc.kafka.canal;
+
+import org.apache.paimon.flink.action.cdc.ComputedColumn;
+import org.apache.paimon.flink.action.cdc.TableNameConverter;
+import org.apache.paimon.flink.action.cdc.mysql.MySqlTypeUtils;
+import org.apache.paimon.flink.sink.cdc.CdcRecord;
+import org.apache.paimon.flink.sink.cdc.EventParser;
+import org.apache.paimon.types.DataField;
+import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.RowKind;
+import org.apache.paimon.utils.Preconditions;
+import org.apache.paimon.utils.StringUtils;
+
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.core.type.TypeReference;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.JsonNode;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.node.ArrayNode;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.node.NullNode;
+
+import com.alibaba.druid.DbType;
+import com.alibaba.druid.sql.SQLUtils;
+import com.alibaba.druid.sql.ast.SQLName;
+import com.alibaba.druid.sql.ast.SQLStatement;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddColumn;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropColumnItem;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableItem;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement;
+import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
+import 
com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn;
+import 
com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+import static org.apache.paimon.utils.Preconditions.checkArgument;
+
+/** {@link EventParser} for Canal-json. */
+public class CanalJsonEventParser implements EventParser<String> {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(CanalJsonEventParser.class);
+
+    private static final String FIELD_DATA = "data";
+    private static final String FIELD_OLD = "old";
+    private static final String TYPE = "type";
+    private static final String OP_INSERT = "INSERT";
+    private static final String OP_UPDATE = "UPDATE";
+    private static final String OP_DELETE = "DELETE";
+    private static final String OP_CREATE = "CREATE";
+
+    private final ObjectMapper objectMapper = new ObjectMapper();
+
+    private JsonNode root;
+    private Map<String, String> mySqlFieldTypes;
+    private Map<String, DataType> paimonFieldTypes;
+
+    private final boolean caseSensitive;
+    private final TableNameConverter tableNameConverter;
+    private final List<ComputedColumn> computedColumns;
+
+    public CanalJsonEventParser(boolean caseSensitive, List<ComputedColumn> 
computedColumns) {
+        this(caseSensitive, new TableNameConverter(caseSensitive), 
computedColumns);
+    }
+
+    public CanalJsonEventParser(boolean caseSensitive, TableNameConverter 
tableNameConverter) {
+        this(caseSensitive, tableNameConverter, Collections.emptyList());
+    }
+
+    public CanalJsonEventParser(
+            boolean caseSensitive,
+            TableNameConverter tableNameConverter,
+            List<ComputedColumn> computedColumns) {
+        this.caseSensitive = caseSensitive;
+        this.tableNameConverter = tableNameConverter;
+        this.computedColumns = computedColumns;
+    }
+
+    @Override
+    public void setRawEvent(String rawEvent) {
+        try {
+            root = objectMapper.readValue(rawEvent, JsonNode.class);
+            Preconditions.checkNotNull(
+                    root.get("mysqlType"),
+                    "CanalJsonEventParser only supports canal-json format,"
+                            + "please make sure that your topic's format is 
accurate.");
+            JsonNode mysqlType = root.get("mysqlType");
+
+            if (!isUpdatedDataFields()) {
+                updateFieldTypes(mysqlType);
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public String tableName() {
+        String tableName = root.get("table").asText();
+        return tableNameConverter.convert(tableName);
+    }
+
+    private void updateFieldTypes(JsonNode schema) {
+        mySqlFieldTypes = new LinkedHashMap<>();
+        paimonFieldTypes = new LinkedHashMap<>();
+        Iterator<String> iterator = schema.fieldNames();
+        while (iterator.hasNext()) {
+            String fieldName = iterator.next();
+            String fieldType = schema.get(fieldName).asText();
+            mySqlFieldTypes.put(fieldName, fieldType);
+            paimonFieldTypes.put(fieldName, 
MySqlTypeUtils.toDataType(fieldType));
+        }
+    }
+
+    @Override
+    public boolean isUpdatedDataFields() {
+        if (root.get("isDdl") == null) {
+            return false;
+        } else {
+            return "true".equals(root.get("isDdl").asText());
+        }
+    }
+
+    @Override
+    public Optional<List<DataField>> getUpdatedDataFields() {
+        String sql = root.get("sql").asText();
+
+        if (StringUtils.isEmpty(sql)) {
+            return Optional.empty();
+        }
+
+        List<DataField> result = new ArrayList<>();
+        int id = 0;
+        for (Map.Entry<String, DataType> fieldType : 
paimonFieldTypes.entrySet()) {
+            result.add(new DataField(id++, fieldType.getKey(), 
fieldType.getValue()));
+        }
+        SQLStatement sqlStatement = SQLUtils.parseSingleStatement(sql, 
DbType.mysql);
+        if (sqlStatement instanceof SQLAlterTableStatement) {
+            SQLAlterTableStatement sqlAlterTableStatement = 
(SQLAlterTableStatement) sqlStatement;
+            for (int i = 0; i < sqlAlterTableStatement.getItems().size(); i++) 
{
+                SQLAlterTableItem sqlAlterTableItem = 
sqlAlterTableStatement.getItems().get(i);
+                if (sqlAlterTableItem instanceof SQLAlterTableAddColumn) {
+                    SQLAlterTableAddColumn sqlAlterTableAddColumn =
+                            (SQLAlterTableAddColumn) sqlAlterTableItem;
+                    List<SQLColumnDefinition> columns = 
sqlAlterTableAddColumn.getColumns();
+                    for (int j = 0; j < columns.size(); j++) {
+                        String columnName = 
columns.get(j).getColumnName().replace("`", "");
+                        String columnType = 
columns.get(j).getDataType().toString();
+                        DataType dataType = 
MySqlTypeUtils.toDataType(columnType);
+                        boolean notNull =
+                                
columns.get(j).toString().toUpperCase().contains("NOT NULL");
+                        dataType = notNull ? dataType.notNull() : 
dataType.nullable();
+                        result =
+                                result.stream()
+                                        .filter(dataField -> 
!dataField.name().equals(columnName))
+                                        .collect(Collectors.toList());
+                        result.add(new DataField(id, columnName, dataType));
+                        mySqlFieldTypes.put(columnName, columnType);
+                        paimonFieldTypes.put(columnName, 
MySqlTypeUtils.toDataType(columnType));
+                    }
+                } else if (sqlAlterTableItem instanceof 
SQLAlterTableDropColumnItem) {
+                    SQLAlterTableDropColumnItem sqlAlterTableDropColumnItem =
+                            (SQLAlterTableDropColumnItem) sqlAlterTableItem;
+                    List<String> columnNames =
+                            sqlAlterTableDropColumnItem.getColumns().stream()
+                                    .map(SQLName::getSimpleName)
+                                    .collect(Collectors.toList());
+                    columnNames.forEach(
+                            columnName -> {
+                                mySqlFieldTypes.remove(columnName);
+                                paimonFieldTypes.remove(columnName);
+                            });
+
+                    result =
+                            result.stream()
+                                    .filter(dataField -> 
!columnNames.contains(dataField.name()))
+                                    .collect(Collectors.toList());
+                } else if (sqlAlterTableItem instanceof 
MySqlAlterTableModifyColumn) {
+                    MySqlAlterTableModifyColumn mySqlAlterTableModifyColumn =
+                            (MySqlAlterTableModifyColumn) sqlAlterTableItem;
+                    SQLColumnDefinition newColumnDefinition =
+                            
mySqlAlterTableModifyColumn.getNewColumnDefinition();
+                    String columnName = 
newColumnDefinition.getColumnName().replace("`", "");
+                    String columnType = 
newColumnDefinition.getDataType().toString();
+                    DataType dataType = MySqlTypeUtils.toDataType(columnType);
+                    boolean notNull =
+                            
newColumnDefinition.toString().toUpperCase().contains("NOT NULL");
+                    dataType = notNull ? dataType.notNull() : 
dataType.nullable();
+                    result =
+                            result.stream()
+                                    .filter(dataField -> 
!dataField.name().equals(columnName))
+                                    .collect(Collectors.toList());
+                    result.add(new DataField(id, columnName, dataType));
+                    mySqlFieldTypes.put(columnName, columnType);
+                    paimonFieldTypes.put(columnName, 
MySqlTypeUtils.toDataType(columnType));
+                } else if (sqlAlterTableItem instanceof 
MySqlAlterTableChangeColumn) {
+                    MySqlAlterTableChangeColumn mySqlAlterTableChangeColumn =
+                            (MySqlAlterTableChangeColumn) sqlAlterTableItem;
+                    SQLColumnDefinition newColumnDefinition =
+                            
mySqlAlterTableChangeColumn.getNewColumnDefinition();
+                    String oldColumnName =
+                            
mySqlAlterTableChangeColumn.getColumnName().getSimpleName();
+                    String columnName = 
newColumnDefinition.getColumnName().replace("`", "");
+                    String columnType = 
newColumnDefinition.getDataType().toString();
+                    DataType dataType = MySqlTypeUtils.toDataType(columnType);
+                    boolean notNull =
+                            
newColumnDefinition.toString().toUpperCase().contains("NOT NULL");
+                    dataType = notNull ? dataType.notNull() : 
dataType.nullable();
+                    result =
+                            result.stream()
+                                    .filter(dataField -> 
!dataField.name().equals(oldColumnName))
+                                    .collect(Collectors.toList());
+                    result.add(new DataField(id, columnName, dataType));
+                    mySqlFieldTypes.remove(oldColumnName);
+                    paimonFieldTypes.remove(oldColumnName);
+                    mySqlFieldTypes.put(columnName, columnType);
+                    paimonFieldTypes.put(columnName, 
MySqlTypeUtils.toDataType(columnType));
+                }
+            }
+        }
+
+        return Optional.of(result);
+    }
+
+    @Override
+    public List<CdcRecord> getRecords() {
+        List<CdcRecord> records = new ArrayList<>();
+        String type = root.get(TYPE).asText();
+        if (OP_UPDATE.equals(type)) {
+            ArrayNode data = (ArrayNode) root.get(FIELD_DATA);
+            ArrayNode old =
+                    root.get(FIELD_OLD) instanceof NullNode
+                            ? null
+                            : (ArrayNode) root.get(FIELD_OLD);
+            for (int i = 0; i < data.size(); i++) {
+                Map<String, String> after = extractRow(data.get(i));
+                if (old != null) {
+                    Map<String, String> before = extractRow(old.get(i));
+                    // fields in "old" (before) means the fields are changed
+                    // fields not in "old" (before) means the fields are not 
changed
+                    // so we just copy the not changed fields into before
+                    for (Map.Entry<String, String> entry : after.entrySet()) {
+                        if (!before.containsKey(entry.getKey())) {
+                            before.put(entry.getKey(), entry.getValue());
+                        }
+                    }
+                    before = caseSensitive ? before : 
keyCaseInsensitive(before);
+                    records.add(new CdcRecord(RowKind.DELETE, before));
+                }
+                after = caseSensitive ? after : keyCaseInsensitive(after);
+                records.add(new CdcRecord(RowKind.INSERT, after));
+            }
+        } else if (OP_INSERT.equals(type)) {
+            ArrayNode data = (ArrayNode) root.get(FIELD_DATA);
+            for (int i = 0; i < data.size(); i++) {
+                Map<String, String> after = extractRow(data.get(i));
+                after = caseSensitive ? after : keyCaseInsensitive(after);
+                records.add(new CdcRecord(RowKind.INSERT, after));
+            }
+        } else if (OP_DELETE.equals(type)) {
+            ArrayNode data = (ArrayNode) root.get(FIELD_DATA);
+            for (int i = 0; i < data.size(); i++) {
+                Map<String, String> after = extractRow(data.get(i));
+                after = caseSensitive ? after : keyCaseInsensitive(after);
+                records.add(new CdcRecord(RowKind.DELETE, after));
+            }
+        }
+
+        return records;
+    }
+
+    private Map<String, String> extractRow(JsonNode recordRow) {
+        Map<String, Object> jsonMap =
+                objectMapper.convertValue(recordRow, new 
TypeReference<Map<String, Object>>() {});
+        if (jsonMap == null) {
+            return new HashMap<>();
+        }
+        Map<String, String> resultMap = new HashMap<>();
+        for (Map.Entry<String, String> field : mySqlFieldTypes.entrySet()) {
+            String fieldName = field.getKey();
+            String mySqlType = field.getValue();
+            Object objectValue = jsonMap.get(fieldName);
+            if (objectValue == null) {
+                continue;
+            }
+
+            String oldValue = objectValue.toString();
+            String newValue = oldValue;
+            if 
(MySqlTypeUtils.isSetType(MySqlTypeUtils.getShortType(mySqlType))) {
+                newValue = CanalFieldParser.convertSet(newValue, mySqlType);
+            } else if 
(MySqlTypeUtils.isEnumType(MySqlTypeUtils.getShortType(mySqlType))) {
+                newValue = CanalFieldParser.convertEnum(newValue, mySqlType);
+            } else if 
(MySqlTypeUtils.isGeoType(MySqlTypeUtils.getShortType(mySqlType))) {
+                try {

Review Comment:
   No test cover



##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/action/cdc/kafka/canal/CanalFieldParser.java:
##########
@@ -0,0 +1,126 @@
+/*
+ * 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.paimon.flink.action.cdc.kafka.canal;
+
+/** Converts some special types such as enum、set、geometry. */
+public class CanalFieldParser {

Review Comment:
   No test cover for this class



##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/action/cdc/kafka/canal/CanalJsonEventParser.java:
##########
@@ -0,0 +1,354 @@
+/*
+ * 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.paimon.flink.action.cdc.kafka.canal;
+
+import org.apache.paimon.flink.action.cdc.ComputedColumn;
+import org.apache.paimon.flink.action.cdc.TableNameConverter;
+import org.apache.paimon.flink.action.cdc.mysql.MySqlTypeUtils;
+import org.apache.paimon.flink.sink.cdc.CdcRecord;
+import org.apache.paimon.flink.sink.cdc.EventParser;
+import org.apache.paimon.types.DataField;
+import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.RowKind;
+import org.apache.paimon.utils.Preconditions;
+import org.apache.paimon.utils.StringUtils;
+
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.core.type.TypeReference;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.JsonNode;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.node.ArrayNode;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.node.NullNode;
+
+import com.alibaba.druid.DbType;
+import com.alibaba.druid.sql.SQLUtils;
+import com.alibaba.druid.sql.ast.SQLName;
+import com.alibaba.druid.sql.ast.SQLStatement;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddColumn;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropColumnItem;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableItem;
+import com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement;
+import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
+import 
com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn;
+import 
com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+import static org.apache.paimon.utils.Preconditions.checkArgument;
+
+/** {@link EventParser} for Canal-json. */
+public class CanalJsonEventParser implements EventParser<String> {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(CanalJsonEventParser.class);
+
+    private static final String FIELD_DATA = "data";
+    private static final String FIELD_OLD = "old";
+    private static final String TYPE = "type";
+    private static final String OP_INSERT = "INSERT";
+    private static final String OP_UPDATE = "UPDATE";
+    private static final String OP_DELETE = "DELETE";
+    private static final String OP_CREATE = "CREATE";
+
+    private final ObjectMapper objectMapper = new ObjectMapper();
+
+    private JsonNode root;
+    private Map<String, String> mySqlFieldTypes;
+    private Map<String, DataType> paimonFieldTypes;
+
+    private final boolean caseSensitive;
+    private final TableNameConverter tableNameConverter;
+    private final List<ComputedColumn> computedColumns;
+
+    public CanalJsonEventParser(boolean caseSensitive, List<ComputedColumn> 
computedColumns) {
+        this(caseSensitive, new TableNameConverter(caseSensitive), 
computedColumns);
+    }
+
+    public CanalJsonEventParser(boolean caseSensitive, TableNameConverter 
tableNameConverter) {
+        this(caseSensitive, tableNameConverter, Collections.emptyList());
+    }
+
+    public CanalJsonEventParser(
+            boolean caseSensitive,
+            TableNameConverter tableNameConverter,
+            List<ComputedColumn> computedColumns) {
+        this.caseSensitive = caseSensitive;
+        this.tableNameConverter = tableNameConverter;
+        this.computedColumns = computedColumns;
+    }
+
+    @Override
+    public void setRawEvent(String rawEvent) {
+        try {
+            root = objectMapper.readValue(rawEvent, JsonNode.class);
+            Preconditions.checkNotNull(
+                    root.get("mysqlType"),
+                    "CanalJsonEventParser only supports canal-json format,"
+                            + "please make sure that your topic's format is 
accurate.");
+            JsonNode mysqlType = root.get("mysqlType");
+
+            if (!isUpdatedDataFields()) {
+                updateFieldTypes(mysqlType);
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public String tableName() {
+        String tableName = root.get("table").asText();
+        return tableNameConverter.convert(tableName);
+    }
+
+    private void updateFieldTypes(JsonNode schema) {
+        mySqlFieldTypes = new LinkedHashMap<>();
+        paimonFieldTypes = new LinkedHashMap<>();
+        Iterator<String> iterator = schema.fieldNames();
+        while (iterator.hasNext()) {
+            String fieldName = iterator.next();
+            String fieldType = schema.get(fieldName).asText();
+            mySqlFieldTypes.put(fieldName, fieldType);
+            paimonFieldTypes.put(fieldName, 
MySqlTypeUtils.toDataType(fieldType));
+        }
+    }
+
+    @Override
+    public boolean isUpdatedDataFields() {
+        if (root.get("isDdl") == null) {
+            return false;
+        } else {
+            return "true".equals(root.get("isDdl").asText());
+        }
+    }
+
+    @Override
+    public Optional<List<DataField>> getUpdatedDataFields() {
+        String sql = root.get("sql").asText();
+
+        if (StringUtils.isEmpty(sql)) {
+            return Optional.empty();
+        }
+
+        List<DataField> result = new ArrayList<>();
+        int id = 0;
+        for (Map.Entry<String, DataType> fieldType : 
paimonFieldTypes.entrySet()) {
+            result.add(new DataField(id++, fieldType.getKey(), 
fieldType.getValue()));
+        }
+        SQLStatement sqlStatement = SQLUtils.parseSingleStatement(sql, 
DbType.mysql);
+        if (sqlStatement instanceof SQLAlterTableStatement) {
+            SQLAlterTableStatement sqlAlterTableStatement = 
(SQLAlterTableStatement) sqlStatement;
+            for (int i = 0; i < sqlAlterTableStatement.getItems().size(); i++) 
{
+                SQLAlterTableItem sqlAlterTableItem = 
sqlAlterTableStatement.getItems().get(i);
+                if (sqlAlterTableItem instanceof SQLAlterTableAddColumn) {
+                    SQLAlterTableAddColumn sqlAlterTableAddColumn =
+                            (SQLAlterTableAddColumn) sqlAlterTableItem;
+                    List<SQLColumnDefinition> columns = 
sqlAlterTableAddColumn.getColumns();
+                    for (int j = 0; j < columns.size(); j++) {
+                        String columnName = 
columns.get(j).getColumnName().replace("`", "");
+                        String columnType = 
columns.get(j).getDataType().toString();
+                        DataType dataType = 
MySqlTypeUtils.toDataType(columnType);
+                        boolean notNull =
+                                
columns.get(j).toString().toUpperCase().contains("NOT NULL");
+                        dataType = notNull ? dataType.notNull() : 
dataType.nullable();
+                        result =
+                                result.stream()
+                                        .filter(dataField -> 
!dataField.name().equals(columnName))
+                                        .collect(Collectors.toList());
+                        result.add(new DataField(id, columnName, dataType));
+                        mySqlFieldTypes.put(columnName, columnType);
+                        paimonFieldTypes.put(columnName, 
MySqlTypeUtils.toDataType(columnType));
+                    }
+                } else if (sqlAlterTableItem instanceof 
SQLAlterTableDropColumnItem) {
+                    SQLAlterTableDropColumnItem sqlAlterTableDropColumnItem =
+                            (SQLAlterTableDropColumnItem) sqlAlterTableItem;
+                    List<String> columnNames =
+                            sqlAlterTableDropColumnItem.getColumns().stream()
+                                    .map(SQLName::getSimpleName)
+                                    .collect(Collectors.toList());
+                    columnNames.forEach(
+                            columnName -> {
+                                mySqlFieldTypes.remove(columnName);
+                                paimonFieldTypes.remove(columnName);
+                            });
+
+                    result =
+                            result.stream()
+                                    .filter(dataField -> 
!columnNames.contains(dataField.name()))
+                                    .collect(Collectors.toList());
+                } else if (sqlAlterTableItem instanceof 
MySqlAlterTableModifyColumn) {
+                    MySqlAlterTableModifyColumn mySqlAlterTableModifyColumn =
+                            (MySqlAlterTableModifyColumn) sqlAlterTableItem;
+                    SQLColumnDefinition newColumnDefinition =
+                            
mySqlAlterTableModifyColumn.getNewColumnDefinition();
+                    String columnName = 
newColumnDefinition.getColumnName().replace("`", "");
+                    String columnType = 
newColumnDefinition.getDataType().toString();
+                    DataType dataType = MySqlTypeUtils.toDataType(columnType);
+                    boolean notNull =
+                            
newColumnDefinition.toString().toUpperCase().contains("NOT NULL");
+                    dataType = notNull ? dataType.notNull() : 
dataType.nullable();
+                    result =
+                            result.stream()
+                                    .filter(dataField -> 
!dataField.name().equals(columnName))
+                                    .collect(Collectors.toList());
+                    result.add(new DataField(id, columnName, dataType));
+                    mySqlFieldTypes.put(columnName, columnType);
+                    paimonFieldTypes.put(columnName, 
MySqlTypeUtils.toDataType(columnType));
+                } else if (sqlAlterTableItem instanceof 
MySqlAlterTableChangeColumn) {
+                    MySqlAlterTableChangeColumn mySqlAlterTableChangeColumn =

Review Comment:
   No test cover



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