Skip to content

feat(Spanner): CDC Data Generator: add SQL Server sink support - #4627

Open
Vaibhavch9876 wants to merge 1 commit into
mainfrom
cdcgen-sqlserver-sink
Open

Vaibhavch9876 wants to merge 1 commit into
mainfrom
cdcgen-sqlserver-sink

Conversation

@Vaibhavch9876

@Vaibhavch9876 Vaibhavch9876 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Adds SQLSERVER as a sink type in the CDC Data Generator template.

  • New source/sqlserver package: connector, sink config, type mapper, schema fetcher and data writer.
  • Writer: batched INSERT with MERGE fallback on duplicate keys, MERGE upserts, DELETE by PK, IDENTITY_INSERT for IDENTITY primary keys; server-generated columns (rowversion, computed, non-PK identity) are never written.
  • SQLServerInformationSchemaScanner now also reads size/precision/scale, foreign keys and unique indexes (existing behaviour unchanged).
  • Registered SQLSERVER in CdcDataGeneratorOptions and SourceConnectorFactory; added mssql-jdbc to the module pom.

Testing: unit tests added (84); live runs on Cloud SQL for SQL Server 2022 (26 tables, 55 types) with INSERT/UPDATE/DELETE, no write failures and an empty DLQ.

Adds SQLSERVER as a sink type in the CDC Data Generator template.

- New source/sqlserver package: connector, sink config, type mapper,
  schema fetcher and data writer.
- Writer: batched INSERT with MERGE fallback on duplicate keys, MERGE
  upserts, DELETE by PK, IDENTITY_INSERT for IDENTITY primary keys;
  server-generated columns (rowversion, computed, non-PK identity) are
  never written.
- SQLServerInformationSchemaScanner now also reads size/precision/scale,
  foreign keys and unique indexes (existing behaviour unchanged).
- Registered SQLSERVER in CdcDataGeneratorOptions and
  SourceConnectorFactory; added mssql-jdbc to the module pom.
@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.39466% with 58 lines in your changes missing coverage. Please review.
✅ Project coverage is 60.04%. Comparing base (d189155) to head (a11f46f).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
...eport/v2/source/sqlserver/SqlServerDataWriter.java 92.64% 8 Missing and 17 partials ⚠️
...rt/v2/source/sqlserver/SqlServerSchemaFetcher.java 88.09% 8 Missing and 12 partials ⚠️
...r/sourceddl/SQLServerInformationSchemaScanner.java 89.87% 2 Missing and 6 partials ⚠️
.../v2/source/sqlserver/SqlServerSourceConnector.java 93.18% 0 Missing and 3 partials ⚠️
...eport/v2/source/sqlserver/SqlServerSinkConfig.java 86.66% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #4627      +/-   ##
============================================
+ Coverage     59.74%   60.04%   +0.30%     
- Complexity     7926     8154     +228     
============================================
  Files          1123     1128       +5     
  Lines         72705    73376     +671     
  Branches       8842     8962     +120     
============================================
+ Hits          43435    44057     +622     
- Misses        26369    26379      +10     
- Partials       2901     2940      +39     
Components Coverage Δ
spanner-templates 84.05% <89.87%> (+0.02%) ⬆️
spanner-import-export 72.72% <ø> (+0.11%) ⬆️
spanner-live-forward-migration 87.67% <89.87%> (+0.02%) ⬆️
spanner-live-reverse-replication 80.02% <89.87%> (+0.07%) ⬆️
spanner-bulk-migration 88.30% <89.87%> (+<0.01%) ⬆️
gcs-spanner-dv 86.61% <89.87%> (+0.03%) ⬆️
Files with missing lines Coverage Δ
...oud/teleport/v2/source/SourceConnectorFactory.java 91.30% <100.00%> (+0.82%) ⬆️
...eport/v2/source/sqlserver/SqlServerTypeMapper.java 100.00% <100.00%> (ø)
...teleport/v2/templates/CdcDataGeneratorOptions.java 100.00% <100.00%> (ø)
...e/cloud/teleport/v2/templates/utils/Constants.java 0.00% <ø> (ø)
...eport/v2/source/sqlserver/SqlServerSinkConfig.java 86.66% <86.66%> (ø)
.../v2/source/sqlserver/SqlServerSourceConnector.java 93.18% <93.18%> (ø)
...r/sourceddl/SQLServerInformationSchemaScanner.java 93.12% <89.87%> (-5.02%) ⬇️
...rt/v2/source/sqlserver/SqlServerSchemaFetcher.java 88.09% <88.09%> (ø)
...eport/v2/source/sqlserver/SqlServerDataWriter.java 92.64% <92.64%> (ø)

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
@Vaibhavch9876 Vaibhavch9876 added cdc addition New feature or request and removed cdc labels Nov 24, 2026
@Vaibhavch9876
Vaibhavch9876 marked this pull request as ready for review November 25, 2026 08:29
@Vaibhavch9876
Vaibhavch9876 requested a review from a team as a code owner November 25, 2026 08:29
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces SQL Server as a supported sink for the CDC Data Generator template. It includes the necessary infrastructure to connect to, scan, and write data to SQL Server instances, with specific handling for SQL Server-specific features like IDENTITY columns, complex data types, and idempotent upserts using MERGE.

