Search in sources :

Example 56 with ParameterExpression

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

the class ExpressionTest method testWriteTryCatchFinally.

@Test
void testWriteTryCatchFinally() {
    final ParameterExpression cce_ = Expressions.parameter(Modifier.FINAL, ClassCastException.class, "cce");
    final ParameterExpression re_ = Expressions.parameter(0, RuntimeException.class, "re");
    Node node = Expressions.tryCatchFinally(Expressions.block(Expressions.return_(null, Expressions.call(Expressions.constant("foo"), "length"))), Expressions.statement(Expressions.call(Expressions.constant("foo"), "toUpperCase")), Expressions.catch_(cce_, Expressions.return_(null, Expressions.constant(null))), Expressions.catch_(re_, Expressions.throw_(Expressions.new_(IndexOutOfBoundsException.class))));
    assertEquals("try {\n" + "  return \"foo\".length();\n" + "} catch (final ClassCastException cce) {\n" + "  return null;\n" + "} catch (RuntimeException re) {\n" + "  throw new IndexOutOfBoundsException();\n" + "} finally {\n" + "  \"foo\".toUpperCase();\n" + "}\n", Expressions.toString(node));
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Node(org.apache.calcite.linq4j.tree.Node) Test(org.junit.jupiter.api.Test)

Example 57 with ParameterExpression

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

the class ExpressionTest method testForEach.

@Test
void testForEach() {
    final BlockBuilder builder = new BlockBuilder();
    final ParameterExpression i_ = Expressions.parameter(int.class, "i");
    final ParameterExpression list_ = Expressions.parameter(List.class, "list");
    builder.add(Expressions.forEach(i_, list_, Expressions.ifThen(Expressions.lessThan(Expressions.constant(1), Expressions.constant(2)), Expressions.break_(null))));
    assertThat(Expressions.toString(builder.toBlock()), is("{\n" + "  for (int i : list) {\n" + "    if (1 < 2) {\n" + "      break;\n" + "    }\n" + "  }\n" + "}\n"));
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Test(org.junit.jupiter.api.Test)

Example 58 with ParameterExpression

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

the class ExpressionTest method testLambdaCallsBinaryOpDouble.

@Test
void testLambdaCallsBinaryOpDouble() {
    // A parameter for the lambda expression.
    ParameterExpression paramExpr = Expressions.parameter(Double.TYPE, "arg");
    // This expression represents a lambda expression
    // that adds 1 to the parameter value.
    FunctionExpression lambdaExpr = Expressions.lambda(Expressions.add(paramExpr, Expressions.constant(2d)), Arrays.asList(paramExpr));
    // Print out the expression.
    String s = Expressions.toString(lambdaExpr);
    assertEquals("new org.apache.calcite.linq4j.function.Function1() {\n" + "  public double apply(double arg) {\n" + "    return arg + 2.0D;\n" + "  }\n" + "  public Object apply(Double arg) {\n" + "    return apply(\n" + "      arg.doubleValue());\n" + "  }\n" + "  public Object apply(Object arg) {\n" + "    return apply(\n" + "      (Double) arg);\n" + "  }\n" + "}\n", s);
    // Compile and run the lambda expression.
    // The value of the parameter is 1.5.
    double n = (Double) lambdaExpr.compile().dynamicInvoke(1.5d);
    // This code example produces the following output:
    // 
    // arg => (arg +2)
    // 3.5
    assertEquals(3.5D, 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 59 with ParameterExpression

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

the class ExpressionTest method testFor2.

@Test
void testFor2() throws NoSuchFieldException {
    final BlockBuilder builder = new BlockBuilder();
    final ParameterExpression i_ = Expressions.parameter(int.class, "i");
    final ParameterExpression j_ = Expressions.parameter(int.class, "j");
    builder.add(Expressions.for_(Arrays.asList(Expressions.declare(0, i_, Expressions.constant(0)), Expressions.declare(0, j_, Expressions.constant(10))), null, null, Expressions.block(Expressions.ifThen(Expressions.lessThan(Expressions.preIncrementAssign(i_), Expressions.preDecrementAssign(j_)), Expressions.break_(null)))));
    assertEquals("{\n" + "  for (int i = 0, j = 10; ; ) {\n" + "    if (++i < --j) {\n" + "      break;\n" + "    }\n" + "  }\n" + "}\n", Expressions.toString(builder.toBlock()));
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Test(org.junit.jupiter.api.Test)

Example 60 with ParameterExpression

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

the class ExpressionTest method testLambdaCallsBinaryOpInt.

@Test
void testLambdaCallsBinaryOpInt() {
    // A parameter for the lambda expression.
    ParameterExpression paramExpr = Expressions.parameter(Integer.TYPE, "arg");
    // This expression represents a lambda expression
    // that adds 1 to the parameter value.
    FunctionExpression lambdaExpr = Expressions.lambda(Expressions.add(paramExpr, Expressions.constant(2)), Arrays.asList(paramExpr));
    // Print out the expression.
    String s = Expressions.toString(lambdaExpr);
    assertEquals("new org.apache.calcite.linq4j.function.Function1() {\n" + "  public int apply(int arg) {\n" + "    return arg + 2;\n" + "  }\n" + "  public Object apply(Integer arg) {\n" + "    return apply(\n" + "      arg.intValue());\n" + "  }\n" + "  public Object apply(Object arg) {\n" + "    return apply(\n" + "      (Integer) arg);\n" + "  }\n" + "}\n", s);
    // Compile and run the lambda expression.
    // The value of the parameter is 1
    Integer n = (Integer) lambdaExpr.compile().dynamicInvoke(1);
    // This code example produces the following output:
    // 
    // arg => (arg +2)
    // 3
    assertEquals(3, n, 0);
}
Also used : BigInteger(java.math.BigInteger) 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