use of io.trino.sql.tree.LongLiteral in project trino by trinodb.
the class TestSqlParser method testPrecedenceAndAssociativity.
@Test
public void testPrecedenceAndAssociativity() {
assertThat(expression("1 AND 2 AND 3 AND 4")).isEqualTo(new LogicalExpression(location(1, 1), LogicalExpression.Operator.AND, ImmutableList.of(new LongLiteral(location(1, 1), "1"), new LongLiteral(location(1, 7), "2"), new LongLiteral(location(1, 13), "3"), new LongLiteral(location(1, 19), "4"))));
assertThat(expression("1 OR 2 OR 3 OR 4")).isEqualTo(new LogicalExpression(location(1, 1), LogicalExpression.Operator.OR, ImmutableList.of(new LongLiteral(location(1, 1), "1"), new LongLiteral(location(1, 6), "2"), new LongLiteral(location(1, 11), "3"), new LongLiteral(location(1, 16), "4"))));
assertThat(expression("1 AND 2 AND 3 OR 4 AND 5 AND 6 OR 7 AND 8 AND 9")).isEqualTo(new LogicalExpression(location(1, 1), LogicalExpression.Operator.OR, ImmutableList.of(new LogicalExpression(location(1, 1), LogicalExpression.Operator.AND, ImmutableList.of(new LongLiteral(location(1, 1), "1"), new LongLiteral(location(1, 7), "2"), new LongLiteral(location(1, 13), "3"))), new LogicalExpression(location(1, 18), LogicalExpression.Operator.AND, ImmutableList.of(new LongLiteral(location(1, 18), "4"), new LongLiteral(location(1, 24), "5"), new LongLiteral(location(1, 30), "6"))), new LogicalExpression(location(1, 35), LogicalExpression.Operator.AND, ImmutableList.of(new LongLiteral(location(1, 35), "7"), new LongLiteral(location(1, 41), "8"), new LongLiteral(location(1, 47), "9"))))));
assertExpression("1 AND 2 OR 3", LogicalExpression.or(LogicalExpression.and(new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3")));
assertExpression("1 OR 2 AND 3", LogicalExpression.or(new LongLiteral("1"), LogicalExpression.and(new LongLiteral("2"), new LongLiteral("3"))));
assertExpression("NOT 1 AND 2", LogicalExpression.and(new NotExpression(new LongLiteral("1")), new LongLiteral("2")));
assertExpression("NOT 1 OR 2", LogicalExpression.or(new NotExpression(new LongLiteral("1")), new LongLiteral("2")));
assertExpression("-1 + 2", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.ADD, new LongLiteral("-1"), new LongLiteral("2")));
assertExpression("1 - 2 - 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.SUBTRACT, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.SUBTRACT, new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3")));
assertExpression("1 / 2 / 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.DIVIDE, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.DIVIDE, new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3")));
assertExpression("1 + 2 * 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.ADD, new LongLiteral("1"), new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.MULTIPLY, new LongLiteral("2"), new LongLiteral("3"))));
}
use of io.trino.sql.tree.LongLiteral 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))));
}
use of io.trino.sql.tree.LongLiteral in project trino by trinodb.
the class TestSqlParser method testSelectWithLimit.
@Test
public void testSelectWithLimit() {
assertStatement("SELECT * FROM table1 LIMIT 2", simpleQuery(selectList(new AllColumns()), new Table(QualifiedName.of("table1")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Limit(new LongLiteral("2")))));
assertStatement("SELECT * FROM table1 LIMIT ALL", simpleQuery(selectList(new AllColumns()), new Table(QualifiedName.of("table1")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Limit(new AllRows()))));
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(new AllRows()))));
}
use of io.trino.sql.tree.LongLiteral in project trino by trinodb.
the class TestSqlParser method testValues.
@Test
public void testValues() {
Query valuesQuery = query(values(row(new StringLiteral("a"), new LongLiteral("1"), new DoubleLiteral("2.2")), row(new StringLiteral("b"), new LongLiteral("2"), new DoubleLiteral("3.3"))));
assertStatement("VALUES ('a', 1, 2.2e0), ('b', 2, 3.3e0)", valuesQuery);
assertStatement("SELECT * FROM (VALUES ('a', 1, 2.2e0), ('b', 2, 3.3e0))", simpleQuery(selectList(new AllColumns()), subquery(valuesQuery)));
}
use of io.trino.sql.tree.LongLiteral in project trino by trinodb.
the class TestSqlParser method testArithmeticUnary.
@Test
public void testArithmeticUnary() {
assertExpression("9", new LongLiteral("9"));
assertExpression("+9", positive(new LongLiteral("9")));
assertExpression("+ 9", positive(new LongLiteral("9")));
assertExpression("++9", positive(positive(new LongLiteral("9"))));
assertExpression("+ +9", positive(positive(new LongLiteral("9"))));
assertExpression("+ + 9", positive(positive(new LongLiteral("9"))));
assertExpression("+++9", positive(positive(positive(new LongLiteral("9")))));
assertExpression("+ + +9", positive(positive(positive(new LongLiteral("9")))));
assertExpression("+ + + 9", positive(positive(positive(new LongLiteral("9")))));
assertExpression("-9", new LongLiteral("-9"));
assertExpression("- 9", new LongLiteral("-9"));
assertExpression("- + 9", negative(positive(new LongLiteral("9"))));
assertExpression("-+9", negative(positive(new LongLiteral("9"))));
assertExpression("+ - + 9", positive(negative(positive(new LongLiteral("9")))));
assertExpression("+-+9", positive(negative(positive(new LongLiteral("9")))));
assertExpression("- -9", negative(new LongLiteral("-9")));
assertExpression("- - 9", negative(new LongLiteral("-9")));
assertExpression("- + - + 9", negative(positive(negative(positive(new LongLiteral("9"))))));
assertExpression("-+-+9", negative(positive(negative(positive(new LongLiteral("9"))))));
assertExpression("+ - + - + 9", positive(negative(positive(negative(positive(new LongLiteral("9")))))));
assertExpression("+-+-+9", positive(negative(positive(negative(positive(new LongLiteral("9")))))));
assertExpression("- - -9", negative(negative(new LongLiteral("-9"))));
assertExpression("- - - 9", negative(negative(new LongLiteral("-9"))));
}
Aggregations