Thorne created FLINK-41700:
------------------------------

             Summary: [CDC][SQLServer] Fix incremental snapshots for tables 
without primary keys
                 Key: FLINK-41700
                 URL: https://issues.apache.org/jira/browse/FLINK-41700
             Project: Flink
          Issue Type: Improvement
          Components: Flink CDC
    Affects Versions: cdc-3.7.0, cdc-3.6.0, cdc-3.5.0
            Reporter: Thorne
             Fix For: cdc-3.8.0


SQL Server CDC documents support for tables without primary keys when 
{{scan.incremental.snapshot.chunk.key-column}} specifies a non-null column. 
However, the SQL Server snapshot reader still applies primary-key-only 
validation and rejects these tables even when a chunk key is explicitly 
configured. See the [SQL Server CDC 
documentation|https://nightlies.apache.org/flink/flink-cdc-docs-stable/docs/connectors/flink-sources/sqlserver-cdc/#tables-without-primary-keys].

The split-column validation is inconsistent between chunk splitting and 
snapshot reading. {{SqlServerUtils.getSplitColumn()}} requires a primary key 
and only searches primary-key columns when resolving an explicit chunk key.

Removing this restriction alone is insufficient. Split-key extraction assumes 
the chunk column exists in the Debezium message key, while snapshot buffering 
uses that message key as a unique map key. For keyless records, multiple rows 
can overwrite the same null-key entry. The fix must preserve row multiplicity, 
including identical duplicate rows, without treating the chunk key as a unique 
identifier.

*Steps to reproduce*
 # In a CDC-enabled SQL Server database, create a table without a primary key:

{{CREATE TABLE dbo.keyless_orders (
    order_ref NVARCHAR(64),
    tenant_id INT NOT NULL
);

EXEC sys.sp_cdc_enable_table
    @source_schema = N'dbo',
    @source_name = N'keyless_orders',
    @role_name = NULL,
    @supports_net_changes = 0;

INSERT INTO dbo.keyless_orders VALUES
    (N'A', 1),
    (N'B', 1),
    (N'A', 1),
    (N'C', 2);}}
 # Read this table using an initial incremental snapshot with:

{{scan.startup.mode = initial
scan.incremental.snapshot.enabled = true
scan.incremental.snapshot.chunk.key-column = tenant_id}}

The corresponding DataStream option is {{{}.chunkKeyColumn("tenant_id"){}}}.

*Current behavior*

Snapshot reading fails with:

{{Incremental snapshot for tables requires primary key,
but table ... doesn't have primary key.}}

*Expected behavior*

The connector should use {{tenant_id}} to determine chunk boundaries and read 
all four rows, including both copies of {{{}(A, 1){}}}.

Snapshot buffering and backfill reconciliation should preserve duplicate rows 
and correctly apply changes within the snapshot window.

*Proposed fix and regression coverage*
 * Resolve the configured chunk column consistently during splitting and 
reading, and extract split-key values from the appropriate row image.
 * Preserve keyless snapshot rows, including repeated chunk-key values and 
identical duplicates.
 * Cover single-chunk and multiple-chunk snapshots.
 * Cover concurrent inserts, updates to non-chunk columns, and deletion of one 
duplicate row during snapshot backfill.
 * Preserve existing behavior for tables with primary keys and retain clear 
validation errors when a keyless table has no configured chunk key.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to