Skip to content

Commit 2548b73

Browse files
committed
intersection complete
1 parent ace86d2 commit 2548b73

File tree

5 files changed

+166
-36
lines changed

5 files changed

+166
-36
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static AggregateMappings empty() {
5555
}
5656

5757
@Nonnull
58-
public static AggregateMappings of(@Nonnull final BiMap<Value, Value> matchedAggregateMap,
58+
public static AggregateMappings of(@Nonnull final Map<Value, Value> matchedAggregateMap,
5959
@Nonnull final BiMap<CorrelationIdentifier, Value> unmatchedAggregateMap) {
6060
return new AggregateMappings(ImmutableMap.copyOf(matchedAggregateMap), ImmutableBiMap.copyOf(unmatchedAggregateMap));
6161
}

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
import com.apple.foundationdb.record.query.plan.cascades.expressions.RelationalExpression;
2828
import com.apple.foundationdb.record.query.plan.cascades.predicates.QueryPredicate;
2929
import com.apple.foundationdb.record.query.plan.cascades.rules.DataAccessRule;
30+
import com.apple.foundationdb.record.query.plan.cascades.values.Value;
3031
import com.google.common.base.Suppliers;
3132
import com.google.common.base.Verify;
33+
import com.google.common.collect.ImmutableBiMap;
3234
import com.google.common.collect.ImmutableList;
3335
import com.google.common.collect.ImmutableMap;
3436
import com.google.common.collect.ImmutableSet;
@@ -583,6 +585,19 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
583585
.collect(ImmutableMap.toImmutableMap(Map.Entry::getKey,
584586
Map.Entry::getValue,
585587
(l, r) -> l));
588+
final var newUnmatchedAggregateMapBuilder =
589+
ImmutableBiMap.<CorrelationIdentifier, Value>builder();
590+
for (final var entry : getAggregateMappings().getUnmatchedAggregateMap().entrySet()) {
591+
if (!newMatchedAggregateMap.containsKey(entry.getValue())) {
592+
newUnmatchedAggregateMapBuilder.put(entry);
593+
}
594+
}
595+
for (final var entry : otherWithSelectCompensation.getAggregateMappings().getUnmatchedAggregateMap().entrySet()) {
596+
if (!newMatchedAggregateMap.containsKey(entry.getValue())) {
597+
newUnmatchedAggregateMapBuilder.put(entry);
598+
}
599+
}
600+
final var newAggregateMappings = AggregateMappings.of(newMatchedAggregateMap, newUnmatchedAggregateMapBuilder.build());
586601

587602
final ResultCompensationFunction newResultResultCompensationFunction;
588603
final var resultCompensationFunction = getResultCompensationFunction();
@@ -594,7 +609,7 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
594609
Verify.verify(resultCompensationFunction.isNeeded());
595610
Verify.verify(otherResultCompensationFunction.isNeeded());
596611
// pick the one from this side -- it does not matter as both candidates have the same shape
597-
newResultResultCompensationFunction = resultCompensationFunction;
612+
newResultResultCompensationFunction = resultCompensationFunction.amend(newAggregateMappings);
598613
}
599614

600615
final var otherCompensationMap = otherWithSelectCompensation.getPredicateCompensationMap();
@@ -608,7 +623,8 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
608623
// reapplication we need to generate plan variants with either compensation and let the planner
609624
// figure out which one wins.
610625
// We just pick one side here.
611-
combinedPredicateMap.put(entry.getKey(), entry.getValue());
626+
combinedPredicateMap.put(entry.getKey(),
627+
entry.getValue().amend(newAggregateMappings));
612628
}
613629
}
614630

@@ -642,7 +658,7 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
642658
intersectedUnmatchedQuantifiers,
643659
getCompensatedAliases(), // both compensated aliases must be identical, but too expensive to check
644660
newResultResultCompensationFunction,
645-
AggregateMappings.empty());
661+
newAggregateMappings);
646662
}
647663
}
648664

