Search in sources :

Example 46 with RexProgram

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexProgram in project flink by apache.

the class RedundantRankNumberColumnRemoveRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    FlinkLogicalCalc calc = call.rel(0);
    FlinkLogicalRank rank = call.rel(1);
    FlinkLogicalRank newRank = new FlinkLogicalRank(rank.getCluster(), rank.getTraitSet(), rank.getInput(), rank.partitionKey(), rank.orderKey(), rank.rankType(), rank.rankRange(), rank.rankNumberType(), false);
    RexProgram oldProgram = calc.getProgram();
    Pair<List<RexNode>, RexNode> projectsAndCondition = getProjectsAndCondition(oldProgram);
    RexProgram newProgram = RexProgram.create(newRank.getRowType(), projectsAndCondition.left, projectsAndCondition.right, oldProgram.getOutputRowType(), rank.getCluster().getRexBuilder());
    FlinkLogicalCalc newCalc = FlinkLogicalCalc.create(newRank, newProgram);
    call.transformTo(newCalc);
}
Also used : FlinkLogicalCalc(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc) FlinkLogicalRank(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalRank) RexProgram(org.apache.calcite.rex.RexProgram) List(java.util.List) RexNode(org.apache.calcite.rex.RexNode)

Example 47 with RexProgram

use of org.apache.beam.vendor.calcite.v1_28_0.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);
}
Also used : RexProgram(org.apache.calcite.rex.RexProgram) RexLocalRef(org.apache.calcite.rex.RexLocalRef) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder) RexNode(org.apache.calcite.rex.RexNode)

Example 48 with RexProgram

use of org.apache.beam.vendor.calcite.v1_28_0.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()));
}
Also used : RexProgram(org.apache.calcite.rex.RexProgram) RexLocalRef(org.apache.calcite.rex.RexLocalRef) RexNode(org.apache.calcite.rex.RexNode)

Example 49 with RexProgram

use of org.apache.beam.vendor.calcite.v1_28_0.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);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RexProgram(org.apache.calcite.rex.RexProgram) RelNode(org.apache.calcite.rel.RelNode) RexLocalRef(org.apache.calcite.rex.RexLocalRef) LogicalCalc(org.apache.calcite.rel.logical.LogicalCalc) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder) RexNode(org.apache.calcite.rex.RexNode) Pair(org.apache.calcite.util.Pair)

Example 50 with RexProgram

use of org.apache.beam.vendor.calcite.v1_28_0.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);
}
Also used : Filter(org.apache.calcite.rel.core.Filter) RexProgram(org.apache.calcite.rex.RexProgram) RexBuilder(org.apache.calcite.rex.RexBuilder) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RexProgram (org.apache.calcite.rex.RexProgram)46 RexNode (org.apache.calcite.rex.RexNode)29 RexProgramBuilder (org.apache.calcite.rex.RexProgramBuilder)26 RexBuilder (org.apache.calcite.rex.RexBuilder)21 RelNode (org.apache.calcite.rel.RelNode)17 ArrayList (java.util.ArrayList)13 RelDataType (org.apache.calcite.rel.type.RelDataType)13 RexLocalRef (org.apache.calcite.rex.RexLocalRef)12 LogicalCalc (org.apache.calcite.rel.logical.LogicalCalc)11 FlinkLogicalCalc (org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc)10 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)9 List (java.util.List)7 RexInputRef (org.apache.calcite.rex.RexInputRef)7 RexLocalRef (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLocalRef)6 Collectors (java.util.stream.Collectors)5 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)5 RexCall (org.apache.calcite.rex.RexCall)5 RexShuttle (org.apache.calcite.rex.RexShuttle)5 Collections (java.util.Collections)4 RelOptUtil (org.apache.calcite.plan.RelOptUtil)4