Search in sources :

Example 1 with IfExpression

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

the class TestSqlParser method testIf.

@Test
public void testIf() {
    assertExpression("if(true, 1, 0)", new IfExpression(new BooleanLiteral("true"), new LongLiteral("1"), new LongLiteral("0")));
    assertExpression("if(true, 3, null)", new IfExpression(new BooleanLiteral("true"), new LongLiteral("3"), new NullLiteral()));
    assertExpression("if(false, null, 4)", new IfExpression(new BooleanLiteral("false"), new NullLiteral(), new LongLiteral("4")));
    assertExpression("if(false, null, null)", new IfExpression(new BooleanLiteral("false"), new NullLiteral(), new NullLiteral()));
    assertExpression("if(true, 3)", new IfExpression(new BooleanLiteral("true"), new LongLiteral("3"), null));
    assertInvalidExpression("IF(true)", "Invalid number of arguments for 'if' function");
    assertInvalidExpression("IF(true, 1, 0) FILTER (WHERE true)", "FILTER not valid for 'if' function");
    assertInvalidExpression("IF(true, 1, 0) OVER()", "OVER clause not valid for 'if' function");
}
Also used : IfExpression(io.prestosql.sql.tree.IfExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) LongLiteral(io.prestosql.sql.tree.LongLiteral) BooleanLiteral(io.prestosql.sql.tree.BooleanLiteral) NullLiteral(io.prestosql.sql.tree.NullLiteral) Test(org.testng.annotations.Test)

Example 2 with IfExpression

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

the class TestEqualityInference method testExpressionsThatMayReturnNullOnNonNullInput.

