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