use of org.apache.calcite.rex.RexProgram in project flink by apache.
the class RelTimeIndicatorConverter method mergeCalcToMaterializeTimeIndicators.
private RelNode mergeCalcToMaterializeTimeIndicators(FlinkLogicalCalc calc, Set<Integer> refIndices) {
RexProgram program = calc.getProgram();
RexProgramBuilder newProgramBuilder = new RexProgramBuilder(program.getInputRowType(), rexBuilder);
for (int idx = 0; idx < program.getNamedProjects().size(); idx++) {
Pair<RexLocalRef, String> pair = program.getNamedProjects().get(idx);
RexNode project = program.expandLocalRef(pair.left);
if (refIndices.contains(idx)) {
project = materializeTimeIndicators(project);
}
newProgramBuilder.addProject(project, pair.right);
}
if (program.getCondition() != null) {
newProgramBuilder.addCondition(program.expandLocalRef(program.getCondition()));
}
RexProgram newProgram = newProgramBuilder.getProgram();
return FlinkLogicalCalc.create(calc.getInput(), newProgram);
}
use of org.apache.calcite.rex.RexProgram in project calcite by apache.
the class Calc method accept.
public RelNode accept(RexShuttle shuttle) {
List<RexNode> oldExprs = program.getExprList();
List<RexNode> exprs = shuttle.apply(oldExprs);
List<RexLocalRef> oldProjects = program.getProjectList();
List<RexLocalRef> projects = shuttle.apply(oldProjects);
RexLocalRef oldCondition = program.getCondition();
RexNode condition;
if (oldCondition != null) {
condition = shuttle.apply(oldCondition);
assert condition instanceof RexLocalRef : "Invalid condition after rewrite. Expected RexLocalRef, got " + condition;
} else {
condition = null;
}
if (exprs == oldExprs && projects == oldProjects && condition == oldCondition) {
return this;
}
return copy(traitSet, getInput(), new RexProgram(program.getInputRowType(), exprs, projects, (RexLocalRef) condition, program.getOutputRowType()));
}
use of org.apache.calcite.rex.RexProgram in project calcite by apache.
the class RelStructuredTypeFlattener method rewriteRel.
public void rewriteRel(LogicalCalc rel) {
// Translate the child.
final RelNode newInput = getNewForOldRel(rel.getInput());
final RelOptCluster cluster = rel.getCluster();
RexProgramBuilder programBuilder = new RexProgramBuilder(newInput.getRowType(), cluster.getRexBuilder());
// Convert the common expressions.
final RexProgram program = rel.getProgram();
final RewriteRexShuttle shuttle = new RewriteRexShuttle();
for (RexNode expr : program.getExprList()) {
programBuilder.registerInput(expr.accept(shuttle));
}
// Convert the projections.
final List<Pair<RexNode, String>> flattenedExpList = Lists.newArrayList();
List<String> fieldNames = rel.getRowType().getFieldNames();
flattenProjections(new RewriteRexShuttle(), program.getProjectList(), fieldNames, "", flattenedExpList);
// Register each of the new projections.
for (Pair<RexNode, String> flattenedExp : flattenedExpList) {
programBuilder.addProject(flattenedExp.left, flattenedExp.right);
}
// Translate the condition.
final RexLocalRef conditionRef = program.getCondition();
if (conditionRef != null) {
programBuilder.addCondition(new RexLocalRef(getNewForOldInput(conditionRef.getIndex()), conditionRef.getType()));
}
RexProgram newProgram = programBuilder.getProgram();
// Create a new calc relational expression.
LogicalCalc newRel = LogicalCalc.create(newInput, newProgram);
setNewForOldRel(rel, newRel);
}
use of org.apache.calcite.rex.RexProgram in project drill by apache.
the class DrillMergeFilterRule method onMatch.
// ~ Methods ----------------------------------------------------------------
// implement RelOptRule
public void onMatch(RelOptRuleCall call) {
Filter topFilter = call.rel(0);
Filter bottomFilter = call.rel(1);
// use RexPrograms to merge the two FilterRels into a single program
// so we can convert the two FilterRel conditions to directly
// reference the bottom FilterRel's child
RexBuilder rexBuilder = topFilter.getCluster().getRexBuilder();
RexProgram bottomProgram = createProgram(bottomFilter);
RexProgram topProgram = createProgram(topFilter);
RexProgram mergedProgram = RexProgramBuilder.mergePrograms(topProgram, bottomProgram, rexBuilder);
RexNode newCondition = mergedProgram.expandLocalRef(mergedProgram.getCondition());
// if(!RexUtil.isFlat(newCondition)){
// RexCall newCall = (RexCall) newCondition;
// newCondition = rexBuilder.makeFlatCall( newCall.getOperator(), newCall.getOperands());
// }
Filter newFilterRel = (Filter) filterFactory.createFilter(bottomFilter.getInput(), RexUtil.flatten(rexBuilder, newCondition));
call.transformTo(newFilterRel);
}
Aggregations