Skip to content
Merged
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ public class PgJdbcCatalog {
+ " WHEN SPANNER_TYPE = 'jsonb[]' THEN 3807\n"
+ " WHEN SPANNER_TYPE = 'timestamp with time zone' THEN 1184\n"
+ " WHEN SPANNER_TYPE = 'timestamp with time zone[]' THEN 1185\n"
+ " WHEN SPANNER_TYPE = 'oid' THEN 26\n"
+ " WHEN SPANNER_TYPE = 'oid[]' THEN 1028\n"
+ " WHEN SPANNER_TYPE = 'uuid' THEN 2950\n"
+ " WHEN SPANNER_TYPE = 'uuid[]' THEN 2951\n"
+ " END AS atttypid,\n"
+ " -1 as atttypmod,\n"
+ " CASE\n"
Expand All @@ -280,6 +284,8 @@ public class PgJdbcCatalog {
+ " WHEN DATA_TYPE LIKE 'character varying' THEN CHARACTER_MAXIMUM_LENGTH\n"
+ " WHEN DATA_TYPE = 'jsonb' THEN 2621440\n"
+ " WHEN DATA_TYPE = 'timestamp with time zone' THEN 35\n"
+ " WHEN DATA_TYPE = 'oid' THEN 10\n"
+ " WHEN DATA_TYPE = 'uuid' THEN 36\n"
+ " END AS \"COLUMN_SIZE\",\n"
+ " 0 AS \"BUFFER_LENGTH\",\n"
+ " CASE\n"
Expand All @@ -293,6 +299,7 @@ public class PgJdbcCatalog {
+ " WHEN DATA_TYPE LIKE 'numeric' THEN 10\n"
+ " WHEN DATA_TYPE LIKE 'real' THEN 2\n"
+ " WHEN DATA_TYPE LIKE 'double precision' THEN 2\n"
+ " WHEN DATA_TYPE = 'oid' THEN 10\n"
+ " ELSE NULL\n"
+ " END AS \"NUM_PREC_RADIX\",\n"
+ " CASE\n"
Expand Down Expand Up @@ -701,6 +708,10 @@ public class PgJdbcCatalog {
+ "\t\twhen 'numeric'\t\t\t\t\tthen 1700\n"
+ "\t\twhen 'timestamp with time zone'\tthen 1184\n"
+ "\t\twhen 'character varying'\t\tthen 1043\n"
+ "\t\twhen 'date'\t\t\t\t\tthen 1082\n"
+ "\t\twhen 'jsonb'\t\t\t\tthen 3802\n"
+ "\t\twhen 'oid'\t\t\t\t\tthen 26\n"
+ "\t\twhen 'uuid'\t\t\t\t\tthen 2950\n"
+ "\t\telse 0\n"
+ "\tend as spanner_atttypid\n"
+ "from pg_catalog.pg_attribute a\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ int getOidTypeSize(int oid_type) {
case Oid.INT2:
return 2;
case Oid.INT4:
case Oid.OID:
return 4;
case Oid.INT8:
return 8;
Expand All @@ -181,6 +182,8 @@ int getOidTypeSize(int oid_type) {
return 1;
case Oid.BOOL:
return 1;
case Oid.UUID:
return 16;
case Oid.DATE:
return 8;
case Oid.TIME:
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/google/cloud/spanner/pgadapter/ITJdbcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,9 @@ public void testSelectTypes() throws SQLException {
assertEquals(Oid.TEXT, types.getInt(1));
assertEquals("text", types.getString(2));
assertTrue(types.next());
assertEquals(Oid.OID, types.getInt(1));
assertEquals("oid", types.getString(2));
assertTrue(types.next());
assertEquals(Oid.FLOAT4, types.getInt(1));
assertEquals("float4", types.getString(2));
assertTrue(types.next());
Expand Down Expand Up @@ -1315,6 +1318,9 @@ public void testSelectTypes() throws SQLException {
assertEquals(Oid.FLOAT8_ARRAY, types.getInt(1));
assertEquals("_float8", types.getString(2));
assertTrue(types.next());
assertEquals(Oid.OID_ARRAY, types.getInt(1));
assertEquals("_oid", types.getString(2));
assertTrue(types.next());
assertEquals(Oid.VARCHAR, types.getInt(1));
assertEquals("varchar", types.getString(2));
assertTrue(types.next());
Expand Down Expand Up @@ -1348,6 +1354,12 @@ public void testSelectTypes() throws SQLException {
assertEquals(Oid.NUMERIC, types.getInt(1));
assertEquals("numeric", types.getString(2));
assertTrue(types.next());
assertEquals(Oid.UUID, types.getInt(1));
assertEquals("uuid", types.getString(2));
assertTrue(types.next());
assertEquals(Oid.UUID_ARRAY, types.getInt(1));
assertEquals("_uuid", types.getString(2));
assertTrue(types.next());
assertEquals(Oid.JSONB, types.getInt(1));
assertEquals("jsonb", types.getString(2));
assertTrue(types.next());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public void testPgType() throws SQLException {
assertEquals(0, types.getInt("typrelid"));
count++;
}
assertEquals(31, count);
assertEquals(35, count);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ public void testSelectPgCatalogTables() throws IOException, InterruptedException
+ " int2\n"
+ " int4\n"
+ " text\n"
+ " oid\n"
+ " float4\n"
+ " float8\n"
+ " unknown\n"
Expand All @@ -490,6 +491,7 @@ public void testSelectPgCatalogTables() throws IOException, InterruptedException
+ " _int8\n"
+ " _float4\n"
+ " _float8\n"
+ " _oid\n"
+ " varchar\n"
+ " date\n"
+ " timestamp\n"
Expand All @@ -501,9 +503,11 @@ public void testSelectPgCatalogTables() throws IOException, InterruptedException
+ " _interval\n"
+ " _numeric\n"
+ " numeric\n"
+ " uuid\n"
+ " _uuid\n"
+ " jsonb\n"
+ " _jsonb\n"
+ "(31 rows)\n",
+ "(35 rows)\n",
output);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import com.google.cloud.spanner.pgadapter.wireprotocol.StartupMessage;
import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.ListValue;
import com.google.protobuf.Value;
import com.google.spanner.admin.database.v1.GetDatabaseDdlResponse;
Expand Down Expand Up @@ -5199,6 +5200,67 @@ public void testSelectPgTypeAndPgNamespace() throws SQLException {
}
}

@Test
public void testSelectTypesFromPgType() throws SQLException {
ImmutableMap<Long, String> types =
ImmutableMap.of(
26L, "oid",
1028L, "_oid",
2950L, "uuid",
2951L, "_uuid");
try (Connection connection = DriverManager.getConnection(createUrl())) {
for (Map.Entry<Long, String> entry : types.entrySet()) {
String sqlByOid =
String.format("select typname from pg_catalog.pg_type where oid = %d", entry.getKey());
String expectedTranslatedSqlByOid =
"with "
+ PG_TYPE_PREFIX
+ String.format("\nselect typname from pg_type where oid = %d", entry.getKey());
mockSpanner.putStatementResult(
StatementResult.query(
Statement.of(expectedTranslatedSqlByOid),
com.google.spanner.v1.ResultSet.newBuilder()
.setMetadata(createMetadata(ImmutableList.of(TypeCode.STRING)))
.addRows(
ListValue.newBuilder()
.addValues(Value.newBuilder().setStringValue(entry.getValue()).build())
.build())
.build()));
try (ResultSet resultSet = connection.createStatement().executeQuery(sqlByOid)) {
assertTrue(resultSet.next());
assertEquals(entry.getValue(), resultSet.getString(1));
assertFalse(resultSet.next());
}

String sqlByName =
String.format(
"select oid from pg_catalog.pg_type where typname = '%s'", entry.getValue());
String expectedTranslatedSqlByName =
"with "
+ PG_TYPE_PREFIX
+ String.format("\nselect oid from pg_type where typname = '%s'", entry.getValue());
mockSpanner.putStatementResult(
StatementResult.query(
Statement.of(expectedTranslatedSqlByName),
com.google.spanner.v1.ResultSet.newBuilder()
.setMetadata(createMetadata(ImmutableList.of(TypeCode.INT64)))
.addRows(
ListValue.newBuilder()
.addValues(
Value.newBuilder()
.setStringValue(entry.getKey().toString())
.build())
.build())
.build()));
try (ResultSet resultSet = connection.createStatement().executeQuery(sqlByName)) {
assertTrue(resultSet.next());
assertEquals(entry.getKey().longValue(), resultSet.getLong(1));
assertFalse(resultSet.next());
}
}
}
}

@Test
public void testDefaultReplacePgCatalogTables() throws SQLException {
try (Connection connection = DriverManager.getConnection(createUrl())) {
Expand Down
Loading
Loading