Search in sources :

Example 86 with Expression

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

the class ExpressionTest method testBlockBuilder3.

@Test
public void testBlockBuilder3() {
    /*
    int a = 1;
    int b = a + 2;
    int c = a + 3;
    int d = a + 4;
    int e = {
      int b = a + 3;
      foo(b);
    }
    bar(a, b, c, d, e);
*/
    BlockBuilder builder0 = new BlockBuilder();
    final Expression a = builder0.append("_a", Expressions.constant(1));
    final Expression b = builder0.append("_b", Expressions.add(a, Expressions.constant(2)));
    final Expression c = builder0.append("_c", Expressions.add(a, Expressions.constant(3)));
    final Expression d = builder0.append("_d", Expressions.add(a, Expressions.constant(4)));
    BlockBuilder builder1 = new BlockBuilder();
    final Expression b1 = builder1.append("_b", Expressions.add(a, Expressions.constant(3)));
    builder1.add(Expressions.statement(Expressions.call(ExpressionTest.class, "foo", b1)));
    final Expression e = builder0.append("e", builder1.toBlock());
    builder0.add(Expressions.statement(Expressions.call(ExpressionTest.class, "bar", a, b, c, d, e)));
    // With the bug in BlockBuilder.append(String, BlockExpression),
    // bar(1, _b, _c, _d, foo(_d));
    // Correct result is
    // bar(1, _b, _c, _d, foo(_c));
    // because _c has the same expression (a + 3) as inner b.
    BlockStatement expression = builder0.toBlock();
    assertEquals("{\n" + "  final int _b = 1 + 2;\n" + "  final int _c = 1 + 3;\n" + "  final int _d = 1 + 4;\n" + "  org.apache.calcite.linq4j.test.ExpressionTest.bar(1, _b, _c, _d, org.apache.calcite.linq4j.test.ExpressionTest.foo(_c));\n" + "}\n", Expressions.toString(expression));
    expression.accept(new Shuttle());
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) NewExpression(org.apache.calcite.linq4j.tree.NewExpression) FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockStatement(org.apache.calcite.linq4j.tree.BlockStatement) Shuttle(org.apache.calcite.linq4j.tree.Shuttle) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Test(org.junit.Test)

Example 87 with Expression

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

the class ExpressionTest method testSubExpressionElimination.

/**
 * Test for common sub-expression elimination.
 */
@Test
public void testSubExpressionElimination() {
    final BlockBuilder builder = new BlockBuilder(true);
    ParameterExpression x = Expressions.parameter(Object.class, "p");
    Expression current4 = builder.append("current4", Expressions.convert_(x, Object[].class));
    Expression v = builder.append("v", Expressions.convert_(Expressions.arrayIndex(current4, Expressions.constant(4)), Short.class));
    Expression v0 = builder.append("v0", Expressions.convert_(v, Number.class));
    Expression v1 = builder.append("v1", Expressions.convert_(Expressions.arrayIndex(current4, Expressions.constant(4)), Short.class));
    Expression v2 = builder.append("v2", Expressions.convert_(v, Number.class));
    Expression v3 = builder.append("v3", Expressions.convert_(Expressions.arrayIndex(current4, Expressions.constant(4)), Short.class));
    Expression v4 = builder.append("v4", Expressions.convert_(v3, Number.class));
    Expression v5 = builder.append("v5", Expressions.call(v4, "intValue"));
    Expression v6 = builder.append("v6", Expressions.condition(Expressions.equal(v2, Expressions.constant(null)), Expressions.constant(null), Expressions.equal(v5, Expressions.constant(1997))));
    builder.add(Expressions.return_(null, v6));
    assertEquals("{\n" + "  final Short v = (Short) ((Object[]) p)[4];\n" + "  return (Number) v == null ? (Boolean) null : (" + "(Number) v).intValue() == 1997;\n" + "}\n", Expressions.toString(builder.toBlock()));
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) NewExpression(org.apache.calcite.linq4j.tree.NewExpression) FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Test(org.junit.Test)

Example 88 with Expression

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

the class InlinerTest method testAssignInConditionMultipleUsage.

@Test
public void testAssignInConditionMultipleUsage() {
    // int t;
    // return (t = 1) != a ? t : c
    final BlockBuilder builder = new BlockBuilder(true);
    final ParameterExpression t = Expressions.parameter(int.class, "t");
    builder.add(Expressions.declare(0, 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")), t, Expressions.parameter(int.class, "c")));
    builder.add(Expressions.return_(null, v));
    assertEquals("{\n" + "  int t;\n" + "  return (t = 1) != a ? t : c;\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.Test)

Example 89 with Expression

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

the class InlinerTest method testAssignInConditionMultipleUsageNonOptimized.

@Test
public void testAssignInConditionMultipleUsageNonOptimized() {
    // int t = 2;
    // return (t = 1) != a ? 1 : c
    final BlockBuilder builder = new BlockBuilder(true);
    final ParameterExpression t = Expressions.parameter(int.class, "t");
    builder.add(Expressions.declare(0, t, TWO));
    Expression v = builder.append("v", Expressions.makeTernary(ExpressionType.Conditional, Expressions.makeBinary(ExpressionType.NotEqual, Expressions.assign(t, Expressions.constant(1)), Expressions.parameter(int.class, "a")), t, Expressions.parameter(int.class, "c")));
    builder.add(Expressions.return_(null, v));
    assertEquals("{\n" + "  int t = 2;\n" + "  return (t = 1) != a ? t : c;\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.Test)

Example 90 with Expression

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

the class OptimizerTest method testConditionalIfBoolFalse.

@Test
public void testConditionalIfBoolFalse() {
    // if (bool) {return 1} else if (false) {return 2}
    Expression bool = Expressions.parameter(boolean.class, "bool");
    assertEquals("{\n" + "  if (bool) {\n" + "    return 1;\n" + "  }\n" + "}\n", optimize(Expressions.ifThenElse(bool, Expressions.return_(null, ONE), FALSE, Expressions.return_(null, TWO))));
}
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)

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