private void checkNonNullArrayOfStruct(int columnIndex, Object columnNameForError) { Type actualType = getColumnType(columnIndex); checkState( actualType.getCode() == Type.Code.ARRAY && actualType.getArrayElementType().getCode() == Type.Code.STRUCT, "Column %s is not of correct type: expected ARRAY<STRUCT<...>> but was %s", columnNameForError, actualType); checkNonNull(columnIndex, columnNameForError); } }
@Override public Struct getStruct() { if (getType().getCode() != Type.Code.STRUCT) { throw new IllegalStateException( "Illegal call to getter of incorrect type. Expected: STRUCT<...> actual: " + getType()); } throw new AssertionError("Should have been overridden"); }
@Override public List<Struct> getStructArray() { if (getType().getCode() != Type.Code.ARRAY || getType().getArrayElementType().getCode() != Type.Code.STRUCT) { throw new IllegalStateException( "Illegal call to getter of incorrect type. Expected: ARRAY<STRUCT<...>> actual: " + getType()); } throw new AssertionError("Should have been overridden"); }
/** Returns a descriptor for an array of {@code elementType}. */ public static Type array(Type elementType) { Preconditions.checkNotNull(elementType); switch (elementType.getCode()) { case BOOL: return TYPE_ARRAY_BOOL; case INT64: return TYPE_ARRAY_INT64; case FLOAT64: return TYPE_ARRAY_FLOAT64; case STRING: return TYPE_ARRAY_STRING; case BYTES: return TYPE_ARRAY_BYTES; case TIMESTAMP: return TYPE_ARRAY_TIMESTAMP; case DATE: return TYPE_ARRAY_DATE; default: return new Type(Code.ARRAY, elementType, null); } }
private void checkNonNullStruct(int columnIndex, Object columnNameForError) { Type actualType = getColumnType(columnIndex); checkState( actualType.getCode() == Code.STRUCT, "Column %s is not of correct type: expected STRUCT<...> but was %s", columnNameForError, actualType); checkNonNull(columnIndex, columnNameForError); }
PrePopulatedResultSet(Type type, Iterable<Struct> rows) { Preconditions.checkNotNull(rows); Preconditions.checkNotNull(type); Preconditions.checkArgument(type.getCode() == Type.Code.STRUCT); for (StructField field : type.getStructFields()) { if (field.getType().getCode() == Code.STRUCT) { throw new UnsupportedOperationException( "STRUCT-typed columns are not supported inside ResultSets."); } } this.type = type; this.rows = rows instanceof List<?> ? (List<Struct>) rows : Lists.newArrayList(rows); for (Struct row : rows) { Preconditions.checkArgument(row.getType().equals(type)); } }
/** * Returns a {@code STRUCT} value of {@code Type} type. * * @param type the type of the {@code STRUCT} value * @param v the struct {@code STRUCT} value. This may be {@code null} to produce a value for which * {@code isNull()} is {@code true}. If non-{@code null}, {@link Struct#getType()} must match * type. */ public static Value struct(Type type, @Nullable Struct v) { if (v == null) { Preconditions.checkArgument( type.getCode() == Code.STRUCT, "Illegal call to create a NULL struct with a non-struct type."); return new StructImpl(type); } else { Preconditions.checkArgument( type.equals(v.getType()), "Mismatch between struct value and type."); return new StructImpl(v); } }
/** * Returns an {@code ARRAY<STRUCT<...>>} value. * * @param elementType * @param v the source of element values. This may be {@code null} to produce a value for which * {@code isNull()} is {@code true}. Individual elements may also be {@code null}. */ public static Value structArray(Type elementType, @Nullable Iterable<Struct> v) { if (v == null) { Preconditions.checkArgument( elementType.getCode() == Code.STRUCT, "Illegal call to create a NULL array-of-struct with a non-struct element type."); return new StructArrayImpl(elementType, null); } List<Struct> values = immutableCopyOf(v); for (Struct value : values) { if (value != null) { Preconditions.checkArgument( value.getType().equals(elementType), "Members of v must have type %s (found %s)", elementType, value.getType()); } } return new StructArrayImpl(elementType, values); }
@Test public void emptyStruct() { Type t = Type.struct(); assertThat(t.getCode()).isEqualTo(Type.Code.STRUCT); assertThat(t.getStructFields()).isEmpty(); assertThat(t.toString()).isEqualTo("STRUCT<>"); assertProtoEquals(t.toProto(), "code: STRUCT struct_type {}"); }
private List<?> getRawList(Struct actualRow, int index, Type elementType) throws Exception { List<?> rawList = null; switch (elementType.getCode()) { case BOOL: rawList = actualRow.getBooleanList(index); break; case STRING: rawList = actualRow.getStringList(index); break; case BYTES: rawList = actualRow.getBytesList(index); break; case INT64: rawList = actualRow.getLongList(index); break; case FLOAT64: rawList = actualRow.getDoubleList(index); break; case STRUCT: rawList = actualRow.getStructList(index); break; default: Assert.fail("Unexpected type code:" + elementType.getCode()); } return rawList; }
void test() { Type t = newType(); assertThat(t.getCode()).isEqualTo(expectedCode); assertThat(newType()).isSameAs(t); // Interned. // String form is deliberately the same as the corresponding type enum in the public API. assertThat(t.toString()).isEqualTo(expectedProtoCode.toString()); com.google.spanner.v1.Type proto = t.toProto(); assertThat(proto.getCode()).isEqualTo(expectedProtoCode); assertThat(proto.hasArrayElementType()).isFalse(); assertThat(proto.hasStructType()).isFalse(); // Round trip. Type fromProto = Type.fromProto(proto); assertThat(fromProto).isEqualTo(t); assertThat(fromProto).isSameAs(t); reserializeAndAssert(t); } }
private void assertRow(Struct actualRow, JSONArray expectedRow) throws Exception { assertThat(actualRow.getColumnCount()).isEqualTo(expectedRow.length()); for (int i = 0; i < expectedRow.length(); i++) { switch (actualRow.getColumnType(i).getCode()) { case BOOL: assertThat(actualRow.getBoolean(i)).isEqualTo(expectedRow.getBoolean(i)); break; case STRING: assertThat(actualRow.getString(i)).isEqualTo(expectedRow.getString(i)); break; case INT64: assertThat(actualRow.getLong(i)).isEqualTo(expectedRow.getLong(i)); break; case FLOAT64: assertThat(actualRow.getDouble(i)).isEqualTo(expectedRow.getDouble(i)); break; case BYTES: assertThat(actualRow.getBytes(i)) .isEqualTo(ByteArray.fromBase64(expectedRow.getString(i))); break; case ARRAY: Type elementType = actualRow.getColumnType(i).getArrayElementType(); assertArray(getRawList(actualRow, i, elementType), expectedRow.getJSONArray(i)); break; default: Assert.fail("Unexpected type code:" + actualRow.getColumnType(i).getCode()); } } }
void test() { Type elementType = newElementType(); Type t = Type.array(elementType); assertThat(t.getCode()).isEqualTo(Type.Code.ARRAY); assertThat(t.getArrayElementType()).isEqualTo(elementType); if (expectInterned) { assertThat(Type.array(newElementType())).isSameAs(t); } assertThat(t.toString()).isEqualTo("ARRAY<" + elementType.toString() + ">"); com.google.spanner.v1.Type proto = t.toProto(); assertThat(proto.getCode()).isEqualTo(TypeCode.ARRAY); assertThat(proto.getArrayElementType()).isEqualTo(elementType.toProto()); assertThat(proto.hasStructType()).isFalse(); Type fromProto = Type.fromProto(proto); assertThat(fromProto).isEqualTo(t); if (expectInterned) { assertThat(fromProto).isSameAs(t); } reserializeAndAssert(t); } }
@Test public void struct() { Type t = Type.struct(StructField.of("f1", Type.int64()), StructField.of("f2", Type.string())); assertThat(t.getCode()).isEqualTo(Type.Code.STRUCT); // Exercise StructField equality. assertThat(t.getStructFields()) .containsExactly(StructField.of("f1", Type.int64()), StructField.of("f2", Type.string())) .inOrder(); // Exercise StructField getters. assertThat(t.getStructFields().get(0).getName()).isEqualTo("f1"); assertThat(t.getStructFields().get(0).getType()).isEqualTo(Type.int64()); assertThat(t.getStructFields().get(1).getName()).isEqualTo("f2"); assertThat(t.getStructFields().get(1).getType()).isEqualTo(Type.string()); assertThat(t.toString()).isEqualTo("STRUCT<f1 INT64, f2 STRING>"); assertThat(t.getFieldIndex("f1")).isEqualTo(0); assertThat(t.getFieldIndex("f2")).isEqualTo(1); assertProtoEquals( t.toProto(), "code: STRUCT struct_type { fields { name: 'f1' type { code: INT64 } }" + " fields { name: 'f2' type { code: STRING } } }"); }
Object value = rowData.get(i); Type fieldType = field.getType(); switch (fieldType.getCode()) { case BOOL: builder.set(fieldName).to((Boolean) value); break; case ARRAY: switch (fieldType.getArrayElementType().getCode()) { case BOOL: builder.set(fieldName).toBoolArray((Iterable<Boolean>) value); throw new AssertionError("Unhandled type code: " + fieldType.getCode());
switch (row.getColumnType(i).getCode()) { case BOOL: sum += 1;