use of org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc 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);
}
use of org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc 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;
}
use of org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc in project flink by apache.
the class PushWatermarkIntoTableSourceScanAcrossCalcRule method onMatch.
@Override
public void onMatch(RelOptRuleCall call) {
FlinkLogicalWatermarkAssigner watermarkAssigner = call.rel(0);
FlinkLogicalCalc calc = call.rel(1);
RexProgram originProgram = calc.getProgram();
List<RexNode> projectList = originProgram.getProjectList().stream().map(originProgram::expandLocalRef).collect(Collectors.toList());
// get watermark expression
RexNode rowTimeColumn = projectList.get(watermarkAssigner.rowtimeFieldIndex());
RexNode newWatermarkExpr = watermarkAssigner.watermarkExpr().accept(new RexShuttle() {
@Override
public RexNode visitInputRef(RexInputRef inputRef) {
return projectList.get(inputRef.getIndex());
}
});
// push watermark assigner into the scan
FlinkLogicalTableSourceScan newScan = getNewScan(watermarkAssigner, newWatermarkExpr, call.rel(2), ShortcutUtils.unwrapContext(calc).getTableConfig(), // useWatermarkAssignerRowType
false);
FlinkTypeFactory typeFactory = ShortcutUtils.unwrapTypeFactory(calc);
RexBuilder builder = call.builder().getRexBuilder();
// cast timestamp/timestamp_ltz type to rowtime type.
RexNode newRowTimeColumn = builder.makeReinterpretCast(typeFactory.createRowtimeIndicatorType(rowTimeColumn.getType().isNullable(), rowTimeColumn.getType().getSqlTypeName() == SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE), rowTimeColumn, null);
// build new calc program
RexProgramBuilder programBuilder = new RexProgramBuilder(newScan.getRowType(), builder);
List<String> outputFieldNames = originProgram.getOutputRowType().getFieldNames();
for (int i = 0; i < projectList.size(); i++) {
if (i == watermarkAssigner.rowtimeFieldIndex()) {
// replace the origin computed column to keep type consistent
programBuilder.addProject(newRowTimeColumn, outputFieldNames.get(i));
} else {
programBuilder.addProject(projectList.get(i), outputFieldNames.get(i));
}
}
if (originProgram.getCondition() != null) {
programBuilder.addCondition(originProgram.expandLocalRef(originProgram.getCondition()));
}
FlinkLogicalCalc newCalc = FlinkLogicalCalc.create(newScan, programBuilder.getProgram());
call.transformTo(newCalc);
}
use of org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc 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;
}
use of org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc in project flink by apache.
the class PythonCorrelateSplitRule method createNewLeftCalc.
private FlinkLogicalCalc createNewLeftCalc(RelNode left, RexBuilder rexBuilder, ArrayBuffer<RexNode> extractedRexNodes, FlinkLogicalCorrelate correlate) {
// add the fields of the primitive left input.
List<RexNode> leftCalcProjects = new LinkedList<>();
RelDataType leftRowType = left.getRowType();
List<String> leftCalcCalcFieldNames = createNewFieldNames(leftRowType, rexBuilder, leftRowType.getFieldCount(), extractedRexNodes, leftCalcProjects);
// create a new calc
return new FlinkLogicalCalc(correlate.getCluster(), correlate.getTraitSet(), left, RexProgram.create(leftRowType, leftCalcProjects, null, leftCalcCalcFieldNames, rexBuilder));
}
Aggregations