Skip to content

test: create prototype for values with unrecognized types #2095

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -38,6 +38,7 @@
import com.google.common.util.concurrent.Uninterruptibles;
import com.google.protobuf.ByteString;
import com.google.protobuf.ListValue;
import com.google.protobuf.NullValue;
import com.google.protobuf.Value.KindCase;
import com.google.spanner.v1.PartialResultSet;
import com.google.spanner.v1.ResultSetMetadata;
Expand Down Expand Up @@ -520,6 +521,8 @@ private static Object decodeValue(Type fieldType, com.google.protobuf.Value prot
checkType(fieldType, proto, KindCase.LIST_VALUE);
ListValue structValue = proto.getListValue();
return decodeStructValue(fieldType, structValue);
case UNRECOGNIZED:
return proto;
default:
throw new AssertionError("Unhandled type code: " + fieldType.getCode());
}
Expand Down Expand Up @@ -736,6 +739,13 @@ protected Value getValueInternal(int columnIndex) {
return Value.date(isNull ? null : getDateInternal(columnIndex));
case STRUCT:
return Value.struct(isNull ? null : getStructInternal(columnIndex));
case UNRECOGNIZED:
return Value.untyped(
isNull
? com.google.protobuf.Value.newBuilder()
.setNullValue(NullValue.NULL_VALUE)
.build()
: (com.google.protobuf.Value) rowData.get(columnIndex));
case ARRAY:
final Type elementType = columnType.getArrayElementType();
switch (elementType.getCode()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,15 @@ public final class Type implements Serializable {
private static final Type TYPE_ARRAY_TIMESTAMP = new Type(Code.ARRAY, TYPE_TIMESTAMP, null);
private static final Type TYPE_ARRAY_DATE = new Type(Code.ARRAY, TYPE_DATE, null);

private static final Type TYPE_UNRECOGNIZED = new Type(Code.UNRECOGNIZED, null, null);

private static final int AMBIGUOUS_FIELD = -1;
private static final long serialVersionUID = -3076152125004114582L;

static Type unrecognized() {
return TYPE_UNRECOGNIZED;
}

/** Returns the descriptor for the {@code BOOL type}. */
public static Type bool() {
return TYPE_BOOL;
Expand Down Expand Up @@ -211,6 +217,7 @@ private Type(

/** Enumerates the categories of types. */
public enum Code {
UNRECOGNIZED(TypeCode.UNRECOGNIZED),
BOOL(TypeCode.BOOL),
INT64(TypeCode.INT64),
NUMERIC(TypeCode.NUMERIC),
Expand Down Expand Up @@ -442,6 +449,8 @@ com.google.spanner.v1.Type toProto() {
static Type fromProto(com.google.spanner.v1.Type proto) {
Code type = Code.fromProto(proto.getCode(), proto.getTypeAnnotation());
switch (type) {
case UNRECOGNIZED:
return unrecognized();
case BOOL:
return bool();
case INT64:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,14 @@ public String toString() {
return b.toString();
}

/**
* Returns this value as a raw string representation. This is guaranteed to work for all types,
* regardless of the underlying data type.
*/
public String getAsString() {
return toString();
}

// END OF PUBLIC API.

static com.google.protobuf.Value toProto(Value value) {
Expand Down Expand Up @@ -1053,6 +1061,30 @@ public double getFloat64() {
return value.getNumberValue();
}

@Override
public String getAsString() {
switch (value.getKindCase()) {
case NULL_VALUE:
return "null";
case NUMBER_VALUE:
return Double.toString(value.getNumberValue());
case STRING_VALUE:
return value.getStringValue();
case BOOL_VALUE:
return Boolean.toString(value.getBoolValue());
case LIST_VALUE:
return value.getListValue().getValuesList().stream()
.map(element -> Value.untyped(element).getAsString())
.collect(Collectors.joining(",", "[", "]"));
case STRUCT_VALUE:
throw new IllegalArgumentException(
"Struct value with unrecognized type is not supported");
case KIND_NOT_SET:
default:
throw new IllegalArgumentException("Kind of value is not set or unknown");
}
}

@Override
void valueToString(StringBuilder b) {
b.append(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static com.google.cloud.spanner.SpannerApiFutures.get;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
Expand Down Expand Up @@ -53,6 +54,8 @@
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.SettableFuture;
import com.google.protobuf.AbstractMessage;
import com.google.protobuf.ListValue;
import com.google.protobuf.NullValue;
import com.google.spanner.v1.CommitRequest;
import com.google.spanner.v1.DeleteSessionRequest;
import com.google.spanner.v1.ExecuteBatchDmlRequest;
Expand All @@ -61,6 +64,10 @@
import com.google.spanner.v1.ExecuteSqlRequest.QueryOptions;
import com.google.spanner.v1.ReadRequest;
import com.google.spanner.v1.RequestOptions.Priority;
import com.google.spanner.v1.ResultSetMetadata;
import com.google.spanner.v1.StructType;
import com.google.spanner.v1.StructType.Field;
import com.google.spanner.v1.Type;
import io.grpc.Context;
import io.grpc.Server;
import io.grpc.Status;
Expand Down Expand Up @@ -2327,4 +2334,94 @@ public void testGetDatabaseRole() {
spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
assertEquals(TEST_DATABASE_ROLE, client.getDatabaseRole());
}

@Test
public void testUnknownType() {
mockSpanner.putStatementResult(
StatementResult.query(
Statement.of("SELECT * FROM foo"),
com.google.spanner.v1.ResultSet.newBuilder()
.setMetadata(
ResultSetMetadata.newBuilder()
.setRowType(
StructType.newBuilder()
.addFields(
Field.newBuilder()
.setName("c")
.setType(
Type.newBuilder()
.setCodeValue(Integer.MAX_VALUE)
.build())
.build())
.build())
.build())
.addRows(
ListValue.newBuilder()
.addValues(
com.google.protobuf.Value.newBuilder().setStringValue("bar").build())
.build())
.addRows(
ListValue.newBuilder()
.addValues(
com.google.protobuf.Value.newBuilder().setBoolValue(true).build())
.build())
.addRows(
ListValue.newBuilder()
.addValues(
com.google.protobuf.Value.newBuilder().setNumberValue(3.14d).build())
.build())
.addRows(
ListValue.newBuilder()
.addValues(
com.google.protobuf.Value.newBuilder()
.setNullValue(NullValue.NULL_VALUE)
.build())
.build())
.addRows(
ListValue.newBuilder()
.addValues(
com.google.protobuf.Value.newBuilder()
.setListValue(
ListValue.newBuilder()
.addValues(
com.google.protobuf.Value.newBuilder()
.setStringValue("baz")
.build())
.addValues(
com.google.protobuf.Value.newBuilder()
.setBoolValue(false)
.build())
.addValues(
com.google.protobuf.Value.newBuilder()
.setNumberValue(6.626)
.build())
.addValues(
com.google.protobuf.Value.newBuilder()
.setNullValue(NullValue.NULL_VALUE)
.build())
.build())
.build())
.build())
.build()));
DatabaseClient client =
spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
try (ResultSet resultSet = client.singleUse().executeQuery(Statement.of("SELECT * FROM foo"))) {
assertTrue(resultSet.next());
assertEquals("bar", resultSet.getValue("c").getAsString());

assertTrue(resultSet.next());
assertEquals("true", resultSet.getValue("c").getAsString());

assertTrue(resultSet.next());
assertEquals("3.14", resultSet.getValue("c").getAsString());

assertTrue(resultSet.next());
assertEquals("null", resultSet.getValue("c").getAsString());

assertTrue(resultSet.next());
assertEquals("[baz,false,6.626,null]", resultSet.getValue("c").getAsString());

assertFalse(resultSet.next());
}
}
}