Search in sources :

Example 1 with FlinkLogicalCorrelate

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

the class CalcPythonCorrelateTransposeRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    FlinkLogicalCorrelate correlate = call.rel(0);
    FlinkLogicalCalc right = call.rel(2);
    RexBuilder rexBuilder = call.builder().getRexBuilder();
    FlinkLogicalCalc mergedCalc = StreamPhysicalCorrelateRule.getMergedCalc(right);
    FlinkLogicalTableFunctionScan tableScan = StreamPhysicalCorrelateRule.getTableScan(mergedCalc);
    RexProgram mergedCalcProgram = mergedCalc.getProgram();
    InputRefRewriter inputRefRewriter = new InputRefRewriter(correlate.getRowType().getFieldCount() - mergedCalc.getRowType().getFieldCount());
    List<RexNode> correlateFilters = RelOptUtil.conjunctions(mergedCalcProgram.expandLocalRef(mergedCalcProgram.getCondition())).stream().map(x -> x.accept(inputRefRewriter)).collect(Collectors.toList());
    FlinkLogicalCorrelate newCorrelate = new FlinkLogicalCorrelate(correlate.getCluster(), correlate.getTraitSet(), correlate.getLeft(), tableScan, correlate.getCorrelationId(), correlate.getRequiredColumns(), correlate.getJoinType());
    RexNode topCalcCondition = RexUtil.composeConjunction(rexBuilder, correlateFilters);
    RexProgram rexProgram = new RexProgramBuilder(newCorrelate.getRowType(), rexBuilder).getProgram();
    FlinkLogicalCalc newTopCalc = new FlinkLogicalCalc(newCorrelate.getCluster(), newCorrelate.getTraitSet(), newCorrelate, RexProgram.create(newCorrelate.getRowType(), rexProgram.getExprList(), topCalcCondition, newCorrelate.getRowType(), rexBuilder));
    call.transformTo(newTopCalc);
}
Also used : PythonUtil(org.apache.flink.table.planner.plan.utils.PythonUtil) RexProgram(org.apache.calcite.rex.RexProgram) RexBuilder(org.apache.calcite.rex.RexBuilder) FlinkLogicalTableFunctionScan(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableFunctionScan) FlinkLogicalCalc(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc) RelOptUtil(org.apache.calcite.plan.RelOptUtil) Collectors(java.util.stream.Collectors) RelOptRuleCall(org.apache.calcite.plan.RelOptRuleCall) FlinkLogicalRel(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalRel) RelOptRule(org.apache.calcite.plan.RelOptRule) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder) RexUtil(org.apache.calcite.rex.RexUtil) List(java.util.List) StreamPhysicalCorrelateRule(org.apache.flink.table.planner.plan.rules.physical.stream.StreamPhysicalCorrelateRule) RexNode(org.apache.calcite.rex.RexNode) JoinRelType(org.apache.calcite.rel.core.JoinRelType) FlinkLogicalCorrelate(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCorrelate) FlinkLogicalCorrelate(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCorrelate) FlinkLogicalCalc(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc) RexProgram(org.apache.calcite.rex.RexProgram) RexBuilder(org.apache.calcite.rex.RexBuilder) FlinkLogicalTableFunctionScan(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableFunctionScan) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder) RexNode(org.apache.calcite.rex.RexNode)

Example 2 with FlinkLogicalCorrelate

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

the class CalcPythonCorrelateTransposeRule method matches.

@Override
public boolean matches(RelOptRuleCall call) {
    FlinkLogicalCorrelate correlate = call.rel(0);
    FlinkLogicalCalc right = call.rel(2);
    JoinRelType joinType = correlate.getJoinType();
    FlinkLogicalCalc mergedCalc = StreamPhysicalCorrelateRule.getMergedCalc(right);
    FlinkLogicalTableFunctionScan scan = StreamPhysicalCorrelateRule.getTableScan(mergedCalc);
    return joinType == JoinRelType.INNER && PythonUtil.isPythonCall(scan.getCall(), null) && mergedCalc.getProgram().getCondition() != null;
}
Also used : FlinkLogicalCorrelate(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCorrelate) JoinRelType(org.apache.calcite.rel.core.JoinRelType) FlinkLogicalCalc(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc) FlinkLogicalTableFunctionScan(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableFunctionScan)

Example 3 with FlinkLogicalCorrelate

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

the class PythonCorrelateSplitRule method matches.

