TaoZex commented on code in PR #6106:
URL: https://github.com/apache/seatunnel/pull/6106#discussion_r1439062489


##########
seatunnel-connectors-v2/connector-cdc/connector-cdc-base/src/main/java/org/apache/seatunnel/connectors/cdc/base/option/JdbcSourceOptions.java:
##########
@@ -141,4 +142,17 @@ public class JdbcSourceOptions extends SourceOptions {
                                     + "The value represents the denominator of 
the sampling rate fraction. "
                                     + "For example, a value of 1000 means a 
sampling rate of 1/1000. "
                                     + "This parameter is used when the sample 
sharding strategy is triggered.");
+
+    public static final Option<List<JdbcSourceTableConfig>> TABLE_NAMES_CONFIG 
=
+            Options.key("table-names-config")
+                    .listType(JdbcSourceTableConfig.class)
+                    .noDefaultValue()
+                    .withDescription(
+                            "Config table configs. example: "

Review Comment:
   ```suggestion
                               "Config table configs. Example: "
   ```



##########
seatunnel-connectors-v2/connector-cdc/connector-cdc-base/src/main/java/org/apache/seatunnel/connectors/cdc/base/utils/CatalogTableUtils.java:
##########
@@ -0,0 +1,145 @@
+/*
+ * 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.seatunnel.connectors.cdc.base.utils;
+
+import org.apache.seatunnel.api.table.catalog.CatalogTable;
+import org.apache.seatunnel.api.table.catalog.Column;
+import org.apache.seatunnel.api.table.catalog.PhysicalColumn;
+import org.apache.seatunnel.api.table.catalog.PrimaryKey;
+import org.apache.seatunnel.api.table.catalog.TablePath;
+import org.apache.seatunnel.api.table.catalog.TableSchema;
+import org.apache.seatunnel.connectors.cdc.base.config.JdbcSourceTableConfig;
+
+import io.debezium.relational.Table;
+import io.debezium.relational.TableId;
+import lombok.extern.slf4j.Slf4j;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+@Slf4j
+public class CatalogTableUtils {
+
+    public static List<CatalogTable> mergeCatalogTableConfig(
+            List<CatalogTable> tables,
+            List<JdbcSourceTableConfig> tableConfigs,
+            Function<String, TablePath> parser) {
+        Map<TablePath, CatalogTable> catalogTableMap =
+                tables.stream()
+                        .collect(Collectors.toMap(t -> 
t.getTableId().toTablePath(), t -> t));
+        for (JdbcSourceTableConfig catalogTableConfig : tableConfigs) {
+            TablePath tablePath = parser.apply(catalogTableConfig.getTable());
+            CatalogTable catalogTable = catalogTableMap.get(tablePath);
+            if (catalogTable != null) {
+                catalogTable = mergeCatalogTableConfig(catalogTable, 
catalogTableConfig);
+                catalogTableMap.put(tablePath, catalogTable);
+                log.info(
+                        "Override primary key({}) for catalog table {}",
+                        catalogTableConfig.getPrimaryKeys(),
+                        catalogTableConfig.getTable());
+            } else {
+                log.warn(
+                        "Table {} is not found in catalog tables, skip to 
merge config",
+                        catalogTableConfig.getTable());
+            }
+        }
+        return new ArrayList<>(catalogTableMap.values());
+    }
+
+    public static CatalogTable mergeCatalogTableConfig(
+            final CatalogTable table, JdbcSourceTableConfig config) {
+        List<String> columnNames =
+                table.getTableSchema().getColumns().stream()
+                        .map(c -> c.getName())
+                        .collect(Collectors.toList());
+        for (String pk : config.getPrimaryKeys()) {
+            if (!columnNames.contains(pk)) {
+                throw new IllegalArgumentException(
+                        String.format(
+                                "Primary key(%s) is not in table(%s) 
columns(%s)",
+                                pk, table.getTablePath(), columnNames));
+            }
+        }
+        PrimaryKey primaryKeys =
+                PrimaryKey.of(
+                        "pk" + Math.abs(config.getPrimaryKeys().hashCode()),

Review Comment:
   ```suggestion
                           "pk" + (config.getPrimaryKeys().hashCode() & 
Integer.MAX_VALUE),
   ```
   When hashcode values is Integer.MIN_VALUE overflow occurs, it may be better 
to use hashcode & Integer.MAX_VALUE



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