lvyanquan commented on code in PR #3360: URL: https://github.com/apache/flink-cdc/pull/3360#discussion_r1847724959
########## flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-oceanbase/src/test/java/org/apache/flink/cdc/connectors/oceanbase/sink/OceanBaseMetadataApplierTest.java: ########## @@ -0,0 +1,256 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.cdc.connectors.oceanbase.sink; + +import org.apache.flink.cdc.common.event.AlterColumnTypeEvent; +import org.apache.flink.cdc.common.event.CreateTableEvent; +import org.apache.flink.cdc.common.event.DropColumnEvent; +import org.apache.flink.cdc.common.event.DropTableEvent; +import org.apache.flink.cdc.common.event.TableId; +import org.apache.flink.cdc.common.event.TruncateTableEvent; +import org.apache.flink.cdc.common.schema.Schema; +import org.apache.flink.cdc.common.types.BooleanType; +import org.apache.flink.cdc.common.types.DataType; +import org.apache.flink.cdc.common.types.IntType; +import org.apache.flink.cdc.common.types.LocalZonedTimestampType; +import org.apache.flink.cdc.connectors.oceanbase.OceanBaseTestUtils; +import org.apache.flink.cdc.connectors.oceanbase.catalog.OceanBaseColumn; +import org.apache.flink.cdc.connectors.oceanbase.catalog.OceanBaseTable; +import org.apache.flink.cdc.connectors.oceanbase.testutils.OceanBaseContainer; +import org.apache.flink.cdc.connectors.oceanbase.utils.OceanBaseTestMySQLCatalog; + +import org.apache.flink.shaded.guava31.com.google.common.collect.ImmutableMap; +import org.apache.flink.shaded.guava31.com.google.common.collect.Lists; + +import com.oceanbase.connector.flink.OceanBaseConnectorOptions; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.containers.output.Slf4jLogConsumer; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import static org.apache.flink.cdc.connectors.oceanbase.table.OceanBaseMySQLModeITCase.NETWORK; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** Tests for {@link OceanBaseMetadataApplier}. */ +public class OceanBaseMetadataApplierTest { + private static final Logger LOG = LoggerFactory.getLogger(OceanBaseMetadataApplierTest.class); + + private OceanBaseMetadataApplier metadataApplier; + private OceanBaseTestMySQLCatalog catalog; + + @ClassRule + public static final OceanBaseContainer OB_SERVER = + OceanBaseTestUtils.createOceanBaseContainerForJdbc() + .withNetwork(NETWORK) + .withNetworkAliases("oceanbase") + .withLogConsumer(new Slf4jLogConsumer(LOG)); + + @Before + public void setup() throws Exception { + final ImmutableMap<String, String> configMap = + ImmutableMap.<String, String>builder() + .put("url", OB_SERVER.getJdbcUrl()) + .put("username", OB_SERVER.getUsername()) + .put("password", OB_SERVER.getPassword()) + .build(); + OceanBaseConnectorOptions connectorOptions = new OceanBaseConnectorOptions(configMap); + metadataApplier = new OceanBaseMetadataApplier(connectorOptions); + catalog = new OceanBaseTestMySQLCatalog(connectorOptions); + catalog.open(); + } + + @AfterClass + public static void close() { + OB_SERVER.close(); + } + + @Test + public void testCreateTable() { + TableId tableId = TableId.parse("test.tbl1"); + Schema schema = + Schema.newBuilder() + .physicalColumn("col1", new IntType(false)) + .physicalColumn("col2", new BooleanType()) + .physicalColumn("col3", new LocalZonedTimestampType()) + .primaryKey("col1") + .build(); + CreateTableEvent createTableEvent = new CreateTableEvent(tableId, schema); + metadataApplier.applySchemaChange(createTableEvent); + + OceanBaseTable actualTable = + catalog.getTable(tableId.getSchemaName(), tableId.getTableName()).orElse(null); + assertNotNull(actualTable); + + List<OceanBaseColumn> columns = new ArrayList<>(); + columns.add( + new OceanBaseColumn.Builder() + .setColumnName("col1") + .setOrdinalPosition(0) + .setDataType("int") + .setNumericScale(0) + .setNullable(false) + .build()); + columns.add( + new OceanBaseColumn.Builder() + .setColumnName("col2") + .setOrdinalPosition(1) + .setDataType("tinyint") + .setNumericScale(0) + .setNullable(true) + .build()); + columns.add( + new OceanBaseColumn.Builder() + .setColumnName("col3") + .setOrdinalPosition(2) + .setDataType("datetime") + .setNullable(true) + .build()); + OceanBaseTable expectTable = + new OceanBaseTable.Builder() + .setDatabaseName(tableId.getSchemaName()) + .setTableName(tableId.getTableName()) + .setTableType(OceanBaseTable.TableType.PRIMARY_KEY) + .setColumns(columns) + .setTableKeys(schema.primaryKeys()) + .build(); + + System.out.println(expectTable); + System.out.println(actualTable); Review Comment: These can be removed. ########## flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-oceanbase/src/main/java/org/apache/flink/cdc/connectors/oceanbase/sink/OceanBaseEventSerializationSchema.java: ########## @@ -0,0 +1,141 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.cdc.connectors.oceanbase.sink; + +import org.apache.flink.cdc.common.data.RecordData; +import org.apache.flink.cdc.common.event.CreateTableEvent; +import org.apache.flink.cdc.common.event.DataChangeEvent; +import org.apache.flink.cdc.common.event.Event; +import org.apache.flink.cdc.common.event.OperationType; +import org.apache.flink.cdc.common.event.SchemaChangeEvent; +import org.apache.flink.cdc.common.event.TableId; +import org.apache.flink.cdc.common.schema.Column; +import org.apache.flink.cdc.common.schema.Schema; +import org.apache.flink.cdc.common.utils.Preconditions; +import org.apache.flink.cdc.common.utils.SchemaUtils; + +import org.apache.flink.shaded.guava31.com.google.common.collect.Lists; + +import com.oceanbase.connector.flink.table.DataChangeRecord; +import com.oceanbase.connector.flink.table.Record; +import com.oceanbase.connector.flink.table.RecordSerializationSchema; +import com.oceanbase.connector.flink.table.TableInfo; + +import java.time.ZoneId; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +/** A serializer for Event to Record. */ +public class OceanBaseEventSerializationSchema implements RecordSerializationSchema<Event> { + + private final Map<TableId, Schema> schemaMaps = new HashMap<>(); + + /** ZoneId from pipeline config to support timestamp with local time zone. */ + public final ZoneId pipelineZoneId; + + public OceanBaseEventSerializationSchema(ZoneId zoneId) { + pipelineZoneId = zoneId; + } + + @Override + public Record serialize(Event event) { + if (event instanceof DataChangeEvent) { + return applyDataChangeEvent((DataChangeEvent) event); + } else if (event instanceof SchemaChangeEvent) { + SchemaChangeEvent schemaChangeEvent = (SchemaChangeEvent) event; + TableId tableId = schemaChangeEvent.tableId(); + if (event instanceof CreateTableEvent) { + schemaMaps.put(tableId, ((CreateTableEvent) event).getSchema()); + } else { + if (!schemaMaps.containsKey(tableId)) { + throw new RuntimeException("schema of " + tableId + " is not existed."); + } + schemaMaps.put( + tableId, + SchemaUtils.applySchemaChangeEvent( + schemaMaps.get(tableId), schemaChangeEvent)); + } + } + return null; + } + + private Record applyDataChangeEvent(DataChangeEvent event) { + TableId tableId = event.tableId(); + Schema schema = schemaMaps.get(tableId); + Preconditions.checkNotNull(schema, event.tableId() + " is not existed"); + Object[] values; + OperationType op = event.op(); + boolean isDelete = false; + switch (op) { + case INSERT: + case UPDATE: + case REPLACE: + values = serializerRecord(event.after(), schema); + break; + case DELETE: + values = serializerRecord(event.before(), schema); + isDelete = true; + break; + default: + throw new UnsupportedOperationException("Unsupported Operation " + op); + } + return buildDataChangeRecord(tableId, schema, values, isDelete); + } + + private DataChangeRecord buildDataChangeRecord( + TableId tableId, Schema schema, Object[] values, boolean isDelete) { + Preconditions.checkState( + Objects.nonNull(tableId.getSchemaName()), "Schema name cannot be null or empty."); + com.oceanbase.connector.flink.table.TableId oceanBaseTableId = + new com.oceanbase.connector.flink.table.TableId( + tableId.getSchemaName(), tableId.getTableName()); + TableInfo tableInfo = + new TableInfo( + oceanBaseTableId, + schema.primaryKeys(), + schema.getColumnNames(), + Lists.newArrayList(), + null); + + return new DataChangeRecord( + tableInfo, + isDelete ? DataChangeRecord.Type.DELETE : DataChangeRecord.Type.UPSERT, + values); + } + + /** serializer RecordData to oceanbase data change record. */ + public Object[] serializerRecord(RecordData recordData, Schema schema) { + List<Column> columns = schema.getColumns(); + Preconditions.checkState( + columns.size() == recordData.getArity(), + "Column size does not match the data size"); + Object[] values = new Object[columns.size()]; + + for (int i = 0; i < recordData.getArity(); i++) { + OceanBaseRowConvert.SerializationConverter converter = Review Comment: I think the benefit of reusing this SerializationConverter is that we can reduce the judgments of https://github.com/apache/flink-cdc/pull/3360/files#diff-c42ada3283c29232d90a4184ca6196a7f24194229384773ff49792a5d9667318R75 ########## flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-oceanbase/src/test/java/org/apache/flink/cdc/connectors/oceanbase/sink/OceanBaseEventSerializationSchemaTest.java: ########## @@ -0,0 +1,219 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.cdc.connectors.oceanbase.sink; + +import org.apache.flink.cdc.common.data.DecimalData; +import org.apache.flink.cdc.common.data.LocalZonedTimestampData; +import org.apache.flink.cdc.common.data.TimestampData; +import org.apache.flink.cdc.common.data.binary.BinaryStringData; +import org.apache.flink.cdc.common.event.AddColumnEvent; +import org.apache.flink.cdc.common.event.CreateTableEvent; +import org.apache.flink.cdc.common.event.DataChangeEvent; +import org.apache.flink.cdc.common.event.DropColumnEvent; +import org.apache.flink.cdc.common.event.TableId; +import org.apache.flink.cdc.common.schema.Column; +import org.apache.flink.cdc.common.schema.Schema; +import org.apache.flink.cdc.common.types.BooleanType; +import org.apache.flink.cdc.common.types.DataType; +import org.apache.flink.cdc.common.types.DateType; +import org.apache.flink.cdc.common.types.DecimalType; +import org.apache.flink.cdc.common.types.FloatType; +import org.apache.flink.cdc.common.types.IntType; +import org.apache.flink.cdc.common.types.LocalZonedTimestampType; +import org.apache.flink.cdc.common.types.SmallIntType; +import org.apache.flink.cdc.common.types.TimestampType; +import org.apache.flink.cdc.common.types.VarCharType; +import org.apache.flink.cdc.common.utils.SchemaUtils; +import org.apache.flink.cdc.runtime.typeutils.BinaryRecordDataGenerator; + +import com.oceanbase.connector.flink.table.Record; +import org.junit.Test; + +import java.math.BigDecimal; +import java.sql.Timestamp; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.util.Arrays; +import java.util.Objects; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +/** Tests for {@link OceanBaseEventSerializationSchema}. */ +public class OceanBaseEventSerializationSchemaTest { + + private static final OceanBaseEventSerializationSchema serializer = + new OceanBaseEventSerializationSchema(ZoneId.of("+08")); + + @Test + public void testMixedSchemaAndDataChanges() throws Exception { + // 1. create table1, and insert/delete/update data + TableId table1 = TableId.parse("test.tbl1"); + Schema schema1 = + Schema.newBuilder() + .physicalColumn("col1", new IntType(false)) + .physicalColumn("col2", new BooleanType()) + .physicalColumn("col3", new TimestampType()) Review Comment: It's better to add a test to cover all supported types like the following: https://github.com/apache/flink-cdc/blob/8e6c361f961966c25c9c34ffecdfc87cf2b599a3/flink-cdc-runtime/src/test/java/org/apache/flink/cdc/runtime/typeutils/BinaryRecordDataGeneratorTest.java#L43-L71 -- 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]
