Skip to content

Commit d0a4bc7

Browse files
Adding Support for Timestamp PKey. IT for fractional dateTime and fractional Timestamp (#2468)
1 parent 2a754c7 commit d0a4bc7

File tree

11 files changed

+59
-12
lines changed

11 files changed

+59
-12
lines changed

v2/sourcedb-to-spanner/src/main/java/com/google/cloud/teleport/v2/source/reader/io/jdbc/dialectadapter/mysql/MysqlDialectAdapter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,6 @@ private ImmutableMap<String, SourceColumnType> getTableCols(
357357
ImmutableMap.<String, SourceColumnIndexInfo.IndexType>builder()
358358
.put("BIGINT UNSIGNED", IndexType.BIG_INT_UNSIGNED)
359359
.put("BIGINT", IndexType.NUMERIC)
360-
.put("DATETIME", IndexType.DATE_TIME)
361360
.put("INTEGER", IndexType.NUMERIC)
362361
.put("INTEGER UNSIGNED", IndexType.NUMERIC)
363362
.put("MEDIUMINT", IndexType.NUMERIC)
@@ -372,6 +371,8 @@ private ImmutableMap<String, SourceColumnType> getTableCols(
372371
.put("VARBINARY", IndexType.BINARY)
373372
.put("TINYBLOB", IndexType.BINARY)
374373
.put("TINYTEXT", IndexType.STRING)
374+
.put("DATETIME", IndexType.TIME_STAMP)
375+
.put("TIMESTAMP", IndexType.TIME_STAMP)
375376
.build();
376377

377378
/**

v2/sourcedb-to-spanner/src/main/java/com/google/cloud/teleport/v2/source/reader/io/jdbc/dialectadapter/postgresql/PostgreSQLDialectAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ private SourceColumnIndexInfo.IndexType indexTypeFrom(String typeCategory) {
508508
case "N":
509509
return SourceColumnIndexInfo.IndexType.NUMERIC;
510510
case "D":
511-
return SourceColumnIndexInfo.IndexType.DATE_TIME;
511+
return SourceColumnIndexInfo.IndexType.TIME_STAMP;
512512
case "S":
513513
return SourceColumnIndexInfo.IndexType.STRING;
514514
default:

v2/sourcedb-to-spanner/src/main/java/com/google/cloud/teleport/v2/source/reader/io/jdbc/iowrapper/JdbcIoWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ private static TableConfig getTableConfig(
311311
IndexType.STRING,
312312
IndexType.BIG_INT_UNSIGNED,
313313
IndexType.BINARY,
314-
IndexType.DATE_TIME);
314+
IndexType.TIME_STAMP);
315315
// As of now only Primary key index with Numeric type is supported.
316316
// TODO:
317317
// 1. support non-primary unique indexes.

v2/sourcedb-to-spanner/src/main/java/com/google/cloud/teleport/v2/source/reader/io/jdbc/uniformsplitter/range/BoundaryExtractorFactory.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.sql.ResultSet;
2222
import java.sql.SQLException;
2323
import java.sql.Timestamp;
24+
import java.util.Calendar;
25+
import java.util.TimeZone;
2426
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions;
2527
import org.checkerframework.checker.nullness.qual.Nullable;
2628

@@ -158,6 +160,8 @@ private static Boundary<String> fromStrings(
158160
.build();
159161
}
160162

163+
private static final Calendar utcCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
164+
161165
private static Boundary<Timestamp> fromTimestamps(
162166
PartitionColumn partitionColumn,
163167
ResultSet resultSet,
@@ -167,8 +171,8 @@ private static Boundary<Timestamp> fromTimestamps(
167171
resultSet.next();
168172
return Boundary.<Timestamp>builder()
169173
.setPartitionColumn(partitionColumn)
170-
.setStart(resultSet.getTimestamp(1))
171-
.setEnd(resultSet.getTimestamp(2))
174+
.setStart(resultSet.getTimestamp(1, utcCalendar))
175+
.setEnd(resultSet.getTimestamp(2, utcCalendar))
172176
.setBoundarySplitter(BoundarySplitterFactory.create(Timestamp.class))
173177
.setBoundaryTypeMapper(boundaryTypeMapper)
174178
.build();

v2/sourcedb-to-spanner/src/main/java/com/google/cloud/teleport/v2/source/reader/io/schema/SourceColumnIndexInfo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public enum IndexType {
142142
BIG_INT_UNSIGNED,
143143
BINARY,
144144
STRING,
145-
DATE_TIME,
145+
TIME_STAMP,
146146
OTHER
147147
};
148148

@@ -153,5 +153,5 @@ public enum IndexType {
153153
IndexType.STRING, String.class,
154154
IndexType.BIG_INT_UNSIGNED, BigDecimal.class,
155155
IndexType.BINARY, BoundaryExtractorFactory.BYTE_ARRAY_CLASS,
156-
IndexType.DATE_TIME, Timestamp.class);
156+
IndexType.TIME_STAMP, Timestamp.class);
157157
}

v2/sourcedb-to-spanner/src/test/java/com/google/cloud/teleport/v2/source/reader/io/jdbc/dialectadapter/postgresql/PostgreSQLDialectAdapterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public void testDiscoverTableIndexes() throws SQLException, RetriableSchemaDisco
270270
.setIsPrimary(false)
271271
.setCardinality(2L)
272272
.setOrdinalPosition(2L)
273-
.setIndexType(SourceColumnIndexInfo.IndexType.DATE_TIME)
273+
.setIndexType(SourceColumnIndexInfo.IndexType.TIME_STAMP)
274274
.build()));
275275
}
276276

v2/sourcedb-to-spanner/src/test/java/com/google/cloud/teleport/v2/source/reader/io/jdbc/uniformsplitter/range/BoundaryExtractorFactoryTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import static com.google.cloud.teleport.v2.source.reader.io.jdbc.uniformsplitter.range.BoundaryExtractorFactory.BYTE_ARRAY_CLASS;
1919
import static com.google.common.truth.Truth.assertThat;
2020
import static org.junit.Assert.assertThrows;
21+
import static org.mockito.ArgumentMatchers.any;
22+
import static org.mockito.ArgumentMatchers.eq;
2123
import static org.mockito.Mockito.doReturn;
2224
import static org.mockito.Mockito.when;
2325

@@ -246,9 +248,10 @@ public void testFromTimestamp() throws SQLException {
246248
BoundaryExtractor<Timestamp> extractor = BoundaryExtractorFactory.create(Timestamp.class);
247249
Timestamp start = Timestamp.valueOf("0000-01-01 00:00:00.000000000");
248250
Timestamp end = Timestamp.valueOf("2041-01-31 23:59:59.999999999");
251+
249252
when(mockResultSet.next()).thenReturn(true);
250-
when(mockResultSet.getTimestamp(1)).thenReturn(start);
251-
when(mockResultSet.getTimestamp(2)).thenReturn(end);
253+
when(mockResultSet.getTimestamp(eq(1), any())).thenReturn(start);
254+
when(mockResultSet.getTimestamp(eq(2), any())).thenReturn(end);
252255
Boundary<Timestamp> boundary = extractor.getBoundary(partitionColumn, mockResultSet, null);
253256
assertThat(boundary.start()).isEqualTo(start);
254257
assertThat(boundary.end()).isEqualTo(end);

v2/sourcedb-to-spanner/src/test/java/com/google/cloud/teleport/v2/source/reader/io/jdbc/uniformsplitter/range/BoundarySplitterFactoryTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ public void testInstantMapping() {
302302
@Test
303303
public void testTimeStampSplitting() {
304304
assertThat(BoundarySplitterFactory.splitInstants(null, null)).isEqualTo(null);
305+
assertThat(BoundarySplitterFactory.splitInstants(null, Instant.EPOCH))
306+
.isEqualTo(BoundarySplitterFactory.splitInstants(Instant.EPOCH, null));
305307
assertThat(BoundarySplitterFactory.splitInstants(Instant.MIN, Instant.MAX))
306308
.isEqualTo(Instant.parse("0000-07-01T23:59:59.999999999Z"));
307309

v2/sourcedb-to-spanner/src/test/java/com/google/cloud/teleport/v2/templates/MySQLDataTypesIT.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,22 @@ private Map<String, List<Map<String, Object>>> getExpectedData() {
257257
"date_time_pk",
258258
"1000-01-01T00:00:00Z",
259259
"1000-01-01T00:00:01Z",
260+
"2001-01-01T00:01:54.123456000Z",
261+
/* DateTime does not depend on time zone. */
262+
"2005-01-01T05:31:54.123456000Z",
260263
"9999-12-30T23:59:59Z",
261264
"9999-12-31T23:59:59Z"));
265+
expectedData.put(
266+
"timestamp_pk",
267+
createRows(
268+
"timestamp_pk",
269+
"1970-01-01T00:00:01Z",
270+
"1970-01-01T00:00:02Z",
271+
"2001-01-01T00:01:54.123456000Z",
272+
/* Timestamp offsets by time zone. We always read in UTC. */
273+
"2005-01-01T00:01:54.123456000Z",
274+
"2037-12-30T23:59:59Z",
275+
"2038-01-18T23:59:59Z"));
262276
return expectedData;
263277
}
264278

