Skip to content

Commit 6c00818

Browse files
committed
extract common code out of ::compensate
1 parent 4a486eb commit 6c00818

File tree

69 files changed

+5062
-5093
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+5062
-5093
lines changed

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
import com.apple.foundationdb.record.query.plan.cascades.expressions.LogicalFilterExpression;
2727
import com.apple.foundationdb.record.query.plan.cascades.expressions.RelationalExpression;
2828
import com.apple.foundationdb.record.query.plan.cascades.predicates.QueryPredicate;
29+
import com.apple.foundationdb.record.query.plan.cascades.values.QuantifiedObjectValue;
2930
import com.apple.foundationdb.record.query.plan.cascades.values.Value;
31+
import com.apple.foundationdb.record.query.plan.cascades.values.translation.PullUp;
3032
import com.apple.foundationdb.record.query.plan.cascades.values.translation.TranslationMap;
3133
import com.google.common.base.Suppliers;
3234
import com.google.common.base.Verify;
@@ -37,8 +39,10 @@
3739
import com.google.common.collect.Sets;
3840

3941
import javax.annotation.Nonnull;
42+
import javax.annotation.Nullable;
4043
import java.util.Collection;
4144
import java.util.Map;
45+
import java.util.Optional;
4246
import java.util.Set;
4347
import java.util.function.Function;
4448
import java.util.function.Supplier;
@@ -429,6 +433,86 @@ default ForMatch derived(final boolean isImpossible,
429433
unmatchedQuantifiers, compensatedAliases, resultCompensationFunction, groupByMappings);
430434
}
431435

