Search in sources :

Example 1 with SubscriptExpression

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

the class TestSqlParser method testSelectWithRowType.

@Test
public void testSelectWithRowType() throws Exception {
    assertStatement("SELECT col1.f1, col2, col3.f1.f2.f3 FROM table1", new Query(Optional.empty(), new QuerySpecification(selectList(new DereferenceExpression(new Identifier("col1"), "f1"), new Identifier("col2"), new DereferenceExpression(new DereferenceExpression(new DereferenceExpression(new Identifier("col3"), "f1"), "f2"), "f3")), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty()));
    assertStatement("SELECT col1.f1[0], col2, col3[2].f2.f3, col4[4] FROM table1", new Query(Optional.empty(), new QuerySpecification(selectList(new SubscriptExpression(new DereferenceExpression(new Identifier("col1"), "f1"), new LongLiteral("0")), new Identifier("col2"), new DereferenceExpression(new DereferenceExpression(new SubscriptExpression(new Identifier("col3"), new LongLiteral("2")), "f2"), "f3"), new SubscriptExpression(new Identifier("col4"), new LongLiteral("4"))), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty()));
    assertStatement("SELECT CAST(ROW(11, 12) AS ROW(COL0 INTEGER, COL1 INTEGER)).col0", new Query(Optional.empty(), new QuerySpecification(selectList(new DereferenceExpression(new Cast(new Row(Lists.newArrayList(new LongLiteral("11"), new LongLiteral("12"))), "ROW(COL0 INTEGER,COL1 INTEGER)"), "col0")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty(), Optional.empty()));
}
Also used : Cast(com.facebook.presto.sql.tree.Cast) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) Identifier(com.facebook.presto.sql.tree.Identifier) CreateTable(com.facebook.presto.sql.tree.CreateTable) DropTable(com.facebook.presto.sql.tree.DropTable) Table(com.facebook.presto.sql.tree.Table) RenameTable(com.facebook.presto.sql.tree.RenameTable) Query(com.facebook.presto.sql.tree.Query) QueryUtil.simpleQuery(com.facebook.presto.sql.QueryUtil.simpleQuery) WithQuery(com.facebook.presto.sql.tree.WithQuery) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) Row(com.facebook.presto.sql.tree.Row) Test(org.testng.annotations.Test)

Example 2 with SubscriptExpression

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

the class TestSqlParser method testArraySubscript.

@Test
public void testArraySubscript() throws Exception {
    assertExpression("ARRAY [1, 2][1]", new SubscriptExpression(new ArrayConstructor(ImmutableList.of(new LongLiteral("1"), new LongLiteral("2"))), new LongLiteral("1")));
    try {
        assertExpression("CASE WHEN TRUE THEN ARRAY[1,2] END[1]", null);
        fail();
    } catch (RuntimeException e) {
    // Expected
    }
}
Also used : LongLiteral(com.facebook.presto.sql.tree.LongLiteral) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) ArrayConstructor(com.facebook.presto.sql.tree.ArrayConstructor) Test(org.testng.annotations.Test)

Example 3 with SubscriptExpression

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

the class TestEqualityInference method testExpressionsThatMayReturnNullOnNonNullInput.

@Test
public void testExpressionsThatMayReturnNullOnNonNullInput() throws Exception {
    List<Expression> candidates = ImmutableList.of(// try_cast
    new Cast(nameReference("b"), "BIGINT", true), new FunctionCall(QualifiedName.of("try"), ImmutableList.of(nameReference("b"))), new NullIfExpression(nameReference("b"), number(1)), new IfExpression(nameReference("b"), number(1), new NullLiteral()), new DereferenceExpression(nameReference("b"), "x"), new InPredicate(nameReference("b"), new InListExpression(ImmutableList.of(new NullLiteral()))), new SearchedCaseExpression(ImmutableList.of(new WhenClause(new IsNotNullPredicate(nameReference("b")), new NullLiteral())), Optional.empty()), new SimpleCaseExpression(nameReference("b"), ImmutableList.of(new WhenClause(number(1), new NullLiteral())), Optional.empty()), new SubscriptExpression(new ArrayConstructor(ImmutableList.of(new NullLiteral())), nameReference("b")));
    for (Expression candidate : candidates) {
        EqualityInference.Builder builder = new EqualityInference.Builder();
        builder.extractInferenceCandidates(equals(nameReference("b"), nameReference("x")));
        builder.extractInferenceCandidates(equals(nameReference("a"), candidate));
        EqualityInference inference = builder.build();
        List<Expression> equalities = inference.generateEqualitiesPartitionedBy(matchesSymbols("b")).getScopeStraddlingEqualities();
        assertEquals(equalities.size(), 1);
        assertTrue(equalities.get(0).equals(equals(nameReference("x"), nameReference("b"))) || equalities.get(0).equals(equals(nameReference("b"), nameReference("x"))));
    }
}
Also used : Cast(com.facebook.presto.sql.tree.Cast) NullIfExpression(com.facebook.presto.sql.tree.NullIfExpression) IfExpression(com.facebook.presto.sql.tree.IfExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) InPredicate(com.facebook.presto.sql.tree.InPredicate) IsNotNullPredicate(com.facebook.presto.sql.tree.IsNotNullPredicate) SimpleCaseExpression(com.facebook.presto.sql.tree.SimpleCaseExpression) WhenClause(com.facebook.presto.sql.tree.WhenClause) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) NullIfExpression(com.facebook.presto.sql.tree.NullIfExpression) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) IfExpression(com.facebook.presto.sql.tree.IfExpression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) SimpleCaseExpression(com.facebook.presto.sql.tree.SimpleCaseExpression) NullIfExpression(com.facebook.presto.sql.tree.NullIfExpression) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) ArrayConstructor(com.facebook.presto.sql.tree.ArrayConstructor) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) NullLiteral(com.facebook.presto.sql.tree.NullLiteral) Test(org.testng.annotations.Test)

Aggregations

SubscriptExpression (com.facebook.presto.sql.tree.SubscriptExpression)3 Test (org.testng.annotations.Test)3 ArrayConstructor (com.facebook.presto.sql.tree.ArrayConstructor)2 Cast (com.facebook.presto.sql.tree.Cast)2 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)2 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)2 QueryUtil.simpleQuery (com.facebook.presto.sql.QueryUtil.simpleQuery)1 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)1 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)1 CreateTable (com.facebook.presto.sql.tree.CreateTable)1 DropTable (com.facebook.presto.sql.tree.DropTable)1 Expression (com.facebook.presto.sql.tree.Expression)1 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)1 Identifier (com.facebook.presto.sql.tree.Identifier)1 IfExpression (com.facebook.presto.sql.tree.IfExpression)1 InListExpression (com.facebook.presto.sql.tree.InListExpression)1 InPredicate (com.facebook.presto.sql.tree.InPredicate)1 IsNotNullPredicate (com.facebook.presto.sql.tree.IsNotNullPredicate)1 NullIfExpression (com.facebook.presto.sql.tree.NullIfExpression)1 NullLiteral (com.facebook.presto.sql.tree.NullLiteral)1