Copilot commented on code in PR #65325: URL: https://github.com/apache/doris/pull/65325#discussion_r3535456319
########## fs_brokers/cdc_client/src/main/java/org/apache/doris/cdcclient/source/parse/mysql/CustomAlterTableParserListener.java: ########## @@ -0,0 +1,98 @@ +// 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.doris.cdcclient.source.parse.mysql; + +import io.debezium.connector.mysql.antlr.MySqlAntlrDdlParser; +import io.debezium.ddl.parser.mysql.generated.MySqlParser; +import io.debezium.ddl.parser.mysql.generated.MySqlParserBaseListener; +import io.debezium.relational.Table; +import io.debezium.relational.TableId; + +import java.util.List; + +final class CustomAlterTableParserListener extends MySqlParserBaseListener { + private final MySqlAntlrDdlParser parser; + private final List<MySqlSchemaChange> changes; + private TableId currentTableId; + + CustomAlterTableParserListener( + MySqlAntlrDdlParser parser, List<MySqlSchemaChange> changes) { + this.parser = parser; + this.changes = changes; + } + + @Override + public void enterAlterTable(MySqlParser.AlterTableContext ctx) { + currentTableId = parser.parseQualifiedTableId(ctx.tableName().fullId()); + super.enterAlterTable(ctx); + } + + @Override + public void exitAlterTable(MySqlParser.AlterTableContext ctx) { + currentTableId = null; + super.exitAlterTable(ctx); + } + + @Override + public void exitAlterByAddColumn(MySqlParser.AlterByAddColumnContext ctx) { + String columnName = parser.parseName(ctx.uid(0)); + Table table = parser.databaseTables().forTable(currentTableId); + changes.add(MySqlSchemaChange.add(currentTableId, table.columnWithName(columnName))); + super.exitAlterByAddColumn(ctx); + } Review Comment: exitAlterByAddColumn assumes the Debezium parser has already materialized the added column in databaseTables; if the table or column cannot be resolved (e.g., listener ordering/version differences), table.columnWithName(...) may be null and MySqlSchemaChange.add will throw NPE, crashing schema-change handling. ########## fs_brokers/cdc_client/src/main/java/org/apache/doris/cdcclient/utils/SchemaChangeHelper.java: ########## @@ -136,6 +136,95 @@ public static String columnToDorisType(Column column) { return pgTypeNameToDorisType(column.typeName(), column.length(), column.scale().orElse(-1)); } + /** Convert a Debezium MySQL Column to a Doris column type string. */ + public static String mysqlColumnToDorisType(Column column) { + Preconditions.checkNotNull(column.typeName()); + String mysqlTypeName = column.typeName().toUpperCase(); + String[] typeFields = mysqlTypeName.split(" "); + String mysqlType = typeFields[0]; + boolean unsigned = typeFields.length > 1 && "UNSIGNED".equals(typeFields[1]); + int length = column.length(); Review Comment: MySQL `ZEROFILL` columns are implicitly UNSIGNED, but unsigned detection only checks the second token for "UNSIGNED". For types like `INT ZEROFILL`, this will be treated as signed and may generate a too-narrow Doris type during ADD COLUMN schema change. ########## fs_brokers/cdc_client/src/main/java/org/apache/doris/cdcclient/source/parse/mysql/CustomAlterTableParserListener.java: ########## @@ -0,0 +1,98 @@ +// 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.doris.cdcclient.source.parse.mysql; + +import io.debezium.connector.mysql.antlr.MySqlAntlrDdlParser; +import io.debezium.ddl.parser.mysql.generated.MySqlParser; +import io.debezium.ddl.parser.mysql.generated.MySqlParserBaseListener; +import io.debezium.relational.Table; +import io.debezium.relational.TableId; + +import java.util.List; + +final class CustomAlterTableParserListener extends MySqlParserBaseListener { + private final MySqlAntlrDdlParser parser; + private final List<MySqlSchemaChange> changes; + private TableId currentTableId; + + CustomAlterTableParserListener( + MySqlAntlrDdlParser parser, List<MySqlSchemaChange> changes) { + this.parser = parser; + this.changes = changes; + } + + @Override + public void enterAlterTable(MySqlParser.AlterTableContext ctx) { + currentTableId = parser.parseQualifiedTableId(ctx.tableName().fullId()); + super.enterAlterTable(ctx); + } + + @Override + public void exitAlterTable(MySqlParser.AlterTableContext ctx) { + currentTableId = null; + super.exitAlterTable(ctx); + } + + @Override + public void exitAlterByAddColumn(MySqlParser.AlterByAddColumnContext ctx) { + String columnName = parser.parseName(ctx.uid(0)); + Table table = parser.databaseTables().forTable(currentTableId); + changes.add(MySqlSchemaChange.add(currentTableId, table.columnWithName(columnName))); + super.exitAlterByAddColumn(ctx); + } + + @Override + public void exitAlterByAddColumns(MySqlParser.AlterByAddColumnsContext ctx) { + for (MySqlParser.UidContext uidContext : ctx.uid()) { + String columnName = parser.parseName(uidContext); + Table table = parser.databaseTables().forTable(currentTableId); + changes.add(MySqlSchemaChange.add(currentTableId, table.columnWithName(columnName))); + } + super.exitAlterByAddColumns(ctx); + } Review Comment: exitAlterByAddColumns has the same null-resolution risk as exitAlterByAddColumn: table.columnWithName(...) may return null and cause an NPE when building MySqlSchemaChange.add. Guarding here avoids crashing on partially parsed/unsupported DDL. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
