Search in sources :

Example 6 with QueryError

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

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 : StatementClient(io.prestosql.client.StatementClient) StatementClientFactory.newStatementClient(io.prestosql.client.StatementClientFactory.newStatementClient) QueryError(io.prestosql.client.QueryError)

Example 7 with QueryError

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

the class TestServer method testInvalidSessionError.

@Test
public void testInvalidSessionError() {
    String invalidTimeZone = "this_is_an_invalid_time_zone";
    Request request = preparePost().setHeader(PRESTO_USER, "user").setUri(uriFor("/v1/statement")).setBodyGenerator(createStaticBodyGenerator("show catalogs", UTF_8)).setHeader(PRESTO_SOURCE, "source").setHeader(PRESTO_CATALOG, "catalog").setHeader(PRESTO_SCHEMA, "schema").setHeader(PRESTO_PATH, "path").setHeader(PRESTO_TIME_ZONE, invalidTimeZone).build();
    QueryResults queryResults = client.execute(request, createJsonResponseHandler(QUERY_RESULTS_CODEC));
    while (queryResults.getNextUri() != null) {
        queryResults = client.execute(prepareGet().setUri(queryResults.getNextUri()).build(), createJsonResponseHandler(QUERY_RESULTS_CODEC));
    }
    QueryError queryError = queryResults.getError();
    assertNotNull(queryError);
    TimeZoneNotSupportedException expected = new TimeZoneNotSupportedException(invalidTimeZone);
    assertEquals(queryError.getErrorCode(), expected.getErrorCode().getCode());
    assertEquals(queryError.getErrorName(), expected.getErrorCode().getName());
    assertEquals(queryError.getErrorType(), expected.getErrorCode().getType().name());
    assertEquals(queryError.getMessage(), expected.getMessage());
}
Also used : Request(io.airlift.http.client.Request) QueryError(io.prestosql.client.QueryError) TimeZoneNotSupportedException(io.prestosql.spi.type.TimeZoneNotSupportedException) QueryResults(io.prestosql.client.QueryResults) Test(org.testng.annotations.Test)

Example 8 with QueryError

use of io.prestosql.client.QueryError 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 9 with QueryError

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

the class Execution method updateJobInfo.

protected void updateJobInfo(Set<Table> usedTables, List<Column> columns, QueryStats queryStats, JobState state, QueryError error) {
    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.getSemanticErrorName(), error.getErrorType(), error.getErrorLocation(), failureInfo);
        job.setError(queryError);
    }
}
Also used : FailureInfo(io.prestosql.client.FailureInfo) QueryError(io.prestosql.client.QueryError)

Example 10 with QueryError

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

the class Query method toQueryError.

private static QueryError toQueryError(QueryInfo queryInfo) {
    QueryState state = queryInfo.getState();
    if (state != FAILED) {
        return null;
    }
    ExecutionFailureInfo executionFailure;
    if (queryInfo.getFailureInfo() != null) {
        executionFailure = queryInfo.getFailureInfo();
    } else {
        log.warn("Query %s in state %s has no failure info", queryInfo.getQueryId(), state);
        executionFailure = toFailure(new RuntimeException(format("Query is %s (reason unknown)", state)));
    }
    FailureInfo failure = executionFailure.toFailureInfoWithoutStack();
    ErrorCode errorCode;
    if (queryInfo.getErrorCode() != null) {
        errorCode = queryInfo.getErrorCode();
    } else {
        errorCode = GENERIC_INTERNAL_ERROR.toErrorCode();
        log.warn("Failed query %s has no error code", queryInfo.getQueryId());
    }
    return new QueryError(firstNonNull(failure.getMessage(), "Internal error"), null, errorCode.getCode(), errorCode.getName(), executionFailure.getSemanticErrorCode().map(SemanticErrorCode::name), errorCode.getType().toString(), failure.getErrorLocation(), failure);
}
Also used : FailureInfo(io.prestosql.client.FailureInfo) ExecutionFailureInfo(io.prestosql.execution.ExecutionFailureInfo) QueryState(io.prestosql.execution.QueryState) SemanticErrorCode(io.prestosql.sql.analyzer.SemanticErrorCode) ErrorCode(io.prestosql.spi.ErrorCode) QueryError(io.prestosql.client.QueryError) ExecutionFailureInfo(io.prestosql.execution.ExecutionFailureInfo)

Aggregations

QueryError (io.prestosql.client.QueryError)11 QueryStatusInfo (io.prestosql.client.QueryStatusInfo)4 StatementClient (io.prestosql.client.StatementClient)3 ClientSession (io.prestosql.client.ClientSession)2 FailureInfo (io.prestosql.client.FailureInfo)2 StatementClientFactory.newStatementClient (io.prestosql.client.StatementClientFactory.newStatementClient)2 SQLException (java.sql.SQLException)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 Request (io.airlift.http.client.Request)1 Duration (io.airlift.units.Duration)1 Column (io.prestosql.client.Column)1 ErrorLocation (io.prestosql.client.ErrorLocation)1 QueryData (io.prestosql.client.QueryData)1 QueryResults (io.prestosql.client.QueryResults)1 ExecutionFailureInfo (io.prestosql.execution.ExecutionFailureInfo)1 QueryState (io.prestosql.execution.QueryState)1 QueryStats (io.prestosql.execution.QueryStats)1 ExecutionFailureException (io.prestosql.queryeditorui.execution.ExecutionClient.ExecutionFailureException)1