Search in sources :

Example 16 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 17 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 18 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 19 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 20 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)24 Test (org.junit.Test)12 IntegrationTest (com.google.cloud.spanner.IntegrationTest)10 Statement (com.google.cloud.spanner.Statement)7 KeySet (com.google.cloud.spanner.KeySet)3 Mutation (com.google.cloud.spanner.Mutation)3 DBException (com.yahoo.ycsb.DBException)3 HashMap (java.util.HashMap)3 ByteArray (com.google.cloud.ByteArray)2 DatabaseClient (com.google.cloud.spanner.DatabaseClient)2 Struct (com.google.cloud.spanner.Struct)2 TransactionContext (com.google.cloud.spanner.TransactionContext)2 Joiner (com.google.common.base.Joiner)2 ResultSetStats (com.google.spanner.v1.ResultSetStats)2 ByteIterator (com.yahoo.ycsb.ByteIterator)2 StringByteIterator (com.yahoo.ycsb.StringByteIterator)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 Database (com.google.cloud.spanner.Database)1 SpannerException (com.google.cloud.spanner.SpannerException)1