Search in sources :

Example 6 with QueryResults

use of io.trino.client.QueryResults in project trino by trinodb.

the class TestServer method testNoTransactionSupport.

@Test
public void testNoTransactionSupport() {
    QueryResults queryResults = postQuery(request -> request.setBodyGenerator(createStaticBodyGenerator("start transaction", UTF_8))).map(JsonResponse::getValue).filter(result -> result.getError() != null).findFirst().orElseThrow(() -> new RuntimeException("Error expected"));
    assertNull(queryResults.getNextUri());
    assertEquals(queryResults.getError().getErrorCode(), INCOMPATIBLE_CLIENT.toErrorCode().getCode());
}
Also used : QueryResults(io.trino.client.QueryResults) Test(org.testng.annotations.Test)

Example 7 with QueryResults

use of io.trino.client.QueryResults in project trino by trinodb.

the class TestServer method testInvalidSessionError.

@Test
public void testInvalidSessionError() {
    String invalidTimeZone = "this_is_an_invalid_time_zone";
    QueryResults queryResults = postQuery(request -> request.setBodyGenerator(createStaticBodyGenerator("show catalogs", UTF_8)).setHeader(TRINO_HEADERS.requestCatalog(), "catalog").setHeader(TRINO_HEADERS.requestSchema(), "schema").setHeader(TRINO_HEADERS.requestPath(), "path").setHeader(TRINO_HEADERS.requestTimeZone(), invalidTimeZone)).map(JsonResponse::getValue).peek(result -> checkState((result.getError() == null) != (result.getNextUri() == null))).collect(last());
    QueryError queryError = queryResults.getError();
    assertNotNull(queryError);
    TimeZoneNotSupportedException expected = new TimeZoneNotSupportedException(invalidTimeZone);
    assertEquals(queryError.getErrorCode(), expected.getErrorCode().getCode());
    assertEquals(queryError.getErrorName(), expected.getErrorCode().getName());
    assertEquals(queryError.getErrorType(), expected.getErrorCode().getType().name());
    assertEquals(queryError.getMessage(), expected.getMessage());
}
Also used : QueryId(io.trino.spi.QueryId) QueryError(io.trino.client.QueryError) X_FORWARDED_PORT(com.google.common.net.HttpHeaders.X_FORWARDED_PORT) BasicQueryInfo(io.trino.server.BasicQueryInfo) FullJsonResponseHandler.createFullJsonResponseHandler(io.airlift.http.client.FullJsonResponseHandler.createFullJsonResponseHandler) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.testng.annotations.Test) Closeables.closeAll(io.airlift.testing.Closeables.closeAll) QueryResults(io.trino.client.QueryResults) URI(java.net.URI) Collector(java.util.stream.Collector) TestingTrinoServer(io.trino.server.testing.TestingTrinoServer) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) Splitter(com.google.common.base.Splitter) X_FORWARDED_PROTO(com.google.common.net.HttpHeaders.X_FORWARDED_PROTO) HttpUriBuilder(io.airlift.http.client.HttpUriBuilder) SEE_OTHER(javax.ws.rs.core.Response.Status.SEE_OTHER) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) Builder.prepareHead(io.airlift.http.client.Request.Builder.prepareHead) QUERY_MAX_MEMORY(io.trino.SystemSessionProperties.QUERY_MAX_MEMORY) ImmutableMap(com.google.common.collect.ImmutableMap) OK(javax.ws.rs.core.Response.Status.OK) MemoryPlugin(io.trino.plugin.memory.MemoryPlugin) BeforeClass(org.testng.annotations.BeforeClass) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Assert.assertNotNull(org.testng.Assert.assertNotNull) Streams(com.google.common.collect.Streams) TestingTrinoClient(io.trino.testing.TestingTrinoClient) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) TestingSession.testSessionBuilder(io.trino.testing.TestingSession.testSessionBuilder) Stream(java.util.stream.Stream) Optional(java.util.Optional) StaticBodyGenerator.createStaticBodyGenerator(io.airlift.http.client.StaticBodyGenerator.createStaticBodyGenerator) JsonCodec(io.airlift.json.JsonCodec) HttpClient(io.airlift.http.client.HttpClient) IntStream(java.util.stream.IntStream) Assert.assertNull(org.testng.Assert.assertNull) Assert.assertEquals(org.testng.Assert.assertEquals) Function(java.util.function.Function) CONTENT_TYPE(com.google.common.net.HttpHeaders.CONTENT_TYPE) AbstractSequentialIterator(com.google.common.collect.AbstractSequentialIterator) INCOMPATIBLE_CLIENT(io.trino.spi.StandardErrorCode.INCOMPATIBLE_CLIENT) JsonResponse(io.airlift.http.client.FullJsonResponseHandler.JsonResponse) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) Request(io.airlift.http.client.Request) X_FORWARDED_HOST(com.google.common.net.HttpHeaders.X_FORWARDED_HOST) AfterClass(org.testng.annotations.AfterClass) Builder.preparePost(io.airlift.http.client.Request.Builder.preparePost) Builder.prepareGet(io.airlift.http.client.Request.Builder.prepareGet) Language(org.intellij.lang.annotations.Language) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assert.fail(org.testng.Assert.fail) HASH_PARTITION_COUNT(io.trino.SystemSessionProperties.HASH_PARTITION_COUNT) Throwables.getStackTraceAsString(com.google.common.base.Throwables.getStackTraceAsString) JsonCodec.jsonCodec(io.airlift.json.JsonCodec.jsonCodec) StatusResponseHandler.createStatusResponseHandler(io.airlift.http.client.StatusResponseHandler.createStatusResponseHandler) JOIN_DISTRIBUTION_TYPE(io.trino.SystemSessionProperties.JOIN_DISTRIBUTION_TYPE) TimeZoneNotSupportedException(io.trino.spi.type.TimeZoneNotSupportedException) StatusResponse(io.airlift.http.client.StatusResponseHandler.StatusResponse) Collections(java.util.Collections) TRINO_HEADERS(io.trino.client.ProtocolHeaders.TRINO_HEADERS) Throwables.getStackTraceAsString(com.google.common.base.Throwables.getStackTraceAsString) QueryError(io.trino.client.QueryError) TimeZoneNotSupportedException(io.trino.spi.type.TimeZoneNotSupportedException) QueryResults(io.trino.client.QueryResults) JsonResponse(io.airlift.http.client.FullJsonResponseHandler.JsonResponse) Test(org.testng.annotations.Test)

