Search in sources :

Example 11 with FlinkLogicalCalc

use of org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc in project flink by apache.

the class StreamPhysicalPythonCorrelateRule method matches.

@Override
public boolean matches(RelOptRuleCall call) {
    FlinkLogicalCorrelate correlate = call.rel(0);
    RelNode right = ((RelSubset) correlate.getRight()).getOriginal();
    if (right instanceof FlinkLogicalTableFunctionScan) {
        // right node is a table function
        FlinkLogicalTableFunctionScan scan = (FlinkLogicalTableFunctionScan) right;
        // return true if the table function is python table function
        return PythonUtil.isPythonCall(scan.getCall(), null);
    } else if (right instanceof FlinkLogicalCalc) {
        // a filter is pushed above the table function
        return findTableFunction((FlinkLogicalCalc) right);
    }
    return false;
}
Also used : FlinkLogicalCorrelate(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCorrelate) RelNode(org.apache.calcite.rel.RelNode) FlinkLogicalCalc(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc) FlinkLogicalTableFunctionScan(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableFunctionScan) RelSubset(org.apache.calcite.plan.volcano.RelSubset)

Example 12 with FlinkLogicalCalc

use of org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc in project flink by apache.

the class PythonMapMergeRule method matches.

@Override
public boolean matches(RelOptRuleCall call) {
    FlinkLogicalCalc topCalc = call.rel(0);
    FlinkLogicalCalc middleCalc = call.rel(1);
    FlinkLogicalCalc bottomCalc = call.rel(2);
    RexProgram topProgram = topCalc.getProgram();
    List<RexNode> topProjects = topProgram.getProjectList().stream().map(topProgram::expandLocalRef).collect(Collectors.toList());
    if (topProjects.size() != 1 || !PythonUtil.isPythonCall(topProjects.get(0), null) || !PythonUtil.takesRowAsInput((RexCall) topProjects.get(0))) {
        return false;
    }
    RexProgram bottomProgram = bottomCalc.getProgram();
    List<RexNode> bottomProjects = bottomProgram.getProjectList().stream().map(bottomProgram::expandLocalRef).collect(Collectors.toList());
    if (bottomProjects.size() != 1 || !PythonUtil.isPythonCall(bottomProjects.get(0), null)) {
        return false;
    }
    // Only Python Functions with same Python function kind can be merged together.
    if (PythonUtil.isPythonCall(topProjects.get(0), PythonFunctionKind.GENERAL) ^ PythonUtil.isPythonCall(bottomProjects.get(0), PythonFunctionKind.GENERAL)) {
        return false;
    }
    RexProgram middleProgram = middleCalc.getProgram();
    if (topProgram.getCondition() != null || middleProgram.getCondition() != null || bottomProgram.getCondition() != null) {
        return false;
    }
    List<RexNode> middleProjects = middleProgram.getProjectList().stream().map(middleProgram::expandLocalRef).collect(Collectors.toList());
    int inputRowFieldCount = middleProgram.getInputRowType().getFieldList().get(0).getValue().getFieldList().size();
    return isFlattenCalc(middleProjects, inputRowFieldCount) && isTopCalcTakesWholeMiddleCalcAsInputs((RexCall) topProjects.get(0), middleProjects.size());
}
Also used : RexCall(org.apache.calcite.rex.RexCall) FlinkLogicalCalc(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc) RexProgram(org.apache.calcite.rex.RexProgram) RexNode(org.apache.calcite.rex.RexNode)

Example 13 with FlinkLogicalCalc

use of org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc in project flink by apache.

