Skip to content

Commit

Permalink
feat: report whether column is generated in JDBC metadata (#291)
Browse files Browse the repository at this point in the history
Cloud Spanner now supports generated columns. These were reported as normal columns
in the JDBC metadata. These are now reported correctly as generated columns.

Fixes #290
  • Loading branch information
olavloite committed Dec 10, 2020
1 parent ac77715 commit 9aa9a1f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
Expand Up @@ -72,7 +72,10 @@ SELECT TABLE_CATALOG AS TABLE_CAT, TABLE_SCHEMA AS TABLE_SCHEM, TABLE_NAME, COLU
NULL AS SCOPE_TABLE,
NULL AS SOURCE_DATA_TYPE,
'NO' AS IS_AUTOINCREMENT,
'NO' AS IS_GENERATEDCOLUMN
CASE
WHEN (IS_GENERATED = 'NEVER') THEN 'NO'
ELSE 'YES'
END AS IS_GENERATEDCOLUMN
FROM INFORMATION_SCHEMA.COLUMNS C
WHERE UPPER(C.TABLE_CATALOG) LIKE ?
AND UPPER(C.TABLE_SCHEMA) LIKE ?
Expand Down
Expand Up @@ -25,6 +25,7 @@

import com.google.cloud.spanner.IntegrationTest;
import com.google.cloud.spanner.jdbc.ITAbstractJdbcTest;
import com.google.cloud.spanner.testing.EmulatorSpannerHelper;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
Expand Down Expand Up @@ -53,7 +54,8 @@ public class ITJdbcDatabaseMetaDataTest extends ITAbstractJdbcTest {

@BeforeClass
public static void skipOnEmulator() {
assumeFalse("foreign keys are not supported on the emulator", env.getTestHelper().isEmulator());
assumeFalse(
"foreign keys are not supported on the emulator", EmulatorSpannerHelper.isUsingEmulator());
}

@Override
Expand All @@ -70,6 +72,7 @@ private static final class Column {
private final Integer radix;
private final boolean nullable;
private final Integer charOctetLength;
private final boolean computed;

private Column(
String name,
Expand All @@ -80,6 +83,19 @@ private Column(
Integer radix,
boolean nullable,
Integer charOctetLength) {
this(name, type, typeName, colSize, decimalDigits, radix, nullable, charOctetLength, false);
}

private Column(
String name,
int type,
String typeName,
Integer colSize,
Integer decimalDigits,
Integer radix,
boolean nullable,
Integer charOctetLength,
boolean computed) {
this.name = name;
this.type = type;
this.typeName = typeName;
Expand All @@ -88,6 +104,7 @@ private Column(
this.radix = radix;
this.nullable = nullable;
this.charOctetLength = charOctetLength;
this.computed = computed;
}
}

Expand Down Expand Up @@ -133,7 +150,17 @@ private Column(
new Column("ColDateArray", Types.ARRAY, "ARRAY<DATE>", 10, null, null, true, null),
new Column(
"ColTimestampArray", Types.ARRAY, "ARRAY<TIMESTAMP>", 35, null, null, true, null),
new Column("ColNumericArray", Types.ARRAY, "ARRAY<NUMERIC>", 15, null, 10, true, null));
new Column("ColNumericArray", Types.ARRAY, "ARRAY<NUMERIC>", 15, null, 10, true, null),
new Column(
"ColComputed",
Types.NVARCHAR,
"STRING(MAX)",
2621440,
null,
null,
true,
2621440,
true));

@Test
public void testGetColumns() throws SQLException {
Expand Down Expand Up @@ -195,7 +222,7 @@ public void testGetColumns() throws SQLException {
assertThat(rs.getShort("SOURCE_DATA_TYPE"), is(equalTo((short) 0)));
assertThat(rs.wasNull(), is(true));
assertThat(rs.getString("IS_AUTOINCREMENT"), is(equalTo("NO")));
assertThat(rs.getString("IS_GENERATEDCOLUMN"), is(equalTo("NO")));
assertThat(rs.getString("IS_GENERATEDCOLUMN"), is(equalTo(col.computed ? "YES" : "NO")));
assertThat(rs.getMetaData().getColumnCount(), is(equalTo(24)));

pos++;
Expand Down
Expand Up @@ -84,7 +84,9 @@ CREATE TABLE TableWithAllColumnTypes (
ColBytesMaxArray ARRAY<BYTES(MAX)>,
ColDateArray ARRAY<DATE>,
ColTimestampArray ARRAY<TIMESTAMP>,
ColNumericArray ARRAY<NUMERIC>
ColNumericArray ARRAY<NUMERIC>,

ColComputed STRING(MAX) AS (CONCAT(COALESCE(ColString, ''), ' ', COALESCE(ColStringMax, ''))) STORED,
) PRIMARY KEY (ColInt64)
;

Expand Down

0 comments on commit 9aa9a1f

Please sign in to comment.