use of org.apache.calcite.rex.RexProgramBuilder in project flink by apache.
the class WatermarkAssignerChangelogNormalizeTransposeRule method pushDownTransformedWatermarkAndCalc.
private RelNode pushDownTransformedWatermarkAndCalc(RexProgram newPushDownProgram, StreamPhysicalWatermarkAssigner watermark, StreamPhysicalExchange exchange, StreamPhysicalChangelogNormalize changelogNormalize, StreamPhysicalCalc calc) {
final RelNode pushDownCalc = calc.copy(// Clears distribution on new Calc
calc.getTraitSet().plus(FlinkRelDistribution.DEFAULT()), exchange.getInput(), newPushDownProgram);
final Mappings.TargetMapping mappingOfPushDownCalc = buildMapping(newPushDownProgram);
final RelDistribution newDistribution = exchange.getDistribution().apply(mappingOfPushDownCalc);
final RelNode newChangelogNormalize = buildTreeInOrder(pushDownCalc, Tuple2.of(watermark, watermark.getTraitSet().plus(FlinkRelDistribution.DEFAULT())), // mapping of Calc
Tuple2.of(exchange, exchange.getTraitSet().plus(newDistribution)), Tuple2.of(changelogNormalize, changelogNormalize.getTraitSet().plus(newDistribution)));
final List<String> newInputFieldNames = newChangelogNormalize.getRowType().getFieldNames();
final RexProgramBuilder topProgramBuilder = new RexProgramBuilder(newChangelogNormalize.getRowType(), changelogNormalize.getCluster().getRexBuilder());
for (int fieldIdx = 0; fieldIdx < calc.getRowType().getFieldCount(); fieldIdx++) {
topProgramBuilder.addProject(RexInputRef.of(fieldIdx, newChangelogNormalize.getRowType()), newInputFieldNames.get(fieldIdx));
}
final RexProgram topProgram = topProgramBuilder.getProgram();
return calc.copy(calc.getTraitSet(), newChangelogNormalize, topProgram);
}
use of org.apache.calcite.rex.RexProgramBuilder 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));
}
use of org.apache.calcite.rex.RexProgramBuilder 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.calcite.rex.RexProgramBuilder in project hive by apache.
the class HiveSemiJoinProjectTransposeRule method adjustCondition.
/**
* Pulls the project above the semijoin and returns the resulting semijoin
* condition. As a result, the semijoin condition should be modified such
* that references to the LHS of a semijoin should now reference the
* children of the project that's on the LHS.
*
* @param project Project on the LHS of the semijoin
* @param semiJoin the semijoin
* @return the modified semijoin condition
*/
private RexNode adjustCondition(Project project, Join semiJoin) {
// create two RexPrograms -- the bottom one representing a
// concatenation of the project and the RHS of the semijoin and the
// top one representing the semijoin condition
RexBuilder rexBuilder = project.getCluster().getRexBuilder();
RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory();
RelNode rightChild = semiJoin.getRight();
// for the bottom RexProgram, the input is a concatenation of the
// child of the project and the RHS of the semijoin
RelDataType bottomInputRowType = SqlValidatorUtil.deriveJoinRowType(project.getInput().getRowType(), rightChild.getRowType(), JoinRelType.INNER, typeFactory, null, semiJoin.getSystemFieldList());
RexProgramBuilder bottomProgramBuilder = new RexProgramBuilder(bottomInputRowType, rexBuilder);
// of the semijoin
for (Pair<RexNode, String> pair : project.getNamedProjects()) {
bottomProgramBuilder.addProject(pair.left, pair.right);
}
int nLeftFields = project.getInput().getRowType().getFieldCount();
List<RelDataTypeField> rightFields = rightChild.getRowType().getFieldList();
int nRightFields = rightFields.size();
for (int i = 0; i < nRightFields; i++) {
final RelDataTypeField field = rightFields.get(i);
RexNode inputRef = rexBuilder.makeInputRef(field.getType(), i + nLeftFields);
bottomProgramBuilder.addProject(inputRef, field.getName());
}
RexProgram bottomProgram = bottomProgramBuilder.getProgram();
// input rowtype into the top program is the concatenation of the
// project and the RHS of the semijoin
RelDataType topInputRowType = SqlValidatorUtil.deriveJoinRowType(project.getRowType(), rightChild.getRowType(), JoinRelType.INNER, typeFactory, null, semiJoin.getSystemFieldList());
RexProgramBuilder topProgramBuilder = new RexProgramBuilder(topInputRowType, rexBuilder);
topProgramBuilder.addIdentity();
topProgramBuilder.addCondition(semiJoin.getCondition());
RexProgram topProgram = topProgramBuilder.getProgram();
// merge the programs and expand out the local references to form
// the new semijoin condition; it now references a concatenation of
// the project's child and the RHS of the semijoin
RexProgram mergedProgram = RexProgramBuilder.mergePrograms(topProgram, bottomProgram, rexBuilder);
return mergedProgram.expandLocalRef(mergedProgram.getCondition());
}
use of org.apache.calcite.rex.RexProgramBuilder in project drill by axbaretto.
the class DrillMergeFilterRule method createProgram.
/**
* Creates a RexProgram corresponding to a LogicalFilter
*
* @param filterRel the LogicalFilter
* @return created RexProgram
*/
private RexProgram createProgram(Filter filterRel) {
RexProgramBuilder programBuilder = new RexProgramBuilder(filterRel.getRowType(), filterRel.getCluster().getRexBuilder());
programBuilder.addIdentity();
programBuilder.addCondition(filterRel.getCondition());
return programBuilder.getProgram();
}
Aggregations