Java Code Examples for org.apache.calcite.rex.RexProgramBuilder#mergePrograms()
The following examples show how to use
org.apache.calcite.rex.RexProgramBuilder#mergePrograms() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FilterMergeCrule.java From dremio-oss with Apache License 2.0 | 6 votes |
public void onMatch(RelOptRuleCall call) { final Filter topFilter = call.rel(0); final Filter bottomFilter = call.rel(1); // use RexPrograms to merge the two FilterRels into a single program // so we can convert the two LogicalFilter conditions to directly // reference the bottom LogicalFilter's child RexBuilder rexBuilder = topFilter.getCluster().getRexBuilder(); RexProgram bottomProgram = createProgram(bottomFilter); RexProgram topProgram = createProgram(topFilter); RexProgram mergedProgram = RexProgramBuilder.mergePrograms(topProgram, bottomProgram, rexBuilder); RexNode newCondition = mergedProgram.expandLocalRef(mergedProgram.getCondition()); final RelBuilder relBuilder = call.builder(); relBuilder.push(bottomFilter.getInput()).filter(newCondition); call.transformTo(relBuilder.build()); }
Example 2
Source File: FilterMergeRule.java From Bats with Apache License 2.0 | 5 votes |
public void onMatch(RelOptRuleCall call) { final Filter topFilter = call.rel(0); final Filter bottomFilter = call.rel(1); // use RexPrograms to merge the two FilterRels into a single program // so we can convert the two LogicalFilter conditions to directly // reference the bottom LogicalFilter's child RexBuilder rexBuilder = topFilter.getCluster().getRexBuilder(); RexProgram bottomProgram = createProgram(bottomFilter); RexProgram topProgram = createProgram(topFilter); RexProgram mergedProgram = RexProgramBuilder.mergePrograms( topProgram, bottomProgram, rexBuilder); RexNode newCondition = mergedProgram.expandLocalRef( mergedProgram.getCondition()); final RelBuilder relBuilder = call.builder(); relBuilder.push(bottomFilter.getInput()) .filter(newCondition); call.transformTo(relBuilder.build()); }
Example 3
Source File: FilterCalcMergeRule.java From Bats with Apache License 2.0 | 5 votes |
public void onMatch(RelOptRuleCall call) { final LogicalFilter filter = call.rel(0); final LogicalCalc calc = call.rel(1); // Don't merge a filter onto a calc which contains windowed aggregates. // That would effectively be pushing a multiset down through a filter. // We'll have chance to merge later, when the over is expanded. if (calc.getProgram().containsAggs()) { return; } // Create a program containing the filter. final RexBuilder rexBuilder = filter.getCluster().getRexBuilder(); final RexProgramBuilder progBuilder = new RexProgramBuilder( calc.getRowType(), rexBuilder); progBuilder.addIdentity(); progBuilder.addCondition(filter.getCondition()); RexProgram topProgram = progBuilder.getProgram(); RexProgram bottomProgram = calc.getProgram(); // Merge the programs together. RexProgram mergedProgram = RexProgramBuilder.mergePrograms( topProgram, bottomProgram, rexBuilder); final LogicalCalc newCalc = LogicalCalc.create(calc.getInput(), mergedProgram); call.transformTo(newCalc); }
Example 4
Source File: DrillMergeFilterRule.java From Bats with Apache License 2.0 | 5 votes |
public void onMatch(RelOptRuleCall call) { Filter topFilter = call.rel(0); Filter bottomFilter = call.rel(1); // use RexPrograms to merge the two FilterRels into a single program // so we can convert the two FilterRel conditions to directly // reference the bottom FilterRel's child RexBuilder rexBuilder = topFilter.getCluster().getRexBuilder(); RexProgram bottomProgram = createProgram(bottomFilter); RexProgram topProgram = createProgram(topFilter); RexProgram mergedProgram = RexProgramBuilder.mergePrograms( topProgram, bottomProgram, rexBuilder); RexNode newCondition = mergedProgram.expandLocalRef( mergedProgram.getCondition()); // if(!RexUtil.isFlat(newCondition)){ // RexCall newCall = (RexCall) newCondition; // newCondition = rexBuilder.makeFlatCall( newCall.getOperator(), newCall.getOperands()); // } Filter newFilterRel = (Filter) filterFactory.createFilter( bottomFilter.getInput(), RexUtil.flatten(rexBuilder, newCondition)); call.transformTo(newFilterRel); }
Example 5
Source File: FilterCalcMergeRule.java From calcite with Apache License 2.0 | 5 votes |
public void onMatch(RelOptRuleCall call) { final LogicalFilter filter = call.rel(0); final LogicalCalc calc = call.rel(1); // Don't merge a filter onto a calc which contains windowed aggregates. // That would effectively be pushing a multiset down through a filter. // We'll have chance to merge later, when the over is expanded. if (calc.getProgram().containsAggs()) { return; } // Create a program containing the filter. final RexBuilder rexBuilder = filter.getCluster().getRexBuilder(); final RexProgramBuilder progBuilder = new RexProgramBuilder( calc.getRowType(), rexBuilder); progBuilder.addIdentity(); progBuilder.addCondition(filter.getCondition()); RexProgram topProgram = progBuilder.getProgram(); RexProgram bottomProgram = calc.getProgram(); // Merge the programs together. RexProgram mergedProgram = RexProgramBuilder.mergePrograms( topProgram, bottomProgram, rexBuilder); final LogicalCalc newCalc = LogicalCalc.create(calc.getInput(), mergedProgram); call.transformTo(newCalc); }
Example 6
Source File: ProjectCalcMergeRule.java From Bats with Apache License 2.0 | 4 votes |
public void onMatch(RelOptRuleCall call) { final LogicalProject project = call.rel(0); final LogicalCalc calc = call.rel(1); // Don't merge a project which contains windowed aggregates onto a // calc. That would effectively be pushing a windowed aggregate down // through a filter. Transform the project into an identical calc, // which we'll have chance to merge later, after the over is // expanded. final RelOptCluster cluster = project.getCluster(); RexProgram program = RexProgram.create( calc.getRowType(), project.getProjects(), null, project.getRowType(), cluster.getRexBuilder()); if (RexOver.containsOver(program)) { LogicalCalc projectAsCalc = LogicalCalc.create(calc, program); call.transformTo(projectAsCalc); return; } // Create a program containing the project node's expressions. final RexBuilder rexBuilder = cluster.getRexBuilder(); final RexProgramBuilder progBuilder = new RexProgramBuilder( calc.getRowType(), rexBuilder); for (Pair<RexNode, String> field : project.getNamedProjects()) { progBuilder.addProject(field.left, field.right); } RexProgram topProgram = progBuilder.getProgram(); RexProgram bottomProgram = calc.getProgram(); // Merge the programs together. RexProgram mergedProgram = RexProgramBuilder.mergePrograms( topProgram, bottomProgram, rexBuilder); final LogicalCalc newCalc = LogicalCalc.create(calc.getInput(), mergedProgram); call.transformTo(newCalc); }
Example 7
Source File: SemiJoinProjectTransposeRule.java From Bats with Apache License 2.0 | 4 votes |
/** * 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 LogicalProject on the LHS of the semijoin * @param semiJoin the semijoin * @return the modified semijoin condition */ private RexNode adjustCondition(LogicalProject project, SemiJoin 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); // add the project expressions, then add input references for the RHS // 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()); }
Example 8
Source File: FlinkSemiAntiJoinProjectTransposeRule.java From flink with Apache License 2.0 | 4 votes |
/** * Pulls the project above the semi/anti join and returns the resulting semi/anti join * condition. As a result, the semi/anti join condition should be modified such * that references to the LHS of a semi/anti join should now reference the * children of the project that's on the LHS. * * @param project LogicalProject on the LHS of the semi/anti join * @param join the semi/anti join * @return the modified semi/anti join condition */ private RexNode adjustCondition(LogicalProject project, Join join) { // create two RexPrograms -- the bottom one representing a // concatenation of the project and the RHS of the semi/anti join and the // top one representing the semi/anti join condition RexBuilder rexBuilder = project.getCluster().getRexBuilder(); RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory(); RelNode rightChild = join.getRight(); // for the bottom RexProgram, the input is a concatenation of the // child of the project and the RHS of the semi/anti join RelDataType bottomInputRowType = SqlValidatorUtil.deriveJoinRowType( project.getInput().getRowType(), rightChild.getRowType(), JoinRelType.INNER, typeFactory, null, join.getSystemFieldList()); RexProgramBuilder bottomProgramBuilder = new RexProgramBuilder(bottomInputRowType, rexBuilder); // add the project expressions, then add input references for the RHS // of the semi/anti join 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 semi/anti join RelDataType topInputRowType = SqlValidatorUtil.deriveJoinRowType( project.getRowType(), rightChild.getRowType(), JoinRelType.INNER, typeFactory, null, join.getSystemFieldList()); RexProgramBuilder topProgramBuilder = new RexProgramBuilder( topInputRowType, rexBuilder); topProgramBuilder.addIdentity(); topProgramBuilder.addCondition(join.getCondition()); RexProgram topProgram = topProgramBuilder.getProgram(); // merge the programs and expand out the local references to form // the new semi/anti join condition; it now references a concatenation of // the project's child and the RHS of the semi/anti join RexProgram mergedProgram = RexProgramBuilder.mergePrograms( topProgram, bottomProgram, rexBuilder); return mergedProgram.expandLocalRef( mergedProgram.getCondition()); }
Example 9
Source File: FlinkSemiAntiJoinProjectTransposeRule.java From flink with Apache License 2.0 | 4 votes |
/** * Pulls the project above the semi/anti join and returns the resulting semi/anti join * condition. As a result, the semi/anti join condition should be modified such * that references to the LHS of a semi/anti join should now reference the * children of the project that's on the LHS. * * @param project LogicalProject on the LHS of the semi/anti join * @param join the semi/anti join * @return the modified semi/anti join condition */ private RexNode adjustCondition(LogicalProject project, Join join) { // create two RexPrograms -- the bottom one representing a // concatenation of the project and the RHS of the semi/anti join and the // top one representing the semi/anti join condition RexBuilder rexBuilder = project.getCluster().getRexBuilder(); RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory(); RelNode rightChild = join.getRight(); // for the bottom RexProgram, the input is a concatenation of the // child of the project and the RHS of the semi/anti join RelDataType bottomInputRowType = SqlValidatorUtil.deriveJoinRowType( project.getInput().getRowType(), rightChild.getRowType(), JoinRelType.INNER, typeFactory, null, join.getSystemFieldList()); RexProgramBuilder bottomProgramBuilder = new RexProgramBuilder(bottomInputRowType, rexBuilder); // add the project expressions, then add input references for the RHS // of the semi/anti join 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 semi/anti join RelDataType topInputRowType = SqlValidatorUtil.deriveJoinRowType( project.getRowType(), rightChild.getRowType(), JoinRelType.INNER, typeFactory, null, join.getSystemFieldList()); RexProgramBuilder topProgramBuilder = new RexProgramBuilder( topInputRowType, rexBuilder); topProgramBuilder.addIdentity(); topProgramBuilder.addCondition(join.getCondition()); RexProgram topProgram = topProgramBuilder.getProgram(); // merge the programs and expand out the local references to form // the new semi/anti join condition; it now references a concatenation of // the project's child and the RHS of the semi/anti join RexProgram mergedProgram = RexProgramBuilder.mergePrograms( topProgram, bottomProgram, rexBuilder); return mergedProgram.expandLocalRef( mergedProgram.getCondition()); }
Example 10
Source File: ProjectCalcMergeRule.java From calcite with Apache License 2.0 | 4 votes |
public void onMatch(RelOptRuleCall call) { final LogicalProject project = call.rel(0); final LogicalCalc calc = call.rel(1); // Don't merge a project which contains windowed aggregates onto a // calc. That would effectively be pushing a windowed aggregate down // through a filter. Transform the project into an identical calc, // which we'll have chance to merge later, after the over is // expanded. final RelOptCluster cluster = project.getCluster(); RexProgram program = RexProgram.create( calc.getRowType(), project.getProjects(), null, project.getRowType(), cluster.getRexBuilder()); if (RexOver.containsOver(program)) { LogicalCalc projectAsCalc = LogicalCalc.create(calc, program); call.transformTo(projectAsCalc); return; } // Create a program containing the project node's expressions. final RexBuilder rexBuilder = cluster.getRexBuilder(); final RexProgramBuilder progBuilder = new RexProgramBuilder( calc.getRowType(), rexBuilder); for (Pair<RexNode, String> field : project.getNamedProjects()) { progBuilder.addProject(field.left, field.right); } RexProgram topProgram = progBuilder.getProgram(); RexProgram bottomProgram = calc.getProgram(); // Merge the programs together. RexProgram mergedProgram = RexProgramBuilder.mergePrograms( topProgram, bottomProgram, rexBuilder); final LogicalCalc newCalc = LogicalCalc.create(calc.getInput(), mergedProgram); call.transformTo(newCalc); }
Example 11
Source File: SemiJoinProjectTransposeRule.java From calcite with Apache License 2.0 | 4 votes |
/** * 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 LogicalProject on the LHS of the semijoin * @param semiJoin the semijoin * @return the modified semijoin condition */ private RexNode adjustCondition(LogicalProject project, LogicalJoin 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); // add the project expressions, then add input references for the RHS // 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()); }