v2/sourcedb-to-spanner/src/test/resources/DataTypesIT/mysql-data-types.sql

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,14 @@ CREATE TABLE tiny_text_pk_table (
261261
);
262262

263263
CREATE TABLE date_time_pk_table (
264-
id DATETIME,
265-
date_time_pk_col DATETIME NOT NULL,
264+
id DATETIME(6),
265+
date_time_pk_col DATETIME(6) NOT NULL,
266+
CONSTRAINT PRIMARY KEY (id)
267+
);
268+
269+
CREATE TABLE timestamp_pk_table (
270+
id TIMESTAMP(6),
271+
timestamp_pk_col TIMESTAMP(6) NOT NULL,
266272
CONSTRAINT PRIMARY KEY (id)
267273
);
268274

@@ -403,7 +409,19 @@ INSERT INTO `char_pk_table` (`id`, `char_pk_col`) VALUES ('AA==', 'AA=='), ('gAA
403409
INSERT INTO `varchar_pk_table` (`id`, `varchar_pk_col`) VALUES ('AA==', 'AA=='), ('gAAAAAAAAAA=', 'gAAAAAAAAAA=');
404410
INSERT INTO `tiny_text_pk_table` (`id`, `tiny_text_pk_col`) VALUES ('AA==', 'AA=='), ('gAAAAAAAAAA=', 'gAAAAAAAAAA=');
405411
INSERT INTO `date_time_pk_table` (`id`, `date_time_pk_col`) VALUES ('1000-01-01 00:00:00', '1000-01-01 00:00:00'), ('1000-01-01 00:00:01', '1000-01-01 00:00:01'),
412+
('2001-01-01 00:01:54.123456', '2001-01-01 00:01:54.123456'),
406413
('9999-12-30 23:59:59', '9999-12-30 23:59:59'), ('9999-12-31 23:59:59', '9999-12-31 23:59:59');
414+
SET time_zone = 'Asia/Kolkata';
415+
INSERT INTO `date_time_pk_table` (`id`, `date_time_pk_col`) VALUES ('2005-01-01 05:31:54.123456', '2005-01-01 05:31:54.123456');
416+
SET time_zone = SYSTEM;
417+
418+
SET time_zone = 'UTC';
419+
INSERT INTO `timestamp_pk_table` (`id`, `timestamp_pk_col`) VALUES ('1970-01-01 00:00:01', '1970-01-01 00:00:01'), ('1970-01-01 00:00:02', '1970-01-01 00:00:02'),
420+
('2001-01-01 00:01:54.123456', '2001-01-01 00:01:54.123456'),
421+
('2037-12-30 23:59:59', '2037-12-30 23:59:59'), ('2038-01-18 23:59:59', '2038-01-18 23:59:59');
422+
SET time_zone = 'Asia/Kolkata';
423+
INSERT INTO `timestamp_pk_table` (`id`, `timestamp_pk_col`) VALUES ('2005-01-01 05:31:54.123456', '2005-01-01 05:31:54.123456');
424+
SET time_zone = SYSTEM;
407425

408426
INSERT INTO `bigint_table` (`bigint_col`) VALUES (NULL);
409427
INSERT INTO `bigint_unsigned_table` (`bigint_unsigned_col`) VALUES (NULL);

0 commit comments

Comments
 (0)