Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions v2/spanner-to-sourcedb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@ Steps to perform customization:
4. If any events are filtered/skipped during the reverse transformation they can be written to a GCS directory by passing the GCS path via the *filterEventsDirectoryName* input to the runner.
5. If any custom parameters are needed in the custom transformation logic, they can be passed via the *transformationCustomParameters* input to the runner. These parameters will be passed to the *init* method of the custom class. The *init* method is invoked once per worker setup.

> **Note:** `toSourceRow()` output values are bound strictly as query parameters (raw data), never as SQL fragments. Do **not** wrap string values in single quotes (`'abc'`) or return SQL expressions (such as `now()` or `decode(...)`), as quotes and function calls will be stored literally or rejected by the database.

## Cost

1. Cloud Spanner change stream incur additional storage requirement, refer [here](https://cloud.google.com/spanner/docs/change-streams#data-retention).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@ public static String getSpannerValueAsString(
return valuesJson.getString(colName);
}

/**
* Returns {@code true} if {@code trimmed} is a case-insensitive non-finite float / numeric
* literal ({@code NaN}, {@code Infinity}, {@code +Infinity}, or {@code -Infinity}).
*/
public static boolean isNonFiniteTrimmed(String trimmed) {
return NON_FINITE_VALUES.contains(trimmed.toLowerCase(Locale.ROOT));
}

/**
* Returns the text of a non-finite float / numeric value ({@code NaN}, {@code Infinity}, {@code
* -Infinity}), which the change stream writes as a JSON string, or {@code null} otherwise.
Expand All @@ -235,7 +243,7 @@ public static String getNonFiniteText(JSONObject valuesJson, String colName) {
Object value = valuesJson.get(colName);
if (value instanceof String) {
String text = ((String) value).trim();
if (NON_FINITE_VALUES.contains(text.toLowerCase(Locale.ROOT))) {
if (isNonFiniteTrimmed(text)) {
return text;
}
} else if (value instanceof Double || value instanceof Float) {
Expand Down Expand Up @@ -268,6 +276,16 @@ public static boolean isStringArray(Type colType) {
|| colType.getArrayElementType().getCode().equals(Type.Code.PG_TEXT)));
}

/** Normalizes a source database column type name by trimming and lowercasing it. */
public static String normalizeSourceType(@Nullable String sourceType) {
return sourceType == null ? "" : sourceType.trim().toLowerCase(Locale.ROOT);
}

/** Strips NUL ({@code U+0000}) characters from {@code input}. */
public static String removeNullBytes(String input) {
Comment thread
aasthabharill marked this conversation as resolved.
Resolved
return StringUtils.replace(input, "\u0000", "");
}

