Search in sources :

Example 41 with ResultSet

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

the class ITWriteTest method writeBytesRandom.

@Test
public void writeBytesRandom() {
    // Pseudo-random test for byte encoding.  We explicitly set a random seed so that multiple
    // test runs cover more data, but any failing test run can be reproduced easily.
    Random rnd = new Random();
    long seed = rnd.nextLong();
    rnd.setSeed(seed);
    Map<String, ByteArray> expected = new HashMap<>();
    boolean pass = false;
    try {
        for (int length : new int[] { 1, 2, 5, 11 }) {
            byte[] data = new byte[length];
            for (int i = 0; i < 3; ++i) {
                rnd.nextBytes(data);
                String key = uniqueString();
                ByteArray value = ByteArray.copyFrom(data);
                expected.put(key, value);
                write(Mutation.newInsertOrUpdateBuilder("T").set("K").to(key).set("BytesValue").to(value).build());
            }
        }
        KeySet.Builder keys = KeySet.newBuilder();
        for (String key : expected.keySet()) {
            keys.addKey(Key.of(key));
        }
        ResultSet resultSet = client.singleUse(TimestampBound.strong()).read("T", keys.build(), Arrays.asList("K", "BytesValue"));
        while (resultSet.next()) {
            String key = resultSet.getString(0);
            ByteArray value = resultSet.getBytes(1);
            assertThat(expected).containsKey(key);
            ByteArray expectedValue = expected.remove(key);
            assertThat(value).isEqualTo(expectedValue);
        }
        assertThat(expected).isEmpty();
        pass = true;
    } finally {
        if (!pass) {
            System.out.println("To reproduce failure, use seed " + seed);
        }
    }
}
Also used : KeySet(com.google.cloud.spanner.KeySet) Random(java.util.Random) HashMap(java.util.HashMap) ResultSet(com.google.cloud.spanner.ResultSet) ByteArray(com.google.cloud.ByteArray) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 42 with ResultSet

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

the class ITQueryTest method queryRealTable.

@Test
public void queryRealTable() {
    Database populatedDb = env.getTestHelper().createTestDatabase("CREATE TABLE T ( K STRING(MAX) NOT NULL, V STRING(MAX) ) PRIMARY KEY (K)");
    DatabaseClient client = env.getTestHelper().getDatabaseClient(populatedDb);
    client.writeAtLeastOnce(Arrays.asList(Mutation.newInsertBuilder("T").set("K").to("k1").set("V").to("v1").build(), Mutation.newInsertBuilder("T").set("K").to("k2").set("V").to("v2").build(), Mutation.newInsertBuilder("T").set("K").to("k3").set("V").to("v3").build(), Mutation.newInsertBuilder("T").set("K").to("k4").set("V").to("v4").build()));
    Statement statement = Statement.newBuilder("SELECT K, V FROM T WHERE K >= @min AND K < @max ORDER BY K ASC").bind("min").to("k13").bind("max").to("k32").build();
    ResultSet resultSet = statement.executeQuery(client.singleUse(TimestampBound.strong()));
    assertThat(resultSet.next()).isTrue();
    assertThat(resultSet.getType()).isEqualTo(Type.struct(StructField.of("K", Type.string()), StructField.of("V", Type.string())));
    assertThat(resultSet.getString(0)).isEqualTo("k2");
    assertThat(resultSet.getString(1)).isEqualTo("v2");
    assertThat(resultSet.next()).isTrue();
    assertThat(resultSet.getString("K")).isEqualTo("k3");
    assertThat(resultSet.getString("V")).isEqualTo("v3");
    assertThat(resultSet.next()).isFalse();
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) Statement(com.google.cloud.spanner.Statement) Database(com.google.cloud.spanner.Database) ResultSet(com.google.cloud.spanner.ResultSet) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 43 with ResultSet

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

the class ITQueryTest method analyzePlan.

