Skip to content

Commit 6039a06

Browse files
feat: add UUID support for GSQL and PG dialect mappings in AvroToValueMapper
1 parent e6e9753 commit 6039a06

11 files changed

Lines changed: 40 additions & 28 deletions

File tree

v2/datastream-to-spanner/src/test/java/com/google/cloud/teleport/v2/templates/MySQLDatastreamToSpannerDataTypesAndExpressionIT.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ private Map<String, List<Map<String, Object>>> getExpectedData() {
611611
expectedData.put("set", createRows("set", "v1,v2", "NULL"));
612612
expectedData.put(
613613
"integer_unsigned", createRows("integer_unsigned", "0", "42", "4294967295", "NULL"));
614+
expectedData.put("uuid", createRows("uuid", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", "NULL"));
614615
return expectedData;
615616
}
616617

v2/datastream-to-spanner/src/test/resources/MySQLDataTypesIT/mysql-data-types.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,4 +739,9 @@ CREATE TABLE IF NOT EXISTS spatial_geometrycollection (
739739

740740
INSERT INTO spatial_geometrycollection (geoms) VALUES (MultiPoint(Point(77.5946, 12.9716), Point(77.6100, 12.9600)));
741741

742+
CREATE TABLE IF NOT EXISTS `uuid_table` (
743+
`id` INT PRIMARY KEY,
744+
`uuid_col` VARCHAR(36) DEFAULT NULL
745+
);
742746

747+
INSERT INTO `uuid_table` (`id`, `uuid_col`) VALUES (1, 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'), (2, NULL);

v2/datastream-to-spanner/src/test/resources/MySQLDataTypesIT/pg-dialect-spanner-schema.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,3 +477,9 @@ CREATE TABLE IF NOT EXISTS spatial_geometrycollection (
477477
geom_coll VARCHAR,
478478
PRIMARY KEY (id)
479479
);
480+
481+
CREATE TABLE IF NOT EXISTS uuid_table (
482+
id INT8 NOT NULL,
483+
uuid_col UUID,
484+
PRIMARY KEY (id)
485+
);

v2/datastream-to-spanner/src/test/resources/MySQLDataTypesIT/spanner-schema.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,3 +424,8 @@ CREATE TABLE IF NOT EXISTS `generated_to_non_generated_column_table` (
424424
`generated_column_col` STRING(100) DEFAULT(NULL),
425425
`generated_column_pk_col` STRING(100) DEFAULT(NULL),
426426
) PRIMARY KEY (`generated_column_pk_col`);
427+
428+
CREATE TABLE IF NOT EXISTS uuid_table (
429+
id INT64 NOT NULL,
430+
uuid_col UUID,
431+
) PRIMARY KEY(id);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ private Map<String, List<Map<String, Object>>> getExpectedData() {
474474
"-2.2250738585072014E-308"));
475475
expectedData.put(
476476
"time_pk", createRows("time_pk", "15:50:00.200000", "838:59:58.123000", "-838:59:59"));
477+
expectedData.put("uuid", createRows("uuid", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", "NULL"));
477478
return expectedData;
478479
}
479480

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,4 +899,9 @@ CREATE TABLE IF NOT EXISTS spatial_geometrycollection (
899899

900900
INSERT INTO spatial_geometrycollection (geoms) VALUES (MultiPoint(Point(77.5946, 12.9716), Point(77.6100, 12.9600)));
901901

902+
CREATE TABLE IF NOT EXISTS `uuid_table` (
903+
`id` INT PRIMARY KEY,
904+
`uuid_col` VARCHAR(36) DEFAULT NULL
905+
);
902906

907+
INSERT INTO `uuid_table` (`id`, `uuid_col`) VALUES (1, 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'), (2, NULL);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,3 +546,8 @@ CREATE TABLE time_pk_table (
546546
id STRING(MAX) NOT NULL,
547547
time_pk_col STRING(MAX) NOT NULL
548548
) PRIMARY KEY(id);
549+
550+
CREATE TABLE IF NOT EXISTS uuid_table (
551+
id INT64 NOT NULL,
552+
uuid_col UUID,
553+
) PRIMARY KEY(id);

v2/spanner-common/src/main/java/com/google/cloud/teleport/v2/spanner/migrations/avro/AvroToValueMapper.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ static Map<Type, AvroToValueFunction> getGsqlMap() {
148148
avroArrayFieldToSpannerArray(
149149
recordValue, fieldSchema, AvroToValueMapper::avroFieldToString)));
150150

151+
gsqlFunctions.put(
152+
Type.uuid(),
153+
(recordValue, fieldSchema) -> Value.string(avroFieldToString(recordValue, fieldSchema)));
154+
151155
gsqlFunctions.put(
152156
Type.json(),
153157
(recordValue, fieldSchema) -> Value.string(avroFieldToString(recordValue, fieldSchema)));
@@ -245,6 +249,9 @@ static Map<Type, AvroToValueFunction> getPgMap() {
245249
pgFunctions.put(
246250
Type.pgDate(),
247251
(recordValue, fieldSchema) -> Value.date(avroFieldToDate(recordValue, fieldSchema)));
252+
pgFunctions.put(
253+
Type.pgUuid(),
254+
(recordValue, fieldSchema) -> Value.string(avroFieldToString(recordValue, fieldSchema)));
248255
return pgFunctions;
249256
}
250257

v2/spanner-common/src/main/java/com/google/cloud/teleport/v2/spanner/migrations/avro/GenericRecordTypeConvertor.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -572,21 +572,10 @@ private Value getSpannerValueFromObject(
572572
throw new NullPointerException("schemaMapper returned null spanner dialect.");
573573
}
574574
if (AvroToValueMapper.convertorMap().get(dialect).containsKey(spannerType)) {
575-
try {
576-
return AvroToValueMapper.convertorMap()
577-
.get(dialect)
578-
.get(spannerType)
579-
.apply(value, fieldSchema);
580-
} catch (Exception e) {
581-
LOG.error(
582-
"Exception while converting record value {} to Field Schema: {}, colName: {}, spannerType: {}",
583-
value,
584-
fieldSchema,
585-
recordColName,
586-
spannerType,
587-
e);
588-
throw e;
589-
}
575+
return AvroToValueMapper.convertorMap()
576+
.get(dialect)
577+
.get(spannerType)
578+
.apply(value, fieldSchema);
590579
} else {
591580
throw new IllegalArgumentException(
592581
"Found unsupported Spanner column type("

v2/spanner-common/src/test/java/com/google/cloud/teleport/v2/spanner/migrations/avro/AvroToValueMapperTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ public void testAvroValueToArrayBasic() {
561561
assertThat(
562562
AvroToValueMapper.getGsqlMap().keySet().stream()
563563
.filter(t -> !t.getCode().equals(Code.ARRAY))
564+
.filter(t -> !t.getCode().equals(Code.UUID))
564565
.map(t -> t.toString())
565566
.sorted()
566567
.collect(Collectors.toList()))

0 commit comments

Comments
 (0)