Search in sources :

Example 86 with ParameterExpression

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);
}
Also used : FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Test(org.junit.Test)

Example 87 with ParameterExpression

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);
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 88 with ParameterExpression

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();
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression)

Example 89 with ParameterExpression

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);
}
Also used : ClassDeclaration(org.apache.calcite.linq4j.tree.ClassDeclaration) Expression(org.apache.calcite.linq4j.tree.Expression) ExecutableExpression(com.hortonworks.streamline.streams.sql.runtime.ExecutableExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) MemberDeclaration(org.apache.calcite.linq4j.tree.MemberDeclaration) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) ExecutableExpression(com.hortonworks.streamline.streams.sql.runtime.ExecutableExpression)

Example 90 with ParameterExpression

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);
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) UnaryExpression(org.apache.calcite.linq4j.tree.UnaryExpression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ArrayList(java.util.ArrayList)

Aggregations

ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)144 Expression (org.apache.calcite.linq4j.tree.Expression)80 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)58 Test (org.junit.jupiter.api.Test)51 ArrayList (java.util.ArrayList)30 ConstantExpression (org.apache.calcite.linq4j.tree.ConstantExpression)30 RelDataType (org.apache.calcite.rel.type.RelDataType)30 Type (java.lang.reflect.Type)27 RexNode (org.apache.calcite.rex.RexNode)23 FunctionExpression (org.apache.calcite.linq4j.tree.FunctionExpression)19 MethodCallExpression (org.apache.calcite.linq4j.tree.MethodCallExpression)12 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)11 BlockStatement (org.apache.calcite.linq4j.tree.BlockStatement)11 MemberDeclaration (org.apache.calcite.linq4j.tree.MemberDeclaration)11 ImmutableList (com.google.common.collect.ImmutableList)10 List (java.util.List)10 EnumUtils.generateCollatorExpression (org.apache.calcite.adapter.enumerable.EnumUtils.generateCollatorExpression)10 PhysType (org.apache.calcite.adapter.enumerable.PhysType)10 Function1 (org.apache.calcite.linq4j.function.Function1)10 UnaryExpression (org.apache.calcite.linq4j.tree.UnaryExpression)10