@Test
public void testExpressionsThatMayReturnNullOnNonNullInput() {
    List<Expression> candidates = ImmutableList.of(// try_cast
    new Cast(nameReference("b"), "BIGINT", true), new FunctionCallBuilder(metadata).setName(QualifiedName.of(TryFunction.NAME)).addArgument(new FunctionType(ImmutableList.of(), VARCHAR), new LambdaExpression(ImmutableList.of(), nameReference("b"))).build(), new NullIfExpression(nameReference("b"), number(1)), new IfExpression(nameReference("b"), number(1), new NullLiteral()), new DereferenceExpression(nameReference("b"), identifier("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(io.prestosql.sql.tree.Cast) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) IfExpression(io.prestosql.sql.tree.IfExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) FunctionType(io.prestosql.spi.type.FunctionType) InListExpression(io.prestosql.sql.tree.InListExpression) InPredicate(io.prestosql.sql.tree.InPredicate) IsNotNullPredicate(io.prestosql.sql.tree.IsNotNullPredicate) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) WhenClause(io.prestosql.sql.tree.WhenClause) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) InListExpression(io.prestosql.sql.tree.InListExpression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) IfExpression(io.prestosql.sql.tree.IfExpression) Expression(io.prestosql.sql.tree.Expression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) ArrayConstructor(io.prestosql.sql.tree.ArrayConstructor) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) NullLiteral(io.prestosql.sql.tree.NullLiteral) Test(org.testng.annotations.Test)

Example 3 with IfExpression

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

the class LogicalPlanner method noTruncationCast.

private Expression noTruncationCast(Expression expression, Type fromType, Type toType) {
    // cast larger size varchar to small size varchar
    if ((fromType instanceof VarcharType || fromType instanceof CharType) && (toType instanceof VarcharType || toType instanceof CharType)) {
        int targetLength;
        if (toType instanceof VarcharType) {
            if (((VarcharType) toType).isUnbounded()) {
                return new Cast(expression, toType.getTypeSignature().toString());
            }
            targetLength = ((VarcharType) toType).getBoundedLength();
        } else {
            targetLength = ((CharType) toType).getLength();
        }
        Signature spaceTrimmedLength = metadata.getFunctionAndTypeManager().resolveBuiltInFunction(QualifiedName.of("$space_trimmed_length"), fromTypes(VARCHAR));
        Signature fail = metadata.getFunctionAndTypeManager().resolveBuiltInFunction(QualifiedName.of("fail"), fromTypes(VARCHAR));
        return new IfExpression(// check if the trimmed value fits in the target type
        new ComparisonExpression(GREATER_THAN_OR_EQUAL, new GenericLiteral("BIGINT", Integer.toString(targetLength)), new FunctionCall(QualifiedName.of("$space_trimmed_length"), ImmutableList.of(new Cast(expression, VARCHAR.getTypeSignature().toString())))), new Cast(expression, toType.getTypeSignature().toString()), new Cast(new FunctionCall(QualifiedName.of("fail"), ImmutableList.of(new Cast(new StringLiteral(format("Out of range for insert query type: Table: %s, Query: %s", toType.toString(), fromType.toString())), VARCHAR.getTypeSignature().toString()))), toType.getTypeSignature().toString()));
    }
    return new Cast(expression, toType.getTypeSignature().toString());
}
Also used : Cast(io.prestosql.sql.tree.Cast) IfExpression(io.prestosql.sql.tree.IfExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) StringLiteral(io.prestosql.sql.tree.StringLiteral) VarcharType(io.prestosql.spi.type.VarcharType) Signature(io.prestosql.spi.function.Signature) CharType(io.prestosql.spi.type.CharType) FunctionCall(io.prestosql.sql.tree.FunctionCall) GenericLiteral(io.prestosql.sql.tree.GenericLiteral)

Example 4 with IfExpression

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

the class ImpalaAstBuilder method visitFunctionCall.

@Override
public Node visitFunctionCall(ImpalaSqlParser.FunctionCallContext context) {
    Optional<Expression> filter = visitIfPresent(context.filter(), Expression.class);
    Optional<Window> window = visitIfPresent(context.over(), Window.class);
    Optional<OrderBy> orderBy = Optional.empty();
    if (context.ORDER() != null) {
        orderBy = Optional.of(new OrderBy(visit(context.sortItem(), SortItem.class)));
    }
    QualifiedName name = getQualifiedName(context.qualifiedName());
    boolean distinct = isDistinct(context.setQuantifier());
    if (name.toString().equalsIgnoreCase("if")) {
        check(context.expression().size() == 2 || context.expression().size() == 3, "Invalid number of arguments for 'if' function", context);
        check(!window.isPresent(), "OVER clause not valid for 'if' function", context);
        check(!distinct, "DISTINCT not valid for 'if' function", context);
        check(!filter.isPresent(), "FILTER not valid for 'if' function", context);
        Expression elseExpression = null;
        if (context.expression().size() == 3) {
            elseExpression = (Expression) visit(context.expression(2));
        }
        return new IfExpression(getLocation(context), (Expression) visit(context.expression(0)), (Expression) visit(context.expression(1)), elseExpression);
    }
    if (name.toString().equalsIgnoreCase("nullif")) {
        check(context.expression().size() == 2, "Invalid number of arguments for 'nullif' function", context);
        check(!window.isPresent(), "OVER clause not valid for 'nullif' function", context);
        check(!distinct, "DISTINCT not valid for 'nullif' function", context);
        check(!filter.isPresent(), "FILTER not valid for 'nullif' function", context);
        return new NullIfExpression(getLocation(context), (Expression) visit(context.expression(0)), (Expression) visit(context.expression(1)));
    }
    if (name.toString().equalsIgnoreCase("coalesce")) {
        check(context.expression().size() >= 2, "The 'coalesce' function must have at least two arguments", context);
        check(!window.isPresent(), "OVER clause not valid for 'coalesce' function", context);
        check(!distinct, "DISTINCT not valid for 'coalesce' function", context);
        check(!filter.isPresent(), "FILTER not valid for 'coalesce' function", context);
        return new CoalesceExpression(getLocation(context), visit(context.expression(), Expression.class));
    }
    return new FunctionCall(Optional.of(getLocation(context)), getQualifiedName(context.qualifiedName()), window, filter, orderBy, distinct, false, visit(context.expression(), Expression.class));
}
Also used : Window(io.prestosql.sql.tree.Window) OrderBy(io.prestosql.sql.tree.OrderBy) IfExpression(io.prestosql.sql.tree.IfExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) QualifiedName(io.prestosql.sql.tree.QualifiedName) SortItem(io.prestosql.sql.tree.SortItem) ArithmeticUnaryExpression(io.prestosql.sql.tree.ArithmeticUnaryExpression) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) IfExpression(io.prestosql.sql.tree.IfExpression) InListExpression(io.prestosql.sql.tree.InListExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) FunctionCall(io.prestosql.sql.tree.FunctionCall) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression)

Example 5 with IfExpression

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

the class HiveAstBuilder method visitFunctionCall.

@Override
public Node visitFunctionCall(HiveSqlParser.FunctionCallContext context) {
    Optional<Expression> filter = Optional.empty();
    Optional<OrderBy> orderBy = Optional.empty();
    Optional<Window> window = visitIfPresent(context.over(), Window.class);
    boolean distinct = isDistinct(context.setQuantifier());
    QualifiedName name = getQualifiedName(context.qualifiedName());
    if (name.toString().equalsIgnoreCase("if")) {
        check(context.expression().size() == 3, "Illegal arguments for 'if' function", context);
        check(!window.isPresent(), "OVER not valid for 'if' function", context);
        check(!distinct, "DISTINCT not valid for 'if' function", context);
        Expression elseExpression = (Expression) visit(context.expression(2));
        return new IfExpression(getLocation(context), (Expression) visit(context.expression(0)), (Expression) visit(context.expression(1)), elseExpression);
    }
    if (name.toString().equalsIgnoreCase("nullif")) {
        check(context.expression().size() == 2, "Illegal arguments for 'nullif' function", context);
        check(!window.isPresent(), "OVER not valid for 'nullif' function", context);
        check(!distinct, "DISTINCT not valid for 'nullif' function", context);
        return new NullIfExpression(getLocation(context), (Expression) visit(context.expression(0)), (Expression) visit(context.expression(1)));
    }
    if (name.toString().equalsIgnoreCase("coalesce")) {
        check(context.expression().size() > 0, "The 'coalesce' function must have at least one argument", context);
        check(!window.isPresent(), "OVER not valid for 'coalesce' function", context);
        check(!distinct, "DISTINCT not valid for 'coalesce' function", context);
        return new CoalesceExpression(getLocation(context), visit(context.expression(), Expression.class));
    }
    return new FunctionCall(Optional.of(getLocation(context)), getQualifiedName(context.qualifiedName()), window, filter, orderBy, distinct, false, visit(context.expression(), Expression.class));
}
Also used : OrderBy(io.prestosql.sql.tree.OrderBy) Window(io.prestosql.sql.tree.Window) IfExpression(io.prestosql.sql.tree.IfExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) ArithmeticUnaryExpression(io.prestosql.sql.tree.ArithmeticUnaryExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) IfExpression(io.prestosql.sql.tree.IfExpression) InListExpression(io.prestosql.sql.tree.InListExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) QualifiedName(io.prestosql.sql.tree.QualifiedName) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) FunctionCall(io.prestosql.sql.tree.FunctionCall) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression)

