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