Skip to content

feat: return is_autoincrement for getColumns #1944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ SELECT TABLE_CATALOG AS TABLE_CAT, TABLE_SCHEMA AS TABLE_SCHEM, TABLE_NAME, COLU
NULL AS SCOPE_SCHEMA,
NULL AS SCOPE_TABLE,
NULL AS SOURCE_DATA_TYPE,
'NO' AS IS_AUTOINCREMENT,
IS_IDENTITY AS IS_AUTOINCREMENT,
CASE
WHEN (IS_GENERATED = 'NEVER') THEN 'NO'
ELSE 'YES'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ SELECT TABLE_CATALOG AS "TABLE_CAT", TABLE_SCHEMA AS "TABLE_SCHEM", TABLE_NAME A
NULL AS "SCOPE_SCHEMA",
NULL AS "SCOPE_TABLE",
NULL AS "SOURCE_DATA_TYPE",
'NO' AS "IS_AUTOINCREMENT",
IS_IDENTITY AS "IS_AUTOINCREMENT",
CASE
WHEN (IS_GENERATED = 'NEVER') THEN 'NO'
ELSE 'YES'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ private static final class Column {
private final Integer charOctetLength;
private final boolean computed;
private final String defaultValue;
private final boolean autoIncrement;

private Column(
String name,
Expand All @@ -138,7 +139,8 @@ private Column(
nullable,
charOctetLength,
false,
null);
null,
false);
}

private Column(
Expand All @@ -151,7 +153,8 @@ private Column(
boolean nullable,
Integer charOctetLength,
boolean computed,
String defaultValue) {
String defaultValue,
boolean autoIncrement) {
this.name = name;
this.type = type;
this.typeName = typeName;
Expand All @@ -162,14 +165,17 @@ private Column(
this.charOctetLength = charOctetLength;
this.computed = computed;
this.defaultValue = defaultValue;
this.autoIncrement = autoIncrement;
}
}

private static final List<Column> EXPECTED_COLUMNS =
Arrays.asList(
new Column("ColInt64", Types.BIGINT, "INT64", 19, null, 10, false, null),
new Column("ColFloat64", Types.DOUBLE, "FLOAT64", 15, 16, 2, false, null, false, "0.0"),
new Column("ColFloat32", Types.REAL, "FLOAT32", 15, 16, 2, false, null, false, "0.0"),
new Column(
"ColFloat64", Types.DOUBLE, "FLOAT64", 15, 16, 2, false, null, false, "0.0", false),
new Column(
"ColFloat32", Types.REAL, "FLOAT32", 15, 16, 2, false, null, false, "0.0", false),
new Column("ColBool", Types.BOOLEAN, "BOOL", null, null, null, false, null),
new Column(
"ColString",
Expand All @@ -181,7 +187,8 @@ private Column(
false,
100,
false,
"'Hello World!'"),
"'Hello World!'",
false),
new Column(
"ColStringMax", Types.NVARCHAR, "STRING(MAX)", 2621440, null, null, false, 2621440),
new Column("ColBytes", Types.BINARY, "BYTES(100)", 100, null, null, false, null),
Expand All @@ -196,7 +203,8 @@ private Column(
false,
null,
false,
"DATE '2000-01-01'"),
"DATE '2000-01-01'",
false),
new Column("ColTimestamp", Types.TIMESTAMP, "TIMESTAMP", 35, null, null, false, null),
new Column("ColCommitTS", Types.TIMESTAMP, "TIMESTAMP", 35, null, null, false, null),
new Column("ColNumeric", Types.NUMERIC, "NUMERIC", 15, null, 10, false, null),
Expand Down Expand Up @@ -243,7 +251,10 @@ private Column(
true,
2621440,
true,
null));
null,
false),
new Column(
"ColIdentity", Types.BIGINT, "INT64", 19, null, 10, true, null, false, null, true));

@Test
public void testGetColumns() throws SQLException {
Expand Down Expand Up @@ -301,8 +312,11 @@ public void testGetColumns() throws SQLException {
assertNull(rs.getString("SCOPE_TABLE"));
assertEquals(0, rs.getShort("SOURCE_DATA_TYPE"));
assertTrue(rs.wasNull());
assertEquals("NO", rs.getString("IS_AUTOINCREMENT"));
assertEquals(col.computed ? "YES" : "NO", rs.getString("IS_GENERATEDCOLUMN"));
// TODO: Remove check when the emulator correctly returns IS_IDENTITY
if (!EmulatorSpannerHelper.isUsingEmulator()) {
assertEquals(col.autoIncrement ? "YES" : "NO", rs.getString("IS_AUTOINCREMENT"));
}
assertEquals(24, rs.getMetaData().getColumnCount());
}
assertFalse(rs.next());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,7 @@ public void test09_MetaData_FromQuery() throws SQLException {
try (PreparedStatement ps =
con.prepareStatement("SELECT * FROM TableWithAllColumnTypes WHERE ColInt64=?")) {
ResultSetMetaData metadata = ps.getMetaData();
assertEquals(26, metadata.getColumnCount());
assertEquals(27, metadata.getColumnCount());
int index = 0;
assertEquals("ColInt64", metadata.getColumnLabel(++index));
assertEquals("ColFloat64", metadata.getColumnLabel(++index));
Expand Down Expand Up @@ -1157,6 +1157,7 @@ public void test09_MetaData_FromQuery() throws SQLException {
assertEquals("ColNumericArray", metadata.getColumnLabel(++index));
assertEquals("ColJsonArray", metadata.getColumnLabel(++index));
assertEquals("ColComputed", metadata.getColumnLabel(++index));
assertEquals("ColIdentity", metadata.getColumnLabel(++index));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ CREATE TABLE TableWithAllColumnTypes (
ColJsonArray ARRAY<JSON>,

ColComputed STRING(MAX) AS (CONCAT(COALESCE(ColString, ''), ' ', COALESCE(ColStringMax, ''))) STORED,
ColIdentity INT64 GENERATED BY DEFAULT AS IDENTITY (BIT_REVERSED_POSITIVE),
) PRIMARY KEY (ColInt64)
;

Expand Down