Skip to content

Commit c043c46

Browse files
authored
fix: support information_schema.sequences (#708)
Adds support for information_schema.sequences using a common table expression. The CTE always returns zero rows and is there purely for compatibility reasons. Towards #705
1 parent 37ce315 commit c043c46

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

src/main/java/com/google/cloud/spanner/pgadapter/statements/PgCatalog.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.google.common.collect.ImmutableMap;
2828
import com.google.common.collect.ImmutableSet;
2929
import java.util.HashSet;
30+
import java.util.Locale;
3031
import java.util.Map;
3132
import java.util.Map.Entry;
3233
import java.util.Set;
@@ -66,6 +67,9 @@ public class PgCatalog {
6667
.put(
6768
new TableOrIndexName(null, "pg_sequences"),
6869
new TableOrIndexName(null, "pg_sequences"))
70+
.put(
71+
new TableOrIndexName("information_schema", "sequences"),
72+
new TableOrIndexName(null, "pg_information_schema_sequences"))
6973
.put(
7074
new TableOrIndexName("pg_catalog", "pg_settings"),
7175
new TableOrIndexName(null, "pg_settings"))
@@ -95,7 +99,9 @@ public class PgCatalog {
9599
new TableOrIndexName(null, "pg_range"), new PgRange(),
96100
new TableOrIndexName(null, "pg_type"), new PgType(),
97101
new TableOrIndexName(null, "pg_sequence"), new PgSequence(),
98-
new TableOrIndexName(null, "pg_sequences"), new PgSequences());
102+
new TableOrIndexName(null, "pg_sequences"), new PgSequences(),
103+
new TableOrIndexName(null, "pg_information_schema_sequences"),
104+
new InformationSchemaSequences());
99105
private final SessionState sessionState;
100106

101107
public PgCatalog(@Nonnull SessionState sessionState, @Nonnull WellKnownClient wellKnownClient) {
@@ -130,7 +136,8 @@ public PgCatalog(@Nonnull SessionState sessionState, @Nonnull WellKnownClient we
130136
/** Replace supported pg_catalog tables with Common Table Expressions. */
131137
public Statement replacePgCatalogTables(Statement statement) {
132138
// Only replace tables if the statement contains at least one of the known prefixes.
133-
if (checkPrefixes.stream().noneMatch(prefix -> statement.getSql().contains(prefix))) {
139+
String sql = statement.getSql().toLowerCase(Locale.ENGLISH);
140+
if (checkPrefixes.stream().noneMatch(sql::contains)) {
134141
return statement;
135142
}
136143

@@ -514,4 +521,22 @@ public String getTableExpression() {
514521
return PG_SEQUENCE_CTE;
515522
}
516523
}
524+
525+
private static class InformationSchemaSequences implements PgCatalogTable {
526+
// The name of this CTE is a little strange, but that is to make sure it does not accidentally
527+
// collide with any user-defined table or view.
528+
private static final String INFORMATION_SCHEMA_SEQUENCES_CTE =
529+
"pg_information_schema_sequences as (\n"
530+
+ "select * from ("
531+
+ "select ''::varchar as sequence_catalog, ''::varchar as sequence_schema, ''::varchar as sequence_name, "
532+
+ "''::varchar as data_type, 0::bigint as numeric_precision, 0::bigint as numeric_precision_radix, "
533+
+ "''::varchar as start_value, ''::varchar as minimum_value, ''::varchar as maximum_value, "
534+
+ "''::varchar as increment, 'NO'::varchar as cycle_option\n"
535+
+ ") seq where false)";
536+
537+
@Override
538+
public String getTableExpression() {
539+
return INFORMATION_SCHEMA_SEQUENCES_CTE;
540+
}
541+
}
517542
}

src/main/java/com/google/cloud/spanner/pgadapter/utils/ClientAutoDetector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class ClientAutoDetector {
6060
SelectVersionStatement.INSTANCE,
6161
DjangoGetTableNamesStatement.INSTANCE);
6262
private static final ImmutableSet<String> DEFAULT_CHECK_PG_CATALOG_PREFIXES =
63-
ImmutableSet.of("pg_");
63+
ImmutableSet.of("pg_", "information_schema.");
6464
public static final String PGBENCH_USAGE_HINT =
6565
"See https://github.com/GoogleCloudPlatform/pgadapter/blob/-/docs/pgbench.md for how to use pgbench with PGAdapter";
6666

src/test/java/com/google/cloud/spanner/pgadapter/ITJdbcTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,21 @@ public void testPgCatalogViews() throws SQLException {
224224
}
225225
}
226226

227+
@Test
228+
public void testInformationSchemaViews() throws SQLException {
229+
for (String view : new String[] {"sequences"}) {
230+
try (Connection connection = DriverManager.getConnection(getConnectionUrl())) {
231+
try (ResultSet resultSet =
232+
connection.createStatement().executeQuery("SELECT * FROM INFORMATION_SCHEMA." + view)) {
233+
while (resultSet.next()) {
234+
assertNotNull(resultSet.getMetaData());
235+
assertNotEquals(0, resultSet.getMetaData().getColumnCount());
236+
}
237+
}
238+
}
239+
}
240+
}
241+
227242
@Test
228243
public void testCreateTableIfNotExists() throws SQLException {
229244
try (Connection connection = DriverManager.getConnection(getConnectionUrl())) {

0 commit comments

Comments
 (0)