Search in sources :

Example 6 with QueryStatusInfo

use of io.prestosql.client.QueryStatusInfo in project hetu-core by openlookeng.

the class CubeQuery method renderFailure.

public void renderFailure(PrintStream out) {
    QueryStatusInfo results = client.finalStatusInfo();
    QueryError error = results.getError();
    checkState(error != null);
    out.printf("Query %s failed: %s%n", results.getId(), error.getMessage());
    if (debug && (error.getFailureInfo() != null)) {
        error.getFailureInfo().toException().printStackTrace(out);
    }
    if (error.getErrorLocation() != null) {
        renderErrorLocation(client.getQuery(), error.getErrorLocation(), out);
    }
    out.println();
}
Also used : QueryStatusInfo(io.prestosql.client.QueryStatusInfo) QueryError(io.prestosql.client.QueryError)

Example 7 with QueryStatusInfo

use of io.prestosql.client.QueryStatusInfo in project hetu-core by openlookeng.

the class Query method renderQueryOutput.

private boolean renderQueryOutput(Terminal terminal, PrintStream out, PrintStream errorChannel, ClientOptions.OutputFormat outputFormat, boolean usePager, boolean showProgress) {
    StatusPrinter statusPrinter = null;
    WarningsPrinter warningsPrinter = new PrintStreamWarningsPrinter(errorChannel);
    if (showProgress) {
        statusPrinter = new StatusPrinter(client, errorChannel, debug);
        statusPrinter.printInitialStatusUpdates(terminal);
    } else {
        processInitialStatusUpdates(warningsPrinter);
    }
    // if running or finished
    if (client.isRunning() || (client.isFinished() && client.finalStatusInfo().getError() == null)) {
        QueryStatusInfo results = client.isRunning() ? client.currentStatusInfo() : client.finalStatusInfo();
        if (results.getUpdateType() != null) {
            renderUpdate(errorChannel, results);
        } else if (results.getColumns() == null) {
            errorChannel.printf("Query %s has no columns\n", results.getId());
            return false;
        } else {
            renderResults(out, outputFormat, usePager, results.getColumns());
        }
    }
    checkState(!client.isRunning());
    warningsPrinter.print(client.finalStatusInfo().getWarnings(), true, true);
    if (showProgress) {
        statusPrinter.printFinalInfo();
    }
    if (client.isClientAborted()) {
        errorChannel.println("Query aborted by user");
        return false;
    }
    if (client.isClientError()) {
        errorChannel.println("Query is gone (server restarted?)");
        return false;
    }
    verify(client.isFinished());
    if (client.finalStatusInfo().getError() != null) {
        renderFailure(errorChannel);
        return false;
    }
    return true;
}
Also used : QueryStatusInfo(io.prestosql.client.QueryStatusInfo)

Example 8 with QueryStatusInfo

use of io.prestosql.client.QueryStatusInfo in project hetu-core by openlookeng.

the class DataCenterClient method getResults.

private Iterable<List<Object>> getResults(DataCenterClientSession session, String query) throws SQLException {
    try (StatementClient client = DataCenterStatementClient.newStatementClient(this.httpClient, session, query, this.globalQueryIdGenerator.createId())) {
        List<Iterable<List<Object>>> list = new LinkedList<>();
        while (client.isRunning()) {
            if (client.currentData() != null) {
                Iterable<List<Object>> data = client.currentData().getData();
                if (data != null) {
                    list.add(data);
                }
            }
            client.advance();
        }
        verify(client.isFinished());
        QueryStatusInfo results = client.finalStatusInfo();
        if (results.getError() != null) {
            throw resultsException(results);
        }
        return Iterables.concat(list);
    } catch (RuntimeException ex) {
        throw new SQLException(ex.getMessage(), ex);
    }
}
Also used : SQLException(java.sql.SQLException) StatementClient(io.prestosql.client.StatementClient) DataCenterStatementClient(io.prestosql.client.DataCenterStatementClient) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) QueryStatusInfo(io.prestosql.client.QueryStatusInfo) LinkedList(java.util.LinkedList)

Example 9 with QueryStatusInfo

use of io.prestosql.client.QueryStatusInfo in project hetu-core by openlookeng.

the class DataCenterClient method execute.

/**
 * Execute a query by client session.
 *
 * @param query statement.
 * @return a statement client instance.
 * @throws SQLException when new a statement client failed.
 */
private StatementClient execute(String query) throws SQLException {
    try (StatementClient client = DataCenterStatementClient.newStatementClient(this.httpClient, this.clientSession, query, this.globalQueryIdGenerator.createId())) {
        while (client.isRunning()) {
            client.advance();
        }
        verify(client.isFinished());
        QueryStatusInfo results = client.finalStatusInfo();
        if (results.getError() != null) {
            throw resultsException(results);
        }
        return client;
    } catch (RuntimeException ex) {
        throw new SQLException(ex.getMessage(), ex);
    }
}
Also used : SQLException(java.sql.SQLException) StatementClient(io.prestosql.client.StatementClient) DataCenterStatementClient(io.prestosql.client.DataCenterStatementClient) QueryStatusInfo(io.prestosql.client.QueryStatusInfo)

Example 10 with QueryStatusInfo

