Search in sources :

Example 61 with ParameterExpression

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

the class InlinerTest method checkAssignInConditionOptimizedOut.

void checkAssignInConditionOptimizedOut(int modifiers, String s) {
    // int t;
    // return (t = 1) != a ? b : c
    final BlockBuilder builder = new BlockBuilder(true);
    final ParameterExpression t = Expressions.parameter(int.class, "t");
    builder.add(Expressions.declare(modifiers, t, null));
    Expression v = builder.append("v", Expressions.makeTernary(ExpressionType.Conditional, Expressions.makeBinary(ExpressionType.NotEqual, Expressions.assign(t, Expressions.constant(1)), Expressions.parameter(int.class, "a")), Expressions.parameter(int.class, "b"), Expressions.parameter(int.class, "c")));
    builder.add(Expressions.return_(null, v));
    assertThat(Expressions.toString(builder.toBlock()), CoreMatchers.equalTo(s));
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 62 with ParameterExpression

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

the class InlinerTest method testInlineInTryCatchStatement.

@Test
void testInlineInTryCatchStatement() {
    final BlockBuilder builder = new BlockBuilder(true);
    final ParameterExpression t = Expressions.parameter(int.class, "t");
    builder.add(Expressions.declare(Modifier.FINAL, t, ONE));
    final ParameterExpression u = Expressions.parameter(int.class, "u");
    builder.add(Expressions.declare(Modifier.FINAL, u, null));
    Statement st = Expressions.statement(Expressions.assign(u, Expressions.makeBinary(ExpressionType.Add, t, TWO)));
    ParameterExpression e = Expressions.parameter(0, Exception.class, "e");
    CatchBlock cb = Expressions.catch_(e, Expressions.throw_(e));
    builder.add(Expressions.tryCatch(st, cb));
    builder.add(Expressions.return_(null, u));
    assertEquals("{\n" + "  final int u;\n" + "  try {\n" + "    u = 1 + 2;\n" + "  } catch (Exception e) {\n" + "    throw e;\n" + "  }\n" + "  return u;\n" + "}\n", builder.toBlock().toString());
}
Also used : DeclarationStatement(org.apache.calcite.linq4j.tree.DeclarationStatement) Statement(org.apache.calcite.linq4j.tree.Statement) CatchBlock(org.apache.calcite.linq4j.tree.CatchBlock) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Test(org.junit.jupiter.api.Test)

Example 63 with ParameterExpression

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

the class InlinerTest method testInlineParameter.

@Test
void testInlineParameter() {
    ParameterExpression pe = Expressions.parameter(int.class, "p");
    DeclarationStatement decl = Expressions.declare(16, "x", pe);
    b.add(decl);
    b.add(Expressions.return_(null, Expressions.add(decl.parameter, decl.parameter)));
    assertEquals("{\n  return p + p;\n}\n", b.toBlock().toString());
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) DeclarationStatement(org.apache.calcite.linq4j.tree.DeclarationStatement) Test(org.junit.jupiter.api.Test)

Example 64 with ParameterExpression

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

the class InlinerTest method testMultiPassOptimization.

@Test
void testMultiPassOptimization() {
    // int t = u + v;
    // boolean b = t > 1 ? true : true; -- optimized out, thus t can be inlined
    // return b ? t : 2
    final BlockBuilder builder = new BlockBuilder(true);
    final ParameterExpression u = Expressions.parameter(int.class, "u");
    final ParameterExpression v = Expressions.parameter(int.class, "v");
    Expression t = builder.append("t", Expressions.add(u, v));
    Expression b = builder.append("b", Expressions.condition(Expressions.greaterThan(t, ONE), TRUE, TRUE));
    builder.add(Expressions.return_(null, Expressions.condition(b, t, TWO)));
    assertEquals("{\n" + "  return u + v;\n" + "}\n", Expressions.toString(builder.toBlock()));
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Test(org.junit.jupiter.api.Test)

Example 65 with ParameterExpression

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

the class ExpressionTest method testLambdaCallsBinaryOpMixDoubleType.

@Test
void testLambdaCallsBinaryOpMixDoubleType() {
    // A parameter for the lambda expression.
    ParameterExpression paramExpr = Expressions.parameter(Double.TYPE, "arg");
    // This expression represents a lambda expression
    // that adds 10.1d to the parameter value.
    FunctionExpression lambdaExpr = Expressions.lambda(Expressions.add(paramExpr, Expressions.constant(10.1d)), 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 + 10.1D;\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 5.0f.
    double n = (Double) lambdaExpr.compile().dynamicInvoke(5.0f);
    // This code example produces the following output:
    // 
    // arg => (arg +10.1d)
    // 15.1d
    assertEquals(15.1d, n, 0d);
}
Also used : 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