Search in sources :

Example 91 with ParameterExpression

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

the class EnumUtils method generatePredicate.

/**
 * Returns a predicate expression based on a join condition. *
 */
static Expression generatePredicate(EnumerableRelImplementor implementor, RexBuilder rexBuilder, RelNode left, RelNode right, PhysType leftPhysType, PhysType rightPhysType, RexNode condition) {
    final BlockBuilder builder = new BlockBuilder();
    final ParameterExpression left_ = Expressions.parameter(leftPhysType.getJavaRowType(), "left");
    final ParameterExpression right_ = Expressions.parameter(rightPhysType.getJavaRowType(), "right");
    final RexProgramBuilder program = new RexProgramBuilder(implementor.getTypeFactory().builder().addAll(left.getRowType().getFieldList()).addAll(right.getRowType().getFieldList()).build(), rexBuilder);
    program.addCondition(condition);
    builder.add(Expressions.return_(null, RexToLixTranslator.translateCondition(program.getProgram(), implementor.getTypeFactory(), builder, new RexToLixTranslator.InputGetterImpl(ImmutableMap.of(left_, leftPhysType, right_, rightPhysType)), implementor.allCorrelateVariables, implementor.getConformance())));
    return Expressions.lambda(Predicate2.class, builder.toBlock(), left_, right_);
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 92 with ParameterExpression

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

the class BlockBuilderTest method appendBlockWithSameVariable.

private BlockBuilder appendBlockWithSameVariable(Expression initializer1, Expression initializer2) {
    BlockBuilder outer = new BlockBuilder();
    ParameterExpression outerX = Expressions.parameter(int.class, "x");
    outer.add(Expressions.declare(0, outerX, initializer1));
    outer.add(Expressions.statement(Expressions.assign(outerX, Expressions.constant(1))));
    BlockBuilder inner = new BlockBuilder();
    ParameterExpression innerX = Expressions.parameter(int.class, "x");
    inner.add(Expressions.declare(0, innerX, initializer2));
    inner.add(Expressions.statement(Expressions.assign(innerX, Expressions.constant(42))));
    inner.add(Expressions.return_(null, innerX));
    outer.append("x", inner.toBlock());
    return outer;
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 93 with ParameterExpression

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

the class ExpressionTest method testCompile.

@Test
void testCompile() throws NoSuchMethodException {
    // Creating a parameter for the expression tree.
    ParameterExpression param = Expressions.parameter(String.class);
    // Creating an expression for the method call and specifying its
    // parameter.
    MethodCallExpression methodCall = Expressions.call(Integer.class, "valueOf", Collections.<Expression>singletonList(param));
    // The following statement first creates an expression tree,
    // then compiles it, and then runs it.
    int x = Expressions.<Function1<String, Integer>>lambda(methodCall, new ParameterExpression[] { param }).getFunction().apply("1234");
    assertEquals(1234, x);
}
Also used : MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Function1(org.apache.calcite.linq4j.function.Function1) Test(org.junit.jupiter.api.Test)

Example 94 with ParameterExpression

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

the class ExpressionTest method testLambdaCallsBinaryOpMixType.

@Test
void testLambdaCallsBinaryOpMixType() {
    // A parameter for the lambda expression.
    ParameterExpression paramExpr = Expressions.parameter(Long.TYPE, "arg");
    // This expression represents a lambda expression
    // that adds (int)10 to the parameter value.
    FunctionExpression lambdaExpr = Expressions.lambda(Expressions.add(paramExpr, Expressions.constant(10)), Arrays.asList(paramExpr));
    // Print out the expression.
    String s = Expressions.toString(lambdaExpr);
    assertEquals("new org.apache.calcite.linq4j.function.Function1() {\n" + "  public long apply(long arg) {\n" + "    return arg + 10;\n" + "  }\n" + "  public Object apply(Long arg) {\n" + "    return apply(\n" + "      arg.longValue());\n" + "  }\n" + "  public Object apply(Object arg) {\n" + "    return apply(\n" + "      (Long) arg);\n" + "  }\n" + "}\n", s);
    // Compile and run the lambda expression.
    // The value of the parameter is 5L.
    long n = (Long) lambdaExpr.compile().dynamicInvoke(5L);
    // This code example produces the following output:
    // 
    // arg => (arg +10)
    // 15
    assertEquals(15L, n, 0d);
}
Also used : FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Test(org.junit.jupiter.api.Test)

Example 95 with ParameterExpression

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

the class ExpressionTest method testLambdaPrimitiveTwoArgs.

@Test
void testLambdaPrimitiveTwoArgs() {
    // Parameters for the lambda expression.
    ParameterExpression paramExpr = Expressions.parameter(int.class, "key");
    ParameterExpression param2Expr = Expressions.parameter(int.class, "key2");
    FunctionExpression lambdaExpr = Expressions.lambda(Expressions.block((Type) null, Expressions.return_(null, paramExpr)), Arrays.asList(paramExpr, param2Expr));
    // Print out the expression.
    String s = Expressions.toString(lambdaExpr);
    assertEquals("new org.apache.calcite.linq4j.function.Function2() {\n" + "  public int apply(int key, int key2) {\n" + "    return key;\n" + "  }\n" + "  public Integer apply(Integer key, Integer key2) {\n" + "    return apply(\n" + "      key.intValue(),\n" + "      key2.intValue());\n" + "  }\n" + "  public Integer apply(Object key, Object key2) {\n" + "    return apply(\n" + "      (Integer) key,\n" + "      (Integer) key2);\n" + "  }\n" + "}\n", s);
}
Also used : Type(java.lang.reflect.Type) FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Test(org.junit.jupiter.api.Test)

Aggregations

ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)144 Expression (org.apache.calcite.linq4j.tree.Expression)80 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)58 Test (org.junit.jupiter.api.Test)51 ArrayList (java.util.ArrayList)30 ConstantExpression (org.apache.calcite.linq4j.tree.ConstantExpression)30 RelDataType (org.apache.calcite.rel.type.RelDataType)30 Type (java.lang.reflect.Type)27 RexNode (org.apache.calcite.rex.RexNode)23 FunctionExpression (org.apache.calcite.linq4j.tree.FunctionExpression)19 MethodCallExpression (org.apache.calcite.linq4j.tree.MethodCallExpression)12 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)11 BlockStatement (org.apache.calcite.linq4j.tree.BlockStatement)11 MemberDeclaration (org.apache.calcite.linq4j.tree.MemberDeclaration)11 ImmutableList (com.google.common.collect.ImmutableList)10 List (java.util.List)10 EnumUtils.generateCollatorExpression (org.apache.calcite.adapter.enumerable.EnumUtils.generateCollatorExpression)10 PhysType (org.apache.calcite.adapter.enumerable.PhysType)10 Function1 (org.apache.calcite.linq4j.function.Function1)10 UnaryExpression (org.apache.calcite.linq4j.tree.UnaryExpression)10