436+
/**
437+
* Compute the necessary compensation for the result of the top operation's {@link PartialMatch}.
438+
* @param partialMatch the partial match
439+
* @param rootOfMatchPullUp pull up to get expressions to top level
440+
* @return a {@link CompensatedResult}
441+
*/
442+
@Nonnull
443+
static Optional<CompensatedResult> computeResultCompensation(@Nonnull final PartialMatch partialMatch,
444+
@Nullable final PullUp rootOfMatchPullUp) {
445+
final var matchInfo = partialMatch.getMatchInfo();
446+
boolean isCompensationImpossible = false;
447+
final PredicateMultiMap.ResultCompensationFunction resultCompensationFunction;
448+
final GroupByMappings groupByMappings;
449+
450+
if (rootOfMatchPullUp == null) {
451+
// It is possible for the pull up to be null. If that is the case, we do not need compensation.
452+
resultCompensationFunction = PredicateMultiMap.ResultCompensationFunction.noCompensationNeeded();
453+
groupByMappings = GroupByMappings.empty();
454+
} else {
455+
//
456+
// Pull up the result of the query as translated to the candidate side. If that can be done,
457+
// we then attempt to avoid straightforward identity compensations. If that's not possible, we'll
458+
// create a result compensation.
459+
//
460+
final var maxMatchMap = matchInfo.getMaxMatchMap();
461+
final var pulledUpTranslatedResultValueOptional =
462+
rootOfMatchPullUp.pullUpValueMaybe(maxMatchMap.getQueryValue());
463+
if (pulledUpTranslatedResultValueOptional.isEmpty()) {
464+
return Optional.empty();
465+
}
466+
467+
final var pulledUpTranslatedResultValue = pulledUpTranslatedResultValueOptional.get();
468+
469+
if (QuantifiedObjectValue.isSimpleQuantifiedObjectValueOver(pulledUpTranslatedResultValue,
470+
rootOfMatchPullUp.getCandidateAlias())) {
471+
resultCompensationFunction = PredicateMultiMap.ResultCompensationFunction.noCompensationNeeded();
472+
} else {
473+
resultCompensationFunction =
474+
PredicateMultiMap.ResultCompensationFunction.ofValue(pulledUpTranslatedResultValue);
475+
}
476+
isCompensationImpossible = resultCompensationFunction.isImpossible();
477+
478+
// Fix up the group my mappings as well.
479+
groupByMappings =
480+
MatchInfo.RegularMatchInfo.pullUpAggregateCandidateMappings(partialMatch, rootOfMatchPullUp);
481+
}
482+
return Optional.of(new CompensatedResult(isCompensationImpossible, resultCompensationFunction,
483+
groupByMappings));
484+
}
485+
486+
class CompensatedResult {
487+
private final boolean isCompensationImpossible;
488+
@Nonnull
489+
private final PredicateMultiMap.ResultCompensationFunction resultCompensationFunction;
490+
@Nonnull
491+
private final GroupByMappings groupByMappings;
492+
493+
public CompensatedResult(final boolean isCompensationImpossible,
494+
@Nonnull final PredicateMultiMap.ResultCompensationFunction resultCompensationFunction,
495+
@Nonnull final GroupByMappings groupByMappings) {
496+
this.isCompensationImpossible = isCompensationImpossible;
497+
this.resultCompensationFunction = resultCompensationFunction;
498+
this.groupByMappings = groupByMappings;
499+
}
500+
501+
public boolean isCompensationImpossible() {
502+
return isCompensationImpossible;
503+
}
504+
505+
@Nonnull
506+
public PredicateMultiMap.ResultCompensationFunction getResultCompensationFunction() {
507+
return resultCompensationFunction;
508+
}
509+
510+
@Nonnull
511+
public GroupByMappings getGroupByMappings() {
512+
return groupByMappings;
513+
}
514+
}
515+
432516
/**
433517
* Interface for {@link Compensation}s that map original {@link QueryPredicate}s to compensating
434518
* {@link QueryPredicate}s.

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

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import com.apple.foundationdb.record.query.plan.cascades.OrderingPart.RequestedSortOrder;
4242
import com.apple.foundationdb.record.query.plan.cascades.PartialMatch;
4343
import com.apple.foundationdb.record.query.plan.cascades.PredicateMap;
44-
import com.apple.foundationdb.record.query.plan.cascades.PredicateMultiMap.ResultCompensationFunction;
4544
import com.apple.foundationdb.record.query.plan.cascades.Quantifier;
4645
import com.apple.foundationdb.record.query.plan.cascades.Quantifiers;
4746
import com.apple.foundationdb.record.query.plan.cascades.RequestedOrdering;
@@ -56,7 +55,6 @@
5655
import com.apple.foundationdb.record.query.plan.cascades.values.AbstractValue;
5756
import com.apple.foundationdb.record.query.plan.cascades.values.AggregateValue;
5857
import com.apple.foundationdb.record.query.plan.cascades.values.FieldValue;
59-
import com.apple.foundationdb.record.query.plan.cascades.values.QuantifiedObjectValue;
6058
import com.apple.foundationdb.record.query.plan.cascades.values.IndexableAggregateValue;
6159
import com.apple.foundationdb.record.query.plan.cascades.values.RecordConstructorValue;
6260
import com.apple.foundationdb.record.query.plan.cascades.values.Value;
@@ -611,14 +609,12 @@ private RequestedOrdering computeRequestedOrdering() {
611609
innerQuantifier.getCorrelatedTo());
612610
}
613611

614-
@SuppressWarnings("ConstantValue")
615612
@Nonnull
616613
@Override
617614
public Compensation compensate(@Nonnull final PartialMatch partialMatch,
618615
@Nonnull final Map<CorrelationIdentifier, ComparisonRange> boundParameterPrefixMap,
619616
@Nullable final PullUp pullUp,
620617
@Nonnull final CorrelationIdentifier candidateAlias) {
621-
final var matchInfo = partialMatch.getMatchInfo();
622618
final var regularMatchInfo = partialMatch.getRegularMatchInfo();
623619
final var quantifier = Iterables.getOnlyElement(getQuantifiers());
624620

@@ -651,49 +647,26 @@ public Compensation compensate(@Nonnull final PartialMatch partialMatch,
651647
return Compensation.impossibleCompensation();
652648
}
653649

654-
boolean isCompensationImpossible = false;
655-
final ResultCompensationFunction resultCompensationFunction;
656-
final GroupByMappings pulledUpGroupByMappings;
657-
if (rootOfMatchPullUp == null) {
658-
resultCompensationFunction = ResultCompensationFunction.noCompensationNeeded();
659-
pulledUpGroupByMappings = GroupByMappings.empty();
660-
} else {
661-
final var maxMatchMap = matchInfo.getMaxMatchMap();
662-
final var pulledUpTranslatedResultValueOptional =
663-
rootOfMatchPullUp.pullUpValueMaybe(maxMatchMap.getQueryValue());
664-
if (pulledUpTranslatedResultValueOptional.isEmpty()) {
665-
return Compensation.impossibleCompensation();
666-
}
667-
668-
final var pulledUpTranslatedResultValue = pulledUpTranslatedResultValueOptional.get();
669-
670-
if (QuantifiedObjectValue.isSimpleQuantifiedObjectValueOver(pulledUpTranslatedResultValue,
671-
rootOfMatchPullUp.getCandidateAlias())) {
672-
resultCompensationFunction = ResultCompensationFunction.noCompensationNeeded();
673-
} else {
674-
resultCompensationFunction =
675-
ResultCompensationFunction.ofValue(pulledUpTranslatedResultValue);
676-
}
677-
isCompensationImpossible |= resultCompensationFunction.isImpossible();
678-
679-
pulledUpGroupByMappings =
680-
RegularMatchInfo.pullUpAggregateCandidateMappings(partialMatch, rootOfMatchPullUp);
650+
final var compensatedResultOptional =
651+
Compensation.computeResultCompensation(partialMatch, rootOfMatchPullUp);
652+
if (compensatedResultOptional.isEmpty()) {
653+
return Compensation.impossibleCompensation();
654+
}
655+
final var compensatedResult = compensatedResultOptional.get();
656+
if (!compensatedResult.getResultCompensationFunction().isNeeded()) {
657+
return Compensation.noCompensation();
681658
}
682659

683660
final var unmatchedQuantifiers = partialMatch.getUnmatchedQuantifiers();
684661
Verify.verify(unmatchedQuantifiers.isEmpty());
685662

686-
if (!resultCompensationFunction.isNeeded()) {
687-
return Compensation.noCompensation();
688-
}
689-
690-
return childCompensation.derived(isCompensationImpossible,
663+
return childCompensation.derived(compensatedResult.isCompensationImpossible(),
691664
new LinkedIdentityMap<>(),
692665
getMatchedQuantifiers(partialMatch),
693666
unmatchedQuantifiers,
694667
partialMatch.getCompensatedAliases(),
695-
resultCompensationFunction,
696-
pulledUpGroupByMappings);
668+
compensatedResult.getResultCompensationFunction(),
669+
compensatedResult.getGroupByMappings());
697670
}
698671

699672
@Nonnull

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

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@
3030
import com.apple.foundationdb.record.query.plan.cascades.IdentityBiMap;
3131
import com.apple.foundationdb.record.query.plan.cascades.LinkedIdentityMap;
3232
import com.apple.foundationdb.record.query.plan.cascades.MatchInfo;
33-
import com.apple.foundationdb.record.query.plan.cascades.GroupByMappings;
34-
import com.apple.foundationdb.record.query.plan.cascades.MatchInfo.RegularMatchInfo;
3533
import com.apple.foundationdb.record.query.plan.cascades.PartialMatch;
36-
import com.apple.foundationdb.record.query.plan.cascades.PredicateMultiMap.ResultCompensationFunction;
3734
import com.apple.foundationdb.record.query.plan.cascades.Quantifier;
3835
import com.apple.foundationdb.record.query.plan.cascades.explain.Attribute;
3936
import com.apple.foundationdb.record.query.plan.cascades.explain.NodeInfo;
@@ -176,14 +173,12 @@ public Iterable<MatchInfo> subsumedBy(@Nonnull final RelationalExpression candid
176173
return exactlySubsumedBy(candidateExpression, bindingAliasMap, partialMatchMap, translationMapOptional.get());
177174
}
178175

179-
@SuppressWarnings("ConstantValue")
180176
@Nonnull
181177
@Override
182178
public Compensation compensate(@Nonnull final PartialMatch partialMatch,
183179
@Nonnull final Map<CorrelationIdentifier, ComparisonRange> boundParameterPrefixMap,
184180
@Nullable final PullUp pullUp,
185181
@Nonnull final CorrelationIdentifier candidateAlias) {
186-
final var matchInfo = partialMatch.getMatchInfo();
187182
final var regularMatchInfo = partialMatch.getRegularMatchInfo();
188183
final var nestedPullUpPair =
189184
partialMatch.nestPullUp(pullUp, candidateAlias);
@@ -204,49 +199,26 @@ public Compensation compensate(@Nonnull final PartialMatch partialMatch,
204199
return Compensation.impossibleCompensation();
205200
}
206201

207-
boolean isCompensationImpossible = false;
208-
final ResultCompensationFunction resultCompensationFunction;
209-
final GroupByMappings groupByMappings;
210-
if (rootOfMatchPullUp == null) {
211-
resultCompensationFunction = ResultCompensationFunction.noCompensationNeeded();
212-
groupByMappings = GroupByMappings.empty();
213-
} else {
214-
final var maxMatchMap = matchInfo.getMaxMatchMap();
215-
final var pulledUpTranslatedResultValueOptional =
216-
rootOfMatchPullUp.pullUpValueMaybe(maxMatchMap.getQueryValue());
217-
if (pulledUpTranslatedResultValueOptional.isEmpty()) {
218-
return Compensation.impossibleCompensation();
219-
}
220-
221-
final var pulledUpTranslatedResultValue = pulledUpTranslatedResultValueOptional.get();
222-
223-
if (QuantifiedObjectValue.isSimpleQuantifiedObjectValueOver(pulledUpTranslatedResultValue,
224-
rootOfMatchPullUp.getCandidateAlias())) {
225-
resultCompensationFunction = ResultCompensationFunction.noCompensationNeeded();
226-
} else {
227-
resultCompensationFunction =
228-
ResultCompensationFunction.ofValue(pulledUpTranslatedResultValue);
229-
}
230-
isCompensationImpossible |= resultCompensationFunction.isImpossible();
231-
232-
groupByMappings =
233-
RegularMatchInfo.pullUpAggregateCandidateMappings(partialMatch, rootOfMatchPullUp);
202+
final var compensatedResultOptional =
203+
Compensation.computeResultCompensation(partialMatch, rootOfMatchPullUp);
204+
if (compensatedResultOptional.isEmpty()) {
205+
return Compensation.impossibleCompensation();
206+
}
207+
final var compensatedResult = compensatedResultOptional.get();
208+
if (!compensatedResult.getResultCompensationFunction().isNeeded()) {
209+
return Compensation.noCompensation();
234210
}
235211

236212
final var unmatchedQuantifiers = partialMatch.getUnmatchedQuantifiers();
237213
Verify.verify(unmatchedQuantifiers.isEmpty());
238214

239-
if (!resultCompensationFunction.isNeeded()) {
240-
return Compensation.noCompensation();
241-
}
242-
243-
return childCompensation.derived(isCompensationImpossible,
215+
return childCompensation.derived(compensatedResult.isCompensationImpossible(),
244216
new LinkedIdentityMap<>(),
245217
getMatchedQuantifiers(partialMatch),
246218
unmatchedQuantifiers,
247219
partialMatch.getCompensatedAliases(),
248-
resultCompensationFunction,
249-
groupByMappings);
220+
compensatedResult.getResultCompensationFunction(),
221+
compensatedResult.getGroupByMappings());
250222
}
251223

252224
@Nonnull

0 commit comments

Comments
 (0)