Search in sources :

Example 11 with QueryResults

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

the class SchemaCache method queryMetadata.

private Map<String, List<String>> queryMetadata(String query) {
    final Map<String, List<String>> cache = Maps.newHashMap();
    QueryRunner queryRunner = queryRunnerFactory.create();
    QueryClient queryClient = new QueryClient(queryRunner, io.dropwizard.util.Duration.seconds(60), query);
    try {
        queryClient.executeWith(new Function<StatementClient, Void>() {

            @Nullable
            @Override
            public Void apply(StatementClient client) {
                QueryResults results = client.current();
                if (results.getData() != null) {
                    for (List<Object> row : results.getData()) {
                        String schema = (String) row.get(1);
                        String table = (String) row.get(2);
                        if (EXCLUDED_SCHEMAS.contains(schema)) {
                            continue;
                        }
                        List<String> tables = cache.get(schema);
                        if (tables == null) {
                            tables = Lists.newArrayList();
                            cache.put(schema, tables);
                        }
                        tables.add(table);
                    }
                }
                return null;
            }
        });
    } catch (QueryClient.QueryTimeOutException e) {
        log.error("Caught timeout loading columns", e);
    }
    return ImmutableMap.copyOf(cache);
}
Also used : StatementClient(com.facebook.presto.client.StatementClient) List(java.util.List) QueryRunner(com.airbnb.airpal.presto.QueryRunner) Nullable(javax.annotation.Nullable) QueryResults(com.facebook.presto.client.QueryResults) QueryClient(com.airbnb.airpal.core.execution.QueryClient)

Example 12 with QueryResults

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

the class Query method renderQueryOutput.

private void renderQueryOutput(PrintStream out, OutputFormat outputFormat, boolean interactive) {
    StatusPrinter statusPrinter = null;
    @SuppressWarnings("resource") PrintStream errorChannel = interactive ? out : System.err;
    if (interactive) {
        statusPrinter = new StatusPrinter(client, out);
        statusPrinter.printInitialStatusUpdates();
    } else {
        waitForData();
    }
    if ((!client.isFailed()) && (!client.isGone()) && (!client.isClosed())) {
        QueryResults results = client.isValid() ? client.current() : client.finalResults();
        if (results.getUpdateType() != null) {
            renderUpdate(errorChannel, results);
        } else if (results.getColumns() == null) {
            errorChannel.printf("Query %s has no columns\n", results.getId());
            return;
        } else {
            renderResults(out, outputFormat, interactive, results.getColumns());
        }
    }
    if (statusPrinter != null) {
        statusPrinter.printFinalInfo();
    }
    if (client.isClosed()) {
        errorChannel.println("Query aborted by user");
    } else if (client.isGone()) {
        errorChannel.println("Query is gone (server restarted?)");
    } else if (client.isFailed()) {
        renderFailure(errorChannel);
    }
}
Also used : PrintStream(java.io.PrintStream) QueryResults(com.facebook.presto.client.QueryResults)

Example 13 with QueryResults

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

the class TableNameCompleter method queryMetadata.

private List<String> queryMetadata(String query) {
    ImmutableList.Builder<String> cache = ImmutableList.builder();
    try (StatementClient client = queryRunner.startInternalQuery(query)) {
        while (client.isValid() && !Thread.currentThread().isInterrupted()) {
            QueryResults results = client.current();
            if (results.getData() != null) {
                for (List<Object> row : results.getData()) {
                    cache.add((String) row.get(0));
                }
            }
            client.advance();
        }
    }
    return cache.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) StatementClient(com.facebook.presto.client.StatementClient) QueryResults(com.facebook.presto.client.QueryResults)

Example 14 with QueryResults

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

the class AbstractTestingPrestoClient method execute.

public T execute(Session session, @Language("SQL") String sql) {
    ResultsSession<T> resultsSession = getResultSession(session);
    ClientSession clientSession = toClientSession(session, prestoServer.getBaseUrl(), true, new Duration(2, TimeUnit.MINUTES));
    try (StatementClient client = new StatementClient(httpClient, QUERY_RESULTS_CODEC, clientSession, sql)) {
        while (client.isValid()) {
            QueryResults results = client.current();
            resultsSession.addResults(results);
            client.advance();
        }
        if (!client.isFailed()) {
            QueryResults results = client.finalResults();
            if (results.getUpdateType() != null) {
                resultsSession.setUpdateType(results.getUpdateType());
            }
            if (results.getUpdateCount() != null) {
                resultsSession.setUpdateCount(results.getUpdateCount());
            }
            return resultsSession.build(client.getSetSessionProperties(), client.getResetSessionProperties());
        }
        QueryError error = client.finalResults().getError();
        verify(error != null, "no error");
        if (error.getFailureInfo() != null) {
            throw error.getFailureInfo().toException();
        }
        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) StatementClient(com.facebook.presto.client.StatementClient) Duration(io.airlift.units.Duration) QueryError(com.facebook.presto.client.QueryError) QueryResults(com.facebook.presto.client.QueryResults)

Example 15 with QueryResults

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

the class PrestoResultSet method getColumns.

private static List<Column> getColumns(StatementClient client, Consumer<QueryStats> progressCallback) throws SQLException {
    while (client.isValid()) {
        QueryResults results = client.current();
        progressCallback.accept(QueryStats.create(results.getId(), results.getStats()));
        List<Column> columns = results.getColumns();
        if (columns != null) {
            return columns;
        }
        client.advance();
    }
    QueryResults results = client.finalResults();
    if (!client.isFailed()) {
        throw new SQLException(format("Query has no columns (#%s)", results.getId()));
    }
    throw resultsException(results);
}
Also used : Column(com.facebook.presto.client.Column) SQLException(java.sql.SQLException) QueryResults(com.facebook.presto.client.QueryResults)

Aggregations

QueryResults (com.facebook.presto.client.QueryResults)16 StatementClient (com.facebook.presto.client.StatementClient)7 ImmutableList (com.google.common.collect.ImmutableList)7 List (java.util.List)7 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 Request (io.airlift.http.client.Request)4 Test (org.testng.annotations.Test)4 Column (com.facebook.presto.client.Column)3 HiveColumn (com.airbnb.airpal.presto.hive.HiveColumn)2 Duration (io.airlift.units.Duration)2 JobState (com.airbnb.airpal.api.JobState)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