Aggregations

IfExpression (io.prestosql.sql.tree.IfExpression)6 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)5 NullIfExpression (io.prestosql.sql.tree.NullIfExpression)5 ArithmeticBinaryExpression (io.prestosql.sql.tree.ArithmeticBinaryExpression)4 DereferenceExpression (io.prestosql.sql.tree.DereferenceExpression)4 Expression (io.prestosql.sql.tree.Expression)4 FunctionCall (io.prestosql.sql.tree.FunctionCall)4 InListExpression (io.prestosql.sql.tree.InListExpression)4 SearchedCaseExpression (io.prestosql.sql.tree.SearchedCaseExpression)4 SimpleCaseExpression (io.prestosql.sql.tree.SimpleCaseExpression)4 SubscriptExpression (io.prestosql.sql.tree.SubscriptExpression)4 ArithmeticUnaryExpression (io.prestosql.sql.tree.ArithmeticUnaryExpression)3 CoalesceExpression (io.prestosql.sql.tree.CoalesceExpression)3 LambdaExpression (io.prestosql.sql.tree.LambdaExpression)3 LogicalBinaryExpression (io.prestosql.sql.tree.LogicalBinaryExpression)3 NotExpression (io.prestosql.sql.tree.NotExpression)3 OrderBy (io.prestosql.sql.tree.OrderBy)3 QualifiedName (io.prestosql.sql.tree.QualifiedName)3 QuantifiedComparisonExpression (io.prestosql.sql.tree.QuantifiedComparisonExpression)3 SubqueryExpression (io.prestosql.sql.tree.SubqueryExpression)3