use of org.apache.calcite.linq4j.tree.ParameterExpression in project calcite by apache.
the class EnumUtils method generatePredicate.
/**
* Returns a predicate expression based on a join condition. *
*/
static Expression generatePredicate(EnumerableRelImplementor implementor, RexBuilder rexBuilder, RelNode left, RelNode right, PhysType leftPhysType, PhysType rightPhysType, RexNode condition) {
final BlockBuilder builder = new BlockBuilder();
final ParameterExpression left_ = Expressions.parameter(leftPhysType.getJavaRowType(), "left");
final ParameterExpression right_ = Expressions.parameter(rightPhysType.getJavaRowType(), "right");
final RexProgramBuilder program = new RexProgramBuilder(implementor.getTypeFactory().builder().addAll(left.getRowType().getFieldList()).addAll(right.getRowType().getFieldList()).build(), rexBuilder);
program.addCondition(condition);
builder.add(Expressions.return_(null, RexToLixTranslator.translateCondition(program.getProgram(), implementor.getTypeFactory(), builder, new RexToLixTranslator.InputGetterImpl(ImmutableMap.of(left_, leftPhysType, right_, rightPhysType)), implementor.allCorrelateVariables, implementor.getConformance())));
return Expressions.lambda(Predicate2.class, builder.toBlock(), left_, right_);
}
use of org.apache.calcite.linq4j.tree.ParameterExpression in project calcite by apache.
the class BlockBuilderTest method appendBlockWithSameVariable.
private BlockBuilder appendBlockWithSameVariable(Expression initializer1, Expression initializer2) {
BlockBuilder outer = new BlockBuilder();
ParameterExpression outerX = Expressions.parameter(int.class, "x");
outer.add(Expressions.declare(0, outerX, initializer1));
outer.add(Expressions.statement(Expressions.assign(outerX, Expressions.constant(1))));
BlockBuilder inner = new BlockBuilder();
ParameterExpression innerX = Expressions.parameter(int.class, "x");
inner.add(Expressions.declare(0, innerX, initializer2));
inner.add(Expressions.statement(Expressions.assign(innerX, Expressions.constant(42))));
inner.add(Expressions.return_(null, innerX));
outer.append("x", inner.toBlock());
return outer;
}
use of org.apache.calcite.linq4j.tree.ParameterExpression in project calcite by apache.
the class ExpressionTest method testCompile.
@Test
void testCompile() throws NoSuchMethodException {
// Creating a parameter for the expression tree.
ParameterExpression param = Expressions.parameter(String.class);
// Creating an expression for the method call and specifying its
// parameter.
MethodCallExpression methodCall = Expressions.call(Integer.class, "valueOf", Collections.<Expression>singletonList(param));
// The following statement first creates an expression tree,
// then compiles it, and then runs it.
int x = Expressions.<Function1<String, Integer>>lambda(methodCall, new ParameterExpression[] { param }).getFunction().apply("1234");
assertEquals(1234, x);
}
use of org.apache.calcite.linq4j.tree.ParameterExpression in project calcite by apache.
the class ExpressionTest method testLambdaCallsBinaryOpMixType.
@Test
void testLambdaCallsBinaryOpMixType() {
// A parameter for the lambda expression.
ParameterExpression paramExpr = Expressions.parameter(Long.TYPE, "arg");
// This expression represents a lambda expression
// that adds (int)10 to the parameter value.
FunctionExpression lambdaExpr = Expressions.lambda(Expressions.add(paramExpr, Expressions.constant(10)), 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 + 10;\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 5L.
long n = (Long) lambdaExpr.compile().dynamicInvoke(5L);
// This code example produces the following output:
//
// arg => (arg +10)
// 15
assertEquals(15L, n, 0d);
}
use of org.apache.calcite.linq4j.tree.ParameterExpression in project calcite by apache.
the class ExpressionTest method testLambdaPrimitiveTwoArgs.
@Test
void testLambdaPrimitiveTwoArgs() {
// Parameters for the lambda expression.
ParameterExpression paramExpr = Expressions.parameter(int.class, "key");
ParameterExpression param2Expr = Expressions.parameter(int.class, "key2");
FunctionExpression lambdaExpr = Expressions.lambda(Expressions.block((Type) null, Expressions.return_(null, paramExpr)), Arrays.asList(paramExpr, param2Expr));
// Print out the expression.
String s = Expressions.toString(lambdaExpr);
assertEquals("new org.apache.calcite.linq4j.function.Function2() {\n" + " public int apply(int key, int key2) {\n" + " return key;\n" + " }\n" + " public Integer apply(Integer key, Integer key2) {\n" + " return apply(\n" + " key.intValue(),\n" + " key2.intValue());\n" + " }\n" + " public Integer apply(Object key, Object key2) {\n" + " return apply(\n" + " (Integer) key,\n" + " (Integer) key2);\n" + " }\n" + "}\n", s);
}
Aggregations