use of org.apache.calcite.linq4j.tree.ParameterExpression in project calcite by apache.
the class ExpressionTest method testLambdaCallsBinaryOp.
@Test
public void testLambdaCallsBinaryOp() {
// 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
assertEquals(3.5D, n, 0d);
}
use of org.apache.calcite.linq4j.tree.ParameterExpression in project streamline by hortonworks.
the class RexNodeToJavaCodeCompiler method compile.
public String compile(final RexProgram program, String className) {
final ParameterExpression context_ = Expressions.parameter(Context.class, "context");
final ParameterExpression outputValues_ = Expressions.parameter(Object[].class, "outputValues");
BlockBuilder builder = compileToBlock(program, context_, outputValues_);
return baz(context_, outputValues_, builder.toBlock(), className);
}
use of org.apache.calcite.linq4j.tree.ParameterExpression in project streamline by hortonworks.
the class RexNodeToJavaCodeCompiler method compileToBlock.
public BlockStatement compileToBlock(final RexProgram program) {
final ParameterExpression context_ = Expressions.parameter(Context.class, "context");
final ParameterExpression outputValues_ = Expressions.parameter(Object[].class, "outputValues");
return compileToBlock(program, context_, outputValues_).toBlock();
}
use of org.apache.calcite.linq4j.tree.ParameterExpression in project streamline by hortonworks.
the class RexNodeToJavaCodeCompiler method baz.
/**
* Given a method that implements {@link ExecutableExpression#execute(Context, Object[])},
* adds a bridge method that implements {@link ExecutableExpression#execute(Context)}, and
* compiles.
*/
static String baz(ParameterExpression context_, ParameterExpression outputValues_, BlockStatement block, String className) {
final List<MemberDeclaration> declarations = Lists.newArrayList();
// public void execute(Context, Object[] outputValues)
declarations.add(Expressions.methodDecl(Modifier.PUBLIC, void.class, StreamlineBuiltInMethod.EXPR_EXECUTE2.method.getName(), ImmutableList.of(context_, outputValues_), block));
// public Object execute(Context)
final BlockBuilder builder = new BlockBuilder();
final Expression values_ = builder.append("values", Expressions.newArrayBounds(Object.class, 1, Expressions.constant(1)));
builder.add(Expressions.statement(Expressions.call(Expressions.parameter(ExecutableExpression.class, "this"), StreamlineBuiltInMethod.EXPR_EXECUTE2.method, context_, values_)));
builder.add(Expressions.return_(null, Expressions.arrayIndex(values_, Expressions.constant(0))));
declarations.add(Expressions.methodDecl(Modifier.PUBLIC, Object.class, StreamlineBuiltInMethod.EXPR_EXECUTE1.method.getName(), ImmutableList.of(context_), builder.toBlock()));
final ClassDeclaration classDeclaration = Expressions.classDecl(Modifier.PUBLIC, className, null, ImmutableList.<Type>of(ExecutableExpression.class), declarations);
return Expressions.toString(Lists.newArrayList(classDeclaration), "\n", false);
}
use of org.apache.calcite.linq4j.tree.ParameterExpression in project calcite by apache.
the class EnumUtils method tumblingWindowSelector.
/**
* Generates a window selector which appends attribute of the window based on
* the parameters.
*
* Note that it only works for batch scenario. E.g. all data is known and there is no late data.
*/
static Expression tumblingWindowSelector(PhysType inputPhysType, PhysType outputPhysType, Expression wmColExpr, Expression windowSizeExpr, Expression offsetExpr) {
// Generate all fields.
final List<Expression> expressions = new ArrayList<>();
// If input item is just a primitive, we do not generate specialized
// primitive apply override since it won't be called anyway
// Function<T> always operates on boxed arguments
final ParameterExpression parameter = Expressions.parameter(Primitive.box(inputPhysType.getJavaRowType()), "_input");
final int fieldCount = inputPhysType.getRowType().getFieldCount();
for (int i = 0; i < fieldCount; i++) {
Expression expression = inputPhysType.fieldReference(parameter, i, outputPhysType.getJavaFieldType(expressions.size()));
expressions.add(expression);
}
final Expression wmColExprToLong = EnumUtils.convert(wmColExpr, long.class);
// Find the fixed window for a timestamp given a window size and an offset, and return the
// window start.
// wmColExprToLong - (wmColExprToLong + windowSizeMillis - offsetMillis) % windowSizeMillis
Expression windowStartExpr = Expressions.subtract(wmColExprToLong, Expressions.modulo(Expressions.add(wmColExprToLong, Expressions.subtract(windowSizeExpr, offsetExpr)), windowSizeExpr));
expressions.add(windowStartExpr);
// The window end equals to the window start plus window size.
// windowStartMillis + sizeMillis
Expression windowEndExpr = Expressions.add(windowStartExpr, windowSizeExpr);
expressions.add(windowEndExpr);
return Expressions.lambda(Function1.class, outputPhysType.record(expressions), parameter);
}
Aggregations