Skip to content

Commit 72c8f9e

Browse files
committed
intersection complete
1 parent 3dcf180 commit 72c8f9e

File tree

15 files changed

+75
-45
lines changed

15 files changed

+75
-45
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public class AggregateMappings {
3535
@Nonnull
3636
private final BiMap<CorrelationIdentifier, Value> unmatchedAggregateMap;
3737

38-
private AggregateMappings(@Nonnull final Map<Value, Value> matchedAggregateMap, @Nonnull final BiMap<CorrelationIdentifier, Value> unmatchedAggregateMap) {
38+
private AggregateMappings(@Nonnull final Map<Value, Value> matchedAggregateMap,
39+
@Nonnull final BiMap<CorrelationIdentifier, Value> unmatchedAggregateMap) {
3940
this.matchedAggregateMap = matchedAggregateMap;
4041
this.unmatchedAggregateMap = unmatchedAggregateMap;
4142
}

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ public RelationalExpression apply(@Nonnull final Memoizer memoizer,
328328
@Override
329329
public RelationalExpression applyFinal(@Nonnull final Memoizer memoizer,
330330
@Nonnull final RelationalExpression relationalExpression) {
331-
return Compensation.this.apply(memoizer, otherCompensation.apply(memoizer, relationalExpression));
331+
return Compensation.this.applyFinal(memoizer, otherCompensation.applyFinal(memoizer, relationalExpression));
332332
}
333333
};
334334
}
@@ -357,8 +357,8 @@ public RelationalExpression apply(@Nonnull final Memoizer memoizer,
357357
@Override
358358
public RelationalExpression applyFinal(@Nonnull final Memoizer memoizer,
359359
@Nonnull final RelationalExpression relationalExpression) {
360-
return Compensation.this.apply(memoizer,
361-
otherCompensation.apply(memoizer, relationalExpression));
360+
return Compensation.this.applyFinal(memoizer,
361+
otherCompensation.applyFinal(memoizer, relationalExpression));
362362
}
363363
};
364364
}
@@ -592,7 +592,8 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
592592
(l, r) -> l));
593593
final var newUnmatchedAggregateMapBuilder =
594594
ImmutableBiMap.<CorrelationIdentifier, Value>builder();
595-
for (final var entry : getAggregateMappings().getUnmatchedAggregateMap().entrySet()) {
595+
final var unmatchedAggregateMap = getAggregateMappings().getUnmatchedAggregateMap();
596+
for (final var entry : unmatchedAggregateMap.entrySet()) {
596597
if (!newMatchedAggregateMap.containsKey(entry.getValue())) {
597598
newUnmatchedAggregateMapBuilder.put(entry);
598599
}
@@ -614,10 +615,12 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
614615
Verify.verify(resultCompensationFunction.isNeeded());
615616
Verify.verify(otherResultCompensationFunction.isNeeded());
616617
// pick the one from this side -- it does not matter as both candidates have the same shape
617-
newResultResultCompensationFunction = resultCompensationFunction.amend(newAggregateMappings);
618+
newResultResultCompensationFunction =
619+
resultCompensationFunction.amend(unmatchedAggregateMap, newMatchedAggregateMap);
618620
}
619621

620-
final var otherCompensationMap = otherWithSelectCompensation.getPredicateCompensationMap();
622+
final var otherCompensationMap =
623+
otherWithSelectCompensation.getPredicateCompensationMap();
621624
final var combinedPredicateMap = new LinkedIdentityMap<QueryPredicate, PredicateCompensationFunction>();
622625
for (final var entry : getPredicateCompensationMap().entrySet()) {
623626
// if the other side does not have compensation for this key, we don't need compensation
@@ -629,7 +632,7 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
629632
// figure out which one wins.
630633
// We just pick one side here.
631634
combinedPredicateMap.put(entry.getKey(),
632-
entry.getValue().amend(newAggregateMappings));
635+
entry.getValue().amend(unmatchedAggregateMap, newMatchedAggregateMap));
633636
}
634637
}
635638

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ public Map<QueryPredicate, PredicateMapping> getPulledUpPredicateMappings(@Nonnu
368368
}
369369

