use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexProgram in project flink by apache.
the class PushFilterInCalcIntoTableSourceScanRule method matches.
@Override
public boolean matches(RelOptRuleCall call) {
if (!super.matches(call)) {
return false;
}
FlinkLogicalCalc calc = call.rel(0);
RexProgram originProgram = calc.getProgram();
if (originProgram.getCondition() == null) {
return false;
}
FlinkLogicalTableSourceScan scan = call.rel(1);
TableSourceTable tableSourceTable = scan.getTable().unwrap(TableSourceTable.class);
// we can not push filter twice
return canPushdownFilter(tableSourceTable);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexProgram in project flink by apache.
the class PushFilterInCalcIntoTableSourceScanRule method pushFilterIntoScan.
private void pushFilterIntoScan(RelOptRuleCall call, Calc calc, FlinkLogicalTableSourceScan scan, FlinkPreparingTableBase relOptTable) {
RexProgram originProgram = calc.getProgram();
RelBuilder relBuilder = call.builder();
Tuple2<RexNode[], RexNode[]> extractedPredicates = extractPredicates(originProgram.getInputRowType().getFieldNames().toArray(new String[0]), originProgram.expandLocalRef(originProgram.getCondition()), scan, relBuilder.getRexBuilder());
RexNode[] convertiblePredicates = extractedPredicates._1;
RexNode[] unconvertedPredicates = extractedPredicates._2;
if (convertiblePredicates.length == 0) {
// no condition can be translated to expression
return;
}
Tuple2<SupportsFilterPushDown.Result, TableSourceTable> pushdownResultWithScan = resolveFiltersAndCreateTableSourceTable(convertiblePredicates, relOptTable.unwrap(TableSourceTable.class), scan, relBuilder);
SupportsFilterPushDown.Result result = pushdownResultWithScan._1;
TableSourceTable tableSourceTable = pushdownResultWithScan._2;
FlinkLogicalTableSourceScan newScan = FlinkLogicalTableSourceScan.create(scan.getCluster(), scan.getHints(), tableSourceTable);
// build new calc program
RexProgramBuilder programBuilder = RexProgramBuilder.forProgram(originProgram, call.builder().getRexBuilder(), true);
programBuilder.clearCondition();
if (!result.getRemainingFilters().isEmpty() || unconvertedPredicates.length != 0) {
RexNode remainingCondition = createRemainingCondition(relBuilder, result.getRemainingFilters(), unconvertedPredicates);
RexNode simplifiedRemainingCondition = FlinkRexUtil.simplify(relBuilder.getRexBuilder(), remainingCondition, calc.getCluster().getPlanner().getExecutor());
programBuilder.addCondition(simplifiedRemainingCondition);
}
RexProgram program = programBuilder.getProgram();
if (program.isTrivial()) {
call.transformTo(newScan);
} else {
FlinkLogicalCalc newCalc = FlinkLogicalCalc.create(newScan, program);
call.transformTo(newCalc);
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexProgram in project flink by apache.
the class RelTimeIndicatorConverter method visitCalc.
private RelNode visitCalc(FlinkLogicalCalc calc) {
// visit children and update inputs
RelNode newInput = calc.getInput().accept(this);
RexProgram program = calc.getProgram();
// check if input field contains time indicator type
// materialize field if no time indicator is present anymore
// if input field is already materialized, change to timestamp type
RexTimeIndicatorMaterializer materializer = new RexTimeIndicatorMaterializer(newInput);
List<RexNode> newProjects = program.getProjectList().stream().map(project -> program.expandLocalRef(project).accept(materializer)).collect(Collectors.toList());
// materialize condition due to filter will validate condition type
RexNode newCondition = null;
if (program.getCondition() != null) {
newCondition = program.expandLocalRef(program.getCondition()).accept(materializer);
}
RexProgram newProgram = RexProgram.create(newInput.getRowType(), newProjects, newCondition, program.getOutputRowType().getFieldNames(), rexBuilder);
return calc.copy(calc.getTraitSet(), newInput, newProgram);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexProgram in project flink by apache.
the class RelTimeIndicatorConverter method createCalcToMaterializeTimeIndicators.
private RelNode createCalcToMaterializeTimeIndicators(RelNode input, Set<Integer> refIndices) {
// create new calc
List<RexNode> projects = input.getRowType().getFieldList().stream().map(field -> {
RexNode project = new RexInputRef(field.getIndex(), field.getType());
if (refIndices.contains(field.getIndex())) {
project = materializeTimeIndicators(project);
}
return project;
}).collect(Collectors.toList());
RexProgram newProgram = RexProgram.create(input.getRowType(), projects, null, input.getRowType().getFieldNames(), rexBuilder);
return FlinkLogicalCalc.create(input, newProgram);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexProgram in project beam by apache.
the class BeamIOPushDownRule method canDropCalc.
/**
* Perform a series of checks to determine whether a Calc can be dropped. Following conditions
* need to be met in order for that to happen (logical AND):<br>
* 1. Program should do simple projects, project each field once, and project fields in the same
* order when field reordering is not supported.<br>
* 2. Predicate can be completely pushed-down.<br>
* 3. Project push-down is supported by the IO or all fields are projected by a Calc.
*
* @param program A {@code RexProgram} of a {@code Calc}.
* @param projectSupport An enum containing information about IO project push-down capabilities.
* @param tableFilter A class containing information about IO predicate push-down capabilities.
* @return True when Calc can be dropped, false otherwise.
*/
private boolean canDropCalc(RexProgram program, ProjectSupport projectSupport, BeamSqlTableFilter tableFilter) {
RelDataType calcInputRowType = program.getInputRowType();
// Program should do simple projects, project each field once, and project fields in the same
// order when field reordering is not supported.
boolean fieldReorderingSupported = projectSupport.withFieldReordering();
if (!isProjectRenameOnlyProgram(program, fieldReorderingSupported)) {
return false;
}
// Predicate can be completely pushed-down
if (!tableFilter.getNotSupported().isEmpty()) {
return false;
}
// Project push-down is supported by the IO or all fields are projected by a Calc.
boolean isProjectSupported = projectSupport.isSupported();
boolean allFieldsProjected = program.getProjectList().stream().map(ref -> program.getInputRowType().getFieldList().get(ref.getIndex()).getName()).collect(Collectors.toList()).equals(calcInputRowType.getFieldNames());
return isProjectSupported || allFieldsProjected;
}
Aggregations