@Test public void bindArrayOfStruct() { Struct arrayElement = structValue(); List<Struct> p = asList(arrayElement, null); List<Struct> rows = resultRows( Statement.newBuilder("SELECT * FROM UNNEST(@p)") .bind("p") .toStructArray(arrayElement.getType(), p) .build(), arrayElement.getType()); assertThat(rows).hasSize(p.size()); assertThat(rows.get(0)).isEqualTo(p.get(0)); // Field accesses on a null struct element (because of SELECT *) return null values. Struct structElementFromNull = rows.get(1); // assertThat(structElementFromNull.isNull()).isFalse(); for (int i = 0; i < arrayElement.getType().getStructFields().size(); ++i) { assertThat(structElementFromNull.isNull(i)).isTrue(); } }
@Test public void bindStruct() { Struct p = structValue(); String query = "SELECT " + "@p.f_int," + "@p.f_bool," + "@p.f_double," + "@p.f_timestamp," + "@p.f_date," + "@p.f_string," + "@p.f_bytes"; Struct row = executeWithRowResultType(Statement.newBuilder(query).bind("p").to(p).build(), p.getType()); assertThat(row).isEqualTo(p); }
@Test public void unsupportedSelectArrayStructValue() { Struct p = structValue(); expectedException.expect(isSpannerException(ErrorCode.UNIMPLEMENTED)); expectedException.expectMessage( "Unsupported query shape: " + "This query can return a null-valued array of struct, " + "which is not supported by Spanner."); execute( Statement.newBuilder("SELECT @p").bind("p").toStructArray(p.getType(), asList(p)).build(), p.getType()); }
@Test public void unsupportedSelectStructValue() { Struct p = structValue(); expectedException.expect(isSpannerException(ErrorCode.UNIMPLEMENTED)); expectedException.expectMessage( "Unsupported query shape: " + "A struct value cannot be returned as a column value."); execute(Statement.newBuilder("SELECT @p").bind("p").to(p).build(), p.getType()); }