370370
@Nonnull
371-
public Compensation compensateCompleteMatch() {
372-
return queryExpression.compensate(this, getBoundParameterPrefixMap(), null, Quantifier.uniqueID());
371+
public Compensation compensateCompleteMatch(@Nonnull final CorrelationIdentifier candidateTopAlias) {
372+
return queryExpression.compensate(this, getBoundParameterPrefixMap(), null, candidateTopAlias);
373373
}
374374

375375
@Nonnull
@@ -381,7 +381,7 @@ public Compensation compensate(@Nonnull final Map<CorrelationIdentifier, Compari
381381

382382
@Nonnull
383383
public Compensation compensateExistential(@Nonnull final Map<CorrelationIdentifier, ComparisonRange> boundParameterPrefixMap) {
384-
return queryExpression.compensate(this, boundParameterPrefixMap, null, Quantifier.uniqueID());
384+
return queryExpression.compensate(this, boundParameterPrefixMap, null, Quantifier.uniqueId());
385385
}
386386

387387
@Nonnull

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

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.apple.foundationdb.record.query.plan.cascades.values.Value;
3131
import com.apple.foundationdb.record.query.plan.cascades.values.translation.PullUp;
3232
import com.google.common.base.Verify;
33+
import com.google.common.collect.BiMap;
3334
import com.google.common.collect.ImmutableList;
3435
import com.apple.foundationdb.record.query.plan.cascades.values.translation.TranslationMap;
3536
import com.google.common.collect.Multimaps;
@@ -64,15 +65,17 @@ public class PredicateMultiMap {
6465
private final SetMultimap<QueryPredicate, PredicateMapping> map;
6566

6667
@Nonnull
67-
private static Value amendValue(final @Nonnull AggregateMappings aggregateMappings, final Value rootValue) {
68+
private static Value amendValue(@Nonnull final BiMap<CorrelationIdentifier, Value> unmatchedAggregateMap,
69+
@Nonnull final Map<Value, Value> amendedMatchedAggregateMap,
70+
@Nonnull final Value rootValue) {
6871
return Objects.requireNonNull(rootValue.replace(currentValue -> {
6972
if (currentValue instanceof GroupByExpression.UnmatchedAggregateValue) {
7073
final var unmatchedId =
7174
((GroupByExpression.UnmatchedAggregateValue)currentValue).getUnmatchedId();
7275
final var queryValue =
73-
Objects.requireNonNull(aggregateMappings.getUnmatchedAggregateMap().get(unmatchedId));
76+
Objects.requireNonNull(unmatchedAggregateMap.get(unmatchedId));
7477
final var translatedQueryValue =
75-
aggregateMappings.getMatchedAggregateMap().get(queryValue);
78+
amendedMatchedAggregateMap.get(queryValue);
7679
if (translatedQueryValue != null) {
7780
return translatedQueryValue;
7881
}
@@ -110,7 +113,8 @@ public boolean isImpossible() {
110113

111114
@Nonnull
112115
@Override
113-
public PredicateCompensationFunction amend(@Nonnull final AggregateMappings aggregateMappings) {
116+
public PredicateCompensationFunction amend(@Nonnull final BiMap<CorrelationIdentifier, Value> unmatchedAggregateMap,
117+
@Nonnull final Map<Value, Value> amendedMatchedAggregateMap) {
114118
return this;
115119
}
116120

@@ -135,7 +139,8 @@ public boolean isImpossible() {
135139

136140
@Nonnull
137141
@Override
138-
public PredicateCompensationFunction amend(@Nonnull final AggregateMappings aggregateMappings) {
142+
public PredicateCompensationFunction amend(@Nonnull final BiMap<CorrelationIdentifier, Value> unmatchedAggregateMap,
143+
@Nonnull final Map<Value, Value> amendedMatchedAggregateMap) {
139144
return this;
140145
}
141146

@@ -152,7 +157,8 @@ public Set<QueryPredicate> applyCompensationForPredicate(@Nonnull final Correlat
152157
boolean isImpossible();
153158

154159
@Nonnull
155-
PredicateCompensationFunction amend(@Nonnull final AggregateMappings aggregateMappings);
160+
PredicateCompensationFunction amend(@Nonnull BiMap<CorrelationIdentifier, Value> unmatchedAggregateMap,
161+
@Nonnull Map<Value, Value> amendedMatchedAggregateMap);
156162

157163
@Nonnull
158164
Set<QueryPredicate> applyCompensationForPredicate(@Nonnull CorrelationIdentifier baseAlias);
@@ -175,10 +181,12 @@ public boolean isImpossible() {
175181

176182
@Nonnull
177183
@Override
178-
public PredicateCompensationFunction amend(@Nonnull final AggregateMappings aggregateMappings) {
184+
public PredicateCompensationFunction amend(@Nonnull final BiMap<CorrelationIdentifier, Value> unmatchedAggregateMap,
185+
@Nonnull final Map<Value, Value> amendedMatchedAggregateMap) {
179186
final var amendedTranslatedPredicateOptional =
180187
predicate.replaceValuesMaybe(rootValue ->
181-
Optional.of(amendValue(aggregateMappings, rootValue)));
188+
Optional.of(amendValue(unmatchedAggregateMap, amendedMatchedAggregateMap,
189+
rootValue)));
182190
Verify.verify(amendedTranslatedPredicateOptional.isPresent());
183191
return ofPredicate(amendedTranslatedPredicateOptional.get(), compensationFunction);
184192
}
@@ -232,7 +240,8 @@ public boolean isImpossible() {
232240

233241
@Nonnull
234242
@Override
235-
public PredicateCompensationFunction amend(@Nonnull final AggregateMappings aggregateMappings) {
243+
public PredicateCompensationFunction amend(@Nonnull final BiMap<CorrelationIdentifier, Value> unmatchedAggregateMap,
244+
@Nonnull final Map<Value, Value> amendedMatchedAggregateMap) {
236245
return this;
237246
}
238247

@@ -260,10 +269,13 @@ public boolean isImpossible() {
260269

261270
@Nonnull
262271
@Override
263-
public PredicateCompensationFunction amend(@Nonnull final AggregateMappings aggregateMappings) {
272+
public PredicateCompensationFunction amend(@Nonnull final BiMap<CorrelationIdentifier, Value> unmatchedAggregateMap,
273+
@Nonnull final Map<Value, Value> amendedMatchedAggregateMap) {
264274
final var amendedChildrenCompensationFunctions =
265275
childrenCompensationFunctions.stream()
266-
.map(childrenCompensationFunction -> childrenCompensationFunction.amend(aggregateMappings))
276+
.map(childrenCompensationFunction ->
277+
childrenCompensationFunction.amend(unmatchedAggregateMap,
278+
amendedMatchedAggregateMap))
267279
.collect(ImmutableList.toImmutableList());
268280
return ofChildrenCompensationFunctions(amendedChildrenCompensationFunctions, compensationFunction);
269281
}
@@ -305,7 +317,8 @@ public boolean isImpossible() {
305317

306318
@Nonnull
307319
@Override
308-
public ResultCompensationFunction amend(@Nonnull final AggregateMappings aggregateMappings) {
320+
public ResultCompensationFunction amend(@Nonnull final BiMap<CorrelationIdentifier, Value> unmatchedAggregateMap,
321+
@Nonnull final Map<Value, Value> amendedMatchedAggregateMap) {
309322
return this;
310323
}
311324

@@ -330,7 +343,8 @@ public boolean isImpossible() {
330343

331344
@Nonnull
332345
@Override
333-
public ResultCompensationFunction amend(@Nonnull final AggregateMappings aggregateMappings) {
346+
public ResultCompensationFunction amend(@Nonnull final BiMap<CorrelationIdentifier, Value> unmatchedAggregateMap,
347+
@Nonnull final Map<Value, Value> amendedMatchedAggregateMap) {
334348
return this;
335349
}
336350

@@ -347,7 +361,8 @@ public Value applyCompensationForResult(@Nonnull final CorrelationIdentifier bas
347361
boolean isImpossible();
348362

349363
@Nonnull
350-
ResultCompensationFunction amend(@Nonnull AggregateMappings aggregateMappings);
364+
ResultCompensationFunction amend(@Nonnull BiMap<CorrelationIdentifier, Value> unmatchedAggregateMap,
365+
@Nonnull Map<Value, Value> amendedMatchedAggregateMap);
351366

352367
@Nonnull
353368
Value applyCompensationForResult(@Nonnull CorrelationIdentifier baseAlias);
@@ -377,9 +392,10 @@ public boolean isImpossible() {
377392

378393
@Nonnull
379394
@Override
380-
public ResultCompensationFunction amend(@Nonnull final AggregateMappings aggregateMappings) {
395+
public ResultCompensationFunction amend(@Nonnull final BiMap<CorrelationIdentifier, Value> unmatchedAggregateMap,
396+
@Nonnull final Map<Value, Value> amendedMatchedAggregateMap) {
381397
final var amendedTranslatedQueryValue =
382-
amendValue(aggregateMappings, value);
398+
amendValue(unmatchedAggregateMap, amendedMatchedAggregateMap, value);
383399

384400
return ofValue(amendedTranslatedQueryValue, compensationFunction);
385401
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public ForEachBuilder from(final ForEach quantifier) {
171171
@Nonnull
172172
@Override
173173
public ForEach build(@Nonnull final Reference rangesOver) {
174-
return new ForEach(alias == null ? Quantifier.uniqueID() : alias, rangesOver, isNullOnEmpty);
174+
return new ForEach(alias == null ? Quantifier.uniqueId() : alias, rangesOver, isNullOnEmpty);
175175
}
176176
}
177177

@@ -360,7 +360,7 @@ public static class ExistentialBuilder extends Builder<Existential, ExistentialB
360360
@Override
361361
@Nonnull
362362
public Existential build(@Nonnull final Reference rangesOver) {
363-
return new Existential(alias == null ? Quantifier.uniqueID() : alias,
363+
return new Existential(alias == null ? Quantifier.uniqueId() : alias,
364364
rangesOver);
365365
}
366366
}
@@ -471,7 +471,7 @@ public static class PhysicalBuilder extends Builder<Physical, PhysicalBuilder> {
471471
@Nonnull
472472
@Override
473473
public Physical build(@Nonnull final Reference rangesOver) {
474-
return new Physical(alias == null ? Quantifier.uniqueID() : alias, rangesOver);
474+
return new Physical(alias == null ? Quantifier.uniqueId() : alias, rangesOver);
475475
}
476476

477477
/**
@@ -821,7 +821,7 @@ public Quantifier rebase(@Nonnull final AliasMap translationMap) {
821821
}
822822

823823
@Nonnull
824-
public static CorrelationIdentifier uniqueID() {
824+
public static CorrelationIdentifier uniqueId() {
825825
return CorrelationIdentifier.uniqueId(Quantifier.class);
826826
}
827827

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,11 +551,12 @@ private static List<SingleMatchedAccess> prepareMatchesAndCompensations(final @N
551551
Verify.verify(scanDirection == ScanDirection.FORWARD || scanDirection == ScanDirection.REVERSE ||
552552
scanDirection == ScanDirection.BOTH);
553553

554-
final var compensation = partialMatch.compensateCompleteMatch();
554+
final var candidateTopAlias = Quantifier.uniqueId();
555+
final var compensation = partialMatch.compensateCompleteMatch(candidateTopAlias);
555556

556557
if (scanDirection == ScanDirection.FORWARD || scanDirection == ScanDirection.BOTH) {
557558
partialMatchesWithCompensation.add(new SingleMatchedAccess(partialMatch, compensation,
558-
false, satisfyingOrderingsPair.getRight()));
559+
candidateTopAlias, false, satisfyingOrderingsPair.getRight()));
559560
}
560561

561562
//
@@ -568,7 +569,7 @@ private static List<SingleMatchedAccess> prepareMatchesAndCompensations(final @N
568569
//
569570
if (scanDirection == ScanDirection.REVERSE /* || scanDirection == ScanDirection.BOTH */) {
570571
partialMatchesWithCompensation.add(new SingleMatchedAccess(partialMatch, compensation,
571-
true, satisfyingOrderingsPair.getRight()));
572+
candidateTopAlias, true, satisfyingOrderingsPair.getRight()));
572573
}
573574
}
574575

@@ -1144,16 +1145,20 @@ private static class SingleMatchedAccess {
11441145
private final PartialMatch partialMatch;
11451146
@Nonnull
11461147
private final Compensation compensation;
1148+
@Nonnull
1149+
private CorrelationIdentifier candidateTopAlias;
11471150
private final boolean reverseScanOrder;
11481151
@Nonnull
11491152
private final Set<RequestedOrdering> satisfyingRequestedOrderings;
11501153

11511154
public SingleMatchedAccess(@Nonnull final PartialMatch partialMatch,
11521155
@Nonnull final Compensation compensation,
1156+
@Nonnull final CorrelationIdentifier candidateTopAlias,
11531157
final boolean reverseScanOrder,
11541158
@Nonnull final Set<RequestedOrdering> satisfyingRequestedOrderings) {
11551159
this.partialMatch = partialMatch;
11561160
this.compensation = compensation;
1161+
this.candidateTopAlias = candidateTopAlias;
11571162
this.reverseScanOrder = reverseScanOrder;
11581163
this.satisfyingRequestedOrderings = ImmutableSet.copyOf(satisfyingRequestedOrderings);
11591164
}
@@ -1168,6 +1173,11 @@ public Compensation getCompensation() {
11681173
return compensation;
11691174
}
11701175

1176+
@Nonnull
1177+
public CorrelationIdentifier getCandidateTopAlias() {
1178+
return candidateTopAlias;
1179+
}
1180+
11711181
public boolean isReverseScanOrder() {
11721182
return reverseScanOrder;
11731183
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void onMatch(@Nonnull final ImplementationCascadesRuleCall call) {
6565
// if the fetch is able to push all values we can eliminate the fetch as well
6666
final RecordQueryFetchFromPartialRecordPlan fetchPlan = call.get(innerPlanMatcher);
6767
final CorrelationIdentifier oldInnerAlias = Iterables.getOnlyElement(projectionExpression.getQuantifiers()).getAlias();
68-
final CorrelationIdentifier newInnerAlias = Quantifier.uniqueID();
68+
final CorrelationIdentifier newInnerAlias = Quantifier.uniqueId();
6969
final List<? extends Value> projectedValues = projectionExpression.getProjectedValues();
7070
final boolean allPushable = projectedValues
7171
.stream()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public void onMatch(@Nonnull final ExplorationCascadesRuleCall call) {
156156

157157
final CorrelationIdentifier lowerAliasCorrelatedToByUpperAliases;
158158
if (lowersCorrelatedToByUpperAliases.isEmpty()) {
159-
lowerAliasCorrelatedToByUpperAliases = Quantifier.uniqueID();
159+
lowerAliasCorrelatedToByUpperAliases = Quantifier.uniqueId();
160160
} else {
161161
lowerAliasCorrelatedToByUpperAliases = Iterables.getOnlyElement(lowersCorrelatedToByUpperAliases);
162162
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void onMatch(@Nonnull final ImplementationCascadesRuleCall call) {
9292
final RecordQueryFetchFromPartialRecordPlan fetchPlan = bindings.get(fetchPlanMatcher);
9393
final RecordQueryPlan innerPlan = bindings.get(innerPlanMatcher);
9494

95-
final CorrelationIdentifier newInnerAlias = Quantifier.uniqueID();
95+
final CorrelationIdentifier newInnerAlias = Quantifier.uniqueId();
9696

9797
final Quantifier.Physical newInnerQuantifier = Quantifier.physical(call.memoizePlan(innerPlan), newInnerAlias);
9898

0 commit comments

Comments
 (0)