Search in sources :

Example 1 with Limit

use of io.prestosql.sql.tree.Limit in project hetu-core by openlookeng.

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<Node> limit = Optional.empty();
    if (context.FETCH() != null) {
        limit = Optional.of(new FetchFirst(Optional.of(getLocation(context.FETCH())), getTextIfPresent(context.fetchFirst), context.TIES() != null));
    } else if (context.LIMIT() != null) {
        limit = Optional.of(new Limit(Optional.of(getLocation(context.LIMIT())), getTextIfPresent(context.limit).orElseThrow(() -> new IllegalStateException("Missing LIMIT value"))));
    }
    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 or fetch,
        // fold the order by, limit, offset or fetch 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, limit), Optional.empty(), Optional.empty(), Optional.empty());
    }
    return new Query(getLocation(context), Optional.empty(), term, orderBy, offset, limit);
}
Also used : OrderBy(io.prestosql.sql.tree.OrderBy) Query(io.prestosql.sql.tree.Query) WithQuery(io.prestosql.sql.tree.WithQuery) Node(io.prestosql.sql.tree.Node) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) Offset(io.prestosql.sql.tree.Offset) QuerySpecification(io.prestosql.sql.tree.QuerySpecification) SortItem(io.prestosql.sql.tree.SortItem) Limit(io.prestosql.sql.tree.Limit) QueryBody(io.prestosql.sql.tree.QueryBody) FetchFirst(io.prestosql.sql.tree.FetchFirst)

Example 2 with Limit

use of io.prestosql.sql.tree.Limit in project hetu-core by openlookeng.

the class QueryRewriter method getColumns.

private List<Column> getColumns(Connection connection, CreateTableAsSelect createTableAsSelect) throws SQLException {
    io.prestosql.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();
    io.prestosql.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(), querySpecification.getOffset(), Optional.of(new Limit("0")));
        zeroRowsQuery = new io.prestosql.sql.tree.Query(createSelectClause.getWith(), innerQuery, Optional.empty(), Optional.empty(), Optional.empty());
    } else {
        zeroRowsQuery = new io.prestosql.sql.tree.Query(createSelectClause.getWith(), innerQuery, Optional.empty(), Optional.empty(), Optional.of(new Limit("0")));
    }
    ImmutableList.Builder<Column> columns = ImmutableList.builder();
    try (java.sql.Statement jdbcStatement = connection.createStatement()) {
        ExecutorService executor = newSingleThreadExecutor();
        TimeLimiter limiter = SimpleTimeLimiter.create(executor);
        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)));
            }
        } catch (UncheckedTimeoutException e) {
            throw new SQLException("SQL statement execution timed out", e);
        } finally {
            executor.shutdownNow();
        }
    }
    return columns.build();
}
Also used : SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter) TimeLimiter(com.google.common.util.concurrent.TimeLimiter) SQLException(java.sql.SQLException) ImmutableList(com.google.common.collect.ImmutableList) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) ResultSetMetaData(java.sql.ResultSetMetaData) QuerySpecification(io.prestosql.sql.tree.QuerySpecification) SingleColumn(io.prestosql.sql.tree.SingleColumn) ExecutorService(java.util.concurrent.ExecutorService) ResultSet(java.sql.ResultSet) Limit(io.prestosql.sql.tree.Limit) QueryBody(io.prestosql.sql.tree.QueryBody)

Example 3 with Limit

use of io.prestosql.sql.tree.Limit in project hetu-core by openlookeng.

the class ImpalaAstBuilder method visitQueryNoWith.

@Override
public Node visitQueryNoWith(ImpalaSqlParser.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<Node> limit = Optional.empty();
    Optional<Offset> offset = Optional.empty();
    if (context.LIMIT() != null) {
        Optional<String> offsetValue = getTextIfPresent(context.offset);
        Optional<String> rowsValue = getTextIfPresent(context.rows);
        if (offsetValue.isPresent()) {
            offset = Optional.of(new Offset(offsetValue.get()));
        }
        if (rowsValue.isPresent()) {
            limit = Optional.of(new Limit(rowsValue.get()));
        }
    }
    if (term instanceof QuerySpecification) {
        // When we have a simple query specification
        // followed by order by, offset, limit or fetch,
        // fold the order by, limit, offset or fetch 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, limit), Optional.empty(), Optional.empty(), Optional.empty());
    }
    return new Query(getLocation(context), Optional.empty(), term, orderBy, offset, limit);
}
Also used : OrderBy(io.prestosql.sql.tree.OrderBy) Query(io.prestosql.sql.tree.Query) WithQuery(io.prestosql.sql.tree.WithQuery) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) Node(io.prestosql.sql.tree.Node) Offset(io.prestosql.sql.tree.Offset) QuerySpecification(io.prestosql.sql.tree.QuerySpecification) SortItem(io.prestosql.sql.tree.SortItem) Limit(io.prestosql.sql.tree.Limit) QueryBody(io.prestosql.sql.tree.QueryBody)

Example 4 with Limit

use of io.prestosql.sql.tree.Limit in project hetu-core by openlookeng.

the class HiveAstBuilder method visitQueryNoWith.

