Search in sources :

Example 1 with FetchFirst

use of io.trino.sql.tree.FetchFirst in project trino by trinodb.

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) {
        Expression rowCount;
        if (context.offset.INTEGER_VALUE() != null) {
            rowCount = new LongLiteral(getLocation(context.offset.INTEGER_VALUE()), context.offset.getText());
        } else {
            rowCount = new Parameter(getLocation(context.offset.QUESTION_MARK()), parameterPosition);
            parameterPosition++;
        }
        offset = Optional.of(new Offset(Optional.of(getLocation(context.OFFSET())), rowCount));
    }
    Optional<Node> limit = Optional.empty();
    if (context.FETCH() != null) {
        Optional<Expression> rowCount = Optional.empty();
        if (context.fetchFirst != null) {
            if (context.fetchFirst.INTEGER_VALUE() != null) {
                rowCount = Optional.of(new LongLiteral(getLocation(context.fetchFirst.INTEGER_VALUE()), context.fetchFirst.getText()));
            } else {
                rowCount = Optional.of(new Parameter(getLocation(context.fetchFirst.QUESTION_MARK()), parameterPosition));
                parameterPosition++;
            }
        }
        limit = Optional.of(new FetchFirst(Optional.of(getLocation(context.FETCH())), rowCount, context.TIES() != null));
    } else if (context.LIMIT() != null) {
        if (context.limit == null) {
            throw new IllegalStateException("Missing LIMIT value");
        }
        Expression rowCount;
        if (context.limit.ALL() != null) {
            rowCount = new AllRows(getLocation(context.limit.ALL()));
        } else if (context.limit.rowCount().INTEGER_VALUE() != null) {
            rowCount = new LongLiteral(getLocation(context.limit.rowCount().INTEGER_VALUE()), context.limit.getText());
        } else {
            rowCount = new Parameter(getLocation(context.limit.rowCount().QUESTION_MARK()), parameterPosition);
            parameterPosition++;
        }
        limit = Optional.of(new Limit(Optional.of(getLocation(context.LIMIT())), rowCount));
    }
    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(), query.getWindows(), orderBy, offset, limit), Optional.empty(), Optional.empty(), Optional.empty());
    }
    return new Query(getLocation(context), Optional.empty(), term, orderBy, offset, limit);
}
Also used : OrderBy(io.trino.sql.tree.OrderBy) AllRows(io.trino.sql.tree.AllRows) Query(io.trino.sql.tree.Query) WithQuery(io.trino.sql.tree.WithQuery) LongLiteral(io.trino.sql.tree.LongLiteral) Node(io.trino.sql.tree.Node) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) Offset(io.trino.sql.tree.Offset) QuerySpecification(io.trino.sql.tree.QuerySpecification) SortItem(io.trino.sql.tree.SortItem) DereferenceExpression(io.trino.sql.tree.DereferenceExpression) LogicalExpression(io.trino.sql.tree.LogicalExpression) SearchedCaseExpression(io.trino.sql.tree.SearchedCaseExpression) BindExpression(io.trino.sql.tree.BindExpression) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) SimpleCaseExpression(io.trino.sql.tree.SimpleCaseExpression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) LambdaExpression(io.trino.sql.tree.LambdaExpression) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) NullIfExpression(io.trino.sql.tree.NullIfExpression) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) InListExpression(io.trino.sql.tree.InListExpression) NotExpression(io.trino.sql.tree.NotExpression) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) TryExpression(io.trino.sql.tree.TryExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) IfExpression(io.trino.sql.tree.IfExpression) Expression(io.trino.sql.tree.Expression) NumericParameter(io.trino.sql.tree.NumericParameter) DataTypeParameter(io.trino.sql.tree.DataTypeParameter) TypeParameter(io.trino.sql.tree.TypeParameter) Parameter(io.trino.sql.tree.Parameter) Limit(io.trino.sql.tree.Limit) QueryBody(io.trino.sql.tree.QueryBody) FetchFirst(io.trino.sql.tree.FetchFirst)

Example 2 with FetchFirst

use of io.trino.sql.tree.FetchFirst in project trino by trinodb.

the class TestSqlParser method testSelectWithFetch.

