Search in sources :

Example 66 with BlockBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder 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 67 with BlockBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder 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 68 with BlockBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder 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)

Aggregations

BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)67 Expression (org.apache.calcite.linq4j.tree.Expression)56 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)40 RelDataType (org.apache.calcite.rel.type.RelDataType)15 PhysType (org.apache.calcite.adapter.enumerable.PhysType)14 ArrayList (java.util.ArrayList)12 MethodCallExpression (org.apache.calcite.linq4j.tree.MethodCallExpression)12 Test (org.junit.Test)10 BlockStatement (org.apache.calcite.linq4j.tree.BlockStatement)9 Type (java.lang.reflect.Type)8 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)8 MemberDeclaration (org.apache.calcite.linq4j.tree.MemberDeclaration)7 RexToLixTranslator (org.apache.calcite.adapter.enumerable.RexToLixTranslator)6 NewExpression (org.apache.calcite.linq4j.tree.NewExpression)6 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)5 BinaryExpression (org.apache.calcite.linq4j.tree.BinaryExpression)5 FunctionExpression (org.apache.calcite.linq4j.tree.FunctionExpression)5 RexNode (org.apache.calcite.rex.RexNode)5 EnumerableRelImplementor (org.apache.calcite.adapter.enumerable.EnumerableRelImplementor)4 Function1 (org.apache.calcite.linq4j.function.Function1)4