@Test
public void analyzePlan() {
    Statement statement = Statement.of("SELECT 1 AS column UNION ALL SELECT 2");
    ResultSet resultSet = statement.analyzeQuery(client.singleUse(TimestampBound.strong()), QueryAnalyzeMode.PLAN);
    assertThat(resultSet.next()).isFalse();
    assertThat(resultSet.getType()).isEqualTo(Type.struct(StructField.of("column", Type.int64())));
    ResultSetStats receivedStats = resultSet.getStats();
    assertThat(receivedStats).isNotNull();
    assertThat(receivedStats.hasQueryPlan()).isTrue();
    assertThat(receivedStats.hasQueryStats()).isFalse();
}
Also used : ResultSetStats(com.google.spanner.v1.ResultSetStats) Statement(com.google.cloud.spanner.Statement) ResultSet(com.google.cloud.spanner.ResultSet) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 44 with ResultSet

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

the class ITQueryTest method analyzeProfile.

@Test
public void analyzeProfile() {
    Statement statement = Statement.of("SELECT 1 AS column UNION ALL SELECT 2 AS column ORDER BY column");
    ResultSet resultSet = statement.analyzeQuery(client.singleUse(TimestampBound.strong()), QueryAnalyzeMode.PROFILE);
    assertThat(resultSet.next()).isTrue();
    assertThat(resultSet.getType()).isEqualTo(Type.struct(StructField.of("column", Type.int64())));
    assertThat(resultSet.getLong(0)).isEqualTo(1);
    assertThat(resultSet.next()).isTrue();
    assertThat(resultSet.getLong(0)).isEqualTo(2);
    assertThat(resultSet.next()).isFalse();
    ResultSetStats receivedStats = resultSet.getStats();
    assertThat(receivedStats).isNotNull();
    assertThat(receivedStats.hasQueryPlan()).isTrue();
    assertThat(receivedStats.hasQueryStats()).isTrue();
}
Also used : ResultSetStats(com.google.spanner.v1.ResultSetStats) Statement(com.google.cloud.spanner.Statement) ResultSet(com.google.cloud.spanner.ResultSet) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 45 with ResultSet

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

the class ITQueryTest method unboundParameter.

@Test
public void unboundParameter() {
    ResultSet resultSet = Statement.of("SELECT @v").executeQuery(client.singleUse(TimestampBound.strong()));
    expectedException.expect(isSpannerException(ErrorCode.INVALID_ARGUMENT));
    expectedException.expectMessage("No parameter found for binding: v");
    resultSet.next();
}
Also used : ResultSet(com.google.cloud.spanner.ResultSet) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Aggregations

ResultSet (com.google.cloud.spanner.ResultSet)71 Test (org.junit.Test)25 Statement (com.google.cloud.spanner.Statement)15 Struct (com.google.cloud.spanner.Struct)12 ArrayList (java.util.ArrayList)12 KeySet (com.google.cloud.spanner.KeySet)11 IntegrationTest (com.google.cloud.spanner.IntegrationTest)10 ReadContext (com.google.cloud.spanner.ReadContext)10 DatabaseClient (com.google.cloud.spanner.DatabaseClient)8 BatchReadOnlyTransaction (com.google.cloud.spanner.BatchReadOnlyTransaction)5 Mutation (com.google.cloud.spanner.Mutation)5 Partition (com.google.cloud.spanner.Partition)5 ReadOnlyTransaction (com.google.cloud.spanner.ReadOnlyTransaction)5 HashMap (java.util.HashMap)5 Key (com.google.cloud.spanner.Key)4 ReadOption (com.google.cloud.spanner.Options.ReadOption)4 Spanner (com.google.cloud.spanner.Spanner)4 SpannerOptions (com.google.cloud.spanner.SpannerOptions)4 TransactionContext (com.google.cloud.spanner.TransactionContext)4 Joiner (com.google.common.base.Joiner)4