Search in sources :

Example 6 with TimeLimiter

use of com.google.common.util.concurrent.TimeLimiter in project presto by prestodb.

the class QueryRewriter method getColumns.

private List<Column> getColumns(Connection connection, CreateTableAsSelect createTableAsSelect) throws SQLException {
    com.facebook.presto.sql.tree.Query createSelectClause = createTableAsSelect.getQuery();
    // Rewrite the query to select zero rows, so that we can get the column names and types
    QueryBody innerQuery = createSelectClause.getQueryBody();
    com.facebook.presto.sql.tree.Query zeroRowsQuery;
    if (innerQuery instanceof QuerySpecification) {
        QuerySpecification querySpecification = (QuerySpecification) innerQuery;
        innerQuery = new QuerySpecification(querySpecification.getSelect(), querySpecification.getFrom(), querySpecification.getWhere(), querySpecification.getGroupBy(), querySpecification.getHaving(), querySpecification.getOrderBy(), Optional.of("0"));
        zeroRowsQuery = new com.facebook.presto.sql.tree.Query(createSelectClause.getWith(), innerQuery, Optional.empty(), Optional.empty());
    } else {
        zeroRowsQuery = new com.facebook.presto.sql.tree.Query(createSelectClause.getWith(), innerQuery, Optional.empty(), Optional.of("0"));
    }
    ImmutableList.Builder<Column> columns = ImmutableList.builder();
    try (java.sql.Statement jdbcStatement = connection.createStatement()) {
        TimeLimiter limiter = new SimpleTimeLimiter();
        java.sql.Statement limitedStatement = limiter.newProxy(jdbcStatement, java.sql.Statement.class, timeout.toMillis(), TimeUnit.MILLISECONDS);
        try (ResultSet resultSet = limitedStatement.executeQuery(formatSql(zeroRowsQuery, Optional.empty()))) {
            ResultSetMetaData metaData = resultSet.getMetaData();
            for (int i = 1; i <= metaData.getColumnCount(); i++) {
                String name = metaData.getColumnName(i);
                int type = metaData.getColumnType(i);
                columns.add(new Column(name, APPROXIMATE_TYPES.contains(type)));
            }
        }
    }
    return columns.build();
}
Also used : SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter) TimeLimiter(com.google.common.util.concurrent.TimeLimiter) ImmutableList(com.google.common.collect.ImmutableList) ResultSetMetaData(java.sql.ResultSetMetaData) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) SingleColumn(com.facebook.presto.sql.tree.SingleColumn) ResultSet(java.sql.ResultSet) QueryBody(com.facebook.presto.sql.tree.QueryBody) SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter)

Aggregations

SimpleTimeLimiter (com.google.common.util.concurrent.SimpleTimeLimiter)6 TimeLimiter (com.google.common.util.concurrent.TimeLimiter)6 ImmutableList (com.google.common.collect.ImmutableList)2 IExpr (org.matheclipse.core.interfaces.IExpr)2 MathException (org.matheclipse.parser.client.math.MathException)2 PrestoConnection (com.facebook.presto.jdbc.PrestoConnection)1 PrestoStatement (com.facebook.presto.jdbc.PrestoStatement)1 QueryStats (com.facebook.presto.jdbc.QueryStats)1 QueryBody (com.facebook.presto.sql.tree.QueryBody)1 QuerySpecification (com.facebook.presto.sql.tree.QuerySpecification)1 SingleColumn (com.facebook.presto.sql.tree.SingleColumn)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 UncheckedTimeoutException (com.google.common.util.concurrent.UncheckedTimeoutException)1 BoundedExecutor (io.airlift.concurrent.BoundedExecutor)1 ExecutorServiceAdapter (io.airlift.concurrent.ExecutorServiceAdapter)1 Duration (io.airlift.units.Duration)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1