Search in sources :

Example 91 with Expression

use of org.apache.calcite.linq4j.tree.Expression in project calcite by apache.

the class OptimizerTest method testConditionalIfBoolFalseElse.

@Test
public void testConditionalIfBoolFalseElse() {
    // if (bool) {return 1} else if (false) {return 2} else {return 3}
    Expression bool = Expressions.parameter(boolean.class, "bool");
    assertEquals("{\n" + "  if (bool) {\n" + "    return 1;\n" + "  } else {\n" + "    return 3;\n" + "  }\n" + "}\n", optimize(Expressions.ifThenElse(bool, Expressions.return_(null, ONE), FALSE, Expressions.return_(null, TWO), Expressions.return_(null, THREE))));
}
Also used : ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Test(org.junit.Test)

Example 92 with Expression

use of org.apache.calcite.linq4j.tree.Expression in project samza by apache.

the class SamzaSqlScalarFunctionImpl method getImplementor.

@Override
public CallImplementor getImplementor() {
    return RexImpTable.createImplementor((translator, call, translatedOperands) -> {
        final Expression sqlContext = Expressions.parameter(SamzaSqlExecutionContext.class, "sqlContext");
        final Expression samzaContext = Expressions.parameter(SamzaSqlExecutionContext.class, "context");
        final Expression getUdfInstance = Expressions.call(ScalarUdf.class, sqlContext, getUdfMethod, Expressions.constant(udfMethod.getDeclaringClass().getName()), Expressions.constant(udfName), samzaContext);
        List<Expression> convertedOperands = new ArrayList<>();
        // We explicitly Convert the untyped arguments to type that the UDf expects.
        for (int index = 0; index < translatedOperands.size(); index++) {
            if (!udfMetadata.isDisableArgCheck() && translatedOperands.get(index).type == Object.class && udfMethod.getParameters()[index].getType() != Object.class) {
                convertedOperands.add(Expressions.convert_(translatedOperands.get(index), udfMethod.getParameters()[index].getType()));
            } else {
                convertedOperands.add(translatedOperands.get(index));
            }
        }
        final Expression callExpression = Expressions.call(Expressions.convert_(getUdfInstance, udfMethod.getDeclaringClass()), udfMethod, convertedOperands);
        return callExpression;
    }, NullPolicy.NONE, false);
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) ArrayList(java.util.ArrayList)

Aggregations

Expression (org.apache.calcite.linq4j.tree.Expression)91 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)64 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)56 ArrayList (java.util.ArrayList)22 MethodCallExpression (org.apache.calcite.linq4j.tree.MethodCallExpression)19 RelDataType (org.apache.calcite.rel.type.RelDataType)19 ConstantExpression (org.apache.calcite.linq4j.tree.ConstantExpression)16 Test (org.junit.Test)16 Type (java.lang.reflect.Type)11 PhysType (org.apache.calcite.adapter.enumerable.PhysType)11 UnaryExpression (org.apache.calcite.linq4j.tree.UnaryExpression)11 RexNode (org.apache.calcite.rex.RexNode)10 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)9 BinaryExpression (org.apache.calcite.linq4j.tree.BinaryExpression)8 BlockStatement (org.apache.calcite.linq4j.tree.BlockStatement)8 NewExpression (org.apache.calcite.linq4j.tree.NewExpression)8 FunctionExpression (org.apache.calcite.linq4j.tree.FunctionExpression)7 MemberDeclaration (org.apache.calcite.linq4j.tree.MemberDeclaration)7 ImmutableList (com.google.common.collect.ImmutableList)5 List (java.util.List)5