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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package com.google.cloud.teleport.v2.templates.spannerchangestreamstobigquery.schemautils;

import com.google.cloud.spanner.Type;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONObject;

/**
Expand All @@ -24,6 +26,9 @@
*/
public class TypesUtils {

private static final Pattern POSTGRES_VECTOR_TYPE_PATTERN =
Pattern.compile("^(.+)\\[\\]\\s+VECTOR\\s+LENGTH\\s+[1-9][0-9]*$", Pattern.CASE_INSENSITIVE);

public static Type informationSchemaGoogleSQLTypeToSpannerType(String type) {
type = cleanInformationSchemaType(type);
switch (type) {
Expand Down Expand Up @@ -62,6 +67,12 @@ public static Type informationSchemaGoogleSQLTypeToSpannerType(String type) {
}

public static Type informationSchemaPostgreSQLTypeToSpannerType(String type) {
Matcher vectorTypeMatcher = POSTGRES_VECTOR_TYPE_PATTERN.matcher(type);
if (vectorTypeMatcher.matches()) {
Type itemType = informationSchemaPostgreSQLTypeToSpannerType(vectorTypeMatcher.group(1));
return Type.array(itemType);
}

boolean isPostgresArray = isPostgresArray(type);
String cleanedType = "";
if (isPostgresArray) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,37 @@ public void testInformationSchemaPostgreSQLTypeToSpannerType() {
.isEqualTo(Type.array(Type.string()));
assertThat(TypesUtils.informationSchemaPostgreSQLTypeToSpannerType("JSONB[]"))
.isEqualTo(Type.array(Type.pgJsonb()));
assertThat(TypesUtils.informationSchemaPostgreSQLTypeToSpannerType("real[]"))
.isEqualTo(Type.array(Type.float32()));
assertThat(TypesUtils.informationSchemaPostgreSQLTypeToSpannerType("real"))
.isEqualTo(Type.float32());
}

@Test
public void testInformationSchemaPostgreSQLVectorTypeToSpannerType() {
assertThat(TypesUtils.informationSchemaPostgreSQLTypeToSpannerType("real[] vector length 512"))
.isEqualTo(Type.array(Type.float32()));
assertThat(
TypesUtils.informationSchemaPostgreSQLTypeToSpannerType(
"DOUBLE PRECISION[] VECTOR LENGTH 4"))
.isEqualTo(Type.array(Type.float64()));
}

@Test
public void testInformationSchemaPostgreSQLVectorTypeRejectsInvalidTypes() {
assertThrows(
IllegalArgumentException.class,
() ->
TypesUtils.informationSchemaPostgreSQLTypeToSpannerType(
"real[] vector length invalid"));
assertThrows(
IllegalArgumentException.class,
() -> TypesUtils.informationSchemaPostgreSQLTypeToSpannerType("real[] vector length 0"));
assertThrows(
IllegalArgumentException.class,
() -> TypesUtils.informationSchemaPostgreSQLTypeToSpannerType("integer[] vector length 4"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using integer as an unsupported type in this test makes it fragile, as integer is a standard PostgreSQL type that might be supported in the future. If support for integer is added, this test will fail. Consider using a completely dummy/unsupported type name (e.g., unsupportedtype) to ensure the test remains robust and future-proof.

Suggested change
() -> TypesUtils.informationSchemaPostgreSQLTypeToSpannerType("integer[] vector length 4"));
() -> TypesUtils.informationSchemaPostgreSQLTypeToSpannerType("unsupportedtype[] vector length 4"));

}

@Test
public void testExtractTypeFromInvalidTypeCode() {
final JSONObject invalidJsonObject = new JSONObject("{\"type_code\":\"STRING\"}");
Expand Down
Loading