This is an automated email from the ASF dual-hosted git repository.

nzw921rx pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/seatunnel.git


The following commit(s) were added to refs/heads/dev by this push:
     new afbdd0d4c28 [Fix][Connector-V2][JDBC] Do not pick a nullable key 
column as the split key (#13092)
afbdd0d4c28 is described below

commit afbdd0d4c2851b7621dc02d78396b431ec8a79f6
Author: Sepuri Sai Krishna <[email protected]>
AuthorDate: Mon Nov 16 17:18:08 2026 +0530

    [Fix][Connector-V2][JDBC] Do not pick a nullable key column as the split 
key (#13092)
---
 docs/en/connectors/source/Jdbc.md                  |   8 +-
 .../introduction/concepts/incompatible-changes.md  |   7 ++
 docs/zh/connectors/source/Jdbc.md                  |   8 +-
 .../introduction/concepts/incompatible-changes.md  |   7 ++
 .../seatunnel/jdbc/source/ChunkSplitter.java       |  34 ++++-
 .../jdbc/source/DynamicChunkSplitterTest.java      | 137 +++++++++++++++++++++
 .../seatunnel/jdbc/JdbcMysqlSplitIT.java           | 116 +++++++++++++++++
 7 files changed, 306 insertions(+), 11 deletions(-)

diff --git a/docs/en/connectors/source/Jdbc.md 
b/docs/en/connectors/source/Jdbc.md
index 6a30c60df45..0c280a7afd0 100644
--- a/docs/en/connectors/source/Jdbc.md
+++ b/docs/en/connectors/source/Jdbc.md
@@ -166,7 +166,7 @@ Use `query` to select only the required columns.
 | connection_check_timeout_sec               | Int     | No        | 30        
      | The time in seconds to wait for the database operation used to validate 
the connection to complete.                                                     
                                                                                
                                                                                
                                                                                
              [...]
 | connect_timeout_ms                         | Int     | No        | 86400000  
      | Connection timeout in milliseconds when establishing the JDBC 
connection. `0` means no timeout.                                               
                                                                                
                                                                                
                                                                                
                        [...]
 | socket_timeout_ms                          | Int     | No        | 86400000  
      | Socket read timeout in milliseconds after the JDBC connection is 
established. `0` means no timeout.                                              
                                                                                
                                                                                
                                                                                
                     [...]
-| partition_column                           | String  | No        | -         
      | The column name for split data.                                         
                                                                                
                                                                                
                                                                                
                                                                                
              [...]
+| partition_column                           | String  | No        | -         
      | The column name for split data. When the table is split by this column, 
a row whose value in it is NULL falls in no split range or hash bucket and is 
not read, so choose a column without NULL values. With `enable_concurrent_read 
= false` the table is not split, so these rows are read. CockroachDB hash 
splits put NULL into one bucket and read such rows.                             
                       [...]
 | partition_upper_bound                      | String  | No        | -         
      | Inclusive upper value used for query-based partitioning. When omitted, 
SeaTunnel queries the source for the maximum value. For a DATE partition 
column, an ISO-8601 date string (`yyyy-MM-dd`) or a legacy epoch-day number can 
be used.                                                                        
                                                                                
                      [...]
 | partition_lower_bound                      | String  | No        | -         
      | Inclusive lower value used for query-based partitioning. When omitted, 
SeaTunnel queries the source for the minimum value. For a DATE partition 
column, an ISO-8601 date string (`yyyy-MM-dd`) or a legacy epoch-day number can 
be used.                                                                        
                                                                                
                      [...]
 | partition_num                              | Int     | No        | 10        
      | Number of splits when top-level `query` and `partition_column` select 
the fixed splitter, whether or not `table_path` is also present. Dynamic 
`table_path` and `table_list` layouts use `split.size` instead.                 
                                                                                
                                                                                
                       [...]
@@ -339,9 +339,9 @@ Parallelism determines how many readers can run at the same 
time; the split conf
 
 ### Recommended: `table_path` with dynamic splitting
 
-For a full-table snapshot, configure `table_path` and normally leave split-key 
discovery to SeaTunnel. The connector uses `partition_column` when provided. 
Otherwise it searches the primary key and then unique indexes for the first 
supported column. String, numeric, and date columns are supported split-key 
types.
+For a full-table snapshot, configure `table_path` and normally leave split-key 
discovery to SeaTunnel. The connector uses `partition_column` when provided. 
Otherwise it searches the primary key and then unique indexes for the first 
supported column that is NOT NULL; a nullable column is skipped with a WARN, 
because rows with NULL in it would not be read. This automatic choice takes 
nullability from the table metadata, so it applies only to `table_path` reads: 
a `query` without `partition [...]
 
-`split.size` is the target row count per split. It is not a hard row limit: 
actual split sizes depend on key distribution and database statistics. If no 
supported primary key, unique index, or explicit `partition_column` exists, the 
table is read by one split even when job parallelism is greater than one.
+`split.size` is the target row count per split. It is not a hard row limit: 
actual split sizes depend on key distribution and database statistics. If no 
supported NOT NULL primary key or unique index column, and no explicit 
`partition_column`, exists, the table is read by one split even when job 
parallelism is greater than one.
 
 ### Top-level `query` with fixed partitions
 
@@ -617,7 +617,7 @@ Check the database-specific `table_path` format, identifier 
case, and the accoun
 
 ### Parallelism does not increase the number of readers
 
-For dynamic splitting, verify that the table has a supported primary key or 
unique index, or configure `partition_column`. To select the legacy fixed 
splitter for a top-level `query`, configure both `partition_column` and a 
suitable `partition_num`. A table without a safe split key is intentionally 
read by one split.
+For dynamic splitting, verify that the table has a supported NOT NULL primary 
key or unique index column, or configure `partition_column` with a column that 
holds no NULL values. To select the legacy fixed splitter for a top-level 
`query`, configure both `partition_column` and a suitable `partition_num`. A 
table without a safe split key is intentionally read by one split.
 
 ### Rows are missing after setting partition bounds
 
diff --git a/docs/en/introduction/concepts/incompatible-changes.md 
b/docs/en/introduction/concepts/incompatible-changes.md
index 93bbd5f334f..53c0bf9ce74 100644
--- a/docs/en/introduction/concepts/incompatible-changes.md
+++ b/docs/en/introduction/concepts/incompatible-changes.md
@@ -19,6 +19,13 @@ You need to check this document before you upgrade to 
related version.
   - **Impact**: A job that reads a Databend `DECIMAL` column now emits decimal 
values. A sink table that SeaTunnel creates through `schema_save_mode` gets a 
decimal column instead of a string column, and a transform or sink that 
expected a string now receives a decimal.
   - **Migration Guide**: No configuration change is needed. If a downstream 
step needs text, convert the column explicitly, for example `CAST(price AS 
STRING)` in the SQL transform. A sink table that an earlier run already created 
with a string column keeps that type.
 
+### JDBC source: a nullable key column is no longer chosen as the split key
+
+- **Behavior change: the automatic split key selection skips nullable columns**
+  - **Description**: Without `partition_column`, the JDBC source takes the 
split key from the primary key and then the unique indexes. Range and hash 
split conditions never match NULL (CockroachDB hash splits excepted), and a 
unique index can hold NULL values, so when a nullable key column was chosen and 
the table was split by it, the rows with NULL in it were read by no split and 
the job finished without an error. The automatic selection now skips nullable 
primary key and unique key col [...]
+  - **Impact**: A table that was split on a nullable primary key or unique key 
column is now split on the next NOT NULL key column, or read as one split if it 
has none, so all its rows are read, with less parallelism if it falls back to 
one split. Tables split on a NOT NULL primary key or unique key column, `query` 
sources without `partition_column`, and jobs that set `partition_column` are 
not affected. A job restored from a checkpoint keeps the splits it had already 
planned; the change [...]
+  - **Migration Guide**: No configuration change is needed. To read such a 
table in parallel again, declare the key column `NOT NULL`, or set 
`partition_column` to a column without NULL values. If a downstream table was 
loaded by an affected job, reload it to get the rows that were skipped.
+
 ### JDBC Sink: fail safely after losing uncommitted work
 
 - **Affected jobs**: non-XA JDBC sinks with checkpointing enabled and a 
manual-commit connection
diff --git a/docs/zh/connectors/source/Jdbc.md 
b/docs/zh/connectors/source/Jdbc.md
index c5e5613a21a..3a0ec924a8b 100644
--- a/docs/zh/connectors/source/Jdbc.md
+++ b/docs/zh/connectors/source/Jdbc.md
@@ -162,7 +162,7 @@ driver = "org.mariadb.jdbc.Driver"
 | connection_check_timeout_sec               | Int     | 否    | 30      | 
等待用于验证连接的数据库操作完成的时间(秒)。                                                         
                                                                                
                                                                                
                                                                                
                                                                                
                   [...]
 | connect_timeout_ms                         | Int     | 否    | 86400000 | 建立 
JDBC 连接时的连接超时时间,单位毫秒。`0` 表示不超时。                                                 
                                                                                
                                                                                
                                                                                
                                                                                
               [...]
 | socket_timeout_ms                          | Int     | 否    | 86400000 | 
JDBC 连接建立后的 socket 读取超时时间,单位毫秒。`0` 表示不超时。                                       
                                                                                
                                                                                
                                                                                
                                                                                
                  [...]
-| partition_column                           | String  | 否    | -       | 
用于分割数据的列名。                                                                      
                                                                                
                                                                                
                                                                                
                                                                                
                   [...]
+| partition_column                           | String  | 否    | -       | 
用于分割数据的列名。按该列分片时,该列为 NULL 的行不属于任何分片范围或哈希桶,不会被读取,请选择不含 NULL 值的列。设置 
`enable_concurrent_read = false` 时表不分片,这些行也会被读取。CockroachDB 的哈希分片会把 NULL 
放入一个桶,这些行也会被读取。                                                                 
                                                                                
                                                                                
                                        [...]
 | partition_upper_bound                      | String  | 否    | -       | 
query 分区读取使用的闭区间上界。未配置时,SeaTunnel 会从源表查询最大值。对于 DATE 分区列,支持 ISO-8601 
日期字符串(`yyyy-MM-dd`)或兼容历史配置的 epoch-day 数字。                                       
                                                                                
                                                                                
                                                                                
                               [...]
 | partition_lower_bound                      | String  | 否    | -       | 
query 分区读取使用的闭区间下界。未配置时,SeaTunnel 会从源表查询最小值。对于 DATE 分区列,支持 ISO-8601 
日期字符串(`yyyy-MM-dd`)或兼容历史配置的 epoch-day 数字。                                       
                                                                                
                                                                                
                                                                                
                               [...]
 | partition_num                              | Int     | 否    | 10      | 顶层 
`query` 与 `partition_column` 触发 fixed splitter 时的分片数,无论是否同时配置 `table_path`。动态 
`table_path` 和 `table_list` 布局改用 `split.size`。                                  
                                                                                
                                                                                
                                                                                
                  [...]
@@ -337,9 +337,9 @@ CockroachDB 不支持 PostgreSQL 的 `HASHTEXT` 函数。本方言使用 `FNV32`
 
 ### 推荐方式:`table_path` 动态分片
 
-整表快照建议配置 `table_path`,通常让 SeaTunnel 自动发现分片键。显式配置 `partition_column` 
时优先使用该列;否则依次从主键和唯一索引中选择第一个受支持的列。支持 String、数值和 Date 类型的分片键。
+整表快照建议配置 `table_path`,通常让 SeaTunnel 自动发现分片键。显式配置 `partition_column` 
时优先使用该列;否则依次从主键和唯一索引中选择第一个受支持且为 NOT NULL 的列;可为空的列会被跳过并输出 WARN 日志,因为该列为 NULL 
的行不会被读取。自动选择依据表的元数据判断列是否可为空,因此只适用于 `table_path` 读取:未配置 `partition_column` 的 
`query` 使用一个分片读取。显式配置的 `partition_column` 按配置使用,不做此检查。支持 String、数值和 Date 类型的分片键。
 
-`split.size` 表示每个分片的目标行数,并不是严格的行数上限,实际大小会受到键值分布和数据库统计信息影响。如果表没有可用的主键、唯一索引或显式 
`partition_column`,即使任务并行度大于 1,该表仍然只有一个读取分片。
+`split.size` 表示每个分片的目标行数,并不是严格的行数上限,实际大小会受到键值分布和数据库统计信息影响。如果表没有可用的 NOT NULL 
主键或唯一索引列,也没有显式 `partition_column`,即使任务并行度大于 1,该表仍然只有一个读取分片。
 
 ### 顶层 `query` 固定分区
 
@@ -572,7 +572,7 @@ sink {
 
 ### 提高 parallelism 后 Reader 数量没有增加
 
-使用动态分片时,确认表存在受支持的主键或唯一索引,或者显式配置 `partition_column`。要让顶层 `query` 使用旧版 fixed 
splitter,需要同时配置 `partition_column` 和合适的 `partition_num`。没有安全分片键的表会有意使用单个 split 
读取。
+使用动态分片时,确认表存在受支持的 NOT NULL 主键或唯一索引列,或者显式配置不含 NULL 值的 `partition_column`。要让顶层 
`query` 使用旧版 fixed splitter,需要同时配置 `partition_column` 和合适的 
`partition_num`。没有安全分片键的表会有意使用单个 split 读取。
 
 ### 配置分区边界后出现漏数
 
diff --git a/docs/zh/introduction/concepts/incompatible-changes.md 
b/docs/zh/introduction/concepts/incompatible-changes.md
index cde3e0c4b7f..1e9e3bd0dc4 100644
--- a/docs/zh/introduction/concepts/incompatible-changes.md
+++ b/docs/zh/introduction/concepts/incompatible-changes.md
@@ -18,6 +18,13 @@
   - **影响**:读取 Databend `DECIMAL` 列的作业现在输出 decimal 值。SeaTunnel 通过 
`schema_save_mode` 创建的 Sink 表会得到 decimal 列而不是字符串列,原本期望字符串的 Transform 或 Sink 
现在会收到 decimal。
   - **迁移指南**:无需修改配置。如果下游步骤需要文本,请显式转换该列,例如在 SQL Transform 中使用 `CAST(price AS 
STRING)`。早期运行中已创建为字符串列的 Sink 表会保留原有类型。
 
+### JDBC Source:可空的键列不再被选为分片键
+
+- **行为变化:自动选择分片键时跳过可空列**
+  - **描述**:未配置 `partition_column` 时,JDBC Source 
依次从主键和唯一索引中选择分片键。范围和哈希分片条件都不会匹配 NULL(CockroachDB 的哈希分片除外),而唯一索引可以包含 NULL 
值,因此当选中可空的键列并按其分片时,该列为 NULL 的行不会被任何分片读取,作业也不会报错。现在自动选择会跳过可空的主键列和唯一键列,与 CDC 
Source 的做法一致,并为每个被跳过的列输出一条 WARN 日志。
+  - **影响**:之前按可空主键列或唯一键列分片的表,现在会按下一个 NOT NULL 
的键列分片;如果没有这样的列,则作为一个分片读取。这样表中的所有行都会被读取,回退为单个分片时并行度会降低。按 NOT NULL 
主键或唯一键列分片的表、未配置 `partition_column` 的 `query` 数据源,以及配置了 `partition_column` 
的作业不受影响。从 checkpoint 恢复的作业会保留已生成的分片,此变化只作用于恢复后才分片的表。
+  - **迁移指南**:无需修改配置。如需重新并行读取这类表,请将该键列声明为 `NOT NULL`,或将 `partition_column` 
设置为不含 NULL 值的列。如果下游表由受影响的作业写入,请重新加载以补齐被跳过的行。
+
 ### JDBC Sink:未提交数据丢失后拒绝提交
 
 - **影响范围**:开启 checkpoint 且使用手动提交连接的非 XA JDBC Sink(`auto_commit = false`,
diff --git 
a/seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/source/ChunkSplitter.java
 
b/seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/source/ChunkSplitter.java
index d2499ea2498..5636ca68ee8 100644
--- 
a/seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/source/ChunkSplitter.java
+++ 
b/seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/source/ChunkSplitter.java
@@ -396,7 +396,7 @@ public abstract class ChunkSplitter implements 
AutoCloseable, Serializable {
         if (pk != null) {
             for (String pkField : pk.getColumnNames()) {
                 Column column = columnMap.get(pkField);
-                if (isSupportSplitColumn(column)) {
+                if (isAutomaticSplitColumn(column, table)) {
                     return Optional.of(
                             new SeaTunnelRowType(
                                     new String[] {pkField},
@@ -420,7 +420,7 @@ public abstract class ChunkSplitter implements 
AutoCloseable, Serializable {
                             uniqueKey.getColumnNames()) {
                         String uniqueKeyColumnName = 
uniqueKeyColumn.getColumnName();
                         Column column = columnMap.get(uniqueKeyColumnName);
-                        if (isSupportSplitColumn(column)) {
+                        if (isAutomaticSplitColumn(column, table)) {
                             return Optional.of(
                                     new SeaTunnelRowType(
                                             new String[] {uniqueKeyColumnName},
@@ -431,10 +431,38 @@ public abstract class ChunkSplitter implements 
AutoCloseable, Serializable {
             }
         }
 
-        log.warn("No split key found for table {}", table.getTablePath());
+        log.warn("No split key found for table {}, reading it as one split", 
table.getTablePath());
         return Optional.empty();
     }
 
+    /**
+     * Whether the automatic selection may split by this primary key or unique 
key column. A
+     * nullable column is skipped: the range and hash split queries never 
match NULL (CockroachDB
+     * hash splits aside), so the rows holding NULL in it would not be read. 
When no column is left,
+     * the table is read as one split.
+     *
+     * <p>Nullability comes from the catalog metadata of the table, so this 
check is only used for
+     * table reads: {@link #findSplitKey} keeps a {@code query} without {@code 
partition_column} on
+     * one split. An explicit {@code partition_column} is a choice of the user 
and is used as
+     * configured, without this check; its documentation says when its NULL 
rows are not read.
+     */
+    private boolean isAutomaticSplitColumn(Column column, JdbcSourceTable 
table) {
+        if (!isSupportSplitColumn(column)) {
+            return false;
+        }
+        if (column.isNullable()) {
+            log.warn(
+                    "Skip column {} of table {} as split key: it is nullable, 
and rows with NULL in"
+                            + " it would not be read. To read the table in 
parallel, declare a NOT"
+                            + " NULL unique column, or set partition_column to 
a column without"
+                            + " NULL values.",
+                    column.getName(),
+                    table.getTablePath());
+            return false;
+        }
+        return true;
+    }
+
     protected boolean isSupportSplitColumn(Column splitColumn) {
         SeaTunnelDataType<?> dataType = splitColumn.getDataType();
         // currently, we only support these types.
diff --git 
a/seatunnel-connectors-v2/connector-jdbc/src/test/java/org/apache/seatunnel/connectors/seatunnel/jdbc/source/DynamicChunkSplitterTest.java
 
b/seatunnel-connectors-v2/connector-jdbc/src/test/java/org/apache/seatunnel/connectors/seatunnel/jdbc/source/DynamicChunkSplitterTest.java
index a0e60e4732f..398cbd594f4 100644
--- 
a/seatunnel-connectors-v2/connector-jdbc/src/test/java/org/apache/seatunnel/connectors/seatunnel/jdbc/source/DynamicChunkSplitterTest.java
+++ 
b/seatunnel-connectors-v2/connector-jdbc/src/test/java/org/apache/seatunnel/connectors/seatunnel/jdbc/source/DynamicChunkSplitterTest.java
@@ -18,7 +18,11 @@
 package org.apache.seatunnel.connectors.seatunnel.jdbc.source;
 
 import org.apache.seatunnel.api.configuration.ReadonlyConfig;
+import org.apache.seatunnel.api.table.catalog.CatalogTable;
+import org.apache.seatunnel.api.table.catalog.ConstraintKey;
 import org.apache.seatunnel.api.table.catalog.PhysicalColumn;
+import org.apache.seatunnel.api.table.catalog.PrimaryKey;
+import org.apache.seatunnel.api.table.catalog.TableIdentifier;
 import org.apache.seatunnel.api.table.catalog.TablePath;
 import org.apache.seatunnel.api.table.catalog.TableSchema;
 import org.apache.seatunnel.api.table.type.BasicType;
@@ -32,11 +36,13 @@ import org.junit.jupiter.api.Test;
 
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertSame;
 import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -496,4 +502,135 @@ public class DynamicChunkSplitterTest {
         }
         return JdbcSourceConfig.of(ReadonlyConfig.fromMap(options));
     }
+
+    /**
+     * Range and hash split queries never match NULL, so a nullable key column 
chosen automatically
+     * would leave the rows holding NULL in it unread. Such a column is 
skipped, and a table left
+     * without a split key is read as one split.
+     */
+    @Test
+    public void testAutomaticSplitKeySkipsNullableKeyColumns() throws 
Exception {
+        DynamicChunkSplitter splitter = new 
DynamicChunkSplitter(postgresConfig());
+
+        JdbcSourceTable nullableUniqueKeyOnly =
+                table(null, null, 
Collections.singletonList(uniqueKey("uk_code", "code")));
+        assertFalse(splitter.findSplitKey(nullableUniqueKeyOnly).isPresent());
+        Collection<JdbcSourceSplit> splits = 
splitter.generateSplits(nullableUniqueKeyOnly);
+        assertEquals(1, splits.size());
+        assertNull(splits.iterator().next().getSplitKeyName());
+
+        JdbcSourceTable nullableAndNotNullUniqueKeys =
+                table(
+                        null,
+                        null,
+                        Arrays.asList(uniqueKey("uk_code", "code"), 
uniqueKey("uk_no", "no")));
+        assertEquals(
+                "no", 
splitter.findSplitKey(nullableAndNotNullUniqueKeys).get().getFieldName(0));
+
+        JdbcSourceTable nullablePrimaryKeyColumn =
+                table(
+                        null,
+                        PrimaryKey.of("pk_code", 
Collections.singletonList("code")),
+                        Collections.singletonList(uniqueKey("uk_no", "no")));
+        assertEquals("no", 
splitter.findSplitKey(nullablePrimaryKeyColumn).get().getFieldName(0));
+    }
+
+    @Test
+    public void testAutomaticSplitKeyKeepsNotNullKeyColumns() {
+        DynamicChunkSplitter splitter = new 
DynamicChunkSplitter(postgresConfig());
+
+        JdbcSourceTable primaryKey =
+                table(
+                        null,
+                        PrimaryKey.of("pk_id", 
Collections.singletonList("id")),
+                        Collections.singletonList(uniqueKey("uk_no", "no")));
+        assertEquals("id", 
splitter.findSplitKey(primaryKey).get().getFieldName(0));
+
+        JdbcSourceTable notNullUniqueKey =
+                table(null, null, Collections.singletonList(uniqueKey("uk_no", 
"no")));
+        assertEquals("no", 
splitter.findSplitKey(notNullUniqueKey).get().getFieldName(0));
+    }
+
+    /** An explicit {@code partition_column} is used as configured, also when 
it is nullable. */
+    @Test
+    public void testExplicitPartitionColumnMayBeNullable() {
+        DynamicChunkSplitter splitter = new 
DynamicChunkSplitter(postgresConfig());
+
+        JdbcSourceTable table = table("code", null, Collections.emptyList());
+        assertEquals("code", 
splitter.findSplitKey(table).get().getFieldName(0));
+    }
+
+    /**
+     * A {@code query} without {@code partition_column} gets no automatic 
split key, even when the
+     * table has a NOT NULL primary key, and is read as one split; so the 
nullability check of the
+     * automatic choice is only needed for table reads.
+     */
+    @Test
+    public void testQueryWithoutPartitionColumnIsReadAsOneSplit() throws 
Exception {
+        DynamicChunkSplitter splitter = new 
DynamicChunkSplitter(postgresConfig());
+
+        JdbcSourceTable query =
+                table(
+                                null,
+                                PrimaryKey.of("pk_id", 
Collections.singletonList("id")),
+                                Collections.singletonList(uniqueKey("uk_no", 
"no")))
+                        .withQuery("SELECT id, code, no FROM tbl");
+        assertFalse(splitter.findSplitKey(query).isPresent());
+        Collection<JdbcSourceSplit> splits = splitter.generateSplits(query);
+        assertEquals(1, splits.size());
+        assertNull(splits.iterator().next().getSplitKeyName());
+
+        JdbcSourceTable queryWithPartitionColumn =
+                table("code", null, Collections.emptyList())
+                        .withQuery("SELECT id, code, no FROM tbl");
+        assertEquals("code", 
splitter.findSplitKey(queryWithPartitionColumn).get().getFieldName(0));
+    }
+
+    private static JdbcSourceConfig postgresConfig() {
+        return JdbcSourceConfig.builder()
+                .jdbcConnectionConfig(
+                        JdbcConnectionConfig.builder()
+                                .url("jdbc:postgresql://localhost:5432/test")
+                                .driverName("org.postgresql.Driver")
+                                .build())
+                .build();
+    }
+
+    private static ConstraintKey uniqueKey(String name, String column) {
+        return ConstraintKey.of(
+                ConstraintKey.ConstraintType.UNIQUE_KEY,
+                name,
+                Collections.singletonList(
+                        ConstraintKey.ConstraintKeyColumn.of(
+                                column, ConstraintKey.ColumnSortType.ASC)));
+    }
+
+    /**
+     * A table whose INT columns {@code id} and {@code no} are NOT NULL and 
{@code code} is
+     * nullable.
+     */
+    private static JdbcSourceTable table(
+            String partitionColumn, PrimaryKey primaryKey, List<ConstraintKey> 
uniqueKeys) {
+        TableSchema.Builder schema =
+                TableSchema.builder()
+                        .column(PhysicalColumn.of("id", BasicType.INT_TYPE, 
0L, false, null, null))
+                        .column(PhysicalColumn.of("code", BasicType.INT_TYPE, 
0L, true, null, null))
+                        .column(PhysicalColumn.of("no", BasicType.INT_TYPE, 
0L, false, null, null))
+                        .constraintKey(uniqueKeys);
+        if (primaryKey != null) {
+            schema.primaryKey(primaryKey);
+        }
+        TablePath tablePath = TablePath.of("db", "tbl");
+        return JdbcSourceTable.builder()
+                .tablePath(tablePath)
+                .partitionColumn(partitionColumn)
+                .catalogTable(
+                        CatalogTable.of(
+                                TableIdentifier.of("catalog", tablePath),
+                                schema.build(),
+                                Collections.emptyMap(),
+                                Collections.emptyList(),
+                                ""))
+                .build();
+    }
 }
diff --git 
a/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-jdbc-e2e/connector-jdbc-e2e-part-7/src/test/java/org/apache/seatunnel/connectors/seatunnel/jdbc/JdbcMysqlSplitIT.java
 
b/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-jdbc-e2e/connector-jdbc-e2e-part-7/src/test/java/org/apache/seatunnel/connectors/seatunnel/jdbc/JdbcMysqlSplitIT.java
index aa7d7cbea68..6e1ceb25fe5 100644
--- 
a/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-jdbc-e2e/connector-jdbc-e2e-part-7/src/test/java/org/apache/seatunnel/connectors/seatunnel/jdbc/JdbcMysqlSplitIT.java
+++ 
b/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-jdbc-e2e/connector-jdbc-e2e-part-7/src/test/java/org/apache/seatunnel/connectors/seatunnel/jdbc/JdbcMysqlSplitIT.java
@@ -960,4 +960,120 @@ public class JdbcMysqlSplitIT extends TestSuiteBase 
implements TestResource {
             }
         }
     }
+
+    /**
+     * Range split queries never match NULL, so the automatic selection must 
not split by a nullable
+     * unique key: a table with only such a key is read as one split, and a 
table that also has a
+     * NOT NULL unique key is split by that one. Every row is read once in 
both cases.
+     */
+    @Test
+    public void testNullableUniqueKeyIsNotChosenAsSplitKey() throws Exception {
+        try (Connection connection = getJdbcConnection();
+                Statement statement = connection.createStatement()) {
+            String nullableOnly = "nullable_unique_key_only";
+            String withNotNull = "nullable_and_not_null_unique_keys";
+            try {
+                // No primary key; the unique key code is nullable and holds 
10 NULL values.
+                statement.execute(
+                        "CREATE TABLE "
+                                + MYSQL_DATABASE
+                                + "."
+                                + nullableOnly
+                                + " (id INT NOT NULL, code INT NULL, UNIQUE 
KEY uk_code (code))");
+                statement.execute(
+                        "CREATE TABLE "
+                                + MYSQL_DATABASE
+                                + "."
+                                + withNotNull
+                                + " (id INT NOT NULL, code INT NULL, no INT 
NOT NULL,"
+                                + " UNIQUE KEY uk_code (code), UNIQUE KEY 
uk_no (no))");
+                insertRowsWithNullCodes(connection, nullableOnly, false);
+                insertRowsWithNullCodes(connection, withNotNull, true);
+
+                Map<String, Object> configMap = new HashMap<>();
+                configMap.put("url", mysqlUrlInfo.getUrlWithDatabase().get());
+                configMap.put("driver", "com.mysql.cj.jdbc.Driver");
+                configMap.put("username", MYSQL_USERNAME);
+                configMap.put("password", MYSQL_PASSWORD);
+                configMap.put("split.size", "10");
+
+                try (MySqlCatalog mySqlCatalog =
+                                new MySqlCatalog(
+                                        "mysql",
+                                        MYSQL_USERNAME,
+                                        MYSQL_PASSWORD,
+                                        mysqlUrlInfo,
+                                        null);
+                        DynamicChunkSplitter splitter = 
getDynamicChunkSplitter(configMap)) {
+                    mySqlCatalog.open();
+
+                    Collection<JdbcSourceSplit> splits =
+                            readEveryIdOnce(splitter, mySqlCatalog, 
nullableOnly);
+                    Assertions.assertEquals(1, splits.size());
+                    
Assertions.assertNull(splits.iterator().next().getSplitKeyName());
+
+                    splits = readEveryIdOnce(splitter, mySqlCatalog, 
withNotNull);
+                    Assertions.assertTrue(splits.size() > 1, "splits: " + 
splits.size());
+                    for (JdbcSourceSplit split : splits) {
+                        Assertions.assertEquals("no", split.getSplitKeyName());
+                    }
+                }
+            } finally {
+                statement.execute("DROP TABLE IF EXISTS " + MYSQL_DATABASE + 
"." + nullableOnly);
+                statement.execute("DROP TABLE IF EXISTS " + MYSQL_DATABASE + 
"." + withNotNull);
+            }
+        }
+    }
+
+    /** Inserts ids 1 to 100; every tenth row has a NULL code. */
+    private void insertRowsWithNullCodes(Connection connection, String table, 
boolean withNo)
+            throws SQLException {
+        String sql =
+                "INSERT INTO "
+                        + MYSQL_DATABASE
+                        + "."
+                        + table
+                        + (withNo ? " VALUES (?, ?, ?)" : " VALUES (?, ?)");
+        try (PreparedStatement insert = connection.prepareStatement(sql)) {
+            for (int id = 1; id <= 100; id++) {
+                insert.setInt(1, id);
+                insert.setObject(2, id % 10 == 0 ? null : id);
+                if (withNo) {
+                    insert.setInt(3, id);
+                }
+                insert.addBatch();
+            }
+            insert.executeBatch();
+        }
+    }
+
+    /**
+     * Plans the splits of a table filled by {@link #insertRowsWithNullCodes} 
from its real MySQL
+     * catalog metadata, runs the query of every split, and asserts that each 
of the 100 ids is read
+     * exactly once: a duplicate fails at once, a row that no split reads 
fails the final count.
+     *
+     * @return the planned splits, so the caller can check which split key was 
chosen
+     */
+    private Collection<JdbcSourceSplit> readEveryIdOnce(
+            DynamicChunkSplitter splitter, MySqlCatalog catalog, String 
tableName)
+            throws Exception {
+        TablePath tablePath = TablePath.of(MYSQL_DATABASE, tableName);
+        CatalogTable table = catalog.getTable(tablePath);
+        Collection<JdbcSourceSplit> splits =
+                splitter.generateSplits(
+                        
JdbcSourceTable.builder().tablePath(tablePath).catalogTable(table).build());
+        Set<Integer> ids = new HashSet<>();
+        for (JdbcSourceSplit split : splits) {
+            try (PreparedStatement splitStatement =
+                            splitter.generateSplitStatement(split, 
table.getTableSchema());
+                    ResultSet resultSet = splitStatement.executeQuery()) {
+                while (resultSet.next()) {
+                    int id = resultSet.getInt("id");
+                    Assertions.assertTrue(ids.add(id), "duplicated id: " + id);
+                }
+            }
+        }
+        Assertions.assertEquals(100, ids.size(), tableName);
+        return splits;
+    }
 }

Reply via email to