Example 8 with QueryResults

use of io.trino.client.QueryResults in project trino by trinodb.

the class TestQueryResource method runToCompletion.

private String runToCompletion(String sql) {
    URI uri = uriBuilderFrom(server.getBaseUrl().resolve("/v1/statement")).build();
    Request request = preparePost().setHeader(TRINO_HEADERS.requestUser(), "user").setUri(uri).setBodyGenerator(createStaticBodyGenerator(sql, UTF_8)).build();
    QueryResults queryResults = client.execute(request, createJsonResponseHandler(jsonCodec(QueryResults.class)));
    while (queryResults.getNextUri() != null) {
        request = prepareGet().setHeader(TRINO_HEADERS.requestUser(), "user").setUri(queryResults.getNextUri()).build();
        queryResults = client.execute(request, createJsonResponseHandler(jsonCodec(QueryResults.class)));
    }
    return queryResults.getId();
}
Also used : Request(io.airlift.http.client.Request) URI(java.net.URI) QueryResults(io.trino.client.QueryResults)

Example 9 with QueryResults

use of io.trino.client.QueryResults in project trino by trinodb.

the class ExecutingStatementResource method asyncQueryResults.

private void asyncQueryResults(Query query, long token, Duration maxWait, DataSize targetResultSize, UriInfo uriInfo, AsyncResponse asyncResponse) {
    Duration wait = WAIT_ORDERING.min(MAX_WAIT_TIME, maxWait);
    if (targetResultSize == null) {
        targetResultSize = DEFAULT_TARGET_RESULT_SIZE;
    } else {
        targetResultSize = Ordering.natural().min(targetResultSize, MAX_TARGET_RESULT_SIZE);
    }
    ListenableFuture<QueryResults> queryResultsFuture = query.waitForResults(token, uriInfo, wait, targetResultSize);
    ListenableFuture<Response> response = Futures.transform(queryResultsFuture, queryResults -> toResponse(query, queryResults), directExecutor());
    bindAsyncResponse(asyncResponse, response, responseExecutor);
}
Also used : AsyncResponse(javax.ws.rs.container.AsyncResponse) Response(javax.ws.rs.core.Response) AsyncResponseHandler.bindAsyncResponse(io.airlift.jaxrs.AsyncResponseHandler.bindAsyncResponse) Duration(io.airlift.units.Duration) QueryResults(io.trino.client.QueryResults)

