Search in sources :

Example 1 with Type

use of com.google.cloud.spanner.Type in project google-cloud-java by GoogleCloudPlatform.

the class ITQueryTest method arrayOfStruct.

@Test
public void arrayOfStruct() {
    Type structType = Type.struct(StructField.of("C1", Type.string()), StructField.of("C2", Type.int64()));
    Struct row = execute(Statement.of("SELECT ARRAY(SELECT AS STRUCT C1, C2 " + "FROM (SELECT 'a' AS C1, 1 AS C2 UNION ALL SELECT 'b' AS C1, 2 AS C2) " + "ORDER BY C1 ASC)"), Type.array(structType));
    assertThat(row.isNull(0)).isFalse();
    List<Struct> value = row.getStructList(0);
    assertThat(value.size()).isEqualTo(2);
    assertThat(value.get(0).getType()).isEqualTo(structType);
    assertThat(value.get(0).getString(0)).isEqualTo("a");
    assertThat(value.get(0).getLong(1)).isEqualTo(1);
    assertThat(value.get(1).getType()).isEqualTo(structType);
    assertThat(value.get(1).getString(0)).isEqualTo("b");
    assertThat(value.get(1).getLong(1)).isEqualTo(2);
    // Also confirm that an STRUCT<ARRAY<STRUCT>> implements equality correctly with respect to
    // a manually constructed Struct.
    Struct expectedRow = Struct.newBuilder().add("", Arrays.asList(StructField.of("C1", Type.string()), StructField.of("C2", Type.int64())), Arrays.asList(Struct.newBuilder().set("C1").to("a").set("C2").to(1).build(), Struct.newBuilder().set("C1").to("b").set("C2").to(2).build())).build();
    assertThat(row).isEqualTo(expectedRow);
}
Also used : Type(com.google.cloud.spanner.Type) Struct(com.google.cloud.spanner.Struct) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 2 with Type

use of com.google.cloud.spanner.Type in project google-cloud-java by GoogleCloudPlatform.

the class ITQueryTest method arrayOfStructEmpty.

@Test
public void arrayOfStructEmpty() {
    Type structType = Type.struct(StructField.of("", Type.string()), StructField.of("", Type.int64()));
    Struct row = execute(Statement.of("SELECT ARRAY(SELECT AS STRUCT * FROM (SELECT 'a', 1) WHERE 0 = 1)"), Type.array(structType));
    assertThat(row.isNull(0)).isFalse();
    List<Struct> value = row.getStructList(0);
    assertThat(value.size()).isEqualTo(0);
}
Also used : Type(com.google.cloud.spanner.Type) Struct(com.google.cloud.spanner.Struct) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 3 with Type

use of com.google.cloud.spanner.Type in project google-cloud-java by GoogleCloudPlatform.

the class ITQueryTest method arrayOfStructNullElement.

// Not yet supported by the backend.
@Ignore
@Test
public void arrayOfStructNullElement() {
    Type structType = Type.struct(StructField.of("", Type.string()), StructField.of("", Type.int64()));
    Struct row = execute(Statement.of("SELECT ARRAY(SELECT AS STRUCT 'a', 1" + " UNION ALL SELECT CAST (NULL AS STRUCT<string,int64>))"), Type.array(structType));
    assertThat(row.isNull(0)).isFalse();
    List<Struct> value = row.getStructList(0);
    assertThat(value.size()).isEqualTo(2);
    assertThat(value.get(0).getType()).isEqualTo(structType);
    assertThat(value.get(0).getString(0)).isEqualTo("a");
    assertThat(value.get(0).getLong(1)).isEqualTo(1);
    assertThat(value.get(1)).isNull();
}
Also used : Type(com.google.cloud.spanner.Type) Struct(com.google.cloud.spanner.Struct) Ignore(org.junit.Ignore) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 4 with Type

use of com.google.cloud.spanner.Type in project google-cloud-java by GoogleCloudPlatform.

the class ITQueryTest method arrayOfStructNull.

// Not yet supported by the backend.
@Ignore
@Test
public void arrayOfStructNull() {
    Type structType = Type.struct(StructField.of("", Type.string()), StructField.of("", Type.int64()));
    Struct row = execute(Statement.of("SELECT CAST (NULL AS ARRAY<STRUCT<string,int64>>)"), Type.array(structType));
    assertThat(row.isNull(0)).isTrue();
}
Also used : Type(com.google.cloud.spanner.Type) Struct(com.google.cloud.spanner.Struct) Ignore(org.junit.Ignore) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Aggregations

IntegrationTest (com.google.cloud.spanner.IntegrationTest)4 Struct (com.google.cloud.spanner.Struct)4 Type (com.google.cloud.spanner.Type)4 Test (org.junit.Test)4 Ignore (org.junit.Ignore)2