Skip to content

Commit 016b52b

Browse files
committed
intersection complete
1 parent 2548b73 commit 016b52b

18 files changed

+86
-55
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
@@ -323,7 +323,7 @@ public RelationalExpression apply(@Nonnull final Memoizer memoizer,
323323
@Override
324324
public RelationalExpression applyFinal(@Nonnull final Memoizer memoizer,
325325
@Nonnull final RelationalExpression relationalExpression) {
326-
return Compensation.this.apply(memoizer, otherCompensation.apply(memoizer, relationalExpression));
326+
return Compensation.this.applyFinal(memoizer, otherCompensation.applyFinal(memoizer, relationalExpression));
327327
}
328328
};
329329
}
@@ -352,8 +352,8 @@ public RelationalExpression apply(@Nonnull final Memoizer memoizer,
352352
@Override
353353
public RelationalExpression applyFinal(@Nonnull final Memoizer memoizer,
354354
@Nonnull final RelationalExpression relationalExpression) {
355-
return Compensation.this.apply(memoizer,
356-
otherCompensation.apply(memoizer, relationalExpression));
355+
return Compensation.this.applyFinal(memoizer,
356+
otherCompensation.applyFinal(memoizer, relationalExpression));
357357
}
358358
};
359359
}
@@ -587,7 +587,8 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
587587
(l, r) -> l));
588588
final var newUnmatchedAggregateMapBuilder =
589589
ImmutableBiMap.<CorrelationIdentifier, Value>builder();
590-
for (final var entry : getAggregateMappings().getUnmatchedAggregateMap().entrySet()) {
590+
final var unmatchedAggregateMap = getAggregateMappings().getUnmatchedAggregateMap();
591+
for (final var entry : unmatchedAggregateMap.entrySet()) {
591592
if (!newMatchedAggregateMap.containsKey(entry.getValue())) {
592593
newUnmatchedAggregateMapBuilder.put(entry);
593594
}
@@ -609,10 +610,12 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
609610
Verify.verify(resultCompensationFunction.isNeeded());
610611
Verify.verify(otherResultCompensationFunction.isNeeded());
611612
// pick the one from this side -- it does not matter as both candidates have the same shape
612-
newResultResultCompensationFunction = resultCompensationFunction.amend(newAggregateMappings);
613+
newResultResultCompensationFunction =
614+
resultCompensationFunction.amend(unmatchedAggregateMap, newMatchedAggregateMap);
613615
}
614616

615-
final var otherCompensationMap = otherWithSelectCompensation.getPredicateCompensationMap();
617+
final var otherCompensationMap =
618+
otherWithSelectCompensation.getPredicateCompensationMap();
616619
final var combinedPredicateMap = new LinkedIdentityMap<QueryPredicate, PredicateCompensationFunction>();
617620
for (final var entry : getPredicateCompensationMap().entrySet()) {
618621
// if the other side does not have compensation for this key, we don't need compensation
@@ -624,7 +627,7 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
624627
// figure out which one wins.
625628
// We just pick one side here.
626629
combinedPredicateMap.put(entry.getKey(),
627-
entry.getValue().amend(newAggregateMappings));
630+
entry.getValue().amend(unmatchedAggregateMap, newMatchedAggregateMap));
628631
}
629632
}
630633

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.google.common.collect.ImmutableSetMultimap;
3536
import com.google.common.collect.Multimaps;
@@ -64,15 +65,17 @@ public class PredicateMultiMap {
6465
private final ImmutableSetMultimap<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);
@@ -370,9 +385,10 @@ public boolean isImpossible() {
370385

371386
@Nonnull
372387
@Override
373-
public ResultCompensationFunction amend(@Nonnull final AggregateMappings aggregateMappings) {
388+
public ResultCompensationFunction amend(@Nonnull final BiMap<CorrelationIdentifier, Value> unmatchedAggregateMap,
389+
@Nonnull final Map<Value, Value> amendedMatchedAggregateMap) {
374390
final var amendedTranslatedQueryValue =
375-
amendValue(aggregateMappings, value);
391+
amendValue(unmatchedAggregateMap, amendedMatchedAggregateMap, value);
376392

377393
return ofValue(amendedTranslatedQueryValue, compensationFunction);
378394
}

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
@@ -170,7 +170,7 @@ public ForEachBuilder from(final ForEach quantifier) {
170170
@Nonnull
171171
@Override
172172
public ForEach build(@Nonnull final Reference rangesOver) {
173-
return new ForEach(alias == null ? Quantifier.uniqueID() : alias, rangesOver, isNullOnEmpty);
173+
return new ForEach(alias == null ? Quantifier.uniqueId() : alias, rangesOver, isNullOnEmpty);
174174
}
175175
}
176176

@@ -341,7 +341,7 @@ public static class ExistentialBuilder extends Builder<Existential, ExistentialB
341341
@Override
342342
@Nonnull
343343
public Existential build(@Nonnull final Reference rangesOver) {
344-
return new Existential(alias == null ? Quantifier.uniqueID() : alias,
344+
return new Existential(alias == null ? Quantifier.uniqueId() : alias,
345345
rangesOver);
346346
}
347347
}
@@ -443,7 +443,7 @@ public static class PhysicalBuilder extends Builder<Physical, PhysicalBuilder> {
443443
@Nonnull
444444
@Override
445445
public Physical build(@Nonnull final Reference rangesOver) {
446-
return new Physical(alias == null ? Quantifier.uniqueID() : alias, rangesOver);
446+
return new Physical(alias == null ? Quantifier.uniqueId() : alias, rangesOver);
447447
}
448448

449449
/**
@@ -791,7 +791,7 @@ public Quantifier translateCorrelations(@Nonnull final TranslationMap translatio
791791
}
792792

793793
@Nonnull
794-
public static CorrelationIdentifier uniqueID() {
794+
public static CorrelationIdentifier uniqueId() {
795795
return CorrelationIdentifier.uniqueId(Quantifier.class);
796796
}
797797

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
@@ -552,11 +552,12 @@ private static List<SingleMatchedAccess> prepareMatchesAndCompensations(final @N
552552
Verify.verify(scanDirection == ScanDirection.FORWARD || scanDirection == ScanDirection.REVERSE ||
553553
scanDirection == ScanDirection.BOTH);
554554

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

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

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

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

11521155
public SingleMatchedAccess(@Nonnull final PartialMatch partialMatch,
11531156
@Nonnull final Compensation compensation,
1157+
@Nonnull final CorrelationIdentifier candidateTopAlias,
11541158
final boolean reverseScanOrder,
11551159
@Nonnull final Set<RequestedOrdering> satisfyingRequestedOrderings) {
11561160
this.partialMatch = partialMatch;
11571161
this.compensation = compensation;
1162+
this.candidateTopAlias = candidateTopAlias;
11581163
this.reverseScanOrder = reverseScanOrder;
11591164
this.satisfyingRequestedOrderings = ImmutableSet.copyOf(satisfyingRequestedOrderings);
11601165
}
@@ -1169,6 +1174,11 @@ public Compensation getCompensation() {
11691174
return compensation;
11701175
}
11711176

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

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 CascadesRuleCall 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
@@ -150,7 +150,7 @@ public void onMatch(@Nonnull final CascadesRuleCall call) {
150150

151151
final CorrelationIdentifier lowerAliasCorrelatedToByUpperAliases;
152152
if (lowersCorrelatedToByUpperAliases.isEmpty()) {
153-
lowerAliasCorrelatedToByUpperAliases = Quantifier.uniqueID();
153+
lowerAliasCorrelatedToByUpperAliases = Quantifier.uniqueId();
154154
} else {
155155
lowerAliasCorrelatedToByUpperAliases = Iterables.getOnlyElement(lowersCorrelatedToByUpperAliases);
156156
}

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 CascadesRuleCall 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.memoizePlans(innerPlan), newInnerAlias);
9898

0 commit comments

Comments
 (0)