27
27
import com .apple .foundationdb .record .query .plan .cascades .expressions .RelationalExpression ;
28
28
import com .apple .foundationdb .record .query .plan .cascades .predicates .QueryPredicate ;
29
29
import com .apple .foundationdb .record .query .plan .cascades .rules .DataAccessRule ;
30
- import com .apple .foundationdb .record .query .plan .cascades .values .Value ;
31
30
import com .google .common .base .Suppliers ;
32
31
import com .google .common .base .Verify ;
33
32
import com .google .common .collect .ImmutableList ;
@@ -387,7 +386,7 @@ default ForMatch derived(final boolean isImpossible,
387
386
@ Nonnull final Set <? extends Quantifier > unmatchedQuantifiers ,
388
387
@ Nonnull final Set <CorrelationIdentifier > compensatedAliases ,
389
388
@ Nonnull final ResultCompensationFunction resultCompensationFunction ,
390
- @ Nonnull final Map < Value , Value > matchedAggregateValueMap ) {
389
+ @ Nonnull final AggregateMappings aggregateMappings ) {
391
390
//
392
391
// At least one of these conditions must be true:
393
392
// - it is an impossible compensation (in which case the predicate compensation map may be empty)
@@ -400,7 +399,7 @@ default ForMatch derived(final boolean isImpossible,
400
399
!predicateCompensationMap .isEmpty () || resultCompensationFunction .isNeeded () || isNeededForFiltering ());
401
400
402
401
return new ForMatch (isImpossible , this , predicateCompensationMap , matchedQuantifiers ,
403
- unmatchedQuantifiers , compensatedAliases , resultCompensationFunction , matchedAggregateValueMap );
402
+ unmatchedQuantifiers , compensatedAliases , resultCompensationFunction , aggregateMappings );
404
403
}
405
404
406
405
/**
@@ -451,7 +450,7 @@ default boolean isFinalNeeded() {
451
450
ResultCompensationFunction getResultCompensationFunction ();
452
451
453
452
@ Nonnull
454
- Map < Value , Value > getMatchedAggregateValueMap ();
453
+ AggregateMappings getAggregateMappings ();
455
454
456
455
/**
457
456
* Specific implementation of union-ing two compensations both of type {@link WithSelectCompensation}.
@@ -490,6 +489,11 @@ default Compensation union(@Nonnull Compensation otherCompensation) {
490
489
return impossibleCompensation ();
491
490
}
492
491
492
+ final Compensation unionedChildCompensation = getChildCompensation ().union (otherWithSelectCompensation .getChildCompensation ());
493
+ if (unionedChildCompensation .isImpossible () || !unionedChildCompensation .canBeDeferred ()) {
494
+ return Compensation .impossibleCompensation ();
495
+ }
496
+
493
497
final ResultCompensationFunction newResultResultCompensationFunction ;
494
498
final var resultCompensationFunction = getResultCompensationFunction ();
495
499
final var otherResultCompensationFunction = otherWithSelectCompensation .getResultCompensationFunction ();
@@ -503,7 +507,8 @@ default Compensation union(@Nonnull Compensation otherCompensation) {
503
507
newResultResultCompensationFunction = resultCompensationFunction ;
504
508
}
505
509
506
- final var otherCompensationMap = otherWithSelectCompensation .getPredicateCompensationMap ();
510
+ final var otherCompensationMap =
511
+ otherWithSelectCompensation .getPredicateCompensationMap ();
507
512
final var combinedPredicateMap = new LinkedIdentityMap <QueryPredicate , PredicateCompensationFunction >();
508
513
509
514
combinedPredicateMap .putAll (getPredicateCompensationMap ());
@@ -526,11 +531,6 @@ default Compensation union(@Nonnull Compensation otherCompensation) {
526
531
combinedPredicateMap .put (otherEntry .getKey (), otherEntry .getValue ());
527
532
}
528
533
529
- final Compensation unionedChildCompensation = getChildCompensation ().union (otherWithSelectCompensation .getChildCompensation ());
530
- if (unionedChildCompensation .isImpossible () || !unionedChildCompensation .canBeDeferred ()) {
531
- return Compensation .impossibleCompensation ();
532
- }
533
-
534
534
if (!unionedChildCompensation .isNeededForFiltering () &&
535
535
!newResultResultCompensationFunction .isNeeded () && combinedPredicateMap .isEmpty ()) {
536
536
return Compensation .noCompensation ();
@@ -546,7 +546,7 @@ default Compensation union(@Nonnull Compensation otherCompensation) {
546
546
ImmutableSet .of (),
547
547
Sets .union (getCompensatedAliases (), otherWithSelectCompensation .getCompensatedAliases ()),
548
548
newResultResultCompensationFunction ,
549
- ImmutableMap . of ());
549
+ AggregateMappings . empty ());
550
550
}
551
551
552
552
/**
@@ -567,6 +567,23 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
567
567
}
568
568
final var otherWithSelectCompensation = (WithSelectCompensation )otherCompensation ;
569
569
570
+ final Compensation childCompensation = getChildCompensation ();
571
+ Verify .verify (!(childCompensation instanceof WithSelectCompensation ) ||
572
+ ((WithSelectCompensation )childCompensation ).getUnmatchedForEachQuantifiers ().isEmpty ());
573
+
574
+ final Compensation intersectedChildCompensation =
575
+ childCompensation .intersect (otherWithSelectCompensation .getChildCompensation ());
576
+ if (intersectedChildCompensation .isImpossible () || !intersectedChildCompensation .canBeDeferred ()) {
577
+ return Compensation .impossibleCompensation ();
578
+ }
579
+
580
+ final var newMatchedAggregateMap =
581
+ Stream .concat (getAggregateMappings ().getMatchedAggregateMap ().entrySet ().stream (),
582
+ otherWithSelectCompensation .getAggregateMappings ().getMatchedAggregateMap ().entrySet ().stream ())
583
+ .collect (ImmutableMap .toImmutableMap (Map .Entry ::getKey ,
584
+ Map .Entry ::getValue ,
585
+ (l , r ) -> l ));
586
+
570
587
final ResultCompensationFunction newResultResultCompensationFunction ;
571
588
final var resultCompensationFunction = getResultCompensationFunction ();
572
589
final var otherResultCompensationFunction = otherWithSelectCompensation .getResultCompensationFunction ();
@@ -595,16 +612,6 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
595
612
}
596
613
}
597
614
598
- final Compensation childCompensation = getChildCompensation ();
599
- Verify .verify (!(childCompensation instanceof WithSelectCompensation ) ||
600
- ((WithSelectCompensation )childCompensation ).getUnmatchedForEachQuantifiers ().isEmpty ());
601
-
602
- final Compensation intersectedChildCompensation =
603
- childCompensation .intersect (otherWithSelectCompensation .getChildCompensation ());
604
- if (intersectedChildCompensation .isImpossible () || !intersectedChildCompensation .canBeDeferred ()) {
605
- return Compensation .impossibleCompensation ();
606
- }
607
-
608
615
if (!intersectedChildCompensation .isNeededForFiltering () &&
609
616
!newResultResultCompensationFunction .isNeeded () && combinedPredicateMap .isEmpty ()) {
610
617
return Compensation .noCompensation ();
@@ -614,13 +621,6 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
614
621
return intersectedChildCompensation ;
615
622
}
616
623
617
- final var newMatchedAggregateValueMap =
618
- Stream .concat (getMatchedAggregateValueMap ().entrySet ().stream (),
619
- otherWithSelectCompensation .getMatchedAggregateValueMap ().entrySet ().stream ())
620
- .collect (ImmutableMap .toImmutableMap (Map .Entry ::getKey ,
621
- Map .Entry ::getValue ,
622
- (l , r ) -> l ));
623
-
624
624
// Note that at the current time each side can only contribute at most one foreach quantifier, thus the
625
625
// intersection should also only contain at most one for each quantifier.
626
626
final Sets .SetView <Quantifier > intersectedMatchedQuantifiers =
@@ -642,7 +642,7 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
642
642
intersectedUnmatchedQuantifiers ,
643
643
getCompensatedAliases (), // both compensated aliases must be identical, but too expensive to check
644
644
newResultResultCompensationFunction ,
645
- newMatchedAggregateValueMap );
645
+ AggregateMappings . empty () );
646
646
}
647
647
}
648
648
@@ -672,7 +672,7 @@ class ForMatch implements WithSelectCompensation {
672
672
@ Nonnull
673
673
private final ResultCompensationFunction resultCompensationFunction ;
674
674
@ Nonnull
675
- private final Map < Value , Value > matchedAggregateValueMap ;
675
+ private final AggregateMappings aggregateMappings ;
676
676
677
677
@ Nonnull
678
678
private final Supplier <Set <Quantifier >> unmatchedForEachQuantifiersSupplier ;
@@ -684,7 +684,7 @@ private ForMatch(final boolean isImpossible,
684
684
@ Nonnull final Collection <? extends Quantifier > unmatchedQuantifiers ,
685
685
@ Nonnull final Set <CorrelationIdentifier > compensatedAliases ,
686
686
@ Nonnull final ResultCompensationFunction resultCompensationFunction ,
687
- @ Nonnull final Map < Value , Value > matchedAggregateValueMap ) {
687
+ @ Nonnull final AggregateMappings aggregateMappings ) {
688
688
this .isImpossible = isImpossible ;
689
689
this .childCompensation = childCompensation ;
690
690
this .predicateCompensationMap = new LinkedIdentityMap <>();
@@ -695,7 +695,7 @@ private ForMatch(final boolean isImpossible,
695
695
this .unmatchedQuantifiers .addAll (unmatchedQuantifiers );
696
696
this .compensatedAliases = ImmutableSet .copyOf (compensatedAliases );
697
697
this .resultCompensationFunction = resultCompensationFunction ;
698
- this .matchedAggregateValueMap = ImmutableMap . copyOf ( matchedAggregateValueMap ) ;
698
+ this .aggregateMappings = aggregateMappings ;
699
699
this .unmatchedForEachQuantifiersSupplier = Suppliers .memoize (this ::computeUnmatchedForEachQuantifiers );
700
700
}
701
701
@@ -716,7 +716,6 @@ public Set<Quantifier> getMatchedQuantifiers() {
716
716
return matchedQuantifiers ;
717
717
}
718
718
719
-
720
719
@ Nonnull
721
720
@ Override
722
721
public Set <Quantifier > getUnmatchedQuantifiers () {
@@ -756,8 +755,8 @@ public ResultCompensationFunction getResultCompensationFunction() {
756
755
757
756
@ Nonnull
758
757
@ Override
759
- public Map < Value , Value > getMatchedAggregateValueMap () {
760
- return matchedAggregateValueMap ;
758
+ public AggregateMappings getAggregateMappings () {
759
+ return aggregateMappings ;
761
760
}
762
761
763
762
/**
0 commit comments