Skip to content

Commit 24bb734

Browse files
committed
adjust binary partition rule to send predicates to a more ideal location
1 parent 9bc9b75 commit 24bb734

22 files changed

+991
-674
lines changed

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/cascades/rules/PartitionBinarySelectRule.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,24 @@ public void onMatch(@Nonnull final ExplorationCascadesRuleCall call) {
9595

9696
for (final var predicate : selectExpression.getPredicates()) {
9797
final var correlatedTo = predicate.getCorrelatedTo();
98-
final var correlatedToRight = correlatedTo.contains(rightQuantifier.getAlias());
99-
100-
if (!rightDependsOnLeft || !correlatedToRight) {
101-
leftPredicatesBuilder.add(predicate);
102-
} else {
98+
final var correlatedToLeft = correlatedTo.contains(leftAlias);
99+
final var correlatedToRight = correlatedTo.contains(rightAlias);
100+
101+
if (correlatedToLeft && correlatedToRight) {
102+
// Predicate correlated to both. If the left depends on the right, we need to send it
103+
// to the left to avoid a circular dependency. Otherwise, we push it to the right
104+
if (leftDependsOnRight) {
105+
leftPredicatesBuilder.add(predicate);
106+
} else {
107+
rightPredicatesBuilder.add(predicate);
108+
}
109+
} else if (correlatedToRight) {
110+
// Predicate is correlated to just the right. Send it to the right
103111
rightPredicatesBuilder.add(predicate);
112+
} else {
113+
// Predicate is either correlated to just the left or it is correlated to neither.
114+
// Send it to the left, as this is the left most position it can be
115+
leftPredicatesBuilder.add(predicate);
104116
}
105117
}
106118

fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/query/plan/cascades/rules/PartitionBinarySelectRuleTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ void pushSimplePredicatesWithCorrelatedQuantifiers() {
213213

214214
// Push the predicate on to the t quantifier and the predicate on g onto the g quantifier
215215
final Quantifier newT = forEach(selectWithPredicates(t, fieldPredicate(t, "b", new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, "hello"))));
216-
final Quantifier newG = forEach(selectWithPredicates(g, fieldPredicate(g, "two", new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, "world"))));
216+
final Quantifier newGLower = explodeField(newT, "g");
217+
final Quantifier newG = forEach(selectWithPredicates(newGLower, fieldPredicate(newGLower, "two", new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, "world"))));
217218
final SelectExpression newJoin = join(newT, newG)
218219
.addResultColumn(projectColumn(newT, "a"))
219220
.addResultColumn(projectColumn(newG, "one"))
@@ -267,8 +268,10 @@ void partitionPredicatesWithExists() {
267268
// Push the predicate on t down to t. Push the existential predicate to be on top of g
268269
final Quantifier newT = forEach(selectWithPredicates(t,
269270
fieldPredicate(t, "b", new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, "hello"))));
270-
final Quantifier newG = forEach(new SelectExpression(RCV_OF_ONE, ImmutableList.of(g), ImmutableList.of(new ExistsPredicate(g.getAlias()))));
271+
final Quantifier newGLower = Quantifier.existential(explodeField(newT, "g").getRangesOver());
272+
final Quantifier newG = forEach(new SelectExpression(RCV_OF_ONE, ImmutableList.of(newGLower), ImmutableList.of(new ExistsPredicate(newGLower.getAlias()))));
271273
final SelectExpression newJoin = join(newT, newG)
274+
.addResultColumn(projectColumn(newT, "a"))
272275
.build().buildSelect();
273276

274277
testHelper.assertYields(join, newJoin);
@@ -312,7 +315,8 @@ void partitionSelectWhenResultValueComesFromExplodedField() {
312315
.build().buildSelect();
313316

314317
final Quantifier newT = forEach(selectWithPredicates(t, fieldPredicate(t, "b", new Comparisons.SimpleComparison(Comparisons.Type.LESS_THAN, "hello"))));
315-
final Quantifier newG = forEach(selectWithPredicates(g, fieldPredicate(g, "two", new Comparisons.SimpleComparison(Comparisons.Type.LESS_THAN, "world"))));
318+
final Quantifier newGLower = explodeField(newT, "g");
319+
final Quantifier newG = forEach(selectWithPredicates(newGLower, fieldPredicate(newGLower, "two", new Comparisons.SimpleComparison(Comparisons.Type.LESS_THAN, "world"))));
316320
final SelectExpression newJoin = join(newT, newG)
317321
.addResultColumn(projectColumn(newG, "one"))
318322
.build().buildSelect();

yaml-tests/src/test/resources/aggregate-index-tests.metrics.binpb

Lines changed: 39 additions & 39 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)