diff --git a/v2/datastream-to-sql/src/main/java/com/google/cloud/teleport/v2/utils/DatastreamToMySQLDML.java b/v2/datastream-to-sql/src/main/java/com/google/cloud/teleport/v2/utils/DatastreamToMySQLDML.java index cb63fef5e6..dcddd59eae 100644 --- a/v2/datastream-to-sql/src/main/java/com/google/cloud/teleport/v2/utils/DatastreamToMySQLDML.java +++ b/v2/datastream-to-sql/src/main/java/com/google/cloud/teleport/v2/utils/DatastreamToMySQLDML.java @@ -17,6 +17,14 @@ import com.google.cloud.teleport.v2.datastream.io.CdcJdbcIO.DataSourceConfiguration; import com.google.cloud.teleport.v2.datastream.values.DatastreamRow; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; +import java.time.format.DateTimeParseException; +import java.time.temporal.ChronoField; +import java.util.Locale; +import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -24,6 +32,11 @@ public class DatastreamToMySQLDML extends DatastreamToDML { private static final Logger LOG = LoggerFactory.getLogger(DatastreamToMySQLDML.class); + private static final DateTimeFormatter TIMESTAMP_FORMATTER = + new DateTimeFormatterBuilder() + .appendPattern("uuuu-MM-dd HH:mm:ss") + .appendFraction(ChronoField.NANO_OF_SECOND, 0, 6, true) + .toFormatter(Locale.ROOT); private DatastreamToMySQLDML(DataSourceConfiguration config) { super(config); @@ -69,4 +82,26 @@ public String getTargetCatalogName(DatastreamRow row) { public String getTargetSchemaName(DatastreamRow row) { return ""; } + + @Override + public String cleanDataTypeValueSql( + String columnValue, String columnName, Map tableSchema) { + String dataType = tableSchema.get(columnName); + if (!("TIMESTAMP".equalsIgnoreCase(dataType) || "DATETIME".equalsIgnoreCase(dataType)) + || columnValue.length() < 2 + || !columnValue.startsWith("'") + || !columnValue.endsWith("'")) { + return columnValue; + } + try { + OffsetDateTime timestamp = + OffsetDateTime.parse(columnValue.substring(1, columnValue.length() - 1)); + return "'" + + TIMESTAMP_FORMATTER.format(timestamp.withOffsetSameInstant(ZoneOffset.UTC)) + + "'"; + } catch (DateTimeParseException e) { + // Leave local timestamp literals and invalid values to the existing sink handling. + return columnValue; + } + } } diff --git a/v2/datastream-to-sql/src/test/java/com/google/cloud/teleport/v2/utils/DatastreamToDMLTest.java b/v2/datastream-to-sql/src/test/java/com/google/cloud/teleport/v2/utils/DatastreamToDMLTest.java index 0ae5590d35..08bbfe30b0 100644 --- a/v2/datastream-to-sql/src/test/java/com/google/cloud/teleport/v2/utils/DatastreamToDMLTest.java +++ b/v2/datastream-to-sql/src/test/java/com/google/cloud/teleport/v2/utils/DatastreamToDMLTest.java @@ -108,6 +108,70 @@ public void testGetValueSql() { assertEquals(expectedNullByteTextContent, testNullByteSqlContent); } + @Test + public void testMySqlTimestampLiterals() { + DatastreamToDML dml = DatastreamToMySQLDML.of(null); + Map tableSchema = new HashMap<>(); + String[][] timestamps = { + {"2025-08-01T09:15:00Z", "2025-08-01 09:15:00"}, + {"2025-08-01T09:15:00.000123Z", "2025-08-01 09:15:00.000123"}, + {"2025-08-01T09:15:00.123Z", "2025-08-01 09:15:00.123"}, + {"2025-08-01T00:15:00+02:00", "2025-07-31 22:15:00"} + }; + for (String type : Arrays.asList("TIMESTAMP", "DATETIME")) { + tableSchema.put("created_at", type); + for (String[] timestamp : timestamps) { + JsonNode rowObj = getRowObj("{\"created_at\":\"" + timestamp[0] + "\"}"); + assertEquals("'" + timestamp[1] + "'", dml.getValueSql(rowObj, "created_at", tableSchema)); + } + } + } + + @Test + public void testMySqlTimestampUnchangedValues() { + DatastreamToDML dml = DatastreamToMySQLDML.of(null); + Map tableSchema = new HashMap<>(); + tableSchema.put("created_at", "TIMESTAMP"); + for (String value : + Arrays.asList("", "invalid", "2025-02-30T09:15:00Z", "2025-08-01 09:15:00")) { + JsonNode rowObj = getRowObj("{\"created_at\":\"" + value + "\"}"); + assertEquals("'" + value + "'", dml.getValueSql(rowObj, "created_at", tableSchema)); + } + assertEquals( + "null", dml.getValueSql(getRowObj("{\"created_at\":null}"), "created_at", tableSchema)); + assertEquals("", dml.getValueSql(getRowObj("{}"), "created_at", tableSchema)); + JsonNode rowObj = getRowObj("{\"created_at\":\"2025-08-01T09:15:00Z\",\"id\":123}"); + assertEquals("123", dml.getValueSql(rowObj, "id", tableSchema)); + for (String type : Arrays.asList("VARCHAR", "JSON", null)) { + tableSchema.put("created_at", type); + assertEquals("'2025-08-01T09:15:00Z'", dml.getValueSql(rowObj, "created_at", tableSchema)); + } + tableSchema.clear(); + assertEquals("'2025-08-01T09:15:00Z'", dml.getValueSql(rowObj, "created_at", tableSchema)); + tableSchema.put("created_at", "TIMESTAMP"); + assertEquals( + "'2025-08-01T09:15:00Z'", + DatastreamToPostgresDML.of(null).getValueSql(rowObj, "created_at", tableSchema)); + } + + @Test + public void testMySqlTimestampSqlFragmentsWithColumnCasing() { + DatastreamToDML dml = DatastreamToMySQLDML.of(null).withColumnCasing("UPPERCASE"); + Map tableSchema = new HashMap<>(); + tableSchema.put("CREATED_AT", "timestamp"); + JsonNode rowObj = + getRowObj( + "{\"created_at\":\"2025-08-01T09:15:00.000123Z\"," + + "\"_metadata_source_type\":\"mysql\"," + + "\"_metadata_primary_keys\":[\"created_at\"]}"); + assertEquals("'2025-08-01 09:15:00.000123'", dml.getColumnsValuesSql(rowObj, tableSchema)); + assertEquals( + "`CREATED_AT`='2025-08-01 09:15:00.000123'", dml.getColumnsUpdateSql(rowObj, tableSchema)); + assertEquals( + "`CREATED_AT`='2025-08-01 09:15:00.000123'", + dml.getPrimaryKeyToValueFilterSql(rowObj, Arrays.asList("CREATED_AT"), tableSchema)); + } + /** * Test whether {@link DatastreamToPostgresDML#getValueSql(JsonNode, String, Map)} converts array * data into correct integer array syntax.