Search in sources :

Example 81 with ParameterExpression

use of org.apache.calcite.linq4j.tree.ParameterExpression in project Mycat2 by MyCATApache.

the class PhysTypeImpl method generateAccessor.

public Expression generateAccessor(List<Integer> fields) {
    ParameterExpression v1 = Expressions.parameter(javaRowClass, "v1");
    switch(fields.size()) {
        case 0:
            return Expressions.lambda(Function1.class, Expressions.field(null, BuiltInMethod.COMPARABLE_EMPTY_LIST.field), v1);
        case 1:
            int field0 = fields.get(0);
            // new Function1<Employee, Res> {
            // public Res apply(Employee v1) {
            // return v1.<fieldN>;
            // }
            // }
            Class returnType = fieldClasses.get(field0);
            Expression fieldReference = EnumUtils.convert(fieldReference(v1, field0), returnType);
            return Expressions.lambda(Function1.class, fieldReference, v1);
        default:
            // new Function1<Employee, List> {
            // public List apply(Employee v1) {
            // return Arrays.asList(
            // new Object[] {v1.<fieldN>, v1.<fieldM>});
            // }
            // }
            Expressions.FluentList<Expression> list = Expressions.list();
            for (int field : fields) {
                list.add(fieldReference(v1, field));
            }
            switch(list.size()) {
                case 2:
                    return Expressions.lambda(Function1.class, Expressions.call(List.class, null, BuiltInMethod.LIST2.method, list), v1);
                case 3:
                    return Expressions.lambda(Function1.class, Expressions.call(List.class, null, BuiltInMethod.LIST3.method, list), v1);
                case 4:
                    return Expressions.lambda(Function1.class, Expressions.call(List.class, null, BuiltInMethod.LIST4.method, list), v1);
                case 5:
                    return Expressions.lambda(Function1.class, Expressions.call(List.class, null, BuiltInMethod.LIST5.method, list), v1);
                case 6:
                    return Expressions.lambda(Function1.class, Expressions.call(List.class, null, BuiltInMethod.LIST6.method, list), v1);
                default:
                    return Expressions.lambda(Function1.class, Expressions.call(List.class, null, BuiltInMethod.LIST_N.method, Expressions.newArrayInit(Comparable.class, list)), v1);
            }
    }
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) EnumUtils.generateCollatorExpression(org.apache.calcite.adapter.enumerable.EnumUtils.generateCollatorExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) AbstractList(java.util.AbstractList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Expressions(org.apache.calcite.linq4j.tree.Expressions)

Example 82 with ParameterExpression

use of org.apache.calcite.linq4j.tree.ParameterExpression in project calcite by apache.

the class EnumerableThetaJoin method predicate.

Expression predicate(EnumerableRelImplementor implementor, BlockBuilder builder, PhysType leftPhysType, PhysType rightPhysType, RexNode condition) {
    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(), getCluster().getRexBuilder());
    program.addCondition(condition);
    builder.add(Expressions.return_(null, RexToLixTranslator.translateCondition(program.getProgram(), implementor.getTypeFactory(), builder, new RexToLixTranslator.InputGetterImpl(ImmutableList.of(Pair.of((Expression) left_, leftPhysType), Pair.of((Expression) right_, rightPhysType))), implementor.allCorrelateVariables)));
    return Expressions.lambda(Predicate2.class, builder.toBlock(), left_, right_);
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder)

Example 83 with ParameterExpression

use of org.apache.calcite.linq4j.tree.ParameterExpression in project calcite by apache.

the class RexToLixTranslator method handleNull.

/**
 * Adapts an expression with "normal" result to one that adheres to
 * this particular policy. Wraps the result expression into a new
 * parameter if need be.
 *
 * @param input Expression
 * @param nullAs If false, if expression is definitely not null at
 *   runtime. Therefore we can optimize. For example, we can cast to int
 *   using x.intValue().
 * @return Translated expression
 */
public Expression handleNull(Expression input, RexImpTable.NullAs nullAs) {
    final Expression nullHandled = nullAs.handle(input);
    // If we get ConstantExpression, just return it (i.e. primitive false)
    if (nullHandled instanceof ConstantExpression) {
        return nullHandled;
    }
    // then we can just reuse it
    if (nullHandled == input) {
        return input;
    }
    // If nullHandled is different, then it might be unsafe to compute
    // early (i.e. unbox of null value should not happen _before_ ternary).
    // Thus we wrap it into brand-new ParameterExpression,
    // and we are guaranteed that ParameterExpression will not be shared
    String unboxVarName = "v_unboxed";
    if (input instanceof ParameterExpression) {
        unboxVarName = ((ParameterExpression) input).name + "_unboxed";
    }
    ParameterExpression unboxed = Expressions.parameter(nullHandled.getType(), list.newName(unboxVarName));
    list.add(Expressions.declare(Modifier.FINAL, unboxed, nullHandled));
    return unboxed;
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) UnaryExpression(org.apache.calcite.linq4j.tree.UnaryExpression) ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ByteString(org.apache.calcite.avatica.util.ByteString)

Example 84 with ParameterExpression

use of org.apache.calcite.linq4j.tree.ParameterExpression in project calcite by apache.

the class EnumerableWindow method getBlockBuilderWinAggFrameResultContextFunction.