@@ -905,5 +921,22 @@ public RelationalExpression applyFinal(@Nonnull final Memoizer memoizer,
905921
.build()
906922
.buildSelectWithResultValue(resultValue);
907923
}
924+
925+
@Nonnull
926+
@Override
927+
public String toString() {
928+
final var result = new StringBuilder();
929+
if (isNeeded()) {
930+
result.append("needed; ");
931+
} else {
932+
result.append("not needed; ");
933+
}
934+
if (isImpossible()) {
935+
result.append("impossible");
936+
} else {
937+
result.append("possible");
938+
}
939+
return result.toString();
940+
}
908941
}
909942
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ default AdjustedBuilder adjustedBuilder() {
8585
@Nonnull
8686
default AggregateMappings adjustAggregateMappings(@Nonnull final PartialMatch partialMatch,
8787
@Nonnull final Quantifier candidateQuantifier) {
88-
final var adjustedMatchedAggregateMapBuilder = ImmutableBiMap.<Value, Value>builder();
88+
final var adjustedMatchedAggregateMapBuilder = ImmutableMap.<Value, Value>builder();
8989
final var aggregateMappings = getAggregateMappings();
9090
final var matchedAggregateMap = aggregateMappings.getMatchedAggregateMap();
9191
for (final var matchedAggregateMapEntry : matchedAggregateMap.entrySet()) {
@@ -370,7 +370,7 @@ public static Optional<Map<CorrelationIdentifier, ComparisonRange>> tryMergePara
370370
private static AggregateMappings pullUpAndMergeAggregateMappings(@Nonnull final AliasMap bindingAliasMap,
371371
@Nonnull final IdentityBiMap<Quantifier, PartialMatch> partialMatchMap,
372372
@Nonnull final AggregateMappings additionalAggregateMappings) {
373-
final var matchedAggregateMapBuilder = ImmutableBiMap.<Value, Value>builder();
373+
final var matchedAggregateMapBuilder = ImmutableMap.<Value, Value>builder();
374374
matchedAggregateMapBuilder.putAll(additionalAggregateMappings.getMatchedAggregateMap());
375375
final var unatchedAggregateMapBuilder = ImmutableBiMap.<CorrelationIdentifier, Value>builder();
376376
unatchedAggregateMapBuilder.putAll(additionalAggregateMappings.getUnmatchedAggregateMap());
@@ -405,7 +405,7 @@ public static AggregateMappings pullUpAggregateMappings(@Nonnull final PartialMa
405405
final var queryExpression = partialMatch.getQueryExpression();
406406
final var resultValue = queryExpression.getResultValue();
407407
final var aggregateMappings = matchInfo.getAggregateMappings();
408-
final var matchedAggregateMapBuilder = ImmutableBiMap.<Value, Value>builder();
408+
final var matchedAggregateMapBuilder = ImmutableMap.<Value, Value>builder();
409409
for (final var matchedAggregateMapEntry : aggregateMappings.getMatchedAggregateMap().entrySet()) {
410410
final var queryAggregateValue = matchedAggregateMapEntry.getKey();
411411
final Value pulledUpQueryAggregateValue;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ public Iterable<MatchInfo> subsumedBy(@Nonnull final RelationalExpression candid
343343
ImmutableBiMap.<CorrelationIdentifier, Value>builder();
344344
final var unmatchedTranslatedAggregateValueMapBuilder =
345345
ImmutableMap.<Value, CorrelationIdentifier>builder();
346-
var subsumedAggregations = BooleanWithConstraint.alwaysTrue();
346+
var subsumedAggregations = BooleanWithConstraint.falseValue();
347347
for (final var primitiveAggregateValue : aggregateValues) {
348348
final var translatedPrimitiveAggregateValue =
349349
primitiveAggregateValue.translateCorrelations(translationMap, true);

0 commit comments

Comments
 (0)