Search in sources :

Example 1 with PrestoStatement

use of com.facebook.presto.jdbc.PrestoStatement in project presto by prestodb.

the class Validator method executeQuery.

private QueryResult executeQuery(String url, String username, String password, Query query, String sql, Duration timeout, Map<String, String> sessionProperties) {
    String queryId = null;
    try (Connection connection = DriverManager.getConnection(url, username, password)) {
        trySetConnectionProperties(query, connection);
        for (Map.Entry<String, String> entry : sessionProperties.entrySet()) {
            connection.unwrap(PrestoConnection.class).setSessionProperty(entry.getKey(), entry.getValue());
        }
        try (Statement statement = connection.createStatement()) {
            TimeLimiter limiter = new SimpleTimeLimiter();
            Stopwatch stopwatch = Stopwatch.createStarted();
            Statement limitedStatement = limiter.newProxy(statement, Statement.class, timeout.toMillis(), TimeUnit.MILLISECONDS);
            if (explainOnly) {
                sql = "EXPLAIN " + sql;
            }
            long start = System.nanoTime();
            PrestoStatement prestoStatement = limitedStatement.unwrap(PrestoStatement.class);
            ProgressMonitor progressMonitor = new ProgressMonitor();
            prestoStatement.setProgressMonitor(progressMonitor);
            try {
                boolean isSelectQuery = limitedStatement.execute(sql);
                List<List<Object>> results = null;
                if (isSelectQuery) {
                    results = limiter.callWithTimeout(getResultSetConverter(limitedStatement.getResultSet()), timeout.toMillis() - stopwatch.elapsed(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS, true);
                } else {
                    results = ImmutableList.of(ImmutableList.of(limitedStatement.getLargeUpdateCount()));
                }
                prestoStatement.clearProgressMonitor();
                QueryStats queryStats = progressMonitor.getFinalQueryStats();
                if (queryStats == null) {
                    throw new VerifierException("Cannot fetch query stats");
                }
                Duration queryCpuTime = new Duration(queryStats.getCpuTimeMillis(), TimeUnit.MILLISECONDS);
                queryId = queryStats.getQueryId();
                return new QueryResult(State.SUCCESS, null, nanosSince(start), queryCpuTime, queryId, results);
            } catch (AssertionError e) {
                if (e.getMessage().startsWith("unimplemented type:")) {
                    return new QueryResult(State.INVALID, null, null, null, queryId, ImmutableList.of());
                }
                throw e;
            } catch (SQLException | VerifierException e) {
                throw e;
            } catch (UncheckedTimeoutException e) {
                return new QueryResult(State.TIMEOUT, null, null, null, queryId, ImmutableList.of());
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw Throwables.propagate(e);
            } catch (Exception e) {
                throw Throwables.propagate(e);
            }
        }
    } catch (SQLException e) {
        Exception exception = e;
        if (("Error executing query".equals(e.getMessage()) || "Error fetching results".equals(e.getMessage())) && (e.getCause() instanceof Exception)) {
            exception = (Exception) e.getCause();
        }
        State state = isPrestoQueryInvalid(e) ? State.INVALID : State.FAILED;
        return new QueryResult(state, exception, null, null, null, null);
    } catch (VerifierException e) {
        return new QueryResult(State.TOO_MANY_ROWS, e, null, null, null, null);
    }
}
Also used : SQLException(java.sql.SQLException) Stopwatch(com.google.common.base.Stopwatch) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) PrestoStatement(com.facebook.presto.jdbc.PrestoStatement) Collections.unmodifiableList(java.util.Collections.unmodifiableList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter) TimeLimiter(com.google.common.util.concurrent.TimeLimiter) PrestoStatement(com.facebook.presto.jdbc.PrestoStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) PrestoConnection(com.facebook.presto.jdbc.PrestoConnection) Duration(io.airlift.units.Duration) PrestoConnection(com.facebook.presto.jdbc.PrestoConnection) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) QueryStats(com.facebook.presto.jdbc.QueryStats) State(com.facebook.presto.verifier.QueryResult.State) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Map(java.util.Map) SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter)

Example 2 with PrestoStatement

use of com.facebook.presto.jdbc.PrestoStatement in project presto by prestodb.

the class JdbcPrestoAction method executeOnce.

