use of org.apache.calcite.linq4j.tree.ParameterExpression in project calcite by apache.
the class ExpressionTest method testWriteTryCatchFinally.
@Test
void testWriteTryCatchFinally() {
final ParameterExpression cce_ = Expressions.parameter(Modifier.FINAL, ClassCastException.class, "cce");
final ParameterExpression re_ = Expressions.parameter(0, RuntimeException.class, "re");
Node node = Expressions.tryCatchFinally(Expressions.block(Expressions.return_(null, Expressions.call(Expressions.constant("foo"), "length"))), Expressions.statement(Expressions.call(Expressions.constant("foo"), "toUpperCase")), Expressions.catch_(cce_, Expressions.return_(null, Expressions.constant(null))), Expressions.catch_(re_, Expressions.throw_(Expressions.new_(IndexOutOfBoundsException.class))));
assertEquals("try {\n" + " return \"foo\".length();\n" + "} catch (final ClassCastException cce) {\n" + " return null;\n" + "} catch (RuntimeException re) {\n" + " throw new IndexOutOfBoundsException();\n" + "} finally {\n" + " \"foo\".toUpperCase();\n" + "}\n", Expressions.toString(node));
}
use of org.apache.calcite.linq4j.tree.ParameterExpression in project calcite by apache.
the class ExpressionTest method testForEach.
@Test
void testForEach() {
final BlockBuilder builder = new BlockBuilder();
final ParameterExpression i_ = Expressions.parameter(int.class, "i");
final ParameterExpression list_ = Expressions.parameter(List.class, "list");
builder.add(Expressions.forEach(i_, list_, Expressions.ifThen(Expressions.lessThan(Expressions.constant(1), Expressions.constant(2)), Expressions.break_(null))));
assertThat(Expressions.toString(builder.toBlock()), is("{\n" + " for (int i : list) {\n" + " if (1 < 2) {\n" + " break;\n" + " }\n" + " }\n" + "}\n"));
}
use of org.apache.calcite.linq4j.tree.ParameterExpression in project calcite by apache.
the class ExpressionTest method testLambdaCallsBinaryOpDouble.
@Test
void testLambdaCallsBinaryOpDouble() {
// A parameter for the lambda expression.
ParameterExpression paramExpr = Expressions.parameter(Double.TYPE, "arg");
// This expression represents a lambda expression
// that adds 1 to the parameter value.
FunctionExpression lambdaExpr = Expressions.lambda(Expressions.add(paramExpr, Expressions.constant(2d)), 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 + 2.0D;\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 1.5.
double n = (Double) lambdaExpr.compile().dynamicInvoke(1.5d);
// This code example produces the following output:
//
// arg => (arg +2)
// 3.5
assertEquals(3.5D, n, 0d);
}
use of org.apache.calcite.linq4j.tree.ParameterExpression in project calcite by apache.
the class ExpressionTest method testFor2.
@Test
void testFor2() throws NoSuchFieldException {
final BlockBuilder builder = new BlockBuilder();
final ParameterExpression i_ = Expressions.parameter(int.class, "i");
final ParameterExpression j_ = Expressions.parameter(int.class, "j");
builder.add(Expressions.for_(Arrays.asList(Expressions.declare(0, i_, Expressions.constant(0)), Expressions.declare(0, j_, Expressions.constant(10))), null, null, Expressions.block(Expressions.ifThen(Expressions.lessThan(Expressions.preIncrementAssign(i_), Expressions.preDecrementAssign(j_)), Expressions.break_(null)))));
assertEquals("{\n" + " for (int i = 0, j = 10; ; ) {\n" + " if (++i < --j) {\n" + " break;\n" + " }\n" + " }\n" + "}\n", Expressions.toString(builder.toBlock()));
}
use of org.apache.calcite.linq4j.tree.ParameterExpression in project calcite by apache.
the class ExpressionTest method testLambdaCallsBinaryOpInt.
@Test
void testLambdaCallsBinaryOpInt() {
// A parameter for the lambda expression.
ParameterExpression paramExpr = Expressions.parameter(Integer.TYPE, "arg");
// This expression represents a lambda expression
// that adds 1 to the parameter value.
FunctionExpression lambdaExpr = Expressions.lambda(Expressions.add(paramExpr, Expressions.constant(2)), Arrays.asList(paramExpr));
// Print out the expression.
String s = Expressions.toString(lambdaExpr);
assertEquals("new org.apache.calcite.linq4j.function.Function1() {\n" + " public int apply(int arg) {\n" + " return arg + 2;\n" + " }\n" + " public Object apply(Integer arg) {\n" + " return apply(\n" + " arg.intValue());\n" + " }\n" + " public Object apply(Object arg) {\n" + " return apply(\n" + " (Integer) arg);\n" + " }\n" + "}\n", s);
// Compile and run the lambda expression.
// The value of the parameter is 1
Integer n = (Integer) lambdaExpr.compile().dynamicInvoke(1);
// This code example produces the following output:
//
// arg => (arg +2)
// 3
assertEquals(3, n, 0);
}
Aggregations