@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"); }
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); } }
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 structArrayNull() { Type elementType = Type.struct( Arrays.asList( Type.StructField.of("ff1", Type.string()), Type.StructField.of("ff2", Type.int64()))); Value v = Value.structArray(elementType, null); assertThat(v.isNull()).isTrue(); assertThat(v.getType().getArrayElementType()).isEqualTo(elementType); assertThat(v.toString()).isEqualTo(NULL_STRING); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Illegal call to getter of null value"); v.getStructArray(); }
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()); } } }
break; case ARRAY: switch (fieldType.getArrayElementType().getCode()) { case BOOL: builder.set(fieldName).toBoolArray((Iterable<Boolean>) value); builder .set(fieldName) .toStructArray(fieldType.getArrayElementType(), (Iterable<Struct>) value); break; default: throw new AssertionError( "Unhandled array type code: " + fieldType.getArrayElementType());
return getStructInternal(columnIndex); case ARRAY: switch (type.getArrayElementType().getCode()) { case BOOL: return getBooleanListInternal(columnIndex);
@Test public void structArray() { Type elementType = Type.struct( Arrays.asList( Type.StructField.of("ff1", Type.string()), Type.StructField.of("ff2", Type.int64()))); List<Struct> arrayElements = Arrays.asList( Struct.newBuilder().set("ff1").to("v1").set("ff2").to(1).build(), null, null, Struct.newBuilder().set("ff1").to("v3").set("ff2").to(3).build()); Value v = Value.structArray(elementType, arrayElements); assertThat(v.isNull()).isFalse(); assertThat(v.getType().getArrayElementType()).isEqualTo(elementType); assertThat(v.getStructArray()).isEqualTo(arrayElements); assertThat(v.toString()).isEqualTo("[[v1, 1],NULL,NULL,[v3, 3]]"); }
checkType(fieldType, proto, KindCase.LIST_VALUE); ListValue listValue = proto.getListValue(); return decodeArrayValue(fieldType.getArrayElementType(), listValue); case STRUCT: checkType(fieldType, proto, KindCase.LIST_VALUE);
case ARRAY: Type elementType = fieldType.getArrayElementType(); switch (elementType.getCode()) { case BOOL:
private static String typeString(Type type, Integer size) { switch (type.getCode()) { case BOOL: return "BOOL"; case INT64: return "INT64"; case FLOAT64: return "FLOAT64"; case STRING: return "STRING(" + (size == -1 ? "MAX" : Integer.toString(size)) + ")"; case BYTES: return "BYTES(" + (size == -1 ? "MAX" : Integer.toString(size)) + ")"; case DATE: return "DATE"; case TIMESTAMP: return "TIMESTAMP"; case ARRAY: Type arrayType = type.getArrayElementType(); return "ARRAY<" + typeString(arrayType, size) + ">"; } throw new IllegalArgumentException("Unknown type " + type); }
private String toString(com.google.cloud.spanner.Type spannerType, boolean supportArray) { switch (spannerType.getCode()) { case BOOL: return "BOOL"; case INT64: return "INT64"; case FLOAT64: return "FLOAT64"; case STRING: return "STRING(MAX)"; case BYTES: return "BYTES(MAX)"; case TIMESTAMP: return "TIMESTAMP"; case DATE: return "DATE"; case ARRAY: { if (supportArray) { Type element = spannerType.getArrayElementType(); String elementStr = toString(element, false); return "ARRAY<" + elementStr + ">"; } // otherwise fall through and throw an error. break; } } throw new IllegalArgumentException("Cannot to string the type " + spannerType); } }
@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"); }
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); } }
List getListValue(String colName) { if (this.struct.getColumnType(colName).getCode() != Code.ARRAY) { throw new SpannerDataException("Column is not an ARRAY type: " + colName); } Type.Code innerTypeCode = this.struct.getColumnType(colName).getArrayElementType().getCode(); Class clazz = SpannerTypeMapper.getSimpleJavaClassFor(innerTypeCode); BiFunction<Struct, String, List> readMethod = readIterableMapping.get(clazz); return readMethod.apply(this.struct, colName); }
List getListValue(String colName) { if (this.struct.getColumnType(colName).getCode() != Code.ARRAY) { throw new SpannerDataException("Column is not an ARRAY type: " + colName); } Type.Code innerTypeCode = this.struct.getColumnType(colName).getArrayElementType().getCode(); Class clazz = SpannerTypeMapper.getSimpleJavaClassFor(innerTypeCode); BiFunction<Struct, String, List> readMethod = readIterableMapping.get(clazz); return readMethod.apply(this.struct, colName); }
private Class getSingleItemTypeCode(Type colType) { Code code = colType.getCode(); return code.equals(Code.ARRAY) ? SpannerTypeMapper.getArrayJavaClassFor(colType.getArrayElementType().getCode()) : SpannerTypeMapper.getSimpleJavaClassFor(code); } }
private Class getSingleItemTypeCode(Type colType) { Code code = colType.getCode(); return code.equals(Code.ARRAY) ? SpannerTypeMapper.getArrayJavaClassFor(colType.getArrayElementType().getCode()) : SpannerTypeMapper.getSimpleJavaClassFor(code); } }
return 0; switch (v.getType().getArrayElementType().getCode()) { case BOOL: return v.getBoolArray().size();
private Schema avroType(Type spannerType) { switch (spannerType.getCode()) { case BOOL: return SchemaBuilder.builder().booleanType(); case INT64: return SchemaBuilder.builder().longType(); case FLOAT64: return SchemaBuilder.builder().doubleType(); case STRING: return SchemaBuilder.builder().stringType(); case BYTES: return SchemaBuilder.builder().bytesType(); case TIMESTAMP: return SchemaBuilder.builder().stringType(); case DATE: return SchemaBuilder.builder().stringType(); case ARRAY: Schema avroItemsType = avroType(spannerType.getArrayElementType()); return SchemaBuilder.builder().array().items().type(wrapAsNullable(avroItemsType)); default: throw new IllegalArgumentException("Unknown spanner type " + spannerType); } }