Example 10 with QueryResults

use of io.trino.client.QueryResults in project trino by trinodb.

the class TestServer method testFirstResponseColumns.

@Test
public void testFirstResponseColumns() {
    List<QueryResults> queryResults = postQuery(request -> request.setBodyGenerator(createStaticBodyGenerator("show catalogs", UTF_8)).setHeader(TRINO_HEADERS.requestCatalog(), "catalog").setHeader(TRINO_HEADERS.requestSchema(), "schema").setHeader(TRINO_HEADERS.requestPath(), "path")).map(JsonResponse::getValue).collect(toImmutableList());
    QueryResults first = queryResults.get(0);
    QueryResults last = queryResults.get(queryResults.size() - 1);
    Optional<QueryResults> data = queryResults.stream().filter(results -> results.getData() != null).findFirst();
    assertNull(first.getColumns());
    assertEquals(first.getStats().getState(), "QUEUED");
    assertNull(first.getData());
    assertThat(last.getColumns()).hasSize(1);
    assertThat(last.getColumns().get(0).getName()).isEqualTo("Catalog");
    assertThat(last.getColumns().get(0).getType()).isEqualTo("varchar(6)");
    assertEquals(last.getStats().getState(), "FINISHED");
    assertThat(data).isPresent();
    QueryResults results = data.orElseThrow();
    assertThat(results.getData()).containsOnly(ImmutableList.of("memory"), ImmutableList.of("system"));
}
Also used : QueryId(io.trino.spi.QueryId) QueryError(io.trino.client.QueryError) X_FORWARDED_PORT(com.google.common.net.HttpHeaders.X_FORWARDED_PORT) BasicQueryInfo(io.trino.server.BasicQueryInfo) FullJsonResponseHandler.createFullJsonResponseHandler(io.airlift.http.client.FullJsonResponseHandler.createFullJsonResponseHandler) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.testng.annotations.Test) Closeables.closeAll(io.airlift.testing.Closeables.closeAll) QueryResults(io.trino.client.QueryResults) URI(java.net.URI) Collector(java.util.stream.Collector) TestingTrinoServer(io.trino.server.testing.TestingTrinoServer) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) Splitter(com.google.common.base.Splitter) X_FORWARDED_PROTO(com.google.common.net.HttpHeaders.X_FORWARDED_PROTO) HttpUriBuilder(io.airlift.http.client.HttpUriBuilder) SEE_OTHER(javax.ws.rs.core.Response.Status.SEE_OTHER) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) Builder.prepareHead(io.airlift.http.client.Request.Builder.prepareHead) QUERY_MAX_MEMORY(io.trino.SystemSessionProperties.QUERY_MAX_MEMORY) ImmutableMap(com.google.common.collect.ImmutableMap) OK(javax.ws.rs.core.Response.Status.OK) MemoryPlugin(io.trino.plugin.memory.MemoryPlugin) BeforeClass(org.testng.annotations.BeforeClass) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Assert.assertNotNull(org.testng.Assert.assertNotNull) Streams(com.google.common.collect.Streams) TestingTrinoClient(io.trino.testing.TestingTrinoClient) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) TestingSession.testSessionBuilder(io.trino.testing.TestingSession.testSessionBuilder) Stream(java.util.stream.Stream) Optional(java.util.Optional) StaticBodyGenerator.createStaticBodyGenerator(io.airlift.http.client.StaticBodyGenerator.createStaticBodyGenerator) JsonCodec(io.airlift.json.JsonCodec) HttpClient(io.airlift.http.client.HttpClient) IntStream(java.util.stream.IntStream) Assert.assertNull(org.testng.Assert.assertNull) Assert.assertEquals(org.testng.Assert.assertEquals) Function(java.util.function.Function) CONTENT_TYPE(com.google.common.net.HttpHeaders.CONTENT_TYPE) AbstractSequentialIterator(com.google.common.collect.AbstractSequentialIterator) INCOMPATIBLE_CLIENT(io.trino.spi.StandardErrorCode.INCOMPATIBLE_CLIENT) JsonResponse(io.airlift.http.client.FullJsonResponseHandler.JsonResponse) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) Request(io.airlift.http.client.Request) X_FORWARDED_HOST(com.google.common.net.HttpHeaders.X_FORWARDED_HOST) AfterClass(org.testng.annotations.AfterClass) Builder.preparePost(io.airlift.http.client.Request.Builder.preparePost) Builder.prepareGet(io.airlift.http.client.Request.Builder.prepareGet) Language(org.intellij.lang.annotations.Language) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assert.fail(org.testng.Assert.fail) HASH_PARTITION_COUNT(io.trino.SystemSessionProperties.HASH_PARTITION_COUNT) Throwables.getStackTraceAsString(com.google.common.base.Throwables.getStackTraceAsString) JsonCodec.jsonCodec(io.airlift.json.JsonCodec.jsonCodec) StatusResponseHandler.createStatusResponseHandler(io.airlift.http.client.StatusResponseHandler.createStatusResponseHandler) JOIN_DISTRIBUTION_TYPE(io.trino.SystemSessionProperties.JOIN_DISTRIBUTION_TYPE) TimeZoneNotSupportedException(io.trino.spi.type.TimeZoneNotSupportedException) StatusResponse(io.airlift.http.client.StatusResponseHandler.StatusResponse) Collections(java.util.Collections) TRINO_HEADERS(io.trino.client.ProtocolHeaders.TRINO_HEADERS) QueryResults(io.trino.client.QueryResults) Test(org.testng.annotations.Test)