private Function<BlockBuilder, WinAggFrameResultContext> getBlockBuilderWinAggFrameResultContextFunction(final JavaTypeFactory typeFactory, final Result result, final List<Expression> translatedConstants, final Expression comparator_, final Expression rows_, final ParameterExpression i_, final Expression startX, final Expression endX, final Expression minX, final Expression maxX, final Expression hasRows, final Expression frameRowCount, final Expression partitionRowCount, final DeclarationStatement jDecl, final PhysType inputPhysType) {
    return new Function<BlockBuilder, WinAggFrameResultContext>() {

        public WinAggFrameResultContext apply(final BlockBuilder block) {
            return new WinAggFrameResultContext() {

                public RexToLixTranslator rowTranslator(Expression rowIndex) {
                    Expression row = getRow(rowIndex);
                    final RexToLixTranslator.InputGetter inputGetter = new WindowRelInputGetter(row, inputPhysType, result.physType.getRowType().getFieldCount(), translatedConstants);
                    return RexToLixTranslator.forAggregation(typeFactory, block, inputGetter);
                }

                public Expression computeIndex(Expression offset, WinAggImplementor.SeekType seekType) {
                    Expression index;
                    if (seekType == WinAggImplementor.SeekType.AGG_INDEX) {
                        index = jDecl.parameter;
                    } else if (seekType == WinAggImplementor.SeekType.SET) {
                        index = i_;
                    } else if (seekType == WinAggImplementor.SeekType.START) {
                        index = startX;
                    } else if (seekType == WinAggImplementor.SeekType.END) {
                        index = endX;
                    } else {
                        throw new IllegalArgumentException("SeekSet " + seekType + " is not supported");
                    }
                    if (!Expressions.constant(0).equals(offset)) {
                        index = block.append("idx", Expressions.add(index, offset));
                    }
                    return index;
                }

                private Expression checkBounds(Expression rowIndex, Expression minIndex, Expression maxIndex) {
                    if (rowIndex == i_ || rowIndex == startX || rowIndex == endX) {
                        // No additional bounds check required
                        return hasRows;
                    }
                    // noinspection UnnecessaryLocalVariable
                    Expression res = block.append("rowInFrame", Expressions.foldAnd(ImmutableList.of(hasRows, Expressions.greaterThanOrEqual(rowIndex, minIndex), Expressions.lessThanOrEqual(rowIndex, maxIndex))));
                    return res;
                }

                public Expression rowInFrame(Expression rowIndex) {
                    return checkBounds(rowIndex, startX, endX);
                }

                public Expression rowInPartition(Expression rowIndex) {
                    return checkBounds(rowIndex, minX, maxX);
                }

                public Expression compareRows(Expression a, Expression b) {
                    return Expressions.call(comparator_, BuiltInMethod.COMPARATOR_COMPARE.method, getRow(a), getRow(b));
                }

                public Expression getRow(Expression rowIndex) {
                    return block.append("jRow", RexToLixTranslator.convert(Expressions.arrayIndex(rows_, rowIndex), inputPhysType.getJavaRowType()));
                }

                public Expression index() {
                    return i_;
                }

                public Expression startIndex() {
                    return startX;
                }

                public Expression endIndex() {
                    return endX;
                }

                public Expression hasRows() {
                    return hasRows;
                }

                public Expression getFrameRowCount() {
                    return frameRowCount;
                }

                public Expression getPartitionRowCount() {
                    return partitionRowCount;
                }
            };
        }
    };
}
Also used : Function(com.google.common.base.Function) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) BinaryExpression(org.apache.calcite.linq4j.tree.BinaryExpression) Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 85 with ParameterExpression

use of org.apache.calcite.linq4j.tree.ParameterExpression in project calcite by apache.

the class JaninoRexCompiler method baz.

/**
 * Given a method that implements {@link Scalar#execute(Context, Object[])},
 * adds a bridge method that implements {@link Scalar#execute(Context)}, and
 * compiles.
 */
static Scalar baz(ParameterExpression context_, ParameterExpression outputValues_, BlockStatement block) {
    final List<MemberDeclaration> declarations = Lists.newArrayList();
    // public void execute(Context, Object[] outputValues)
    declarations.add(Expressions.methodDecl(Modifier.PUBLIC, void.class, BuiltInMethod.SCALAR_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(Scalar.class, "this"), BuiltInMethod.SCALAR_EXECUTE2.method, context_, values_)));
    builder.add(Expressions.return_(null, Expressions.arrayIndex(values_, Expressions.constant(0))));
    declarations.add(Expressions.methodDecl(Modifier.PUBLIC, Object.class, BuiltInMethod.SCALAR_EXECUTE1.method.getName(), ImmutableList.of(context_), builder.toBlock()));
    final ClassDeclaration classDeclaration = Expressions.classDecl(Modifier.PUBLIC, "Buzz", null, ImmutableList.<Type>of(Scalar.class), declarations);
    String s = Expressions.toString(declarations, "\n", false);
    if (CalcitePrepareImpl.DEBUG) {
        Util.debugCode(System.out, s);
    }
    try {
        return getScalar(classDeclaration, s);
    } catch (CompileException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ClassDeclaration(org.apache.calcite.linq4j.tree.ClassDeclaration) Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) MemberDeclaration(org.apache.calcite.linq4j.tree.MemberDeclaration) CompileException(org.codehaus.commons.compiler.CompileException) IOException(java.io.IOException) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

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