Skip to content

Commit

Permalink
Add TOKENLIST spanner type (#1997)
Browse files Browse the repository at this point in the history
* Add TOKENLIST spanner type

* Resolve comment

* Extend unit tests for types
  • Loading branch information
Deep1998 authored Nov 8, 2024
1 parent a4bf9d4 commit cb9b943
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ private static String typeString(Type type, Integer size) {
return Type.Code.JSON.getName();
case PG_JSONB:
return Type.Code.PG_JSONB.getName();
case TOKENLIST:
return Type.Code.TOKENLIST.getName();
case ARRAY:
{
Type arrayType = type.getArrayElementType();
Expand Down Expand Up @@ -367,6 +369,9 @@ private static SizedType parseSpannerType(String spannerType, Dialect dialect) {
if (spannerType.equals(Type.Code.JSON.getName())) {
return t(Type.json(), null);
}
if (spannerType.equals(Type.Code.TOKENLIST.getName())) {
return t(Type.tokenlist(), null);
}
if (spannerType.startsWith(Type.Code.ARRAY.getName())) {
// Substring "ARRAY<"xxx">"
String spannerArrayType = spannerType.substring(6, spannerType.length() - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ List<String> getSpannerColumns(String namespace, String spannerTable)
String getSyntheticPrimaryKeyColName(String namespace, String spannerTableName);

/**
* Returns true if the Spanner column exists at the source.
* Returns true if a corresponding source column exists for the provided Spanner column.
*
* @param namespace is currently not operational.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public final class Type implements Serializable {
private static final Type TYPE_NUMERIC = new Type(Type.Code.NUMERIC, null, null);
private static final Type TYPE_STRING = new Type(Type.Code.STRING, null, null);
private static final Type TYPE_JSON = new Type(Type.Code.JSON, null, null);
private static final Type TYPE_TOKENLIST = new Type(Code.TOKENLIST, null, null);
private static final Type TYPE_BYTES = new Type(Type.Code.BYTES, null, null);
private static final Type TYPE_TIMESTAMP = new Type(Type.Code.TIMESTAMP, null, null);
private static final Type TYPE_DATE = new Type(Type.Code.DATE, null, null);
Expand Down Expand Up @@ -133,6 +134,11 @@ public static Type json() {
return TYPE_JSON;
}

/** Returns the descriptor for the {@code TOKENLIST} type. */
public static Type tokenlist() {
return TYPE_TOKENLIST;
}

/** Returns the descriptor for the {@code BYTES} type: a variable-length byte string. */
public static Type bytes() {
return TYPE_BYTES;
Expand Down Expand Up @@ -306,6 +312,8 @@ public enum Code {
FLOAT64("FLOAT64", Dialect.GOOGLE_STANDARD_SQL),
STRING("STRING", Dialect.GOOGLE_STANDARD_SQL),
JSON("JSON", Dialect.GOOGLE_STANDARD_SQL),
// This type is not supported on PG Spanner.
TOKENLIST("TOKENLIST", Dialect.GOOGLE_STANDARD_SQL),
BYTES("BYTES", Dialect.GOOGLE_STANDARD_SQL),
TIMESTAMP("TIMESTAMP", Dialect.GOOGLE_STANDARD_SQL),
DATE("DATE", Dialect.GOOGLE_STANDARD_SQL),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,22 @@
public class TypeTest {
@Test
public void testToString() {
// Primitive types
// Google Standard SQL primitive types
assertEquals("BOOL", Type.bool().toString());
assertEquals("INT64", Type.int64().toString());
assertEquals("NUMERIC", Type.numeric().toString());
assertEquals("FLOAT32", Type.float32().toString());
assertEquals("FLOAT64", Type.float64().toString());
assertEquals("STRING", Type.string().toString());
assertEquals("JSON", Type.json().toString());
assertEquals("TOKENLIST", Type.tokenlist().toString());
assertEquals("BYTES", Type.bytes().toString());
assertEquals("TIMESTAMP", Type.timestamp().toString());
assertEquals("DATE", Type.date().toString());

// Array types
assertEquals("ARRAY<INT64>", Type.array(Type.int64()).toString());
assertEquals("ARRAY<STRING>", Type.array(Type.string()).toString());
assertEquals("ARRAY<ARRAY<STRING>>", Type.array(Type.array(Type.string())).toString());

// Struct type
Expand All @@ -42,6 +50,16 @@ public void testToString() {
// PG types
assertEquals("PG_BOOL", Type.pgBool().toString());
assertEquals("PG_INT8", Type.pgInt8().toString());
assertEquals("PG_FLOAT4", Type.pgFloat4().toString());
assertEquals("PG_FLOAT8", Type.pgFloat8().toString());
assertEquals("PG_TEXT", Type.pgText().toString());
assertEquals("PG_VARCHAR", Type.pgVarchar().toString());
assertEquals("PG_NUMERIC", Type.pgNumeric().toString());
assertEquals("PG_JSONB", Type.pgJsonb().toString());
assertEquals("PG_BYTEA", Type.pgBytea().toString());
assertEquals("PG_TIMESTAMPTZ", Type.pgTimestamptz().toString());
assertEquals("PG_DATE", Type.pgDate().toString());
assertEquals("PG_TEXT[]", Type.pgArray(Type.pgText()).toString());
assertEquals("PG_COMMIT_TIMESTAMP", Type.pgCommitTimestamp().toString());
}
}

0 comments on commit cb9b943

Please sign in to comment.