Aggregations

QueryResults (io.trino.client.QueryResults)12 Request (io.airlift.http.client.Request)8 URI (java.net.URI)8 JettyHttpClient (io.airlift.http.client.jetty.JettyHttpClient)5 Preconditions.checkState (com.google.common.base.Preconditions.checkState)4 Splitter (com.google.common.base.Splitter)4 Throwables.getStackTraceAsString (com.google.common.base.Throwables.getStackTraceAsString)4 AbstractSequentialIterator (com.google.common.collect.AbstractSequentialIterator)4 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 Streams (com.google.common.collect.Streams)4 CONTENT_TYPE (com.google.common.net.HttpHeaders.CONTENT_TYPE)4 X_FORWARDED_HOST (com.google.common.net.HttpHeaders.X_FORWARDED_HOST)4 X_FORWARDED_PORT (com.google.common.net.HttpHeaders.X_FORWARDED_PORT)4 X_FORWARDED_PROTO (com.google.common.net.HttpHeaders.X_FORWARDED_PROTO)4 JsonResponse (io.airlift.http.client.FullJsonResponseHandler.JsonResponse)4 FullJsonResponseHandler.createFullJsonResponseHandler (io.airlift.http.client.FullJsonResponseHandler.createFullJsonResponseHandler)4 HttpClient (io.airlift.http.client.HttpClient)4 HttpUriBuilder (io.airlift.http.client.HttpUriBuilder)4