Search in sources :

Example 1 with QueryData

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

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.isRunning() && !Thread.currentThread().isInterrupted()) {
            QueryData results = client.currentData();
            if (results.getData() != null) {
                for (List<Object> row : results.getData()) {
                    cache.add((String) row.get(0));
                }
            }
            client.advance();
        }
    }
    return cache.build();
}
Also used : QueryData(io.prestosql.client.QueryData) ImmutableList(com.google.common.collect.ImmutableList) StatementClient(io.prestosql.client.StatementClient)

Example 2 with QueryData

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

the class CubeQuery method resultsCubeInitialQuery.

private boolean resultsCubeInitialQuery(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());
            // /populate results from here
            if (cubeConsole.getListRowBufferIterationItems().size() == EMPTY_ITERATION_LIST_SIZE) {
                if (client.isFinished() && client.finalStatusInfo().getError() == null) {
                    QueryData queryData = ((QueryResults) results);
                    if (queryData.getData() != null) {
                        if (queryData.getData().iterator().hasNext()) {
                            if (queryData.getData().iterator().next().iterator().hasNext()) {
                                cubeInitQueryResult = queryData.getData().iterator().next().iterator().next().toString();
                            }
                        }
                    }
                }
            }
        }
    }
    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 : QueryData(io.prestosql.client.QueryData) QueryStatusInfo(io.prestosql.client.QueryStatusInfo) QueryResults(io.prestosql.client.QueryResults)

Example 3 with QueryData

use of io.prestosql.client.QueryData 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)

Example 4 with QueryData

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

the class ColumnService method queryColumns.

private List<Column> queryColumns(String fqnTableName, String user) {
    String statement = format("SHOW COLUMNS FROM %s", fqnTableName);
    QueryRunner queryRunner = queryRunnerFactory.create(QueryEditorUIModule.UI_QUERY_SOURCE, user);
    QueryClient queryClient = new QueryClient(queryRunner, Duration.standardSeconds(60), statement);
    final ImmutableList.Builder<Column> cache = ImmutableList.builder();
    try {
        queryClient.executeWith(new Function<StatementClient, Void>() {

            @Nullable
            @Override
            public Void apply(StatementClient client) {
                QueryData results = client.currentData();
                if (results.getData() != null) {
                    for (List<Object> row : results.getData()) {
                        TypeSignature typeSignature = TypeSignature.parseTypeSignature((String) row.get(1));
                        Column column = new Column((String) row.get(0), (String) row.get(1), Query.toClientTypeSignature(typeSignature));
                        cache.add(column);
                    }
                }
                return null;
            }
        });
    } catch (QueryClient.QueryTimeOutException e) {
        log.error("Caught timeout loading columns", e);
    }
    return cache.build();
}
Also used : QueryData(io.prestosql.client.QueryData) ImmutableList(com.google.common.collect.ImmutableList) StatementClient(io.prestosql.client.StatementClient) QueryRunner(io.prestosql.queryeditorui.execution.QueryRunner) QueryClient(io.prestosql.queryeditorui.execution.QueryClient) TypeSignature(io.prestosql.spi.type.TypeSignature) Column(io.prestosql.client.Column) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Nullable(javax.annotation.Nullable)

Example 5 with QueryData

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

the class PreviewTableService method queryRows.

private List<List<Object>> queryRows(String fqnTableName, String user) {
    String statement = format("SELECT * FROM %s LIMIT %d", fqnTableName, PREVIEW_LIMIT);
    QueryRunner queryRunner = queryRunnerFactory.create(QueryEditorUIModule.UI_QUERY_SOURCE, user);
    QueryClient queryClient = new QueryClient(queryRunner, Duration.standardSeconds(60), statement);
    final ImmutableList.Builder<List<Object>> cache = ImmutableList.builder();
    try {
        queryClient.executeWith(new Function<StatementClient, Void>() {

            @Nullable
            @Override
            public Void apply(StatementClient client) {
                QueryData results = client.currentData();
                if (results.getData() != null) {
                    List<List<Object>> resultsData = ImmutableList.copyOf(results.getData());
                    for (List<Object> row : resultsData) {
                        final String[] values = new String[row.size()];
                        for (int i = 0; i < values.length; i++) {
                            // Display byte array as Hexadecimal in order to keep consistent with OpenLooKeng client
                            if (row.get(i) instanceof byte[]) {
                                byte[] bytes = (byte[]) row.get(i);
                                StringBuilder sb = new StringBuilder();
                                for (byte b : bytes) {
                                    String hex = Integer.toHexString(b & 0xFF);
                                    if (hex.length() < 2) {
                                        sb.append(0);
                                    }
                                    sb.append(hex);
                                }
                                values[i] = sb.toString();
                            } else {
                                final Object value = row.get(i);
                                values[i] = (value == null) ? "" : value.toString();
                            }
                        }
                        cache.add(Arrays.asList(values));
                    }
                }
                return null;
            }
        });
    } catch (QueryClient.QueryTimeOutException e) {
        log.error("Caught timeout loading columns", e);
    }
    return cache.build();
}
Also used : QueryData(io.prestosql.client.QueryData) ImmutableList(com.google.common.collect.ImmutableList) StatementClient(io.prestosql.client.StatementClient) QueryRunner(io.prestosql.queryeditorui.execution.QueryRunner) QueryClient(io.prestosql.queryeditorui.execution.QueryClient) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Nullable(javax.annotation.Nullable)

Aggregations

QueryData (io.prestosql.client.QueryData)6 ImmutableList (com.google.common.collect.ImmutableList)5 StatementClient (io.prestosql.client.StatementClient)4 List (java.util.List)4 QueryClient (io.prestosql.queryeditorui.execution.QueryClient)3 Nullable (javax.annotation.Nullable)3 Column (io.prestosql.client.Column)2 QueryStatusInfo (io.prestosql.client.QueryStatusInfo)2 QueryRunner (io.prestosql.queryeditorui.execution.QueryRunner)2 HashSet (java.util.HashSet)2 ErrorLocation (io.prestosql.client.ErrorLocation)1 QueryError (io.prestosql.client.QueryError)1 QueryResults (io.prestosql.client.QueryResults)1 QueryStats (io.prestosql.execution.QueryStats)1 ExecutionFailureException (io.prestosql.queryeditorui.execution.ExecutionClient.ExecutionFailureException)1 QueryTimeOutException (io.prestosql.queryeditorui.execution.QueryClient.QueryTimeOutException)1 BasicQueryInfo (io.prestosql.queryeditorui.execution.QueryInfoClient.BasicQueryInfo)1 FileTooLargeException (io.prestosql.queryeditorui.output.builders.FileTooLargeException)1 JobOutputBuilder (io.prestosql.queryeditorui.output.builders.JobOutputBuilder)1 Persistor (io.prestosql.queryeditorui.output.persistors.Persistor)1