private <T> T executeOnce(Statement statement, QueryStage queryStage, StatementExecutor<T> statementExecutor) {
    String query = formatSql(statement, Optional.empty());
    String clientInfo = new ClientInfo(testId, testName, verificationContext.getSourceQueryName(), verificationContext.getSuite()).serialize();
    try (PrestoConnection connection = getConnection(queryStage, clientInfo)) {
        try (java.sql.Statement jdbcStatement = connection.createStatement()) {
            PrestoStatement prestoStatement = jdbcStatement.unwrap(PrestoStatement.class);
            prestoStatement.setProgressMonitor(statementExecutor.getProgressMonitor());
            return statementExecutor.execute(prestoStatement, query);
        }
    } catch (SQLException e) {
        throw exceptionClassifier.createException(queryStage, statementExecutor.getProgressMonitor().getLastQueryStats(), e);
    }
}
Also used : PrestoStatement(com.facebook.presto.jdbc.PrestoStatement) SQLException(java.sql.SQLException) PrestoConnection(com.facebook.presto.jdbc.PrestoConnection)

Example 3 with PrestoStatement

use of com.facebook.presto.jdbc.PrestoStatement in project presto by prestodb.

the class JdbcPrestoAction method executeOnce.

private <R> QueryResult<R> executeOnce(Statement statement, Optional<ResultSetConverter<R>> converter) {
    String query = formatSql(statement, Optional.empty());
    ProgressMonitor progressMonitor = new ProgressMonitor();
    try (PrestoConnection connection = getConnection()) {
        try (java.sql.Statement jdbcStatement = connection.createStatement()) {
            PrestoStatement prestoStatement = jdbcStatement.unwrap(PrestoStatement.class);
            prestoStatement.setProgressMonitor(progressMonitor);
            ImmutableList.Builder<R> rows = ImmutableList.builder();
            if (converter.isPresent()) {
                try (ResultSet resultSet = jdbcStatement.executeQuery(query)) {
                    while (resultSet.next()) {
                        rows.add(converter.get().apply(resultSet));
                    }
                }
            } else {
                boolean moreResults = jdbcStatement.execute(query);
                if (moreResults) {
                    consumeResultSet(jdbcStatement.getResultSet());
                }
                do {
                    moreResults = jdbcStatement.getMoreResults();
                    if (moreResults) {
                        consumeResultSet(jdbcStatement.getResultSet());
                    }
                } while (moreResults || jdbcStatement.getUpdateCount() != -1);
            }
            checkState(progressMonitor.getLastQueryStats().isPresent(), "lastQueryStats is missing");
            return new QueryResult<>(rows.build(), progressMonitor.getLastQueryStats().get());
        }
    } catch (SQLException e) {
        throw exceptionClassifier.createException(progressMonitor.getLastQueryStats(), e);
    }
}
Also used : SQLException(java.sql.SQLException) ImmutableList(com.google.common.collect.ImmutableList) PrestoConnection(com.facebook.presto.jdbc.PrestoConnection) PrestoStatement(com.facebook.presto.jdbc.PrestoStatement) QueryResult(com.facebook.presto.benchmark.framework.QueryResult) ResultSet(java.sql.ResultSet)

Aggregations

PrestoConnection (com.facebook.presto.jdbc.PrestoConnection)3 PrestoStatement (com.facebook.presto.jdbc.PrestoStatement)3 SQLException (java.sql.SQLException)3 ImmutableList (com.google.common.collect.ImmutableList)2 QueryResult (com.facebook.presto.benchmark.framework.QueryResult)1 QueryStats (com.facebook.presto.jdbc.QueryStats)1 State (com.facebook.presto.verifier.QueryResult.State)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Stopwatch (com.google.common.base.Stopwatch)1 SimpleTimeLimiter (com.google.common.util.concurrent.SimpleTimeLimiter)1 TimeLimiter (com.google.common.util.concurrent.TimeLimiter)1 UncheckedTimeoutException (com.google.common.util.concurrent.UncheckedTimeoutException)1 Duration (io.airlift.units.Duration)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLClientInfoException (java.sql.SQLClientInfoException)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 Collections.unmodifiableList (java.util.Collections.unmodifiableList)1 List (java.util.List)1