Search in sources :

Example 1 with QueryError

use of com.facebook.presto.client.QueryError 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)

Example 2 with QueryError

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

the class Execution method updateJobInfo.

protected void updateJobInfo(Set<Table> usedTables, List<Column> columns, QueryStats queryStats, JobState state, QueryError error, List<List<Object>> outputPreview, boolean postUpdate) {
    if ((usedTables != null) && (usedTables.size() > 0)) {
        job.getTablesUsed().addAll(usedTables);
    }
    if ((columns != null) && (columns.size() > 0)) {
        job.setColumns(columns);
    }
    if (queryStats != null) {
        job.setQueryStats(queryStats);
    }
    if ((state != null) && (job.getState() != JobState.FINISHED) && (job.getState() != JobState.FAILED)) {
        job.setState(state);
    }
    if (error != null) {
        FailureInfo failureInfo = new FailureInfo(error.getFailureInfo().getType(), error.getFailureInfo().getMessage(), null, Collections.<FailureInfo>emptyList(), Collections.<String>emptyList(), error.getFailureInfo().getErrorLocation());
        QueryError queryError = new QueryError(error.getMessage(), error.getSqlState(), error.getErrorCode(), error.getErrorName(), error.getErrorType(), error.getErrorLocation(), failureInfo);
        job.setError(queryError);
    }
    if (postUpdate) {
        eventBus.post(new JobUpdateEvent(job, outputPreview));
    }
}
Also used : FailureInfo(com.facebook.presto.client.FailureInfo) QueryError(com.facebook.presto.client.QueryError) JobUpdateEvent(com.airbnb.airpal.api.event.JobUpdateEvent)

Example 3 with QueryError

use of com.facebook.presto.client.QueryError 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 4 with QueryError

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

the class PrestoResultSet method resultsException.

static SQLException resultsException(QueryResults results) {
    QueryError error = requireNonNull(results.getError());
    String message = format("Query failed (#%s): %s", results.getId(), error.getMessage());
    Throwable cause = (error.getFailureInfo() == null) ? null : error.getFailureInfo().toException();
    return new SQLException(message, error.getSqlState(), error.getErrorCode(), cause);
}
Also used : SQLException(java.sql.SQLException) QueryError(com.facebook.presto.client.QueryError)

Example 5 with QueryError

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

the class Query method renderFailure.

public void renderFailure(PrintStream out) {
    QueryResults results = client.finalResults();
    QueryError error = results.getError();
    checkState(error != null);
    out.printf("Query %s failed: %s%n", results.getId(), error.getMessage());
    if (client.isDebug() && (error.getFailureInfo() != null)) {
        error.getFailureInfo().toException().printStackTrace(out);
    }
    if (error.getErrorLocation() != null) {
        renderErrorLocation(client.getQuery(), error.getErrorLocation(), out);
    }
    out.println();
}
Also used : QueryError(com.facebook.presto.client.QueryError) QueryResults(com.facebook.presto.client.QueryResults)

Aggregations

QueryError (com.facebook.presto.client.QueryError)9 QueryResults (com.facebook.presto.client.QueryResults)4 StatementClient (com.facebook.presto.client.StatementClient)4 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 Job (com.airbnb.airpal.api.Job)1 JobState (com.airbnb.airpal.api.JobState)1 JobUpdateEvent (com.airbnb.airpal.api.event.JobUpdateEvent)1 InvalidQueryException (com.airbnb.airpal.api.output.InvalidQueryException)1 FileTooLargeException (com.airbnb.airpal.api.output.builders.FileTooLargeException)1 JobOutputBuilder (com.airbnb.airpal.api.output.builders.JobOutputBuilder)1 Persistor (com.airbnb.airpal.api.output.persistors.Persistor)1 ExecutionFailureException (com.airbnb.airpal.core.execution.ExecutionClient.ExecutionFailureException)1 QueryTimeOutException (com.airbnb.airpal.core.execution.QueryClient.QueryTimeOutException)1 BasicQueryInfo (com.airbnb.airpal.presto.QueryInfoClient.BasicQueryInfo)1 Table (com.airbnb.airpal.presto.Table)1 ClientSession (com.facebook.presto.client.ClientSession)1 ErrorLocation (com.facebook.presto.client.ErrorLocation)1 FailureInfo (com.facebook.presto.client.FailureInfo)1 QueryStats (com.facebook.presto.execution.QueryStats)1