use of org.apache.calcite.linq4j.tree.ParameterExpression in project calcite by apache.
the class ExpressionTest method testSubExpressionElimination.
/**
* Test for common sub-expression elimination.
*/
@Test
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.ParameterExpression in project calcite by apache.
the class ExpressionTest method testLambdaCallsBinaryOpLong.
@Test
void testLambdaCallsBinaryOpLong() {
// A parameter for the lambda expression.
ParameterExpression paramExpr = Expressions.parameter(Long.TYPE, "arg");
// This expression represents a lambda expression
// that adds 1L to the parameter value.
FunctionExpression lambdaExpr = Expressions.lambda(Expressions.add(paramExpr, Expressions.constant(2L)), Arrays.asList(paramExpr));
// Print out the expression.
String s = Expressions.toString(lambdaExpr);
assertEquals("new org.apache.calcite.linq4j.function.Function1() {\n" + " public long apply(long arg) {\n" + " return arg + 2L;\n" + " }\n" + " public Object apply(Long arg) {\n" + " return apply(\n" + " arg.longValue());\n" + " }\n" + " public Object apply(Object arg) {\n" + " return apply(\n" + " (Long) arg);\n" + " }\n" + "}\n", s);
// Compile and run the lambda expression.
// The value of the parameter is 1L.
long n = (Long) lambdaExpr.compile().dynamicInvoke(1L);
// This code example produces the following output:
//
// arg => (arg +2)
// 3
assertEquals(3L, n, 0d);
}
use of org.apache.calcite.linq4j.tree.ParameterExpression in project calcite by apache.
the class ExpressionTest method testWriteTryCatch.
@Test
void testWriteTryCatch() {
final ParameterExpression cce_ = Expressions.parameter(Modifier.FINAL, ClassCastException.class, "cce");
final ParameterExpression re_ = Expressions.parameter(0, RuntimeException.class, "re");
Node node = Expressions.tryCatch(Expressions.block(Expressions.return_(null, Expressions.call(Expressions.constant("foo"), "length"))), Expressions.catch_(cce_, Expressions.return_(null, Expressions.constant(null))), Expressions.catch_(re_, Expressions.return_(null, Expressions.call(re_, "toString"))));
assertEquals("try {\n" + " return \"foo\".length();\n" + "} catch (final ClassCastException cce) {\n" + " return null;\n" + "} catch (RuntimeException re) {\n" + " return re.toString();\n" + "}\n", Expressions.toString(node));
}
use of org.apache.calcite.linq4j.tree.ParameterExpression in project calcite by apache.
the class ExpressionTest method testLambdaCallsBinaryOpShort.
@Test
void testLambdaCallsBinaryOpShort() {
// A parameter for the lambda expression.
ParameterExpression paramExpr = Expressions.parameter(Short.TYPE, "arg");
// This expression represents a lambda expression
// that adds 1 to the parameter value.
Short a = 2;
FunctionExpression lambdaExpr = Expressions.lambda(Expressions.add(paramExpr, Expressions.constant(a)), Arrays.asList(paramExpr));
// Print out the expression.
String s = Expressions.toString(lambdaExpr);
assertEquals("new org.apache.calcite.linq4j.function.Function1() {\n" + " public int apply(short arg) {\n" + " return arg + (short)2;\n" + " }\n" + " public Object apply(Short arg) {\n" + " return apply(\n" + " arg.shortValue());\n" + " }\n" + " public Object apply(Object arg) {\n" + " return apply(\n" + " (Short) arg);\n" + " }\n" + "}\n", s);
// Compile and run the lambda expression.
// The value of the parameter is 1.
Short b = 1;
Integer n = (Integer) lambdaExpr.compile().dynamicInvoke(b);
// This code example produces the following output:
//
// arg => (arg +2)
// 3
assertEquals(3, n, 0);
}
use of org.apache.calcite.linq4j.tree.ParameterExpression in project calcite by apache.
the class ExpressionTest method testLambdaCallsBinaryOpFloat.
@Test
void testLambdaCallsBinaryOpFloat() {
// A parameter for the lambda expression.
ParameterExpression paramExpr = Expressions.parameter(Float.TYPE, "arg");
// This expression represents a lambda expression
// that adds 1f to the parameter value.
FunctionExpression lambdaExpr = Expressions.lambda(Expressions.add(paramExpr, Expressions.constant(2.0f)), Arrays.asList(paramExpr));
// Print out the expression.
String s = Expressions.toString(lambdaExpr);
assertEquals("new org.apache.calcite.linq4j.function.Function1() {\n" + " public float apply(float arg) {\n" + " return arg + 2.0F;\n" + " }\n" + " public Object apply(Float arg) {\n" + " return apply(\n" + " arg.floatValue());\n" + " }\n" + " public Object apply(Object arg) {\n" + " return apply(\n" + " (Float) arg);\n" + " }\n" + "}\n", s);
// Compile and run the lambda expression.
// The value of the parameter is 1f
float n = (Float) lambdaExpr.compile().dynamicInvoke(1f);
// This code example produces the following output:
//
// arg => (arg +2)
// 3.0
assertEquals(3.0f, n, 0f);
}
Aggregations