diff --git a/src/main/java/com/google/cloud/spanner/jdbc/JdbcArray.java b/src/main/java/com/google/cloud/spanner/jdbc/JdbcArray.java index 80bea84eb..1dd83f817 100644 --- a/src/main/java/com/google/cloud/spanner/jdbc/JdbcArray.java +++ b/src/main/java/com/google/cloud/spanner/jdbc/JdbcArray.java @@ -60,9 +60,11 @@ class JdbcArray implements Array { * the elements array is not compatible with the base type of the array. */ static JdbcArray createArray(String typeName, Object[] elements) throws SQLException { - for (JdbcDataType type : JdbcDataType.values()) { - if (type.getTypeName().equalsIgnoreCase(typeName)) { - return new JdbcArray(type, elements); + if (typeName != null) { + for (JdbcDataType type : JdbcDataType.values()) { + if (type.matches(typeName)) { + return new JdbcArray(type, elements); + } } } throw JdbcSqlExceptionFactory.of( diff --git a/src/main/java/com/google/cloud/spanner/jdbc/JdbcDataType.java b/src/main/java/com/google/cloud/spanner/jdbc/JdbcDataType.java index 6a77cecc2..1870589fd 100644 --- a/src/main/java/com/google/cloud/spanner/jdbc/JdbcDataType.java +++ b/src/main/java/com/google/cloud/spanner/jdbc/JdbcDataType.java @@ -16,6 +16,7 @@ package com.google.cloud.spanner.jdbc; +import com.google.cloud.spanner.Dialect; import com.google.cloud.spanner.ResultSet; import com.google.cloud.spanner.Struct; import com.google.cloud.spanner.Type; @@ -154,6 +155,7 @@ public Type getSpannerType() { }, FLOAT64 { private final Set> classes = new HashSet<>(Arrays.asList(Float.class, Double.class)); + private final Set aliases = new HashSet<>(Collections.singletonList("float8")); @Override public int getSqlType() { @@ -184,6 +186,11 @@ public List getArrayElements(ResultSet rs, int columnIndex) { public Type getSpannerType() { return Type.float64(); } + + @Override + public Set getAliases() { + return aliases; + } }, INT64 { private final Set> classes = @@ -220,6 +227,9 @@ public Type getSpannerType() { } }, NUMERIC { + + private final Set aliases = new HashSet<>(Collections.singletonList("decimal")); + @Override public int getSqlType() { return Types.NUMERIC; @@ -244,6 +254,11 @@ public List getArrayElements(ResultSet rs, int columnIndex) { public Type getSpannerType() { return Type.numeric(); } + + @Override + public Set getAliases() { + return aliases; + } }, PG_NUMERIC { @Override @@ -272,6 +287,8 @@ public Type getSpannerType() { } }, STRING { + private final Set aliases = new HashSet<>(Arrays.asList("varchar", "text")); + @Override public int getSqlType() { return Types.NVARCHAR; @@ -296,6 +313,11 @@ public List getArrayElements(ResultSet rs, int columnIndex) { public Type getSpannerType() { return Type.string(); } + + @Override + public Set getAliases() { + return aliases; + } }, JSON { @Override @@ -498,6 +520,21 @@ public Type getSpannerType() { public abstract Type getSpannerType(); + public Set getAliases() { + return Collections.emptySet(); + } + + /*** + * @param typeName type of the column + * @return true if type name matches current type name or matches with one of postgres aliases + * or if it matches equivalent postgres type. + */ + public boolean matches(String typeName) { + return getTypeName().equalsIgnoreCase(typeName) + || getAliases().contains(typeName.toLowerCase()) + || getSpannerType().getSpannerTypeName(Dialect.POSTGRESQL).equalsIgnoreCase(typeName); + } + // TODO: Implement and use this method for all types. public int getPrecision() { throw new UnsupportedOperationException(); diff --git a/src/main/resources/com/google/cloud/spanner/jdbc/postgresql/DatabaseMetaData_GetColumns.sql b/src/main/resources/com/google/cloud/spanner/jdbc/postgresql/DatabaseMetaData_GetColumns.sql index a8260ff13..b573831d2 100644 --- a/src/main/resources/com/google/cloud/spanner/jdbc/postgresql/DatabaseMetaData_GetColumns.sql +++ b/src/main/resources/com/google/cloud/spanner/jdbc/postgresql/DatabaseMetaData_GetColumns.sql @@ -28,7 +28,20 @@ SELECT TABLE_CATALOG AS "TABLE_CAT", TABLE_SCHEMA AS "TABLE_SCHEM", TABLE_NAME A WHEN DATA_TYPE = 'jsonb' THEN -9 WHEN DATA_TYPE = 'timestamp with time zone' THEN 93 END AS "DATA_TYPE", - DATA_TYPE AS "TYPE_NAME", + CASE + WHEN DATA_TYPE LIKE 'ARRAY' THEN + CASE + WHEN spanner_type LIKE '%[]' THEN + CONCAT('_', + REPLACE( + REPLACE( + REPLACE(spanner_type, '[]', ''), + 'character varying', 'varchar'), + 'boolean', 'bool')) + ELSE spanner_type + END + ELSE DATA_TYPE + END AS "TYPE_NAME", CASE WHEN DATA_TYPE LIKE 'ARRAY' THEN 0 WHEN DATA_TYPE = 'boolean' THEN NULL