org.apache.calcite.rex.RexPermuteInputsShuttle Java Examples
The following examples show how to use
org.apache.calcite.rex.RexPermuteInputsShuttle.
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: RelMdPredicates.java From Bats with Apache License 2.0 | 6 votes |
private void infer(RexNode predicates, Set<RexNode> allExprs, List<RexNode> inferredPredicates, boolean includeEqualityInference, ImmutableBitSet inferringFields) { for (RexNode r : RelOptUtil.conjunctions(predicates)) { if (!includeEqualityInference && equalityPredicates.contains(r)) { continue; } for (Mapping m : mappings(r)) { RexNode tr = r.accept( new RexPermuteInputsShuttle(m, joinRel.getInput(0), joinRel.getInput(1))); // Filter predicates can be already simplified, so we should work with // simplified RexNode versions as well. It also allows prevent of having // some duplicates in in result pulledUpPredicates RexNode simplifiedTarget = simplify.simplifyFilterPredicates(RelOptUtil.conjunctions(tr)); if (checkTarget(inferringFields, allExprs, tr) && checkTarget(inferringFields, allExprs, simplifiedTarget)) { inferredPredicates.add(simplifiedTarget); allExprs.add(simplifiedTarget); } } } }
Example #2
Source File: FilterNLJMergeRule.java From dremio-oss with Apache License 2.0 | 6 votes |
@Override public void onMatch(RelOptRuleCall call) { FilterPrel filter = call.rel(0); NestedLoopJoinPrel join = call.rel(1); if ((join.getProjectedFields() == null) || join.getProjectedFields().cardinality() == join.getInputRowType().getFieldCount()) { call.transformTo(NestedLoopJoinPrel.create(join.getCluster(), join.getTraitSet(), join.getLeft(), join.getRight(), join.getJoinType(), RelOptUtil.andJoinFilters(join.getCluster().getRexBuilder(), join.getCondition(), filter.getCondition()), join.getProjectedFields())); } else { // Current filter condition is written based on projected fields on join. In order to push this filter down we need to rewrite filter condition final ImmutableBitSet topProjectedColumns = RelOptUtil.InputFinder.bits(filter.getCondition()); final ImmutableBitSet bottomProjectedColumns = join.getProjectedFields(); Mapping mapping = Mappings.create(MappingType.SURJECTION, join.getRowType().getFieldCount(), join.getInputRowType().getFieldCount()); for (Ord<Integer> ord : Ord.zip(bottomProjectedColumns)) { if (topProjectedColumns.get(ord.i)) { mapping.set(ord.i, ord.e); } } RexShuttle shuttle = new RexPermuteInputsShuttle(mapping); RexNode updatedCondition = shuttle.apply(filter.getCondition()); call.transformTo(NestedLoopJoinPrel.create(join.getCluster(), join.getTraitSet(), join.getLeft(), join.getRight(), join.getJoinType(), RelOptUtil.andJoinFilters(join.getCluster().getRexBuilder(), join.getCondition(), updatedCondition), join.getProjectedFields())); } }
Example #3
Source File: RelMdPredicates.java From calcite with Apache License 2.0 | 6 votes |
private void infer(RexNode predicates, Set<RexNode> allExprs, List<RexNode> inferredPredicates, boolean includeEqualityInference, ImmutableBitSet inferringFields) { for (RexNode r : RelOptUtil.conjunctions(predicates)) { if (!includeEqualityInference && equalityPredicates.contains(r)) { continue; } for (Mapping m : mappings(r)) { RexNode tr = r.accept( new RexPermuteInputsShuttle(m, joinRel.getInput(0), joinRel.getInput(1))); // Filter predicates can be already simplified, so we should work with // simplified RexNode versions as well. It also allows prevent of having // some duplicates in in result pulledUpPredicates RexNode simplifiedTarget = simplify.simplifyFilterPredicates(RelOptUtil.conjunctions(tr)); if (checkTarget(inferringFields, allExprs, tr) && checkTarget(inferringFields, allExprs, simplifiedTarget)) { inferredPredicates.add(simplifiedTarget); allExprs.add(simplifiedTarget); } } } }
Example #4
Source File: RelMdPredicates.java From Bats with Apache License 2.0 | 5 votes |
/** * Infers predicates for an Aggregate. * * <p>Pulls up predicates that only contains references to columns in the * GroupSet. For e.g. * * <blockquote><pre> * inputPullUpExprs : { a > 7, b + c < 10, a + e = 9} * groupSet : { a, b} * pulledUpExprs : { a > 7} * </pre></blockquote> */ public RelOptPredicateList getPredicates(Aggregate agg, RelMetadataQuery mq) { final RelNode input = agg.getInput(); final RexBuilder rexBuilder = agg.getCluster().getRexBuilder(); final RelOptPredicateList inputInfo = mq.getPulledUpPredicates(input); final List<RexNode> aggPullUpPredicates = new ArrayList<>(); ImmutableBitSet groupKeys = agg.getGroupSet(); if (groupKeys.isEmpty()) { // "GROUP BY ()" can convert an empty relation to a non-empty relation, so // it is not valid to pull up predicates. In particular, consider the // predicate "false": it is valid on all input rows (trivially - there are // no rows!) but not on the output (there is one row). return RelOptPredicateList.EMPTY; } Mapping m = Mappings.create(MappingType.PARTIAL_FUNCTION, input.getRowType().getFieldCount(), agg.getRowType().getFieldCount()); int i = 0; for (int j : groupKeys) { m.set(j, i++); } for (RexNode r : inputInfo.pulledUpPredicates) { ImmutableBitSet rCols = RelOptUtil.InputFinder.bits(r); if (groupKeys.contains(rCols)) { r = r.accept(new RexPermuteInputsShuttle(m, input)); aggPullUpPredicates.add(r); } } return RelOptPredicateList.of(rexBuilder, aggPullUpPredicates); }
Example #5
Source File: RelFieldTrimmer.java From Bats with Apache License 2.0 | 5 votes |
/** * Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for * {@link org.apache.calcite.rel.logical.LogicalFilter}. */ public TrimResult trimFields(Filter filter, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) { final RelDataType rowType = filter.getRowType(); final int fieldCount = rowType.getFieldCount(); final RexNode conditionExpr = filter.getCondition(); final RelNode input = filter.getInput(); // We use the fields used by the consumer, plus any fields used in the // filter. final Set<RelDataTypeField> inputExtraFields = new LinkedHashSet<>(extraFields); RelOptUtil.InputFinder inputFinder = new RelOptUtil.InputFinder(inputExtraFields); inputFinder.inputBitSet.addAll(fieldsUsed); conditionExpr.accept(inputFinder); final ImmutableBitSet inputFieldsUsed = inputFinder.inputBitSet.build(); // Create input with trimmed columns. TrimResult trimResult = trimChild(filter, input, inputFieldsUsed, inputExtraFields); RelNode newInput = trimResult.left; final Mapping inputMapping = trimResult.right; // If the input is unchanged, and we need to project all columns, // there's nothing we can do. if (newInput == input && fieldsUsed.cardinality() == fieldCount) { return result(filter, Mappings.createIdentity(fieldCount)); } // Build new project expressions, and populate the mapping. final RexVisitor<RexNode> shuttle = new RexPermuteInputsShuttle(inputMapping, newInput); RexNode newConditionExpr = conditionExpr.accept(shuttle); // Use copy rather than relBuilder so that correlating variables get set. relBuilder.push(filter.copy(filter.getTraitSet(), newInput, newConditionExpr)); // The result has the same mapping as the input gave us. Sometimes we // return fields that the consumer didn't ask for, because the filter // needs them for its condition. return result(relBuilder.build(), inputMapping); }
Example #6
Source File: DremioFieldTrimmer.java From dremio-oss with Apache License 2.0 | 5 votes |
private static RexWinAggCall permuteWinAggCall(RexWinAggCall call, RexPermuteInputsShuttle shuttle, int newOrdinal) { // Cast should be safe as the shuttle creates new RexCall instance (but not RexWinAggCall) RexCall newCall = (RexCall) call.accept(shuttle); if (newCall == call && newOrdinal == call.ordinal) { return call; } return new RexWinAggCall( (SqlAggFunction) call.getOperator(), call.getType(), newCall.getOperands(), // remap partition ordinal newOrdinal, call.distinct); }
Example #7
Source File: RelMdPredicates.java From calcite with Apache License 2.0 | 5 votes |
/** * Infers predicates for an Aggregate. * * <p>Pulls up predicates that only contains references to columns in the * GroupSet. For e.g. * * <blockquote><pre> * inputPullUpExprs : { a > 7, b + c < 10, a + e = 9} * groupSet : { a, b} * pulledUpExprs : { a > 7} * </pre></blockquote> */ public RelOptPredicateList getPredicates(Aggregate agg, RelMetadataQuery mq) { final RelNode input = agg.getInput(); final RexBuilder rexBuilder = agg.getCluster().getRexBuilder(); final RelOptPredicateList inputInfo = mq.getPulledUpPredicates(input); final List<RexNode> aggPullUpPredicates = new ArrayList<>(); ImmutableBitSet groupKeys = agg.getGroupSet(); if (groupKeys.isEmpty()) { // "GROUP BY ()" can convert an empty relation to a non-empty relation, so // it is not valid to pull up predicates. In particular, consider the // predicate "false": it is valid on all input rows (trivially - there are // no rows!) but not on the output (there is one row). return RelOptPredicateList.EMPTY; } Mapping m = Mappings.create(MappingType.PARTIAL_FUNCTION, input.getRowType().getFieldCount(), agg.getRowType().getFieldCount()); int i = 0; for (int j : groupKeys) { m.set(j, i++); } for (RexNode r : inputInfo.pulledUpPredicates) { ImmutableBitSet rCols = RelOptUtil.InputFinder.bits(r); if (groupKeys.contains(rCols)) { r = r.accept(new RexPermuteInputsShuttle(m, input)); aggPullUpPredicates.add(r); } } return RelOptPredicateList.of(rexBuilder, aggPullUpPredicates); }
Example #8
Source File: RelMdPredicates.java From Bats with Apache License 2.0 | 4 votes |
/** * Infers predicates for a project. * * <ol> * <li>create a mapping from input to projection. Map only positions that * directly reference an input column. * <li>Expressions that only contain above columns are retained in the * Project's pullExpressions list. * <li>For e.g. expression 'a + e = 9' below will not be pulled up because 'e' * is not in the projection list. * * <blockquote><pre> * inputPullUpExprs: {a > 7, b + c < 10, a + e = 9} * projectionExprs: {a, b, c, e / 2} * projectionPullupExprs: {a > 7, b + c < 10} * </pre></blockquote> * * </ol> */ public RelOptPredicateList getPredicates(Project project, RelMetadataQuery mq) { final RelNode input = project.getInput(); final RexBuilder rexBuilder = project.getCluster().getRexBuilder(); final RelOptPredicateList inputInfo = mq.getPulledUpPredicates(input); final List<RexNode> projectPullUpPredicates = new ArrayList<>(); ImmutableBitSet.Builder columnsMappedBuilder = ImmutableBitSet.builder(); Mapping m = Mappings.create(MappingType.PARTIAL_FUNCTION, input.getRowType().getFieldCount(), project.getRowType().getFieldCount()); for (Ord<RexNode> expr : Ord.zip(project.getProjects())) { if (expr.e instanceof RexInputRef) { int sIdx = ((RexInputRef) expr.e).getIndex(); m.set(sIdx, expr.i); columnsMappedBuilder.set(sIdx); // Project can also generate constants. We need to include them. } else if (RexLiteral.isNullLiteral(expr.e)) { projectPullUpPredicates.add( rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, rexBuilder.makeInputRef(project, expr.i))); } else if (RexUtil.isConstant(expr.e)) { final List<RexNode> args = ImmutableList.of(rexBuilder.makeInputRef(project, expr.i), expr.e); final SqlOperator op = args.get(0).getType().isNullable() || args.get(1).getType().isNullable() ? SqlStdOperatorTable.IS_NOT_DISTINCT_FROM : SqlStdOperatorTable.EQUALS; projectPullUpPredicates.add(rexBuilder.makeCall(op, args)); } } // Go over childPullUpPredicates. If a predicate only contains columns in // 'columnsMapped' construct a new predicate based on mapping. final ImmutableBitSet columnsMapped = columnsMappedBuilder.build(); for (RexNode r : inputInfo.pulledUpPredicates) { RexNode r2 = projectPredicate(rexBuilder, input, r, columnsMapped); if (!r2.isAlwaysTrue()) { r2 = r2.accept(new RexPermuteInputsShuttle(m, input)); projectPullUpPredicates.add(r2); } } return RelOptPredicateList.of(rexBuilder, projectPullUpPredicates); }
Example #9
Source File: JoinPushThroughJoinRule.java From Bats with Apache License 2.0 | 4 votes |
/** * Similar to {@link #onMatch}, but swaps the upper sibling with the left * of the two lower siblings, rather than the right. */ private void onMatchLeft(RelOptRuleCall call) { final Join topJoin = call.rel(0); final Join bottomJoin = call.rel(1); final RelNode relC = call.rel(2); final RelNode relA = bottomJoin.getLeft(); final RelNode relB = bottomJoin.getRight(); final RelOptCluster cluster = topJoin.getCluster(); // topJoin // / \ // bottomJoin C // / \ // A B final int aCount = relA.getRowType().getFieldCount(); final int bCount = relB.getRowType().getFieldCount(); final int cCount = relC.getRowType().getFieldCount(); final ImmutableBitSet aBitSet = ImmutableBitSet.range(aCount); // becomes // // newTopJoin // / \ // newBottomJoin A // / \ // C B // If either join is not inner, we cannot proceed. // (Is this too strict?) if (topJoin.getJoinType() != JoinRelType.INNER || bottomJoin.getJoinType() != JoinRelType.INNER) { return; } // Split the condition of topJoin into a conjunction. Each of the // parts that does not use columns from A can be pushed down. final List<RexNode> intersecting = new ArrayList<>(); final List<RexNode> nonIntersecting = new ArrayList<>(); split(topJoin.getCondition(), aBitSet, intersecting, nonIntersecting); // If there's nothing to push down, it's not worth proceeding. if (nonIntersecting.isEmpty()) { return; } // Split the condition of bottomJoin into a conjunction. Each of the // parts that use columns from A will need to be pulled up. final List<RexNode> bottomIntersecting = new ArrayList<>(); final List<RexNode> bottomNonIntersecting = new ArrayList<>(); split( bottomJoin.getCondition(), aBitSet, bottomIntersecting, bottomNonIntersecting); // target: | C | B | // source: | A | B | C | final Mappings.TargetMapping bottomMapping = Mappings.createShiftMapping( aCount + bCount + cCount, cCount, aCount, bCount, 0, aCount + bCount, cCount); final List<RexNode> newBottomList = new ArrayList<>(); new RexPermuteInputsShuttle(bottomMapping, relC, relB) .visitList(nonIntersecting, newBottomList); new RexPermuteInputsShuttle(bottomMapping, relC, relB) .visitList(bottomNonIntersecting, newBottomList); final RexBuilder rexBuilder = cluster.getRexBuilder(); RexNode newBottomCondition = RexUtil.composeConjunction(rexBuilder, newBottomList); final Join newBottomJoin = bottomJoin.copy(bottomJoin.getTraitSet(), newBottomCondition, relC, relB, bottomJoin.getJoinType(), bottomJoin.isSemiJoinDone()); // target: | C | B | A | // source: | A | B | C | final Mappings.TargetMapping topMapping = Mappings.createShiftMapping( aCount + bCount + cCount, cCount + bCount, 0, aCount, cCount, aCount, bCount, 0, aCount + bCount, cCount); final List<RexNode> newTopList = new ArrayList<>(); new RexPermuteInputsShuttle(topMapping, newBottomJoin, relA) .visitList(intersecting, newTopList); new RexPermuteInputsShuttle(topMapping, newBottomJoin, relA) .visitList(bottomIntersecting, newTopList); RexNode newTopCondition = RexUtil.composeConjunction(rexBuilder, newTopList); @SuppressWarnings("SuspiciousNameCombination") final Join newTopJoin = topJoin.copy(topJoin.getTraitSet(), newTopCondition, newBottomJoin, relA, topJoin.getJoinType(), topJoin.isSemiJoinDone()); final RelBuilder relBuilder = call.builder(); relBuilder.push(newTopJoin); relBuilder.project(relBuilder.fields(topMapping)); call.transformTo(relBuilder.build()); }
Example #10
Source File: JoinAssociateRule.java From Bats with Apache License 2.0 | 4 votes |
public void onMatch(final RelOptRuleCall call) { final Join topJoin = call.rel(0); final Join bottomJoin = call.rel(1); final RelNode relA = bottomJoin.getLeft(); final RelNode relB = bottomJoin.getRight(); final RelSubset relC = call.rel(2); final RelOptCluster cluster = topJoin.getCluster(); final RexBuilder rexBuilder = cluster.getRexBuilder(); if (relC.getConvention() != relA.getConvention()) { // relC could have any trait-set. But if we're matching say // EnumerableConvention, we're only interested in enumerable subsets. return; } // topJoin // / \ // bottomJoin C // / \ // A B final int aCount = relA.getRowType().getFieldCount(); final int bCount = relB.getRowType().getFieldCount(); final int cCount = relC.getRowType().getFieldCount(); final ImmutableBitSet aBitSet = ImmutableBitSet.range(0, aCount); final ImmutableBitSet bBitSet = ImmutableBitSet.range(aCount, aCount + bCount); if (!topJoin.getSystemFieldList().isEmpty()) { // FIXME Enable this rule for joins with system fields return; } // If either join is not inner, we cannot proceed. // (Is this too strict?) if (topJoin.getJoinType() != JoinRelType.INNER || bottomJoin.getJoinType() != JoinRelType.INNER) { return; } // Goal is to transform to // // newTopJoin // / \ // A newBottomJoin // / \ // B C // Split the condition of topJoin and bottomJoin into a conjunctions. A // condition can be pushed down if it does not use columns from A. final List<RexNode> top = new ArrayList<>(); final List<RexNode> bottom = new ArrayList<>(); JoinPushThroughJoinRule.split(topJoin.getCondition(), aBitSet, top, bottom); JoinPushThroughJoinRule.split(bottomJoin.getCondition(), aBitSet, top, bottom); // Mapping for moving conditions from topJoin or bottomJoin to // newBottomJoin. // target: | B | C | // source: | A | B | C | final Mappings.TargetMapping bottomMapping = Mappings.createShiftMapping( aCount + bCount + cCount, 0, aCount, bCount, bCount, aCount + bCount, cCount); final List<RexNode> newBottomList = new ArrayList<>(); new RexPermuteInputsShuttle(bottomMapping, relB, relC) .visitList(bottom, newBottomList); RexNode newBottomCondition = RexUtil.composeConjunction(rexBuilder, newBottomList); final Join newBottomJoin = bottomJoin.copy(bottomJoin.getTraitSet(), newBottomCondition, relB, relC, JoinRelType.INNER, false); // Condition for newTopJoin consists of pieces from bottomJoin and topJoin. // Field ordinals do not need to be changed. RexNode newTopCondition = RexUtil.composeConjunction(rexBuilder, top); @SuppressWarnings("SuspiciousNameCombination") final Join newTopJoin = topJoin.copy(topJoin.getTraitSet(), newTopCondition, relA, newBottomJoin, JoinRelType.INNER, false); call.transformTo(newTopJoin); }
Example #11
Source File: ProjectNLJMergeRule.java From dremio-oss with Apache License 2.0 | 4 votes |
@Override public void onMatch(RelOptRuleCall call) { ProjectPrel project = call.rel(0); NestedLoopJoinPrel nlj = call.rel(1); ImmutableBitSet topProjectedColumns = InputFinder.bits(project.getProjects(), null); ImmutableBitSet bottomProjectedColumns = null; if (nlj.getProjectedFields() == null) { bottomProjectedColumns = ImmutableBitSet.range(nlj.getRowType().getFieldCount()); } else { bottomProjectedColumns = nlj.getProjectedFields(); } ImmutableBitSet.Builder builder = ImmutableBitSet.builder(); int field = 0; Mapping mapping = Mappings.create(MappingType.SURJECTION, bottomProjectedColumns.cardinality(), topProjectedColumns.cardinality()); for (Ord<Integer> ord : Ord.zip(bottomProjectedColumns)) { if (topProjectedColumns.get(ord.i)) { builder.set(ord.e); mapping.set(ord.i, field); field++; } } if (builder.cardinality() == 0) { //project at least one column builder.set(0); } ImmutableBitSet newJoinProjectedFields = builder.build(); if (newJoinProjectedFields.equals(nlj.getProjectedFields())) { return; } RexShuttle shuttle = new RexPermuteInputsShuttle(mapping); List<RexNode> newProjects = shuttle.apply(project.getProjects()); NestedLoopJoinPrel newJoin = (NestedLoopJoinPrel) nlj.copy(newJoinProjectedFields); ProjectPrel newProject = ProjectPrel.create(nlj.getCluster(), project.getTraitSet(), newJoin, newProjects, project.getRowType()); call.transformTo(newProject); }
Example #12
Source File: RelMdPredicates.java From calcite with Apache License 2.0 | 4 votes |
/** * Infers predicates for a project. * * <ol> * <li>create a mapping from input to projection. Map only positions that * directly reference an input column. * <li>Expressions that only contain above columns are retained in the * Project's pullExpressions list. * <li>For e.g. expression 'a + e = 9' below will not be pulled up because 'e' * is not in the projection list. * * <blockquote><pre> * inputPullUpExprs: {a > 7, b + c < 10, a + e = 9} * projectionExprs: {a, b, c, e / 2} * projectionPullupExprs: {a > 7, b + c < 10} * </pre></blockquote> * * </ol> */ public RelOptPredicateList getPredicates(Project project, RelMetadataQuery mq) { final RelNode input = project.getInput(); final RexBuilder rexBuilder = project.getCluster().getRexBuilder(); final RelOptPredicateList inputInfo = mq.getPulledUpPredicates(input); final List<RexNode> projectPullUpPredicates = new ArrayList<>(); ImmutableBitSet.Builder columnsMappedBuilder = ImmutableBitSet.builder(); Mapping m = Mappings.create(MappingType.PARTIAL_FUNCTION, input.getRowType().getFieldCount(), project.getRowType().getFieldCount()); for (Ord<RexNode> expr : Ord.zip(project.getProjects())) { if (expr.e instanceof RexInputRef) { int sIdx = ((RexInputRef) expr.e).getIndex(); m.set(sIdx, expr.i); columnsMappedBuilder.set(sIdx); // Project can also generate constants. We need to include them. } else if (RexLiteral.isNullLiteral(expr.e)) { projectPullUpPredicates.add( rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, rexBuilder.makeInputRef(project, expr.i))); } else if (RexUtil.isConstant(expr.e)) { final List<RexNode> args = ImmutableList.of(rexBuilder.makeInputRef(project, expr.i), expr.e); final SqlOperator op = args.get(0).getType().isNullable() || args.get(1).getType().isNullable() ? SqlStdOperatorTable.IS_NOT_DISTINCT_FROM : SqlStdOperatorTable.EQUALS; projectPullUpPredicates.add(rexBuilder.makeCall(op, args)); } } // Go over childPullUpPredicates. If a predicate only contains columns in // 'columnsMapped' construct a new predicate based on mapping. final ImmutableBitSet columnsMapped = columnsMappedBuilder.build(); for (RexNode r : inputInfo.pulledUpPredicates) { RexNode r2 = projectPredicate(rexBuilder, input, r, columnsMapped); if (!r2.isAlwaysTrue()) { r2 = r2.accept(new RexPermuteInputsShuttle(m, input)); projectPullUpPredicates.add(r2); } } return RelOptPredicateList.of(rexBuilder, projectPullUpPredicates); }
Example #13
Source File: JoinPushThroughJoinRule.java From calcite with Apache License 2.0 | 4 votes |
/** * Similar to {@link #onMatch}, but swaps the upper sibling with the left * of the two lower siblings, rather than the right. */ private void onMatchLeft(RelOptRuleCall call) { final Join topJoin = call.rel(0); final Join bottomJoin = call.rel(1); final RelNode relC = call.rel(2); final RelNode relA = bottomJoin.getLeft(); final RelNode relB = bottomJoin.getRight(); final RelOptCluster cluster = topJoin.getCluster(); // topJoin // / \ // bottomJoin C // / \ // A B final int aCount = relA.getRowType().getFieldCount(); final int bCount = relB.getRowType().getFieldCount(); final int cCount = relC.getRowType().getFieldCount(); final ImmutableBitSet aBitSet = ImmutableBitSet.range(aCount); // becomes // // newTopJoin // / \ // newBottomJoin A // / \ // C B // If either join is not inner, we cannot proceed. // (Is this too strict?) if (topJoin.getJoinType() != JoinRelType.INNER || bottomJoin.getJoinType() != JoinRelType.INNER) { return; } // Split the condition of topJoin into a conjunction. Each of the // parts that does not use columns from A can be pushed down. final List<RexNode> intersecting = new ArrayList<>(); final List<RexNode> nonIntersecting = new ArrayList<>(); split(topJoin.getCondition(), aBitSet, intersecting, nonIntersecting); // If there's nothing to push down, it's not worth proceeding. if (nonIntersecting.isEmpty()) { return; } // Split the condition of bottomJoin into a conjunction. Each of the // parts that use columns from A will need to be pulled up. final List<RexNode> bottomIntersecting = new ArrayList<>(); final List<RexNode> bottomNonIntersecting = new ArrayList<>(); split( bottomJoin.getCondition(), aBitSet, bottomIntersecting, bottomNonIntersecting); // target: | C | B | // source: | A | B | C | final Mappings.TargetMapping bottomMapping = Mappings.createShiftMapping( aCount + bCount + cCount, cCount, aCount, bCount, 0, aCount + bCount, cCount); final List<RexNode> newBottomList = new ArrayList<>(); new RexPermuteInputsShuttle(bottomMapping, relC, relB) .visitList(nonIntersecting, newBottomList); new RexPermuteInputsShuttle(bottomMapping, relC, relB) .visitList(bottomNonIntersecting, newBottomList); final RexBuilder rexBuilder = cluster.getRexBuilder(); RexNode newBottomCondition = RexUtil.composeConjunction(rexBuilder, newBottomList); final Join newBottomJoin = bottomJoin.copy(bottomJoin.getTraitSet(), newBottomCondition, relC, relB, bottomJoin.getJoinType(), bottomJoin.isSemiJoinDone()); // target: | C | B | A | // source: | A | B | C | final Mappings.TargetMapping topMapping = Mappings.createShiftMapping( aCount + bCount + cCount, cCount + bCount, 0, aCount, cCount, aCount, bCount, 0, aCount + bCount, cCount); final List<RexNode> newTopList = new ArrayList<>(); new RexPermuteInputsShuttle(topMapping, newBottomJoin, relA) .visitList(intersecting, newTopList); new RexPermuteInputsShuttle(topMapping, newBottomJoin, relA) .visitList(bottomIntersecting, newTopList); RexNode newTopCondition = RexUtil.composeConjunction(rexBuilder, newTopList); @SuppressWarnings("SuspiciousNameCombination") final Join newTopJoin = topJoin.copy(topJoin.getTraitSet(), newTopCondition, newBottomJoin, relA, topJoin.getJoinType(), topJoin.isSemiJoinDone()); final RelBuilder relBuilder = call.builder(); relBuilder.push(newTopJoin); relBuilder.project(relBuilder.fields(topMapping)); call.transformTo(relBuilder.build()); }
Example #14
Source File: JoinAssociateRule.java From calcite with Apache License 2.0 | 4 votes |
public void onMatch(final RelOptRuleCall call) { final Join topJoin = call.rel(0); final Join bottomJoin = call.rel(1); final RelNode relA = bottomJoin.getLeft(); final RelNode relB = bottomJoin.getRight(); final RelSubset relC = call.rel(2); final RelOptCluster cluster = topJoin.getCluster(); final RexBuilder rexBuilder = cluster.getRexBuilder(); if (relC.getConvention() != relA.getConvention()) { // relC could have any trait-set. But if we're matching say // EnumerableConvention, we're only interested in enumerable subsets. return; } // topJoin // / \ // bottomJoin C // / \ // A B final int aCount = relA.getRowType().getFieldCount(); final int bCount = relB.getRowType().getFieldCount(); final int cCount = relC.getRowType().getFieldCount(); final ImmutableBitSet aBitSet = ImmutableBitSet.range(0, aCount); final ImmutableBitSet bBitSet = ImmutableBitSet.range(aCount, aCount + bCount); if (!topJoin.getSystemFieldList().isEmpty()) { // FIXME Enable this rule for joins with system fields return; } // If either join is not inner, we cannot proceed. // (Is this too strict?) if (topJoin.getJoinType() != JoinRelType.INNER || bottomJoin.getJoinType() != JoinRelType.INNER) { return; } // Goal is to transform to // // newTopJoin // / \ // A newBottomJoin // / \ // B C // Split the condition of topJoin and bottomJoin into a conjunctions. A // condition can be pushed down if it does not use columns from A. final List<RexNode> top = new ArrayList<>(); final List<RexNode> bottom = new ArrayList<>(); JoinPushThroughJoinRule.split(topJoin.getCondition(), aBitSet, top, bottom); JoinPushThroughJoinRule.split(bottomJoin.getCondition(), aBitSet, top, bottom); // Mapping for moving conditions from topJoin or bottomJoin to // newBottomJoin. // target: | B | C | // source: | A | B | C | final Mappings.TargetMapping bottomMapping = Mappings.createShiftMapping( aCount + bCount + cCount, 0, aCount, bCount, bCount, aCount + bCount, cCount); final List<RexNode> newBottomList = new RexPermuteInputsShuttle(bottomMapping, relB, relC) .visitList(bottom); RexNode newBottomCondition = RexUtil.composeConjunction(rexBuilder, newBottomList); final Join newBottomJoin = bottomJoin.copy(bottomJoin.getTraitSet(), newBottomCondition, relB, relC, JoinRelType.INNER, false); // Condition for newTopJoin consists of pieces from bottomJoin and topJoin. // Field ordinals do not need to be changed. RexNode newTopCondition = RexUtil.composeConjunction(rexBuilder, top); @SuppressWarnings("SuspiciousNameCombination") final Join newTopJoin = topJoin.copy(topJoin.getTraitSet(), newTopCondition, relA, newBottomJoin, JoinRelType.INNER, false); call.transformTo(newTopJoin); }
Example #15
Source File: RelFieldTrimmer.java From calcite with Apache License 2.0 | 4 votes |
/** * Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for * {@link org.apache.calcite.rel.logical.LogicalFilter}. */ public TrimResult trimFields( Filter filter, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) { final RelDataType rowType = filter.getRowType(); final int fieldCount = rowType.getFieldCount(); final RexNode conditionExpr = filter.getCondition(); final RelNode input = filter.getInput(); // We use the fields used by the consumer, plus any fields used in the // filter. final Set<RelDataTypeField> inputExtraFields = new LinkedHashSet<>(extraFields); RelOptUtil.InputFinder inputFinder = new RelOptUtil.InputFinder(inputExtraFields, fieldsUsed); conditionExpr.accept(inputFinder); final ImmutableBitSet inputFieldsUsed = inputFinder.build(); // Create input with trimmed columns. TrimResult trimResult = trimChild(filter, input, inputFieldsUsed, inputExtraFields); RelNode newInput = trimResult.left; final Mapping inputMapping = trimResult.right; // If the input is unchanged, and we need to project all columns, // there's nothing we can do. if (newInput == input && fieldsUsed.cardinality() == fieldCount) { return result(filter, Mappings.createIdentity(fieldCount)); } // Build new project expressions, and populate the mapping. final RexVisitor<RexNode> shuttle = new RexPermuteInputsShuttle(inputMapping, newInput); RexNode newConditionExpr = conditionExpr.accept(shuttle); // Build new filter with trimmed input and condition. relBuilder.push(newInput) .filter(filter.getVariablesSet(), newConditionExpr); // The result has the same mapping as the input gave us. Sometimes we // return fields that the consumer didn't ask for, because the filter // needs them for its condition. return result(relBuilder.build(), inputMapping); }
Example #16
Source File: SimplifyNLJConditionRule.java From dremio-oss with Apache License 2.0 | 2 votes |
@Override public void onMatch(RelOptRuleCall call) { NestedLoopJoinPrel nlj = call.rel(0); RexBuilder rexBuilder = nlj.getCluster().getRexBuilder(); RexNode condition = nlj.getCondition(); condition = RexRewriter.rewrite(condition, ImmutableList.of(new SimplifyNLJConditionRule.TrigFractionRewriteRule(rexBuilder), new SimplifyNLJConditionRule.SinCosPlusMinusRewriteRule(rexBuilder))); int leftCount = nlj.getLeft().getRowType().getFieldCount(); List<RexNode> identityProjects = identityProjects(nlj.getRowType(), nlj.getProjectedFields()); SimplifyNLJConditionRule.ExpressionPusher pusher = new SimplifyNLJConditionRule.ExpressionPusher(rexBuilder, identityProjects, leftCount); Pointer<SimplifyNLJConditionRule.Side> side = new Pointer<>(); identityProjects.forEach(p -> p.accept(pusher, side)); side.value = SimplifyNLJConditionRule.Side.EMPTY; RexNode tempCondition = condition.accept(pusher, side); if (pusher.exprs.stream().allMatch(e -> e instanceof RexInputRef)) { return; } List<RexNode> leftExprs = new ArrayList<>(); pusher.leftExprs.forEach(i -> leftExprs.add(pusher.exprs.get(i))); RelDataType leftType = RexUtil.createStructType(nlj.getCluster().getTypeFactory(), leftExprs, null, SqlValidatorUtil.F_SUGGESTER); ProjectPrel newLeft = ProjectPrel.create(nlj.getCluster(), nlj.getLeft().getTraitSet(), nlj.getLeft(), leftExprs, leftType); List<RexNode> rightExprs = new ArrayList<>(); pusher.rightExprs.forEach(i -> rightExprs.add(RexUtil.shift(pusher.exprs.get(i), -leftCount))); RelDataType rightType = RexUtil.createStructType(nlj.getCluster().getTypeFactory(), rightExprs, null, SqlValidatorUtil.F_SUGGESTER); ProjectPrel newRight = ProjectPrel.create(nlj.getCluster(), nlj.getRight().getTraitSet(), nlj.getRight(), rightExprs, rightType); Mapping mapping = Mappings.bijection(ImmutableList.<Integer>builder().addAll(pusher.leftExprs).addAll(pusher.rightExprs).build()); RexShuttle shuttle = new RexPermuteInputsShuttle(mapping.inverse()); RexNode newCondition = tempCondition.accept(shuttle); ImmutableBitSet.Builder projectedBuilder = ImmutableBitSet.builder(); for (RexNode r : pusher.identityProjects()) { RexInputRef newRef = (RexInputRef) r.accept(shuttle); projectedBuilder.set(newRef.getIndex()); } if (newCondition.toString().equals(nlj.getCondition().toString())) { return; } NestedLoopJoinPrel newJoin = NestedLoopJoinPrel.create(nlj.getCluster(), nlj.getTraitSet(), newLeft, newRight, nlj.getJoinType(), newCondition, projectedBuilder.build()); call.transformTo(newJoin); }