Skip to content

Commit 3dcf180

Browse files
committed
intersection complete
1 parent 6e98b4c commit 3dcf180

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;
@@ -588,6 +590,19 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
588590
.collect(ImmutableMap.toImmutableMap(Map.Entry::getKey,
589591
Map.Entry::getValue,
590592
(l, r) -> l));
593+
final var newUnmatchedAggregateMapBuilder =
594+
ImmutableBiMap.<CorrelationIdentifier, Value>builder();
595+
for (final var entry : getAggregateMappings().getUnmatchedAggregateMap().entrySet()) {
596+
if (!newMatchedAggregateMap.containsKey(entry.getValue())) {
597+
newUnmatchedAggregateMapBuilder.put(entry);
598+
}
599+
}
600+
for (final var entry : otherWithSelectCompensation.getAggregateMappings().getUnmatchedAggregateMap().entrySet()) {
601+
if (!newMatchedAggregateMap.containsKey(entry.getValue())) {
602+
newUnmatchedAggregateMapBuilder.put(entry);
603+
}
604+
}
605+
final var newAggregateMappings = AggregateMappings.of(newMatchedAggregateMap, newUnmatchedAggregateMapBuilder.build());
591606

592607
final ResultCompensationFunction newResultResultCompensationFunction;
593608
final var resultCompensationFunction = getResultCompensationFunction();
@@ -599,7 +614,7 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
599614
Verify.verify(resultCompensationFunction.isNeeded());
600615
Verify.verify(otherResultCompensationFunction.isNeeded());
601616
// pick the one from this side -- it does not matter as both candidates have the same shape
602-
newResultResultCompensationFunction = resultCompensationFunction;
617+
newResultResultCompensationFunction = resultCompensationFunction.amend(newAggregateMappings);
603618
}
604619

605620
final var otherCompensationMap = otherWithSelectCompensation.getPredicateCompensationMap();
@@ -613,7 +628,8 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
613628
// reapplication we need to generate plan variants with either compensation and let the planner
614629
// figure out which one wins.
615630
// We just pick one side here.
616-
combinedPredicateMap.put(entry.getKey(), entry.getValue());
631+
combinedPredicateMap.put(entry.getKey(),
632+
entry.getValue().amend(newAggregateMappings));
617633
}
618634
}
619635

@@ -647,7 +663,7 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
647663
intersectedUnmatchedQuantifiers,
648664
getCompensatedAliases(), // both compensated aliases must be identical, but too expensive to check
649665
newResultResultCompensationFunction,
650-
AggregateMappings.empty());
666+
newAggregateMappings);
651667
}
652668
}
653669

@@ -910,5 +926,22 @@ public RelationalExpression applyFinal(@Nonnull final Memoizer memoizer,
910926
.build()
911927
.buildSelectWithResultValue(resultValue);
912928
}
929+
930+
@Nonnull
931+
@Override
932+
public String toString() {
933+
final var result = new StringBuilder();
934+
if (isNeeded()) {
935+
result.append("needed; ");
936+
} else {
937+
result.append("not needed; ");
938+
}
939+
if (isImpossible()) {
940+
result.append("impossible");
941+
} else {
942+
result.append("possible");
943+
}
944+
return result.toString();
945+
}
913946
}
914947
}

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
@@ -344,7 +344,7 @@ public Iterable<MatchInfo> subsumedBy(@Nonnull final RelationalExpression candid
344344
ImmutableBiMap.<CorrelationIdentifier, Value>builder();
345345
final var unmatchedTranslatedAggregateValueMapBuilder =
346346
ImmutableMap.<Value, CorrelationIdentifier>builder();
347-
var subsumedAggregations = BooleanWithConstraint.alwaysTrue();
347+
var subsumedAggregations = BooleanWithConstraint.falseValue();
348348
for (final var primitiveAggregateValue : aggregateValues) {
349349
final var translatedPrimitiveAggregateValue =
350350
primitiveAggregateValue.translateCorrelations(translationMap, true);

0 commit comments

Comments
 (0)