use of io.prestosql.client.QueryStatusInfo in project hetu-core by openlookeng.

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 Set<Table> tables = new HashSet<>();
    try {
        tables.addAll(authorizer.tablesUsedByQuery(query));
    } catch (ParsingException e) {
        job.setError(new QueryError(e.getMessage(), null, -1, null, Optional.empty(), 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);
    }
    JobSessionContext jobSessionContext = JobSessionContext.buildFromClient(queryRunner.getSession());
    job.setSessionContext(jobSessionContext);
    QueryClient queryClient = new QueryClient(queryRunner, timeout, query);
    try {
        queryClient.executeWith((client) -> {
            if (client == null) {
                return null;
            }
            QueryStatusInfo statusInfo = client.currentStatusInfo();
            QueryData data = client.currentData();
            List<Column> resultColumns = null;
            JobState jobState = null;
            QueryError queryError = null;
            QueryStats queryStats = null;
            if (isCancelled) {
                throw new ExecutionFailureException(job, "Query was cancelled", null);
            }
            if (statusInfo.getError() != null) {
                queryError = statusInfo.getError();
                jobState = JobState.FAILED;
            }
            if ((statusInfo.getInfoUri() != null) && (jobState != JobState.FAILED)) {
                BasicQueryInfo queryInfo = queryInfoClient.from(statusInfo.getInfoUri(), statusInfo.getId());
                if (queryInfo != null) {
                    queryStats = queryInfo.getQueryStats();
                }
            }
            if (statusInfo.getInfoUri() != null && job.getInfoUri() == null) {
                URI infoUri = statusInfo.getInfoUri();
                String path = infoUri.getPath();
                path = path.substring(path.indexOf("query.html"));
                infoUri = URI.create(path + "?" + infoUri.getQuery());
                job.setInfoUri(infoUri);
            }
            if (statusInfo.getStats() != null) {
                jobState = JobState.fromStatementState(statusInfo.getStats().getState());
            }
            try {
                if (statusInfo.getColumns() != null) {
                    resultColumns = statusInfo.getColumns();
                    outputBuilder.addColumns(resultColumns);
                }
                if (data.getData() != null) {
                    List<List<Object>> resultsData = ImmutableList.copyOf(data.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);
            return null;
        });
    } catch (QueryTimeOutException e) {
        throw new ExecutionFailureException(job, format("Query exceeded maximum execution time of %s minutes", Duration.millis(e.getElapsedMs()).getStandardMinutes()), e);
    }
    QueryStatusInfo finalResults = queryClient.finalResults();
    if (finalResults != null && finalResults.getInfoUri() != null) {
        BasicQueryInfo queryInfo = queryInfoClient.from(finalResults.getInfoUri(), finalResults.getId());
        if (queryInfo != null) {
            updateJobInfo(null, null, queryInfo.getQueryStats(), JobState.fromStatementState(finalResults.getStats().getState()), finalResults.getError());
        }
    }
    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(io.prestosql.client.ErrorLocation) QueryData(io.prestosql.client.QueryData) QueryStatusInfo(io.prestosql.client.QueryStatusInfo) URI(java.net.URI) JobSessionContext(io.prestosql.queryeditorui.protocol.JobSessionContext) ExecutionFailureException(io.prestosql.queryeditorui.execution.ExecutionClient.ExecutionFailureException) Column(io.prestosql.client.Column) ParsingException(io.prestosql.sql.parser.ParsingException) FileTooLargeException(io.prestosql.queryeditorui.output.builders.FileTooLargeException) JobState(io.prestosql.queryeditorui.protocol.JobState) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) QueryError(io.prestosql.client.QueryError) JobOutputBuilder(io.prestosql.queryeditorui.output.builders.JobOutputBuilder) HashSet(java.util.HashSet) Table(io.prestosql.queryeditorui.protocol.Table) QueryTimeOutException(io.prestosql.queryeditorui.execution.QueryClient.QueryTimeOutException) BasicQueryInfo(io.prestosql.queryeditorui.execution.QueryInfoClient.BasicQueryInfo) IOException(java.io.IOException) Persistor(io.prestosql.queryeditorui.output.persistors.Persistor) QueryStats(io.prestosql.execution.QueryStats)

Aggregations

QueryStatusInfo (io.prestosql.client.QueryStatusInfo)11 QueryError (io.prestosql.client.QueryError)4 StatementClient (io.prestosql.client.StatementClient)4 SQLException (java.sql.SQLException)4 ImmutableList (com.google.common.collect.ImmutableList)2 Duration (io.airlift.units.Duration)2 Column (io.prestosql.client.Column)2 DataCenterStatementClient (io.prestosql.client.DataCenterStatementClient)2 QueryData (io.prestosql.client.QueryData)2 List (java.util.List)2 Duration.succinctDuration (io.airlift.units.Duration.succinctDuration)1 ClientException (io.prestosql.client.ClientException)1 ClientSession (io.prestosql.client.ClientSession)1 ErrorLocation (io.prestosql.client.ErrorLocation)1 QueryResults (io.prestosql.client.QueryResults)1 SnapshotStats (io.prestosql.client.SnapshotStats)1 StatementClientFactory.newStatementClient (io.prestosql.client.StatementClientFactory.newStatementClient)1 StatementStats (io.prestosql.client.StatementStats)1 QueryStats (io.prestosql.execution.QueryStats)1 ExecutionFailureException (io.prestosql.queryeditorui.execution.ExecutionClient.ExecutionFailureException)1