Skip to content

Commit 55cd005

Browse files
committed
updating tests and writing documentation
1 parent dd18b65 commit 55cd005

File tree

70 files changed

+2977
-2910
lines changed

Some content is hidden

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

70 files changed

+2977
-2910
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,14 @@ private static Map<String, BuiltInFunction<? extends Value>> computeAggregateMap
357357
return mapBuilder.build();
358358
}
359359

360+
public static boolean canBeRolledUp(@Nonnull final String indexType) {
361+
return rollUpAggregateMap.get().containsKey(indexType);
362+
}
363+
360364
@Nonnull
361-
public static Optional<AggregateValue> rollUpAggregateValueMaybe(@Nonnull final Index index, @Nonnull final Value argument) {
365+
public static Optional<AggregateValue> rollUpAggregateValueMaybe(@Nonnull final String indexType, @Nonnull final Value argument) {
362366
return Optional.ofNullable(rollUpAggregateMap.get()
363-
.get(index.getType()))
367+
.get(indexType))
364368
.map(fn -> (AggregateValue)fn.encapsulate(ImmutableList.of(argument)));
365369
}
366370

@@ -369,8 +373,8 @@ private static Map<String, BuiltInFunction<? extends Value>> computeRollUpAggreg
369373
final ImmutableMap.Builder<String, BuiltInFunction<? extends Value>> mapBuilder = ImmutableMap.builder();
370374
mapBuilder.put(IndexTypes.MAX_EVER_LONG, new NumericAggregationValue.MaxFn());
371375
mapBuilder.put(IndexTypes.MIN_EVER_LONG, new NumericAggregationValue.MinFn());
372-
// mapBuilder.put(IndexTypes.MAX_EVER_TUPLE, TODO);
373-
// mapBuilder.put(IndexTypes.MIN_EVER_TUPLE, TODO);
376+
mapBuilder.put(IndexTypes.MAX_EVER_TUPLE, new NumericAggregationValue.MaxFn());
377+
mapBuilder.put(IndexTypes.MIN_EVER_TUPLE, new NumericAggregationValue.MinFn());
374378
mapBuilder.put(IndexTypes.SUM, new NumericAggregationValue.SumFn());
375379
mapBuilder.put(IndexTypes.COUNT, new NumericAggregationValue.SumFn());
376380
mapBuilder.put(IndexTypes.COUNT_NOT_NULL, new NumericAggregationValue.SumFn());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ public RecordQueryPlan toEquivalentPlan(@Nonnull final PartialMatch partialMatch
436436
}
437437