the class PythonMapMergeRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    FlinkLogicalCalc topCalc = call.rel(0);
    FlinkLogicalCalc middleCalc = call.rel(1);
    FlinkLogicalCalc bottomCalc = call.rel(2);
    RexProgram topProgram = topCalc.getProgram();
    List<RexCall> topProjects = topProgram.getProjectList().stream().map(topProgram::expandLocalRef).map(x -> (RexCall) x).collect(Collectors.toList());
    RexCall topPythonCall = topProjects.get(0);
    // merge topCalc and middleCalc
    RexCall newPythonCall = topPythonCall.clone(topPythonCall.getType(), Collections.singletonList(RexInputRef.of(0, bottomCalc.getRowType())));
    List<RexCall> topMiddleMergedProjects = Collections.singletonList(newPythonCall);
    FlinkLogicalCalc topMiddleMergedCalc = new FlinkLogicalCalc(middleCalc.getCluster(), middleCalc.getTraitSet(), bottomCalc, RexProgram.create(bottomCalc.getRowType(), topMiddleMergedProjects, null, Collections.singletonList("f0"), call.builder().getRexBuilder()));
    // merge bottomCalc
    RexBuilder rexBuilder = call.builder().getRexBuilder();
    RexProgram mergedProgram = RexProgramBuilder.mergePrograms(topMiddleMergedCalc.getProgram(), bottomCalc.getProgram(), rexBuilder);
    Calc newCalc = topMiddleMergedCalc.copy(topMiddleMergedCalc.getTraitSet(), bottomCalc.getInput(), mergedProgram);
    call.transformTo(newCalc);
}
Also used : RexCall(org.apache.calcite.rex.RexCall) PythonUtil(org.apache.flink.table.planner.plan.utils.PythonUtil) RexFieldAccess(org.apache.calcite.rex.RexFieldAccess) RexProgram(org.apache.calcite.rex.RexProgram) RexBuilder(org.apache.calcite.rex.RexBuilder) FlinkLogicalCalc(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc) Collectors(java.util.stream.Collectors) RelOptRuleCall(org.apache.calcite.plan.RelOptRuleCall) RexInputRef(org.apache.calcite.rex.RexInputRef) RelOptRule(org.apache.calcite.plan.RelOptRule) PythonFunctionKind(org.apache.flink.table.functions.python.PythonFunctionKind) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder) List(java.util.List) RexNode(org.apache.calcite.rex.RexNode) Calc(org.apache.calcite.rel.core.Calc) Collections(java.util.Collections) RexCall(org.apache.calcite.rex.RexCall) FlinkLogicalCalc(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc) RexProgram(org.apache.calcite.rex.RexProgram) RexBuilder(org.apache.calcite.rex.RexBuilder) FlinkLogicalCalc(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc) Calc(org.apache.calcite.rel.core.Calc)

Example 14 with FlinkLogicalCalc

use of org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc in project flink by apache.

the class BatchPhysicalPythonCorrelateRule method matches.

@Override
public boolean matches(RelOptRuleCall call) {
    FlinkLogicalCorrelate join = call.rel(0);
    RelNode right = ((RelSubset) join.getRight()).getOriginal();
    if (right instanceof FlinkLogicalTableFunctionScan) {
        // right node is a table function
        FlinkLogicalTableFunctionScan scan = (FlinkLogicalTableFunctionScan) right;
        // return true if the table function is python table function
        return PythonUtil.isPythonCall(scan.getCall(), null);
    } else if (right instanceof FlinkLogicalCalc) {
        // a filter is pushed above the table function
        FlinkLogicalCalc calc = (FlinkLogicalCalc) right;
        RelNode input = ((RelSubset) calc.getInput()).getOriginal();
        if (input instanceof FlinkLogicalTableFunctionScan) {
            FlinkLogicalTableFunctionScan scan = (FlinkLogicalTableFunctionScan) input;
            // return true if the table function is python table function
            return PythonUtil.isPythonCall(scan.getCall(), null);
        }
    }
    return false;
}
Also used : FlinkLogicalCorrelate(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCorrelate) RelNode(org.apache.calcite.rel.RelNode) FlinkLogicalCalc(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc) FlinkLogicalTableFunctionScan(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableFunctionScan) RelSubset(org.apache.calcite.plan.volcano.RelSubset)

Example 15 with FlinkLogicalCalc

use of org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc in project flink by apache.

