Repository navigation
feat(Spanner): CDC Data Generator: add SQL Server sink support - #4627
Vaibhavch9876 wants to merge 1 commit into
Conversation
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 Report❌ Patch coverage is 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
🚀 New features to boost your workflow:
|
Summary of ChangesHello, 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
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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.
| } finally { | ||
| if (identityInsert) { | ||
| setIdentityInsert(connection, qualifiedTable, false); | ||
| } | ||
| } |
There was a problem hiding this comment.
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);
}
}
}| if (tableInfoCache == null) { | ||
| tableInfoCache = new ConcurrentHashMap<>(); | ||
| } |
There was a problem hiding this comment.
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.
| if (tableInfoCache == null) { | |
| tableInfoCache = new ConcurrentHashMap<>(); | |
| } | |
| synchronized (this) { | |
| if (tableInfoCache == null) { | |
| tableInfoCache = new ConcurrentHashMap<>(); | |
| } | |
| } |
| String schemaName = | ||
| (namespace != null && !namespace.isBlank()) | ||
| ? SqlServerDataWriter.unquoteIdentifier(namespace.trim()) | ||
| : connection.getSchema(); |
There was a problem hiding this comment.
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.
| 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
- 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
left a comment
There was a problem hiding this comment.
The PR is very big >3k LOC, can you see if you can break into smaller reviewable units?
Adds SQLSERVER as a sink type in the CDC Data Generator template.
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.