@Test
public void testSelectWithFetch() {
    assertStatement("SELECT * FROM table1 FETCH FIRST 2 ROWS ONLY", simpleQuery(selectList(new AllColumns()), new Table(QualifiedName.of("table1")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new FetchFirst(new LongLiteral("2")))));
    assertStatement("SELECT * FROM table1 FETCH NEXT ROW ONLY", simpleQuery(selectList(new AllColumns()), new Table(QualifiedName.of("table1")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new FetchFirst(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')) FETCH FIRST ROW ONLY", simpleQuery(selectList(new AllColumns()), subquery(valuesQuery), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new FetchFirst(Optional.empty()))));
    assertStatement("SELECT * FROM (VALUES (1, '1'), (2, '2')) FETCH FIRST ROW WITH TIES", simpleQuery(selectList(new AllColumns()), subquery(valuesQuery), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new FetchFirst(Optional.empty(), true))));
    assertStatement("SELECT * FROM table1 FETCH FIRST 2 ROWS WITH TIES", simpleQuery(selectList(new AllColumns()), new Table(QualifiedName.of("table1")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new FetchFirst(new LongLiteral("2"), true))));
    assertStatement("SELECT * FROM table1 FETCH NEXT ROW WITH TIES", simpleQuery(selectList(new AllColumns()), new Table(QualifiedName.of("table1")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new FetchFirst(Optional.empty(), true))));
}
Also used : CreateTable(io.trino.sql.tree.CreateTable) DropTable(io.trino.sql.tree.DropTable) Table(io.trino.sql.tree.Table) TruncateTable(io.trino.sql.tree.TruncateTable) RenameTable(io.trino.sql.tree.RenameTable) QueryUtil.simpleQuery(io.trino.sql.QueryUtil.simpleQuery) Query(io.trino.sql.tree.Query) WithQuery(io.trino.sql.tree.WithQuery) StringLiteral(io.trino.sql.tree.StringLiteral) LongLiteral(io.trino.sql.tree.LongLiteral) AllColumns(io.trino.sql.tree.AllColumns) FetchFirst(io.trino.sql.tree.FetchFirst) Test(org.junit.jupiter.api.Test)

Example 3 with FetchFirst

use of io.trino.sql.tree.FetchFirst in project trino by trinodb.

the class TestSqlParser method testPrepareWithParameters.

@Test
public void testPrepareWithParameters() {
    assertStatement("PREPARE myquery FROM SELECT ?, ? FROM foo", new Prepare(identifier("myquery"), simpleQuery(selectList(new Parameter(0), new Parameter(1)), table(QualifiedName.of("foo")))));
    assertStatement("PREPARE myquery FROM SELECT * FROM foo LIMIT ?", new Prepare(identifier("myquery"), simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("foo")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Limit(new Parameter(0))))));
    assertStatement("PREPARE myquery FROM SELECT ?, ? FROM foo LIMIT ?", new Prepare(identifier("myquery"), simpleQuery(selectList(new Parameter(0), new Parameter(1)), table(QualifiedName.of("foo")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Limit(new Parameter(2))))));
    assertStatement("PREPARE myquery FROM SELECT ? FROM foo FETCH FIRST ? ROWS ONLY", new Prepare(identifier("myquery"), simpleQuery(selectList(new Parameter(0)), table(QualifiedName.of("foo")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new FetchFirst(new Parameter(1))))));
    assertStatement("PREPARE myquery FROM SELECT ?, ? FROM foo FETCH NEXT ? ROWS WITH TIES", new Prepare(identifier("myquery"), simpleQuery(selectList(new Parameter(0), new Parameter(1)), table(QualifiedName.of("foo")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new FetchFirst(new Parameter(2), true)))));
    assertStatement("PREPARE myquery FROM SELECT ?, ? FROM foo OFFSET ? ROWS", new Prepare(identifier("myquery"), simpleQuery(selectList(new Parameter(0), new Parameter(1)), table(QualifiedName.of("foo")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Offset(new Parameter(2))), Optional.empty())));
    assertStatement("PREPARE myquery FROM SELECT ? FROM foo OFFSET ? ROWS LIMIT ?", new Prepare(identifier("myquery"), simpleQuery(selectList(new Parameter(0)), table(QualifiedName.of("foo")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Offset(new Parameter(1))), Optional.of(new Limit(new Parameter(2))))));
    assertStatement("PREPARE myquery FROM SELECT ? FROM foo OFFSET ? ROWS FETCH FIRST ? ROWS WITH TIES", new Prepare(identifier("myquery"), simpleQuery(selectList(new Parameter(0)), table(QualifiedName.of("foo")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Offset(new Parameter(1))), Optional.of(new FetchFirst(new Parameter(2), true)))));
}
Also used : Prepare(io.trino.sql.tree.Prepare) Parameter(io.trino.sql.tree.Parameter) AllColumns(io.trino.sql.tree.AllColumns) Limit(io.trino.sql.tree.Limit) FetchFirst(io.trino.sql.tree.FetchFirst) Offset(io.trino.sql.tree.Offset) Test(org.junit.jupiter.api.Test)

Aggregations

FetchFirst (io.trino.sql.tree.FetchFirst)3 AllColumns (io.trino.sql.tree.AllColumns)2 Limit (io.trino.sql.tree.Limit)2 LongLiteral (io.trino.sql.tree.LongLiteral)2 Offset (io.trino.sql.tree.Offset)2 Parameter (io.trino.sql.tree.Parameter)2 Query (io.trino.sql.tree.Query)2 WithQuery (io.trino.sql.tree.WithQuery)2 QueryUtil.simpleQuery (io.trino.sql.QueryUtil.simpleQuery)1 AllRows (io.trino.sql.tree.AllRows)1 ArithmeticBinaryExpression (io.trino.sql.tree.ArithmeticBinaryExpression)1 ArithmeticUnaryExpression (io.trino.sql.tree.ArithmeticUnaryExpression)1 BindExpression (io.trino.sql.tree.BindExpression)1 CoalesceExpression (io.trino.sql.tree.CoalesceExpression)1 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)1 CreateTable (io.trino.sql.tree.CreateTable)1 DataTypeParameter (io.trino.sql.tree.DataTypeParameter)1 DereferenceExpression (io.trino.sql.tree.DereferenceExpression)1 DropTable (io.trino.sql.tree.DropTable)1 Expression (io.trino.sql.tree.Expression)1