Skip to content

[SpannerToSourceDb] Use prepared statements for Spanner to PostgreSQL - #4655

Draft
aasthabharill wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
aasthabharill:fix-postgres-prepared-statements
Draft

aasthabharill wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
aasthabharill:fix-postgres-prepared-statements

Conversation

@aasthabharill

Copy link
Copy Markdown
Member

Summary

Migrates PostgreSQLDMLGenerator in v2/spanner-to-sourcedb from string-concatenated SQL to PreparedStatement parameter binding, lifting shared JDBC binding abstractions into dbutils/dml/ and JdbcDao so other JDBC dialects (such as #4644 for MySQL) can reuse them without duplication.

Changes

  • Shared JDBC parameter abstractions (dbutils/dml/):
    • Added PreparedStatementValue and ColumnBinding in com.google.cloud.teleport.v2.templates.dbutils.dml.
    • Added shared getColumnBindings / getPkColumnBindings (with a customizable nullBindingMapper overload for future Oracle/SQL Server reuse) and shared Spanner value helpers in DMLGeneratorUtils. Legacy getColumnValues / getPkColumnValues are kept intact for un-migrated dialects.
  • Shared JdbcDao execution (dbutils/dao/source/JdbcDao.java):
    • Added bindParameter support for PreparedStatementValue (including Types.OTHER for PostgreSQL untyped string binding, Float, and typed setNull), while retaining legacy raw parameter binding and the raw Statement fallback for Oracle and SQL Server.
    • Switched Connection and Statement/PreparedStatement to try-with-resources with explicit rollback() on failure so pooled connections cannot leak when statement.close() throws.
  • Parameterized PostgreSQLDMLGenerator (source/postgres/PostgreSQLDMLGenerator.java):
    • Bound every column value, NULL, customTransformationResponse output, and unknown/default column type as a PreparedStatement parameter (never concatenated into SQL).
    • Escaped embedded double quotes (" -> "") in all table and column identifiers across INSERT, ON CONFLICT, DO UPDATE SET ... EXCLUDED, and DELETE ... WHERE.
    • Dispatched bindings by target PostgreSQL column type for pgjdbc compatibility: Types.OTHER for json/jsonb/uuid/date/time/timetz/arrays/enums/unknown types, ?::timestamptz for Spanner timestamps, ofLong for integer columns (preserving PK B-tree indexes), ofFloat for real/float4, ofDouble for double precision/float8, bounded BigDecimal for numeric/decimal (preserving scientific-notation precision while preventing large-string DoS), ofBoolean strictly for boolean, IS NULL for null predicates in generated-PK deletes, and {...} formatting (with Base64-decoded \x... hex for bytea[]) for native PostgreSQL array columns.
  • Tests:
    • Added PreparedStatementValueTest and expanded DMLGeneratorUtilsTest, JdbcDaoTest (mocking IConnectionHelper without mutating static pool state), and PostgreSQLDMLGeneratorTest (including symmetrical DELETE coverage, generated PKs, cross-type safety, and SQL injection regression tests).

Verification

  • mvn -pl v2/spanner-common,v2/spanner-to-sourcedb test (Checkstyle + 518 tests in spanner-common, 628 tests in spanner-to-sourcedb: all passed)
  • mvn -pl v2/spanner-to-sourcedb spotless:check (passed)
@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.22430% with 63 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.60%. Comparing base (34a7ab7) to head (d65916c).

Files with missing lines Patch % Lines
...plates/source/postgres/PostgreSQLDMLGenerator.java 87.71% 15 Missing and 21 partials ⚠️
...rt/v2/templates/dbutils/dml/DMLGeneratorUtils.java 87.61% 7 Missing and 7 partials ⚠️
.../templates/dbutils/dml/PreparedStatementValue.java 89.28% 2 Missing and 4 partials ⚠️
...leport/v2/templates/dbutils/dml/ColumnBinding.java 82.60% 2 Missing and 2 partials ⚠️
...eport/v2/templates/dbutils/dao/source/JdbcDao.java 94.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #4655      +/-   ##
============================================
+ Coverage     59.69%   66.60%   +6.91%     
+ Complexity     8678     3467    -5211     
============================================
  Files          1122      553     -569     
  Lines         72738    32990   -39748     
  Branches       8854     3873    -4981     
============================================
- Hits          43418    21972   -21446     
+ Misses        26419     9918   -16501     
+ Partials       2901     1100    -1801     
Components Coverage Δ
spanner-templates 84.14% <88.22%> (+0.11%) ⬆️
spanner-import-export ∅ <ø> (∅)
spanner-live-forward-migration 87.60% <ø> (-0.05%) ⬇️
spanner-live-reverse-replication 80.33% <88.22%> (+0.39%) ⬆️
spanner-bulk-migration 88.26% <ø> (-0.03%) ⬇️
gcs-spanner-dv 86.51% <ø> (-0.05%) ⬇️
Files with missing lines Coverage Δ
...eport/v2/templates/dbutils/dao/source/JdbcDao.java 95.08% <94.00%> (+35.62%) ⬆️
...leport/v2/templates/dbutils/dml/ColumnBinding.java 82.60% <82.60%> (ø)
.../templates/dbutils/dml/PreparedStatementValue.java 89.28% <89.28%> (ø)
...rt/v2/templates/dbutils/dml/DMLGeneratorUtils.java 93.08% <87.61%> (-5.96%) ⬇️
...plates/source/postgres/PostgreSQLDMLGenerator.java 87.89% <87.71%> (-3.44%) ⬇️

... and 593 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

1 participant