Search in sources :

Example 1 with ArrayConstructor

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

the class TestSqlParser method testArrayConstructor.

@Test
public void testArrayConstructor() throws Exception {
    assertExpression("ARRAY []", new ArrayConstructor(ImmutableList.of()));
    assertExpression("ARRAY [1, 2]", new ArrayConstructor(ImmutableList.of(new LongLiteral("1"), new LongLiteral("2"))));
    assertExpression("ARRAY [1.0, 2.5]", new ArrayConstructor(ImmutableList.of(new DoubleLiteral("1.0"), new DoubleLiteral("2.5"))));
    assertExpression("ARRAY ['hi']", new ArrayConstructor(ImmutableList.of(new StringLiteral("hi"))));
    assertExpression("ARRAY ['hi', 'hello']", new ArrayConstructor(ImmutableList.of(new StringLiteral("hi"), new StringLiteral("hello"))));
}
Also used : StringLiteral(com.facebook.presto.sql.tree.StringLiteral) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) ArrayConstructor(com.facebook.presto.sql.tree.ArrayConstructor) DoubleLiteral(com.facebook.presto.sql.tree.DoubleLiteral) Test(org.testng.annotations.Test)

Example 2 with ArrayConstructor

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

the class TestSqlParser method testCreateTableAsSelect.

@Test
public void testCreateTableAsSelect() throws Exception {
    Query query = simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t")));
    QualifiedName table = QualifiedName.of("foo");
    assertStatement("CREATE TABLE foo AS SELECT * FROM t", new CreateTableAsSelect(table, query, false, ImmutableMap.of(), true));
    assertStatement("CREATE TABLE IF NOT EXISTS foo AS SELECT * FROM t", new CreateTableAsSelect(table, query, true, ImmutableMap.of(), true));
    assertStatement("CREATE TABLE foo AS SELECT * FROM t WITH NO DATA", new CreateTableAsSelect(table, query, false, ImmutableMap.of(), false));
    ImmutableMap<String, Expression> properties = ImmutableMap.<String, Expression>builder().put("string", new StringLiteral("bar")).put("long", new LongLiteral("42")).put("computed", new FunctionCall(QualifiedName.of("concat"), ImmutableList.of(new StringLiteral("ban"), new StringLiteral("ana")))).put("a", new ArrayConstructor(ImmutableList.of(new StringLiteral("v1"), new StringLiteral("v2")))).build();
    assertStatement("CREATE TABLE foo " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT * FROM t", new CreateTableAsSelect(table, query, false, properties, true));
    assertStatement("CREATE TABLE foo " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT * FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, query, false, properties, false));
}
Also used : Query(com.facebook.presto.sql.tree.Query) QueryUtil.simpleQuery(com.facebook.presto.sql.QueryUtil.simpleQuery) WithQuery(com.facebook.presto.sql.tree.WithQuery) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) QualifiedName(com.facebook.presto.sql.tree.QualifiedName) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect) ArrayConstructor(com.facebook.presto.sql.tree.ArrayConstructor) AllColumns(com.facebook.presto.sql.tree.AllColumns) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Example 3 with ArrayConstructor

use of com.facebook.presto.sql.tree.ArrayConstructor 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 4 with ArrayConstructor

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

ArrayConstructor (com.facebook.presto.sql.tree.ArrayConstructor)4 Test (org.testng.annotations.Test)4 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)3 SubscriptExpression (com.facebook.presto.sql.tree.SubscriptExpression)3 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)2 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)2 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)2 Expression (com.facebook.presto.sql.tree.Expression)2 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)2 StringLiteral (com.facebook.presto.sql.tree.StringLiteral)2 QueryUtil.simpleQuery (com.facebook.presto.sql.QueryUtil.simpleQuery)1 AllColumns (com.facebook.presto.sql.tree.AllColumns)1 Cast (com.facebook.presto.sql.tree.Cast)1 CreateTableAsSelect (com.facebook.presto.sql.tree.CreateTableAsSelect)1 DoubleLiteral (com.facebook.presto.sql.tree.DoubleLiteral)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 LambdaExpression (com.facebook.presto.sql.tree.LambdaExpression)1