/**
* Removes the trailing {@code Z} (UTC designator) of a Spanner timestamp. Values without it (e.g.
* from a Spanner STRING column) are returned as is.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
*
* <p>The value is always one of {@code String}, {@code byte[]}, {@code Boolean}, {@code
* BigDecimal}, {@code Long}, {@code Double} or {@code null}. The {@link #getSqlType() SQL type} is
* used to bind {@code null} values via {@link PreparedStatement#setNull(int, int)}.
* used to bind {@code null} values via {@link PreparedStatement#setNull(int, int)} and to
* distinguish untyped strings ({@link Types#OTHER}) from typed {@code VARCHAR} strings.
*/
public final class PreparedStatementValue {

Expand All @@ -50,6 +51,19 @@ public static PreparedStatementValue ofString(@Nullable String value) {
: new PreparedStatementValue(value, Types.VARCHAR);
}

/**
* Returns an untyped string binding ({@link Types#OTHER}).
*
* <p>Used by PostgreSQL ({@code pgjdbc}), which transmits {@link Types#OTHER} strings with OID
* {@code UNSPECIFIED} ({@code 0}) so the server parses the bound parameter using the target
* column's input function (for example {@code jsonb}, {@code uuid}, {@code date}, {@code time},
* {@code real}, arrays, or enums) instead of rejecting {@code VARCHAR}. Other JDBC drivers may
* interpret {@link Types#OTHER} differently.
*/
public static PreparedStatementValue ofUntypedString(@Nullable String value) {
return value == null ? nullValue(Types.OTHER) : new PreparedStatementValue(value, Types.OTHER);
}

public static PreparedStatementValue ofBytes(@Nullable byte[] value) {
return value == null
? nullValue(Types.VARBINARY)
Expand Down Expand Up @@ -136,11 +150,15 @@ public boolean isNull() {

/**
* Binds this value to parameter {@code paramIdx} of {@code statement} with the setter of its Java
* type, or {@link PreparedStatement#setNull(int, int)} with its SQL type for {@code null}.
* type, {@link PreparedStatement#setObject(int, Object, int)} for {@link Types#OTHER} untyped
* strings, or {@link PreparedStatement#setNull(int, int)} with its SQL type for {@code null}.
*/
public void bind(PreparedStatement statement, int paramIdx) throws SQLException {
if (value == null) {
statement.setNull(paramIdx, sqlType);
} else if (sqlType == Types.OTHER) {
// pgjdbc sends OID UNSPECIFIED (0) so PostgreSQL infers the target column type.
statement.setObject(paramIdx, value, Types.OTHER);
} else if (value instanceof byte[]) {
statement.setBytes(paramIdx, (byte[]) value);
} else if (value instanceof String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -312,16 +311,12 @@ static String quoteIdentifier(String identifier) {
*/
@VisibleForTesting
static boolean isNumericLiteralType(String sourceType) {
String type = normalizeSourceType(sourceType);
String type = DMLGeneratorUtils.normalizeSourceType(sourceType);
return !TEXT_TYPES.contains(type)
&& !TIMESTAMP_TYPES.contains(type)
&& !BINARY_TYPES.contains(type);
}

private static String normalizeSourceType(String sourceType) {
return sourceType == null ? "" : sourceType.toLowerCase(Locale.ROOT);
}

/**
* Maps a non-null Spanner column value to a {@link ColumnBinding} based on the Spanner column
* type and the MySQL source column type.
Expand All @@ -343,7 +338,7 @@ static ColumnBinding getMappedColumnBinding(
return ColumnBinding.of(PreparedStatementValue.ofBytes(bytes));
}

String sourceType = normalizeSourceType(sourceColDef.type());
String sourceType = DMLGeneratorUtils.normalizeSourceType(sourceColDef.type());
if (isNumericLiteralType(sourceType)) {
return ColumnBinding.of(getNumericLiteralTypeValue(colType, valuesJson, colName));
}
Expand All @@ -354,15 +349,17 @@ static ColumnBinding getMappedColumnBinding(
return ColumnBinding.of(
CONVERT_TZ_PLACEHOLDER,
ImmutableList.of(
PreparedStatementValue.ofString(removeNullBytes(utcTimestamp)),
PreparedStatementValue.ofString(DMLGeneratorUtils.removeNullBytes(utcTimestamp)),
PreparedStatementValue.ofString(sourceDbTimezoneOffset)));
}
if (BINARY_TYPES.contains(sourceType)) {
return ColumnBinding.of(
BINARY_PLACEHOLDER,
ImmutableList.of(PreparedStatementValue.ofString(removeNullBytes(stringValue))));
ImmutableList.of(
PreparedStatementValue.ofString(DMLGeneratorUtils.removeNullBytes(stringValue))));
}
return ColumnBinding.of(PreparedStatementValue.ofString(removeNullBytes(stringValue)));
return ColumnBinding.of(
PreparedStatementValue.ofString(DMLGeneratorUtils.removeNullBytes(stringValue)));
}

/**
Expand Down Expand Up @@ -478,8 +475,4 @@ private static void checkFinite(JSONObject valuesJson, String colName) {
nonFiniteText, colName));
}
}

private static String removeNullBytes(String input) {
return StringUtils.replace(input, "\u0000", "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ private static Object toBindValue(
// Non-BYTES Spanner value (e.g. STRING) written to a binary column: Base64 encoded bytes.
return toTypedBindValue(bindSqlType, decodeBase64(colInputValue));
}
return toBindValueByType(sourceType, StringUtils.replace(colInputValue, "\u0000", ""));
return toBindValueByType(sourceType, DMLGeneratorUtils.removeNullBytes(colInputValue));
}

/** Converts a non-BYTES Spanner value to its string form. */
Expand Down
Loading
Loading