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());
}
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()));
}
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()));
}
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()));
}
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))));
}
Aggregations