Search in sources :

Example 1 with StatementClient

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

the class BenchmarkQueryRunner method execute.

private StatementStats execute(ClientSession session, String name, String query) {
    // start query
    StatementClient client = new StatementClient(httpClient, queryResultsCodec, session, query);
    // read query output
    while (client.isValid() && client.advance()) {
    // we do not process the output
    }
    // verify final state
    if (client.isClosed()) {
        throw new IllegalStateException("Query aborted by user");
    }
    if (client.isGone()) {
        throw new IllegalStateException("Query is gone (server restarted?)");
    }
    QueryError resultsError = client.finalResults().getError();
    if (resultsError != null) {
        RuntimeException cause = null;
        if (resultsError.getFailureInfo() != null) {
            cause = resultsError.getFailureInfo().toException();
        }
        throw new BenchmarkDriverExecutionException(format("Query %s failed: %s", name, resultsError.getMessage()), cause);
    }
    return client.finalResults().getStats();
}
Also used : StatementClient(com.facebook.presto.client.StatementClient) QueryError(com.facebook.presto.client.QueryError)

Example 2 with StatementClient

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

the class BenchmarkQueryRunner method execute.

private StatementStats execute(ClientSession session, String query, Consumer<QueryData> queryDataConsumer, Consumer<QueryError> queryErrorConsumer) {
    // start query
    try (StatementClient client = newStatementClient(okHttpClient, session, query)) {
        // read query output
        while (client.isRunning()) {
            queryDataConsumer.accept(client.currentData());
            if (!client.advance()) {
                break;
            }
        }
        // verify final state
        if (client.isClientAborted()) {
            throw new IllegalStateException("Query aborted by user");
        }
        if (client.isClientError()) {
            throw new IllegalStateException("Query is gone (server restarted?)");
        }
        verify(client.isFinished());
        QueryError resultsError = client.finalStatusInfo().getError();
        if (resultsError != null) {
            queryErrorConsumer.accept(resultsError);
        }
        return client.finalStatusInfo().getStats();
    }
}
Also used : StatementClientFactory.newStatementClient(com.facebook.presto.client.StatementClientFactory.newStatementClient) StatementClient(com.facebook.presto.client.StatementClient) QueryError(com.facebook.presto.client.QueryError)

Example 3 with StatementClient

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

the class PrestoStatement method partialCancel.

public void partialCancel() throws SQLException {
    checkOpen();
    StatementClient client = executingClient.get();
    if (client != null) {
        client.cancelLeafStage();
    } else {
        PrestoResultSet resultSet = currentResult.get();
        if (resultSet != null) {
            resultSet.partialCancel();
        }
    }
}
Also used : StatementClient(com.facebook.presto.client.StatementClient)

Example 4 with StatementClient

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

the class PrestoStatement method internalExecute.