Highlights

  • New SQL Server Sink Support: Added comprehensive support for SQL Server as a sink in the CDC Data Generator, including a new connector, configuration, type mapping, and schema fetching.
  • Robust Data Writing: Implemented a batched writer that handles duplicate keys via MERGE fallback, supports IDENTITY_INSERT for primary keys, and correctly skips server-generated columns.
  • Enhanced Schema Scanning: Updated SQLServerInformationSchemaScanner to capture precision, scale, foreign keys, and unique indexes, improving schema fidelity.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@Vaibhavch9876
Vaibhavch9876 requested review from bharadwaj-aditya and dhwanilpatel and removed request for darshan-sj and nanditbansal November 25, 2026 08:30

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces comprehensive support for SQL Server as a sink in the CDC Data Generator, adding a new source connector, schema fetcher, type mapper, and data writer. Feedback on these changes highlights three key areas for improvement: first, wrapping the IDENTITY_INSERT reset in a try-catch block within the finally block to prevent masking primary exceptions; second, synchronizing the initialization of the transient tableInfoCache to ensure thread safety; and third, defaulting the schema name to "dbo" in SqlServerSchemaFetcher if both the namespace and connection schema are null or blank to avoid scanning all database schemas.

Comment on lines +228 to +232
} finally {
if (identityInsert) {
setIdentityInsert(connection, qualifiedTable, false);
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In the finally block, calling setIdentityInsert(connection, qualifiedTable, false) can throw a SQLException. If an exception was already thrown in the try block (e.g., during executeBatch), the cleanup exception will propagate and mask the original write exception, making troubleshooting very difficult. Consider wrapping the cleanup call in a try-catch block to log the cleanup failure without suppressing the primary exception.

      } finally {
        if (identityInsert) {
          try {
            setIdentityInsert(connection, qualifiedTable, false);
          } catch (SQLException e) {
            LOG.warn("Failed to reset IDENTITY_INSERT to OFF for table {}", qualifiedTable, e);
          }
        }
      }
Comment on lines +297 to +299
if (tableInfoCache == null) {
tableInfoCache = new ConcurrentHashMap<>();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The tableInfoCache map is transient and can be null after deserialization. In getTableInfo, initializing it with a simple null check (if (tableInfoCache == null)) is not thread-safe and can lead to race conditions where multiple threads overwrite the cache map. Since ensureInitialized is already synchronized, consider initializing tableInfoCache there or synchronizing its initialization here to ensure thread safety.

Suggested change
if (tableInfoCache == null) {
tableInfoCache = new ConcurrentHashMap<>();
}
synchronized (this) {
if (tableInfoCache == null) {
tableInfoCache = new ConcurrentHashMap<>();
}
}
Comment on lines +202 to +205
String schemaName =
(namespace != null && !namespace.isBlank())
? SqlServerDataWriter.unquoteIdentifier(namespace.trim())
: connection.getSchema();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If namespace is not provided and connection.getSchema() returns null (which can happen with certain JDBC driver versions or configurations), schemaName will be null. This can cause fetchColumnInfo to return an empty map and SQLServerInformationSchemaScanner to scan all schemas (including system schemas), leading to potential write failures or duplicate table mappings. Consider defaulting schemaName to "dbo" if both namespace and connection.getSchema() are null or blank.

Suggested change
String schemaName =
(namespace != null && !namespace.isBlank())
? SqlServerDataWriter.unquoteIdentifier(namespace.trim())
: connection.getSchema();
String schemaName =
(namespace != null && !namespace.isBlank())
? SqlServerDataWriter.unquoteIdentifier(namespace.trim())
: (connection.getSchema() != null && !connection.getSchema().isBlank()
? connection.getSchema()
: "dbo");
References
  1. When querying database metadata (such as tables, columns, or indexes) in dialect adapters, always filter by schema/namespace to prevent duplicate or incorrect mappings. If the schema/namespace is not provided, default to a standard schema (e.g., 'dbo' for SQL Server) instead of dynamically omitting the filter from the query.

@shreyakhajanchi shreyakhajanchi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR is very big >3k LOC, can you see if you can break into smaller reviewable units?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

addition New feature or request size/XXL

2 participants