the class PythonCorrelateSplitRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    FlinkLogicalCorrelate correlate = call.rel(0);
    RexBuilder rexBuilder = call.builder().getRexBuilder();
    RelNode left = ((HepRelVertex) correlate.getLeft()).getCurrentRel();
    RelNode right = ((HepRelVertex) correlate.getRight()).getCurrentRel();
    int primitiveLeftFieldCount = left.getRowType().getFieldCount();
    ArrayBuffer<RexNode> extractedRexNodes = new ArrayBuffer<>();
    RelNode rightNewInput;
    if (right instanceof FlinkLogicalTableFunctionScan) {
        FlinkLogicalTableFunctionScan scan = (FlinkLogicalTableFunctionScan) right;
        rightNewInput = createNewScan(scan, createScalarFunctionSplitter(null, rexBuilder, primitiveLeftFieldCount, extractedRexNodes, scan.getCall()));
    } else {
        FlinkLogicalCalc calc = (FlinkLogicalCalc) right;
        FlinkLogicalTableFunctionScan scan = StreamPhysicalCorrelateRule.getTableScan(calc);
        FlinkLogicalCalc mergedCalc = StreamPhysicalCorrelateRule.getMergedCalc(calc);
        FlinkLogicalTableFunctionScan newScan = createNewScan(scan, createScalarFunctionSplitter(null, rexBuilder, primitiveLeftFieldCount, extractedRexNodes, scan.getCall()));
        rightNewInput = mergedCalc.copy(mergedCalc.getTraitSet(), newScan, mergedCalc.getProgram());
    }
    FlinkLogicalCorrelate newCorrelate;
    if (extractedRexNodes.size() > 0) {
        FlinkLogicalCalc leftCalc = createNewLeftCalc(left, rexBuilder, extractedRexNodes, correlate);
        newCorrelate = new FlinkLogicalCorrelate(correlate.getCluster(), correlate.getTraitSet(), leftCalc, rightNewInput, correlate.getCorrelationId(), correlate.getRequiredColumns(), correlate.getJoinType());
    } else {
        newCorrelate = new FlinkLogicalCorrelate(correlate.getCluster(), correlate.getTraitSet(), left, rightNewInput, correlate.getCorrelationId(), correlate.getRequiredColumns(), correlate.getJoinType());
    }
    FlinkLogicalCalc newTopCalc = createTopCalc(primitiveLeftFieldCount, rexBuilder, extractedRexNodes, correlate.getRowType(), newCorrelate);
    call.transformTo(newTopCalc);
}
Also used : FlinkLogicalCorrelate(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCorrelate) HepRelVertex(org.apache.calcite.plan.hep.HepRelVertex) RelNode(org.apache.calcite.rel.RelNode) FlinkLogicalCalc(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc) RexBuilder(org.apache.calcite.rex.RexBuilder) FlinkLogicalTableFunctionScan(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableFunctionScan) ArrayBuffer(scala.collection.mutable.ArrayBuffer) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

FlinkLogicalCalc (org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc)17 RexNode (org.apache.calcite.rex.RexNode)11 RexProgram (org.apache.calcite.rex.RexProgram)9 FlinkLogicalCorrelate (org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCorrelate)8 FlinkLogicalTableFunctionScan (org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableFunctionScan)8 RelNode (org.apache.calcite.rel.RelNode)6 RexBuilder (org.apache.calcite.rex.RexBuilder)6 RexProgramBuilder (org.apache.calcite.rex.RexProgramBuilder)6 List (java.util.List)5 RexCall (org.apache.calcite.rex.RexCall)5 Collectors (java.util.stream.Collectors)4 RexInputRef (org.apache.calcite.rex.RexInputRef)4 FlinkLogicalTableSourceScan (org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableSourceScan)4 RelOptRule (org.apache.calcite.plan.RelOptRule)3 RelOptRuleCall (org.apache.calcite.plan.RelOptRuleCall)3 HepRelVertex (org.apache.calcite.plan.hep.HepRelVertex)3 RelDataType (org.apache.calcite.rel.type.RelDataType)3 TableSourceTable (org.apache.flink.table.planner.plan.schema.TableSourceTable)3 PythonUtil (org.apache.flink.table.planner.plan.utils.PythonUtil)3 Collections (java.util.Collections)2