final boolean internalExecute(String sql) throws SQLException {
    clearCurrentResults();
    checkOpen();
    StatementClient client = null;
    PrestoResultSet resultSet = null;
    boolean intercepted = false;
    try {
        WarningsManager warningsManager = new WarningsManager();
        currentWarningsManager.set(Optional.of(warningsManager));
        int statementDepth = this.statementDepth.incrementAndGet();
        boolean shouldIntercept = !connection().getQueryInterceptorInstances().isEmpty() && statementDepth == 1;
        if (shouldIntercept) {
            Optional<PrestoResultSet> newResultSet = connection().invokeQueryInterceptorsPre(sql, this);
            if (newResultSet.isPresent()) {
                resultSet = newResultSet.get();
            }
        }
        // Check if no resultSet is returned from an interceptor
        if (resultSet != null) {
            currentResult.set(resultSet);
            intercepted = true;
        } else {
            client = connection().startQuery(sql, getStatementSessionProperties());
            if (client.isFinished()) {
                QueryStatusInfo finalStatusInfo = client.finalStatusInfo();
                if (finalStatusInfo.getError() != null) {
                    throw resultsException(finalStatusInfo);
                }
            }
            executingClient.set(client);
            resultSet = new PrestoResultSet(this, client, maxRows.get(), progressConsumer, warningsManager);
            for (Map.Entry<String, SelectedRole> entry : client.getSetRoles().entrySet()) {
                connection.get().setRole(entry.getKey(), entry.getValue());
            }
        }
        // check if this is a query
        if (intercepted || client.currentStatusInfo().getUpdateType() == null) {
            currentResult.set(resultSet);
            if (shouldIntercept) {
                resultSet = connection().invokeQueryInterceptorsPost(sql, this, resultSet);
                verifyNotNull(resultSet, "invokeQueryInterceptorsPost should never return a null ResultSet");
                currentResult.set(resultSet);
            }
            return true;
        }
        // this is an update, not a query
        while (resultSet.next()) {
        // ignore rows
        }
        connection().updateSession(client);
        Long updateCount = client.finalStatusInfo().getUpdateCount();
        currentUpdateCount.set((updateCount != null) ? updateCount : 0);
        currentUpdateType.set(client.finalStatusInfo().getUpdateType());
        warningsManager.addWarnings(client.finalStatusInfo().getWarnings());
        return false;
    } catch (ClientException e) {
        throw new SQLException(e.getMessage(), e);
    } catch (RuntimeException e) {
        throw new SQLException("Error executing query", e);
    } finally {
        this.statementDepth.decrementAndGet();
        executingClient.set(null);
        if (currentResult.get() == null) {
            if (resultSet != null) {
                resultSet.close();
            }
            if (client != null) {
                client.close();
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) SelectedRole(com.facebook.presto.spi.security.SelectedRole) StatementClient(com.facebook.presto.client.StatementClient) QueryStatusInfo(com.facebook.presto.client.QueryStatusInfo) AtomicLong(java.util.concurrent.atomic.AtomicLong) ClientException(com.facebook.presto.client.ClientException) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 5 with StatementClient

use of com.facebook.presto.client.StatementClient in project airpal by airbnb.

the class Execution method doExecute.

private Job doExecute() throws ExecutionFailureException {
    final String userQuery = QUERY_SPLITTER.splitToList(getJob().getQuery()).get(0);
    final JobOutputBuilder outputBuilder;
    job.setQueryStats(createNoOpQueryStats());
    try {
        outputBuilder = outputBuilderFactory.forJob(job);
    } catch (IOException e) {
        throw new ExecutionFailureException(job, "Could not create output builder for job", e);
    } catch (InvalidQueryException e) {
        throw new ExecutionFailureException(job, e.getMessage(), e);
    }
    final Persistor persistor = persistorFactory.getPersistor(job, job.getOutput());
    final String query = job.getOutput().processQuery(userQuery);
    if (!persistor.canPersist(authorizer)) {
        throw new ExecutionFailureException(job, "Not authorized to create tables", null);
    }
    final List<List<Object>> outputPreview = new ArrayList<>(maxRowsPreviewOutput);
    final Set<Table> tables = new HashSet<>();
    try {
        tables.addAll(authorizer.tablesUsedByQuery(query));
    } catch (ParsingException e) {
        job.setError(new QueryError(e.getMessage(), null, -1, null, null, new ErrorLocation(e.getLineNumber(), e.getColumnNumber()), null));
        throw new ExecutionFailureException(job, "Invalid query, could not parse", e);
    }
    if (!authorizer.isAuthorizedRead(tables)) {
        job.setQueryStats(createNoOpQueryStats());
        throw new ExecutionFailureException(job, "Cannot access tables", null);
    }
    QueryClient queryClient = new QueryClient(queryRunner, timeout, query);
    try {
        queryClient.executeWith(new Function<StatementClient, Void>() {

            @Nullable
            @Override
            public Void apply(@Nullable StatementClient client) {
                if (client == null) {
                    return null;
                }
                QueryResults results = client.current();
                List<Column> resultColumns = null;
                JobState jobState = null;
                QueryError queryError = null;
                QueryStats queryStats = null;
                if (isCancelled) {
                    throw new ExecutionFailureException(job, "Query was cancelled", null);
                }
                if (results.getError() != null) {
                    queryError = results.getError();
                    jobState = JobState.FAILED;
                }
                if ((results.getInfoUri() != null) && (jobState != JobState.FAILED)) {
                    BasicQueryInfo queryInfo = queryInfoClient.from(results.getInfoUri());
                    if (queryInfo != null) {
                        queryStats = queryInfo.getQueryStats();
                    }
                }
                if (results.getStats() != null) {
                    jobState = JobState.fromStatementState(results.getStats().getState());
                }
                try {
                    if (results.getColumns() != null) {
                        resultColumns = results.getColumns();
                        outputBuilder.addColumns(resultColumns);
                    }
                    if (results.getData() != null) {
                        List<List<Object>> resultsData = ImmutableList.copyOf(results.getData());
                        for (List<Object> row : resultsData) {
                            outputBuilder.addRow(row);
                        }
                    }
                } catch (FileTooLargeException e) {
                    throw new ExecutionFailureException(job, "Output file exceeded maximum configured filesize", e);
                }
                rlUpdateJobInfo(tables, resultColumns, queryStats, jobState, queryError, outputPreview);
                return null;
            }
        });
    } catch (QueryTimeOutException e) {
        throw new ExecutionFailureException(job, format("Query exceeded maximum execution time of %s minutes", Duration.millis(e.getElapsedMs()).getStandardMinutes()), e);
    }
    QueryResults finalResults = queryClient.finalResults();
    if (finalResults != null && finalResults.getInfoUri() != null) {
        BasicQueryInfo queryInfo = queryInfoClient.from(finalResults.getInfoUri());
        if (queryInfo != null) {
            updateJobInfo(null, null, queryInfo.getQueryStats(), JobState.fromStatementState(finalResults.getStats().getState()), finalResults.getError(), outputPreview, true);
        }
    }
    if (job.getState() != JobState.FAILED) {
        URI location = persistor.persist(outputBuilder, job);
        if (location != null) {
            job.getOutput().setLocation(location);
        }
    } else {
        throw new ExecutionFailureException(job, null, null);
    }
    return getJob();
}
Also used : ErrorLocation(com.facebook.presto.client.ErrorLocation) ArrayList(java.util.ArrayList) URI(java.net.URI) ExecutionFailureException(com.airbnb.airpal.core.execution.ExecutionClient.ExecutionFailureException) ParsingException(com.facebook.presto.sql.parser.ParsingException) FileTooLargeException(com.airbnb.airpal.api.output.builders.FileTooLargeException) JobState(com.airbnb.airpal.api.JobState) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) QueryError(com.facebook.presto.client.QueryError) JobOutputBuilder(com.airbnb.airpal.api.output.builders.JobOutputBuilder) HashSet(java.util.HashSet) Table(com.airbnb.airpal.presto.Table) QueryTimeOutException(com.airbnb.airpal.core.execution.QueryClient.QueryTimeOutException) BasicQueryInfo(com.airbnb.airpal.presto.QueryInfoClient.BasicQueryInfo) StatementClient(com.facebook.presto.client.StatementClient) IOException(java.io.IOException) Persistor(com.airbnb.airpal.api.output.persistors.Persistor) QueryResults(com.facebook.presto.client.QueryResults) QueryStats(com.facebook.presto.execution.QueryStats) InvalidQueryException(com.airbnb.airpal.api.output.InvalidQueryException) Nullable(javax.annotation.Nullable)

Aggregations

StatementClient (com.facebook.presto.client.StatementClient)15 QueryResults (com.facebook.presto.client.QueryResults)5 ImmutableList (com.google.common.collect.ImmutableList)5 List (java.util.List)5 Nullable (javax.annotation.Nullable)5 QueryClient (com.airbnb.airpal.core.execution.QueryClient)4 QueryRunner (com.airbnb.airpal.presto.QueryRunner)4 QueryError (com.facebook.presto.client.QueryError)4 StatementClientFactory.newStatementClient (com.facebook.presto.client.StatementClientFactory.newStatementClient)3 HiveColumn (com.airbnb.airpal.presto.hive.HiveColumn)2 ClientSession (com.facebook.presto.client.ClientSession)2 Column (com.facebook.presto.client.Column)2 QueryStatusInfo (com.facebook.presto.client.QueryStatusInfo)2 QueryId (com.facebook.presto.spi.QueryId)2 Duration (io.airlift.units.Duration)2 SQLException (java.sql.SQLException)2 Map (java.util.Map)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 JobState (com.airbnb.airpal.api.JobState)1 InvalidQueryException (com.airbnb.airpal.api.output.InvalidQueryException)1