private Struct execute(Statement statement, Type expectedColumnType) { Type rowType = Type.struct(StructField.of("", expectedColumnType)); return executeWithRowResultType(statement, rowType); }
@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 bindStructWithUnnamedFields() { Struct p = Struct.newBuilder().add(Value.int64(1337)).add(Value.int64(7331)).build(); Struct row = executeWithRowResultType( Statement.newBuilder("SELECT * FROM UNNEST([@p])").bind("p").to(p).build(), p.getType()); assertThat(row.getLong(0)).isEqualTo(1337); assertThat(row.getLong(1)).isEqualTo(7331); }
@Test public void bindStructWithStructField() { Struct nestedStruct = Struct.newBuilder().set("ff1").to("abc").build(); Struct p = Struct.newBuilder().set("f1").to(nestedStruct).build(); Struct row = executeWithRowResultType( Statement.newBuilder("SELECT @p.f1.ff1").bind("p").to(p).build(), nestedStruct.getType()); assertThat(row.getString(0)).isEqualTo("abc"); }
@Test public void bindStructWithDuplicateFieldNames() { Struct p = Struct.newBuilder() .set("f1") .to(Value.int64(1337)) .set("f1") .to(Value.string("1337")) .build(); Struct row = executeWithRowResultType( Statement.newBuilder("SELECT * FROM UNNEST([@p])").bind("p").to(p).build(), p.getType()); assertThat(row.getLong(0)).isEqualTo(1337); assertThat(row.getString(1)).isEqualTo("1337"); }