@Override
public Node visitQueryNoWith(HiveSqlParser.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)));
    }
    if (context.clusteredBy() != null) {
        addDiff(DiffType.UNSUPPORTED, context.CLUSTER().getText(), "[CLUSTERED BY] is not supported");
        throw unsupportedError(ErrorType.UNSUPPORTED_STATEMENT, "Unsupported attribute: CLUSTERED BY", context.clusteredBy());
    }
    if (context.distributeBy() != null) {
        addDiff(DiffType.UNSUPPORTED, context.DISTRIBUTE().getText(), "[DISTRIBUTE BY] is not supported");
        throw unsupportedError(ErrorType.UNSUPPORTED_STATEMENT, "Unsupported attribute: DISTRIBUTE BY", context.distributeBy());
    }
    if (context.sortedBy() != null) {
        addDiff(DiffType.UNSUPPORTED, context.SORT().getText(), "[SORT BY] is not supported");
        throw unsupportedError(ErrorType.UNSUPPORTED_STATEMENT, "Unsupported attribute: SORT BY", context.sortedBy());
    }
    Optional<Node> limit = Optional.empty();
    Optional<Offset> offset = Optional.empty();
    if (context.LIMIT() != null) {
        Optional<String> offsetValue = getTextIfPresent(context.offset);
        Optional<String> rowsValue = getTextIfPresent(context.rows);
        if (offsetValue.isPresent()) {
            offset = Optional.of(new Offset(offsetValue.get()));
        }
        if (rowsValue.isPresent()) {
            limit = Optional.of(new Limit(rowsValue.get()));
        }
    }
    if (term instanceof QuerySpecification) {
        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, limit), Optional.empty(), Optional.empty(), Optional.empty());
    }
    return new Query(getLocation(context), Optional.empty(), term, orderBy, offset, limit);
}
Also used : OrderBy(io.prestosql.sql.tree.OrderBy) Query(io.prestosql.sql.tree.Query) WithQuery(io.prestosql.sql.tree.WithQuery) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) Node(io.prestosql.sql.tree.Node) Offset(io.prestosql.sql.tree.Offset) QuerySpecification(io.prestosql.sql.tree.QuerySpecification) SortItem(io.prestosql.sql.tree.SortItem) Limit(io.prestosql.sql.tree.Limit) QueryBody(io.prestosql.sql.tree.QueryBody)

Example 5 with Limit

use of io.prestosql.sql.tree.Limit in project hetu-core by openlookeng.

the class TestSqlParser method testSelectWithLimit.

@Test
public void testSelectWithLimit() {
    assertStatement("SELECT * FROM table1 LIMIT 2", new Query(Optional.empty(), new QuerySpecification(selectList(new AllColumns()), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Limit("2"))), Optional.empty(), Optional.empty(), Optional.empty()));
    assertStatement("SELECT * FROM table1 LIMIT ALL", new Query(Optional.empty(), new QuerySpecification(selectList(new AllColumns()), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Limit("ALL"))), Optional.empty(), Optional.empty(), Optional.empty()));
    Query valuesQuery = query(values(row(new LongLiteral("1"), new StringLiteral("1")), row(new LongLiteral("2"), new StringLiteral("2"))));
    assertStatement("SELECT * FROM (VALUES (1, '1'), (2, '2')) LIMIT ALL", simpleQuery(selectList(new AllColumns()), subquery(valuesQuery), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Limit("ALL"))));
}
Also used : QuerySpecification(io.prestosql.sql.tree.QuerySpecification) Table(io.prestosql.sql.tree.Table) CreateTable(io.prestosql.sql.tree.CreateTable) VacuumTable(io.prestosql.sql.tree.VacuumTable) DropTable(io.prestosql.sql.tree.DropTable) RenameTable(io.prestosql.sql.tree.RenameTable) QueryUtil.simpleQuery(io.prestosql.sql.QueryUtil.simpleQuery) Query(io.prestosql.sql.tree.Query) WithQuery(io.prestosql.sql.tree.WithQuery) StringLiteral(io.prestosql.sql.tree.StringLiteral) LongLiteral(io.prestosql.sql.tree.LongLiteral) AllColumns(io.prestosql.sql.tree.AllColumns) Limit(io.prestosql.sql.tree.Limit) Test(org.testng.annotations.Test)

Aggregations

Limit (io.prestosql.sql.tree.Limit)5 QuerySpecification (io.prestosql.sql.tree.QuerySpecification)5 Query (io.prestosql.sql.tree.Query)4 QueryBody (io.prestosql.sql.tree.QueryBody)4 WithQuery (io.prestosql.sql.tree.WithQuery)4 Node (io.prestosql.sql.tree.Node)3 Offset (io.prestosql.sql.tree.Offset)3 OrderBy (io.prestosql.sql.tree.OrderBy)3 SortItem (io.prestosql.sql.tree.SortItem)3 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)3 ImmutableList (com.google.common.collect.ImmutableList)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 QueryUtil.simpleQuery (io.prestosql.sql.QueryUtil.simpleQuery)1 AllColumns (io.prestosql.sql.tree.AllColumns)1 CreateTable (io.prestosql.sql.tree.CreateTable)1 DropTable (io.prestosql.sql.tree.DropTable)1 FetchFirst (io.prestosql.sql.tree.FetchFirst)1 LongLiteral (io.prestosql.sql.tree.LongLiteral)1