Search in sources :

Example 6 with QueryStatusInfo

use of com.facebook.presto.client.QueryStatusInfo in project presto by prestodb.

the class AbstractTestingPrestoClient method execute.

public ResultWithQueryId<T> execute(Session session, @Language("SQL") String sql) {
    ResultsSession<T> resultsSession = getResultSession(session);
    ClientSession clientSession = toClientSession(session, prestoServer.getBaseUrl(), new Duration(2, TimeUnit.MINUTES));
    try (StatementClient client = newStatementClient(httpClient, clientSession, sql)) {
        while (client.isRunning()) {
            resultsSession.addResults(client.currentStatusInfo(), client.currentData());
            client.advance();
        }
        checkState(client.isFinished());
        QueryError error = client.finalStatusInfo().getError();
        if (error == null) {
            QueryStatusInfo results = client.finalStatusInfo();
            if (results.getUpdateType() != null) {
                resultsSession.setUpdateType(results.getUpdateType());
            }
            if (results.getUpdateCount() != null) {
                resultsSession.setUpdateCount(results.getUpdateCount());
            }
            resultsSession.setWarnings(results.getWarnings());
            T result = resultsSession.build(client.getSetSessionProperties(), client.getResetSessionProperties());
            return new ResultWithQueryId<>(new QueryId(results.getId()), result);
        }
        if (error.getFailureInfo() != null) {
            RuntimeException remoteException = error.getFailureInfo().toException();
            throw new RuntimeException(Optional.ofNullable(remoteException.getMessage()).orElseGet(remoteException::toString), remoteException);
        }
        throw new RuntimeException("Query failed: " + error.getMessage());
    // dump query info to console for debugging (NOTE: not pretty printed)
    // JsonCodec<QueryInfo> queryInfoJsonCodec = createCodecFactory().prettyPrint().jsonCodec(QueryInfo.class);
    // log.info("\n" + queryInfoJsonCodec.toJson(queryInfo));
    }
}
Also used : ClientSession(com.facebook.presto.client.ClientSession) QueryId(com.facebook.presto.spi.QueryId) StatementClientFactory.newStatementClient(com.facebook.presto.client.StatementClientFactory.newStatementClient) StatementClient(com.facebook.presto.client.StatementClient) Duration(io.airlift.units.Duration) QueryError(com.facebook.presto.client.QueryError) QueryStatusInfo(com.facebook.presto.client.QueryStatusInfo)

Aggregations

QueryStatusInfo (com.facebook.presto.client.QueryStatusInfo)6 QueryError (com.facebook.presto.client.QueryError)2 StatementClient (com.facebook.presto.client.StatementClient)2 Duration (io.airlift.units.Duration)2 SQLException (java.sql.SQLException)2 ClientException (com.facebook.presto.client.ClientException)1 ClientSession (com.facebook.presto.client.ClientSession)1 Column (com.facebook.presto.client.Column)1 StatementClientFactory.newStatementClient (com.facebook.presto.client.StatementClientFactory.newStatementClient)1 StatementStats (com.facebook.presto.client.StatementStats)1 RuntimeMetric (com.facebook.presto.common.RuntimeMetric)1 QueryId (com.facebook.presto.spi.QueryId)1 SelectedRole (com.facebook.presto.spi.security.SelectedRole)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 PrintStream (java.io.PrintStream)1 Map (java.util.Map)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1