Search in sources :

Example 76 with ParameterExpression

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

the class EnumUtils method generatePredicate.

/**
 * Returns a predicate expression based on a join condition. *
 */
public 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(ImmutableList.of(Pair.of(left_, leftPhysType), Pair.of(right_, rightPhysType))), implementor.allCorrelateVariables, implementor.getConformance())));
    return Expressions.lambda(Predicate2.class, builder.toBlock(), left_, right_);
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 77 with ParameterExpression

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

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)

Example 78 with ParameterExpression

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

the class EnumUtils method joinSelector.

public static Expression joinSelector(JoinRelType joinType, PhysType physType, List<PhysType> inputPhysTypes) {
    // A parameter for each input.
    final List<ParameterExpression> parameters = new ArrayList<>();
    // Generate all fields.
    final List<Expression> expressions = new ArrayList<>();
    final int outputFieldCount = physType.getRowType().getFieldCount();
    for (Ord<PhysType> ord : Ord.zip(inputPhysTypes)) {
        final PhysType inputPhysType = ord.e.makeNullable(joinType.generatesNullsOn(ord.i));
        // 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()), EnumUtils.LEFT_RIGHT.get(ord.i));
        parameters.add(parameter);
        if (expressions.size() == outputFieldCount) {
            // For instance, if semi-join needs to return just the left inputs
            break;
        }
        final int fieldCount = inputPhysType.getRowType().getFieldCount();
        for (int i = 0; i < fieldCount; i++) {
            Expression expression = inputPhysType.fieldReference(parameter, i, physType.getJavaFieldType(expressions.size()));
            if (joinType.generatesNullsOn(ord.i)) {
                expression = Expressions.condition(Expressions.equal(parameter, Expressions.constant(null)), Expressions.constant(null), expression);
            }
            expressions.add(expression);
        }
    }
    return Expressions.lambda(Function2.class, physType.record(expressions), parameters);
}
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)

Example 79 with ParameterExpression

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

the class PhysTypeImpl method generateSelector.

public Expression generateSelector(final ParameterExpression parameter, final List<Integer> fields, List<Integer> usedFields, JavaRowFormat targetFormat) {
    final PhysType targetPhysType = project(fields, true, targetFormat);
    final List<Expression> expressions = new ArrayList<>();
    for (Ord<Integer> ord : Ord.zip(fields)) {
        final Integer field = ord.e;
        if (usedFields.contains(field)) {
            expressions.add(fieldReference(parameter, field));
        } else {
            final Primitive primitive = Primitive.of(targetPhysType.fieldClass(ord.i));
            expressions.add(Expressions.constant(primitive != null ? primitive.defaultValue : null));
        }
    }
    for (Integer field : fields) {
        expressions.add(Expressions.constant(!usedFields.contains(field)));
    }
    return Expressions.lambda(Function1.class, targetPhysType.record(expressions), parameter);
}
Also used : Primitive(org.apache.calcite.linq4j.tree.Primitive) Expression(org.apache.calcite.linq4j.tree.Expression) EnumUtils.generateCollatorExpression(org.apache.calcite.adapter.enumerable.EnumUtils.generateCollatorExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ArrayList(java.util.ArrayList)

Example 80 with ParameterExpression

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

the class PhysTypeImpl method convertTo.

public Expression convertTo(Expression exp, JavaRowFormat targetFormat) {
    if (format == targetFormat) {
        return exp;
    }
    final ParameterExpression o_ = Expressions.parameter(javaRowClass, "o");
    final int fieldCount = rowType.getFieldCount();
    // The conversion must be strict so optimizations of the targetFormat should not be performed
    // by the code that follows. If necessary the target format can be optimized before calling
    // this method.
    PhysType targetPhysType = PhysTypeImpl.of(typeFactory, rowType, targetFormat, false);
    final Expression selector = Expressions.lambda(Function1.class, targetPhysType.record(fieldReferences(o_, Util.range(fieldCount))), o_);
    return Expressions.call(exp, BuiltInMethod.SELECT.method, selector);
}
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)

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