438438
final var rollUpAggregateValueOptional =
439-
AggregateIndexExpansionVisitor.rollUpAggregateValueMaybe(index,
439+
AggregateIndexExpansionVisitor.rollUpAggregateValueMaybe(index.getType(),
440440
aggregateAccessorValue);
441441

442442
final var aggregateIndexScanQuantifier =

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@
4141
import com.apple.foundationdb.record.query.plan.cascades.rules.ImplementIntersectionRule;
4242
import com.apple.foundationdb.record.query.plan.cascades.rules.ImplementNestedLoopJoinRule;
4343
import com.apple.foundationdb.record.query.plan.cascades.rules.ImplementPhysicalScanRule;
44-
import com.apple.foundationdb.record.query.plan.cascades.rules.ImplementRecursiveUnionRule;
4544
import com.apple.foundationdb.record.query.plan.cascades.rules.ImplementSimpleSelectRule;
4645
import com.apple.foundationdb.record.query.plan.cascades.rules.ImplementStreamingAggregationRule;
47-
import com.apple.foundationdb.record.query.plan.cascades.rules.ImplementTempTableInsertRule;
4846
import com.apple.foundationdb.record.query.plan.cascades.rules.ImplementTempTableScanRule;
4947
import com.apple.foundationdb.record.query.plan.cascades.rules.ImplementTypeFilterRule;
5048
import com.apple.foundationdb.record.query.plan.cascades.rules.ImplementUniqueRule;

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

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.apple.foundationdb.record.PlanSerializationContext;
2626
import com.apple.foundationdb.record.planprotos.PValue;
2727
import com.apple.foundationdb.record.query.expressions.Comparisons;
28+
import com.apple.foundationdb.record.query.plan.cascades.AggregateIndexExpansionVisitor;
2829
import com.apple.foundationdb.record.query.plan.cascades.AliasMap;
2930
import com.apple.foundationdb.record.query.plan.cascades.BooleanWithConstraint;
3031
import com.apple.foundationdb.record.query.plan.cascades.Column;
@@ -85,9 +86,6 @@
8586
import java.util.function.BiFunction;
8687
import java.util.function.Supplier;
8788

88-
import static com.apple.foundationdb.record.query.plan.cascades.BooleanWithConstraint.alwaysTrue;
89-
import static com.apple.foundationdb.record.query.plan.cascades.BooleanWithConstraint.falseValue;
90-
9189
/**
9290
* A logical {@code group by} expression that represents grouping incoming tuples and aggregating each group.
9391
*/
@@ -339,7 +337,7 @@ public Iterable<MatchInfo> subsumedBy(@Nonnull final RelationalExpression candid
339337
if (otherAggregateValues.size() != 1) {
340338
return ImmutableList.of();
341339
}
342-
final var otherPrimitiveAggregateValue = Iterables.getOnlyElement(otherAggregateValues);
340+
final var otherPrimitiveAggregateValue = (IndexableAggregateValue)Iterables.getOnlyElement(otherAggregateValues);
343341
final var matchedAggregatesMapBuilder = ImmutableBiMap.<Value, Value>builder();
344342
final var unmatchedAggregatesMapBuilder =
345343
ImmutableBiMap.<CorrelationIdentifier, Value>builder();
@@ -377,6 +375,12 @@ public Iterable<MatchInfo> subsumedBy(@Nonnull final RelationalExpression candid
377375
final var matchedGroupingsMap = subsumedGroupingsResult.getMatchedGroupingsMap();
378376
final var rollUpToGroupingValues = subsumedGroupingsResult.getRollUpToValues();
379377

378+
if (rollUpToGroupingValues != null &&
379+
!AggregateIndexExpansionVisitor.canBeRolledUp(otherPrimitiveAggregateValue.getIndexTypeName())) {
380+
// We determined we need a roll up, but we cannot do it base on the aggregations.
381+
return ImmutableList.of();
382+
}
383+
380384
final var unmatchedTranslatedAggregateValueMap =
381385
unmatchedTranslatedAggregatesValueMapBuilder.buildKeepingLast();
382386
final var translatedResultValue = getResultValue().translateCorrelations(translationMap, true);
@@ -414,7 +418,7 @@ private SubsumedGroupingsResult subsumedGroupings(@Nonnull final Quantifier cand
414418
@Nonnull final TranslationMap translationMap,
415419
@Nonnull final ValueEquivalence valueEquivalence) {
416420
if (groupingValue == null && candidateGroupingValue == null) {
417-
return SubsumedGroupingsResult.withoutRollUp(alwaysTrue(), ImmutableBiMap.of());
421+
return SubsumedGroupingsResult.withoutRollUp(BooleanWithConstraint.alwaysTrue(), ImmutableBiMap.of());
418422
}
419423
if (candidateGroupingValue == null) {
420424
return SubsumedGroupingsResult.noSubsumption();
@@ -424,7 +428,7 @@ private SubsumedGroupingsResult subsumedGroupings(@Nonnull final Quantifier cand
424428
final BiMap<Value, Value> matchedGroupingsMap;
425429
if (groupingValue != null) {
426430
final var translatedGroupingsValuesBuilder = ImmutableList.<Value>builder();
427-
final var matchedGroupingsMapBuilder = ImmutableBiMap.<Value, Value>builder();
431+
final var matchedGroupingsMapBuilder = ImmutableMap.<Value, Value>builder();
428432
final var groupingValues =
429433
Values.primitiveAccessorsForType(groupingValue.getResultType(), () -> groupingValue).stream()
430434
.map(primitiveGroupingValue -> primitiveGroupingValue.simplify(AliasMap.emptyMap(),
@@ -438,7 +442,14 @@ private SubsumedGroupingsResult subsumedGroupings(@Nonnull final Quantifier cand
438442
matchedGroupingsMapBuilder.put(primitiveGroupingValue, translatedPrimitiveGroupingValue);
439443
}
440444
translatedGroupingValues = translatedGroupingsValuesBuilder.build();
441-
matchedGroupingsMap = matchedGroupingsMapBuilder.build();
445+
446+
//
447+
// We know that if there are duplicates, they will be on the query side. Immutable bi-maps do not support
448+
// duplicated keys at all while regular maps do. The simplest and also the cheapest solution is to just
449+
// use an immutable map builder (which then is de-duped when built) and then use that map to build the
450+
// bi-map.
451+
//
452+
matchedGroupingsMap = ImmutableBiMap.copyOf(matchedGroupingsMapBuilder.buildKeepingLast());
442453
} else {
443454
translatedGroupingValues = ImmutableList.of();
444455
matchedGroupingsMap = ImmutableBiMap.of();
@@ -474,7 +485,7 @@ private SubsumedGroupingsResult subsumedGroupings(@Nonnull final Quantifier cand
474485
// 3. For each candidate grouping value in the set of (yet) unmatched candidate group values, try to find a
475486
// predicate that binds that groupingValue.
476487
//
477-
var booleanWithConstraint = alwaysTrue();
488+
var booleanWithConstraint = BooleanWithConstraint.alwaysTrue();
478489
for (final var translatedGroupingValue : translatedGroupingValuesSet) {
479490
var found = false;
480491

@@ -561,7 +572,8 @@ private SubsumedGroupingsResult subsumedGroupings(@Nonnull final Quantifier cand
561572
}
562573

563574
if (!unmatchedCandidateValues.isEmpty()) {
564-
Verify.verify(candidateGroupingValues.size() > translatedGroupingValues.size());
575+
Verify.verify(candidateGroupingValues.size() > translatedGroupingValuesSet.size());
576+
565577
//
566578
// This is a potential roll-up case, but only if the query side's groupings are completely subsumed
567579
// by the prefix of the candidate side. Iterate up to the smaller query side's grouping values to
@@ -629,14 +641,7 @@ public Compensation compensate(@Nonnull final PartialMatch partialMatch,
629641

630642
final var childCompensation = childCompensationOptional.get();
631643

632-
if (childCompensation.isImpossible()
633-
// ||
634-
// //
635-
// // TODO This needs some improvement as GB a, b, c WHERE a= AND c= needs to reapply the
636-
// // predicate on c which is currently refused here.
637-
// //
638-
// childCompensation.isNeededForFiltering()
639-
) {
644+
if (childCompensation.isImpossible()) {
640645
//
641646
// Note that it may be better to just return the child compensation verbatim as that compensation
642647
// may be combinable with something else to make it possible while the statically impossible compensation
@@ -730,7 +735,7 @@ public static Value flattenedResults(@Nullable final Value groupingKeyValue,
730735
return rcv.simplify(AliasMap.identitiesFor(rcv.getCorrelatedTo()), ImmutableSet.of());
731736
}
732737

733-
public static class UnmatchedAggregateValue extends AbstractValue implements Value.NonEvaluableValue, IndexableAggregateValue {
738+
public static class UnmatchedAggregateValue extends AbstractValue implements Value.NonEvaluableValue {
734739
@Nonnull
735740
private final CorrelationIdentifier unmatchedId;
736741

@@ -749,12 +754,6 @@ protected Iterable<? extends Value> computeChildren() {
749754
return ImmutableList.of();
750755
}
751756

752-
@Nonnull
753-
@Override
754-
public String getIndexTypeName() {
755-
throw new UnsupportedOperationException();
756-
}
757-
758757
@Nonnull
759758
@Override
760759
public ExplainTokensWithPrecedence explain(@Nonnull final Iterable<Supplier<ExplainTokensWithPrecedence>> explainSuppliers) {
@@ -831,7 +830,7 @@ public List<Value> getRollUpToValues() {
831830

832831
@Nonnull
833832
public static SubsumedGroupingsResult noSubsumption() {
834-
return of(falseValue(), ImmutableBiMap.of(), null);
833+
return of(BooleanWithConstraint.falseValue(), ImmutableBiMap.of(), null);
835834
}
836835

837836
@Nonnull

0 commit comments

Comments
 (0)