Search in sources :

Example 1 with QueryBody

use of com.facebook.presto.sql.tree.QueryBody in project presto by prestodb.

the class AstBuilder method visitQueryNoWith.

@Override
public Node visitQueryNoWith(SqlBaseParser.QueryNoWithContext context) {
    QueryBody term = (QueryBody) visit(context.queryTerm());
    Optional<OrderBy> orderBy = Optional.empty();
    if (context.ORDER() != null) {
        orderBy = Optional.of(new OrderBy(getLocation(context.ORDER()), visit(context.sortItem(), SortItem.class)));
    }
    Optional<Offset> offset = Optional.empty();
    if (context.OFFSET() != null) {
        offset = Optional.of(new Offset(Optional.of(getLocation(context.OFFSET())), getTextIfPresent(context.offset).orElseThrow(() -> new IllegalStateException("Missing OFFSET row count"))));
    }
    if (term instanceof QuerySpecification) {
        // When we have a simple query specification
        // followed by order by, offset, limit, fold the order by and limit
        // clauses into the query specification (analyzer/planner
        // expects this structure to resolve references with respect
        // to columns defined in the query specification)
        QuerySpecification query = (QuerySpecification) term;
        return new Query(getLocation(context), Optional.empty(), new QuerySpecification(getLocation(context), query.getSelect(), query.getFrom(), query.getWhere(), query.getGroupBy(), query.getHaving(), orderBy, offset, getTextIfPresent(context.limit)), Optional.empty(), Optional.empty(), Optional.empty());
    }
    return new Query(getLocation(context), Optional.empty(), term, orderBy, offset, getTextIfPresent(context.limit));
}
Also used : OrderBy(com.facebook.presto.sql.tree.OrderBy) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) SortItem(com.facebook.presto.sql.tree.SortItem) Query(com.facebook.presto.sql.tree.Query) WithQuery(com.facebook.presto.sql.tree.WithQuery) QueryBody(com.facebook.presto.sql.tree.QueryBody) Offset(com.facebook.presto.sql.tree.Offset)

Example 2 with QueryBody

use of com.facebook.presto.sql.tree.QueryBody 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)

Example 3 with QueryBody

use of com.facebook.presto.sql.tree.QueryBody in project presto by prestodb.

the class AstBuilder method visitSetOperation.

@Override
public Node visitSetOperation(SqlBaseParser.SetOperationContext context) {
    QueryBody left = (QueryBody) visit(context.left);
    QueryBody right = (QueryBody) visit(context.right);
    Optional<Boolean> distinct = Optional.empty();
    if (context.setQuantifier() != null) {
        if (context.setQuantifier().DISTINCT() != null) {
            distinct = Optional.of(true);
        } else if (context.setQuantifier().ALL() != null) {
            distinct = Optional.of(false);
        }
    }
    switch(context.operator.getType()) {
        case SqlBaseLexer.UNION:
            return new Union(getLocation(context.UNION()), ImmutableList.of(left, right), distinct);
        case SqlBaseLexer.INTERSECT:
            return new Intersect(getLocation(context.INTERSECT()), ImmutableList.of(left, right), distinct);
        case SqlBaseLexer.EXCEPT:
            return new Except(getLocation(context.EXCEPT()), left, right, distinct);
    }
    throw new IllegalArgumentException("Unsupported set operation: " + context.operator.getText());
}
Also used : Intersect(com.facebook.presto.sql.tree.Intersect) QueryBody(com.facebook.presto.sql.tree.QueryBody) Union(com.facebook.presto.sql.tree.Union) Except(com.facebook.presto.sql.tree.Except)

Aggregations

QueryBody (com.facebook.presto.sql.tree.QueryBody)3 QuerySpecification (com.facebook.presto.sql.tree.QuerySpecification)2 Except (com.facebook.presto.sql.tree.Except)1 Intersect (com.facebook.presto.sql.tree.Intersect)1 Offset (com.facebook.presto.sql.tree.Offset)1 OrderBy (com.facebook.presto.sql.tree.OrderBy)1 Query (com.facebook.presto.sql.tree.Query)1 SingleColumn (com.facebook.presto.sql.tree.SingleColumn)1 SortItem (com.facebook.presto.sql.tree.SortItem)1 Union (com.facebook.presto.sql.tree.Union)1 WithQuery (com.facebook.presto.sql.tree.WithQuery)1 ImmutableList (com.google.common.collect.ImmutableList)1 SimpleTimeLimiter (com.google.common.util.concurrent.SimpleTimeLimiter)1 TimeLimiter (com.google.common.util.concurrent.TimeLimiter)1 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1