Copilot commented on code in PR #4212:
URL: https://github.com/apache/flink-cdc/pull/4212#discussion_r2909754072


##########
flink-cdc-common/src/main/java/org/apache/flink/cdc/common/route/TableIdRouter.java:
##########
@@ -105,7 +107,8 @@ public static String convertTableListToRegExpPattern(String 
tables) {
         return standardRegExpTableCaptureList;
     }
 
-    public TableIdRouter(List<RouteRule> routingRules) {
+    public TableIdRouter(List<RouteRule> routingRules, RouteMode routeMode) {
+        this.routeMode = routeMode;
         this.routes = new ArrayList<>();

Review Comment:
   `TableIdRouter` is `@PublicEvolving` and this change removes the previous 
constructor that accepted only `List<RouteRule>`, which is a source/binary 
incompatible API break for existing users. Consider keeping an overload 
`TableIdRouter(List<RouteRule>)` that delegates to `RouteMode.ALL_MATCH` so 
existing code keeps compiling and behavior stays the default.



##########
flink-cdc-common/src/main/java/org/apache/flink/cdc/common/route/TableIdRouter.java:
##########
@@ -139,11 +142,19 @@ public List<TableId> route(TableId sourceTableId) {
     }
 
     private List<TableId> calculateRoute(TableId sourceTableId) {
-        List<TableId> routedTableIds =
-                routes.stream()
-                        .filter(route -> matches(route.f0, sourceTableId))
-                        .map(route -> resolveReplacement(sourceTableId, route))
-                        .collect(Collectors.toList());
+        List<TableId> routedTableIds = new ArrayList<>();
+
+        for (Tuple3<Pattern, String, String> route : routes) {
+            if (matches(route.f0, sourceTableId)) {
+                routedTableIds.add(resolveReplacement(sourceTableId, route));
+
+                // If match mode is FIRST_MATCH, stop after the first match
+                if (routeMode == RouteMode.FIRST_MATCH) {
+                    break;
+                }
+            }
+        }

Review Comment:
   `FIRST_MATCH` is applied in `calculateRoute()`, but 
`groupSourceTablesByRouteRule()` (used by 
`SchemaDerivator.deduceMergedCreateTableEvent`) still groups tables into 
*every* matching rule. With overlapping rules, in `FIRST_MATCH` mode a table 
can be merged into multiple groups and later groups can overwrite the schema 
for the first-match sink table, producing incorrect merged schemas. 
`groupSourceTablesByRouteRule()` should respect `routeMode` (e.g., assign each 
source table to only the first matching rule when `FIRST_MATCH`).



##########
flink-cdc-cli/src/test/resources/definitions/pipeline-definition-with-route-mode.yaml:
##########
@@ -0,0 +1,48 @@
+# 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.
+
+# Example pipeline definition with route match-mode
+source:
+  type: mysql
+  hostname: localhost
+  port: 3306
+  username: root
+  password: 123456
+  tables: mydb.\.*
+  server-id: 5400-5404
+  server-time-zone: UTC
+
+sink:
+  type: doris
+  fenodes: 127.0.0.1:8030
+  username: root
+  password: ""
+
+route:
+  - source-table: mydb.order_.*
+    sink-table: ods_db.ods_orders
+    description: "Merge all order sharded tables"
+  - source-table: mydb.product_.*
+    sink-table: ods_db.ods_products
+    description: "Merge all product sharded tables"
+  - source-table: mydb.*
+    sink-table: ods_db.ods_<>

Review Comment:
   This example route configuration uses unescaped `.*` in `source-table` 
patterns (e.g., `mydb.order_.*`). 
`TableIdRouter.convertTableListToRegExpPattern()` treats unescaped `.` as the 
database/table separator and requires escaping `\.` when you intend regex 
wildcard matching, otherwise the `.*` is rewritten as a literal dot and won’t 
match tables like `mydb.order_1`. The patterns here should use the CDC-style 
form (e.g., `mydb.order_\.*`, `mydb\.*`) to behave as intended.



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