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_);
}
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);
}
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);
}
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);
}
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);
}
Aggregations