@Override
public boolean matches(RelOptRuleCall call) {
    FlinkLogicalCorrelate correlate = call.rel(0);
    RelNode right = ((HepRelVertex) correlate.getRight()).getCurrentRel();
    FlinkLogicalTableFunctionScan tableFunctionScan;
    if (right instanceof FlinkLogicalTableFunctionScan) {
        tableFunctionScan = (FlinkLogicalTableFunctionScan) right;
    } else if (right instanceof FlinkLogicalCalc) {
        tableFunctionScan = StreamPhysicalCorrelateRule.getTableScan((FlinkLogicalCalc) right);
    } else {
        return false;
    }
    RexNode rexNode = tableFunctionScan.getCall();
    if (rexNode instanceof RexCall) {
        return PythonUtil.isPythonCall(rexNode, null) && PythonUtil.containsNonPythonCall(rexNode) || PythonUtil.isNonPythonCall(rexNode) && PythonUtil.containsPythonCall(rexNode, null) || (PythonUtil.isPythonCall(rexNode, null) && RexUtil.containsFieldAccess(rexNode));
    }
    return false;
}
Also used : RexCall(org.apache.calcite.rex.RexCall) 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) FlinkLogicalTableFunctionScan(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableFunctionScan) RexNode(org.apache.calcite.rex.RexNode)

Example 4 with FlinkLogicalCorrelate

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

the class PythonCorrelateSplitRule method createTopCalc.

private FlinkLogicalCalc createTopCalc(int primitiveLeftFieldCount, RexBuilder rexBuilder, ArrayBuffer<RexNode> extractedRexNodes, RelDataType calcRowType, FlinkLogicalCorrelate newCorrelate) {
    RexProgram rexProgram = new RexProgramBuilder(newCorrelate.getRowType(), rexBuilder).getProgram();
    int offset = extractedRexNodes.size() + primitiveLeftFieldCount;
    // extract correlate output RexNode.
    List<RexNode> newTopCalcProjects = rexProgram.getExprList().stream().filter(x -> x instanceof RexInputRef).filter(x -> {
        int index = ((RexInputRef) x).getIndex();
        return index < primitiveLeftFieldCount || index >= offset;
    }).collect(Collectors.toList());
    return new FlinkLogicalCalc(newCorrelate.getCluster(), newCorrelate.getTraitSet(), newCorrelate, RexProgram.create(newCorrelate.getRowType(), newTopCalcProjects, null, calcRowType, rexBuilder));
}
Also used : RexFieldAccess(org.apache.calcite.rex.RexFieldAccess) RexProgram(org.apache.calcite.rex.RexProgram) RexUtil(org.apache.calcite.rex.RexUtil) SqlValidatorUtil(org.apache.calcite.sql.validate.SqlValidatorUtil) RexNode(org.apache.calcite.rex.RexNode) LinkedList(java.util.LinkedList) ArrayBuffer(scala.collection.mutable.ArrayBuffer) PythonUtil(org.apache.flink.table.planner.plan.utils.PythonUtil) RelDataType(org.apache.calcite.rel.type.RelDataType) RexDefaultVisitor(org.apache.flink.table.planner.plan.utils.RexDefaultVisitor) RexBuilder(org.apache.calcite.rex.RexBuilder) Iterator(scala.collection.Iterator) FlinkLogicalTableFunctionScan(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableFunctionScan) FlinkLogicalCalc(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc) RelNode(org.apache.calcite.rel.RelNode) Collectors(java.util.stream.Collectors) RelOptRuleCall(org.apache.calcite.plan.RelOptRuleCall) RexInputRef(org.apache.calcite.rex.RexInputRef) RelOptRule(org.apache.calcite.plan.RelOptRule) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder) List(java.util.List) StreamPhysicalCorrelateRule(org.apache.flink.table.planner.plan.rules.physical.stream.StreamPhysicalCorrelateRule) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RexCorrelVariable(org.apache.calcite.rex.RexCorrelVariable) HepRelVertex(org.apache.calcite.plan.hep.HepRelVertex) FlinkLogicalCorrelate(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCorrelate) RexCall(org.apache.calcite.rex.RexCall) FlinkLogicalCalc(org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc) RexProgram(org.apache.calcite.rex.RexProgram) RexInputRef(org.apache.calcite.rex.RexInputRef) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder) RexNode(org.apache.calcite.rex.RexNode)

Example 5 with FlinkLogicalCorrelate

use of org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCorrelate 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)

Aggregations

FlinkLogicalCalc (org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc)8 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 RexNode (org.apache.calcite.rex.RexNode)5 RexBuilder (org.apache.calcite.rex.RexBuilder)4 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 HepRelVertex (org.apache.calcite.plan.hep.HepRelVertex)3 RexCall (org.apache.calcite.rex.RexCall)3 RexProgram (org.apache.calcite.rex.RexProgram)3 RexProgramBuilder (org.apache.calcite.rex.RexProgramBuilder)3 RelOptRule (org.apache.calcite.plan.RelOptRule)2 RelOptRuleCall (org.apache.calcite.plan.RelOptRuleCall)2 RelOptUtil (org.apache.calcite.plan.RelOptUtil)2 RelSubset (org.apache.calcite.plan.volcano.RelSubset)2 JoinRelType (org.apache.calcite.rel.core.JoinRelType)2 RelDataType (org.apache.calcite.rel.type.RelDataType)2 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)2 RexInputRef (org.apache.calcite.rex.RexInputRef)2