Search in sources :

Example 1 with QueryAnalyzeMode

use of com.google.cloud.spanner.ReadContext.QueryAnalyzeMode in project java-spanner by googleapis.

the class AbstractConnectionImplTest method testAnalyzeQuery.

private void testAnalyzeQuery(final StatementType type) {
    // TODO: add log statements when ANALYZE ... sql statements are supported
    try (Connection connection = getConnection()) {
        for (QueryAnalyzeMode mode : QueryAnalyzeMode.values()) {
            final QueryAnalyzeMode currentMode = mode;
            if (type == StatementType.QUERY && isExecuteAllowed(StatementType.QUERY)) {
                ResultSet rs = connection.analyzeQuery(getTestStatement(type), currentMode);
                assertThat(rs, is(notNullValue()));
                while (rs.next()) {
                }
                assertThat(rs.getStats(), is(notNullValue()));
            } else if (type == StatementType.QUERY) {
                // it is a query, but queries are not allowed for this connection state
                expectSpannerException(type + " should not be allowed", t -> t.analyzeQuery(getTestStatement(type), currentMode), connection, ErrorCode.FAILED_PRECONDITION);
            } else {
                expectSpannerException(type + " should be an invalid argument", t -> t.analyzeQuery(getTestStatement(type), currentMode), connection, ErrorCode.INVALID_ARGUMENT);
            }
        }
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) QueryAnalyzeMode(com.google.cloud.spanner.ReadContext.QueryAnalyzeMode) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) ReadOnlyStalenessUtil.getTimeUnitAbbreviation(com.google.cloud.spanner.connection.ReadOnlyStalenessUtil.getTimeUnitAbbreviation) RunWith(org.junit.runner.RunWith) Timestamp(com.google.cloud.Timestamp) StatementType(com.google.cloud.spanner.connection.AbstractStatementParser.StatementType) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) ResultSet(com.google.cloud.spanner.ResultSet) OutputStreamWriter(java.io.OutputStreamWriter) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ExpectedException(org.junit.rules.ExpectedException) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) PrintWriter(java.io.PrintWriter) SpannerExceptionMatcher.matchCode(com.google.cloud.spanner.connection.SpannerExceptionMatcher.matchCode) AfterClass(org.junit.AfterClass) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Test(org.junit.Test) Mutation(com.google.cloud.spanner.Mutation) JUnit4(org.junit.runners.JUnit4) TimestampBound(com.google.cloud.spanner.TimestampBound) StandardCharsets(java.nio.charset.StandardCharsets) ErrorCode(com.google.cloud.spanner.ErrorCode) SpannerException(com.google.cloud.spanner.SpannerException) Statement(com.google.cloud.spanner.Statement) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Rule(org.junit.Rule) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) QueryAnalyzeMode(com.google.cloud.spanner.ReadContext.QueryAnalyzeMode) ResultSet(com.google.cloud.spanner.ResultSet)

Example 2 with QueryAnalyzeMode

use of com.google.cloud.spanner.ReadContext.QueryAnalyzeMode in project java-spanner by googleapis.

the class ITReadOnlySpannerTest method testAnalyzeQuery.

@Test
public void testAnalyzeQuery() {
    assumeFalse("analyze query is not supported on the emulator", isUsingEmulator());
    try (ITConnection connection = createConnection()) {
        for (QueryAnalyzeMode mode : QueryAnalyzeMode.values()) {
            try (ResultSet rs = connection.analyzeQuery(Statement.of("SELECT (SELECT COUNT(*) FROM PRIME_NUMBERS)/(SELECT COUNT(*) FROM NUMBERS) AS PRIME_NUMBER_RATIO"), mode)) {
                // next has not yet returned false
                assertThat(rs.getStats(), is(nullValue()));
                while (rs.next()) {
                // ignore
                }
                assertThat(rs.getStats(), is(notNullValue()));
            }
        }
    }
}
Also used : QueryAnalyzeMode(com.google.cloud.spanner.ReadContext.QueryAnalyzeMode) ResultSet(com.google.cloud.spanner.ResultSet) ParallelIntegrationTest(com.google.cloud.spanner.ParallelIntegrationTest) Test(org.junit.Test) ITAbstractSpannerTest(com.google.cloud.spanner.connection.ITAbstractSpannerTest)

Aggregations

QueryAnalyzeMode (com.google.cloud.spanner.ReadContext.QueryAnalyzeMode)2 ResultSet (com.google.cloud.spanner.ResultSet)2 Test (org.junit.Test)2 Timestamp (com.google.cloud.Timestamp)1 ErrorCode (com.google.cloud.spanner.ErrorCode)1 Mutation (com.google.cloud.spanner.Mutation)1 ParallelIntegrationTest (com.google.cloud.spanner.ParallelIntegrationTest)1 SpannerException (com.google.cloud.spanner.SpannerException)1 Statement (com.google.cloud.spanner.Statement)1 TimestampBound (com.google.cloud.spanner.TimestampBound)1 StatementType (com.google.cloud.spanner.connection.AbstractStatementParser.StatementType)1 ITAbstractSpannerTest (com.google.cloud.spanner.connection.ITAbstractSpannerTest)1 ReadOnlyStalenessUtil.getTimeUnitAbbreviation (com.google.cloud.spanner.connection.ReadOnlyStalenessUtil.getTimeUnitAbbreviation)1 SpannerExceptionMatcher.matchCode (com.google.cloud.spanner.connection.SpannerExceptionMatcher.matchCode)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Arrays (java.util.Arrays)1