Repository navigation
[SourceDbToSpanner] Use prepared statements for Spanner to MySQL #4644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,14 +38,23 @@ | |
| * again and gets routed back into the DLQ. | ||
| * | ||
| * <p>For the Orders table, it translates the OrderSource column into a custom LegacyOrderSystem | ||
| * column to validate standard data transformation operations. | ||
| * column to validate standard data transformation operations. Use {@code rawValues=true} for | ||
| * sources that bind custom transformation values (e.g. Oracle) to return a plain value instead of a | ||
| * SQL literal from {@code toSourceRow}. | ||
|
Comment on lines
+41
to
+43
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Javadoc/comments here (and on lines 53 and 127) are stale/inaccurate: they reference |
||
| */ | ||
| public class CustomTransformationForDLQIT implements ISpannerMigrationTransformer { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(CustomTransformationForDLQIT.class); | ||
|
|
||
| private String mode = "bad"; | ||
|
|
||
| /** | ||
| * When {@code true} (parameter {@code rawValues=true}), {@link #toSourceRow} returns plain values | ||
| * for sources that bind custom transformation values (e.g. Oracle). Otherwise it returns SQL | ||
| * literals, since the other sources write custom transformation values to the SQL as is. | ||
| */ | ||
| private boolean rawValues = false; | ||
|
|
||
| @Override | ||
| public void init(String parameters) { | ||
| LOG.info("init called with {}", parameters); | ||
|
|
@@ -56,11 +65,13 @@ public void init(String parameters) { | |
| if (kv.length == 2) { | ||
| if (kv[0].trim().equals("mode")) { | ||
| this.mode = kv[1].trim(); | ||
| } else if (kv[0].trim().equals("rawValues")) { | ||
| this.rawValues = Boolean.parseBoolean(kv[1].trim()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| LOG.info("Mode set to {}", this.mode); | ||
| LOG.info("Mode set to {}, rawValues set to {}", this.mode, this.rawValues); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should not be logging all the data. |
||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -113,9 +124,9 @@ public MigrationTransformationResponse toSourceRow(MigrationTransformationReques | |
| if (tableName.equalsIgnoreCase("Orders")) { | ||
| Object sourceObj = requestRow.get("OrderSource"); | ||
| String source = (sourceObj != null) ? (String) sourceObj : "UNKNOWN_SYSTEM"; | ||
| // Enclose in single quotes since DML generator injects custom responses directly as raw | ||
| // values | ||
| String legacySys = "'" + source + "_v1'"; | ||
| // With rawValues the value is bound as is (e.g. Oracle). Otherwise enclose it in single | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this complexity need not be exposed in the IT. In the IT we should just write to spanner and check in source as is. |
||
| // quotes since the DML generator injects custom responses directly as raw values. | ||
| String legacySys = rawValues ? source + "_v1" : "'" + source + "_v1'"; | ||
| responseRow.put("LegacyOrderSystem", legacySys); | ||
| return new MigrationTransformationResponse(responseRow, false); | ||
| } else if (tableName.equalsIgnoreCase("AllDataTypes")) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -134,6 +134,30 @@ public void testToSourceRow_AllDataTypes_SemiFixedMode_NonCrashingId() throws Ex | |
| assertNotNull(response); | ||
| } | ||
|
|
||
| @Test | ||
| public void testToSourceRow_Orders_ReturnsSqlLiteralByDefault() throws Exception { | ||
| transformer.init("mode=bad"); | ||
| Map<String, Object> requestRow = new HashMap<>(); | ||
| requestRow.put("OrderSource", "WEB"); | ||
| MigrationTransformationRequest request = | ||
| new MigrationTransformationRequest("Orders", requestRow, "shard1", "INSERT"); | ||
|
|
||
| MigrationTransformationResponse response = transformer.toSourceRow(request); | ||
| assertEquals("'WEB_v1'", response.getResponseRow().get("LegacyOrderSystem")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testToSourceRow_Orders_RawValues() throws Exception { | ||
| transformer.init("mode=bad,rawValues=true"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need this mode ? |
||
| Map<String, Object> requestRow = new HashMap<>(); | ||
| requestRow.put("OrderSource", "WEB"); | ||
| MigrationTransformationRequest request = | ||
| new MigrationTransformationRequest("Orders", requestRow, "shard1", "INSERT"); | ||
|
|
||
| MigrationTransformationResponse response = transformer.toSourceRow(request); | ||
| assertEquals("WEB_v1", response.getResponseRow().get("LegacyOrderSystem")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTransformFailedSpannerMutation() throws Exception { | ||
| MigrationTransformationResponse response = transformer.transformFailedSpannerMutation(null); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,12 +58,7 @@ public void write( | |
| statement = pstmt; | ||
| int paramIdx = 1; | ||
| for (Object param : params) { | ||
| if (param instanceof byte[]) { | ||
| byte[] bytes = (byte[]) param; | ||
| pstmt.setBlob(paramIdx++, new java.io.ByteArrayInputStream(bytes), bytes.length); | ||
| } else { | ||
| pstmt.setObject(paramIdx++, param); | ||
| } | ||
| bindParameter(pstmt, paramIdx++, param); | ||
| } | ||
| pstmt.executeUpdate(); | ||
| } else { | ||
|
|
@@ -86,4 +81,15 @@ public void write( | |
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pre-existing, but more exposed now that all MySQL writes go through Consider using try-with-resources for |
||
| } | ||
| } | ||
|
|
||
| /** Binds a prepared statement parameter. Sources can override this for type specific binding. */ | ||
| protected void bindParameter(java.sql.PreparedStatement pstmt, int paramIdx, Object param) | ||
| throws java.sql.SQLException { | ||
| if (param instanceof byte[]) { | ||
| byte[] bytes = (byte[]) param; | ||
| pstmt.setBlob(paramIdx, new java.io.ByteArrayInputStream(bytes), bytes.length); | ||
| } else { | ||
| pstmt.setObject(paramIdx, param); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * Copyright (C) 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| * use this file except in compliance with the License. You may obtain a copy of | ||
| * the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
| package com.google.cloud.teleport.v2.templates.source.mysql; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import com.google.common.collect.ImmutableList; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * The SQL fragment used for a single column value in a parameterized DML statement, together with | ||
| * the values to be bound to the {@code ?} placeholders in that fragment (in order). | ||
| * | ||
| * <p>The SQL fragment is always generated by the template (for example {@code ?} or {@code | ||
| * CONVERT_TZ(?, '+00:00', ?)}) and never contains user or record data. | ||
| */ | ||
| public final class ColumnBinding { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this need to be mysql specific ? Why is not at the JDBC level ? |
||
|
|
||
| public static final String PLACEHOLDER = "?"; | ||
|
|
||
| private final String placeholderSql; | ||
| private final ImmutableList<PreparedStatementValue> params; | ||
|
|
||
| private ColumnBinding(String placeholderSql, List<PreparedStatementValue> params) { | ||
| Preconditions.checkNotNull(placeholderSql, "placeholderSql must not be null"); | ||
| Preconditions.checkNotNull(params, "params must not be null"); | ||
| long placeholderCount = placeholderSql.chars().filter(c -> c == '?').count(); | ||
| Preconditions.checkArgument( | ||
| placeholderCount == params.size(), | ||
| "Placeholder count %s does not match parameter count %s for SQL fragment: %s", | ||
| placeholderCount, | ||
| params.size(), | ||
| placeholderSql); | ||
| this.placeholderSql = placeholderSql; | ||
| this.params = ImmutableList.copyOf(params); | ||
| } | ||
|
|
||
| /** A binding with a single {@code ?} placeholder. */ | ||
| public static ColumnBinding of(PreparedStatementValue value) { | ||
| return new ColumnBinding(PLACEHOLDER, ImmutableList.of(value)); | ||
| } | ||
|
|
||
| /** A binding with a template-generated SQL fragment containing one {@code ?} per parameter. */ | ||
| public static ColumnBinding of(String placeholderSql, List<PreparedStatementValue> params) { | ||
| return new ColumnBinding(placeholderSql, params); | ||
| } | ||
|
|
||
| public String getPlaceholderSql() { | ||
| return placeholderSql; | ||
| } | ||
|
|
||
| public ImmutableList<PreparedStatementValue> getParams() { | ||
| return params; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (!(o instanceof ColumnBinding)) { | ||
| return false; | ||
| } | ||
| ColumnBinding that = (ColumnBinding) o; | ||
| return placeholderSql.equals(that.placeholderSql) && params.equals(that.params); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(placeholderSql, params); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "ColumnBinding{placeholderSql=" + placeholderSql + ", params=" + params + "}"; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should remove the rawValues flag here. The contract should be that whatever custom transform returns we pass it into the query. With the prepared queries this should not be a security issue