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 ;
@@ -392,7 +391,7 @@ default ForMatch derived(final boolean isImpossible,
392
391
@ Nonnull final Set <? extends Quantifier > unmatchedQuantifiers ,
393
392
@ Nonnull final Set <CorrelationIdentifier > compensatedAliases ,
394
393
@ Nonnull final ResultCompensationFunction resultCompensationFunction ,
395
- @ Nonnull final Map < Value , Value > matchedAggregateValueMap ) {
394
+ @ Nonnull final AggregateMappings aggregateMappings ) {
396
395
//
397
396
// At least one of these conditions must be true:
398
397
// - it is an impossible compensation (in which case the predicate compensation map may be empty)
@@ -405,7 +404,7 @@ default ForMatch derived(final boolean isImpossible,
405
404
!predicateCompensationMap .isEmpty () || resultCompensationFunction .isNeeded () || isNeededForFiltering ());
406
405
407
406
return new ForMatch (isImpossible , this , predicateCompensationMap , matchedQuantifiers ,
408
- unmatchedQuantifiers , compensatedAliases , resultCompensationFunction , matchedAggregateValueMap );
407
+ unmatchedQuantifiers , compensatedAliases , resultCompensationFunction , aggregateMappings );
409
408
}
410
409
411
410
/**
@@ -456,7 +455,7 @@ default boolean isFinalNeeded() {
456
455
ResultCompensationFunction getResultCompensationFunction ();
457
456
458
457
@ Nonnull
459
- Map < Value , Value > getMatchedAggregateValueMap ();
458
+ AggregateMappings getAggregateMappings ();
460
459
461
460
/**
462
461
* Specific implementation of union-ing two compensations both of type {@link WithSelectCompensation}.
@@ -495,6 +494,11 @@ default Compensation union(@Nonnull Compensation otherCompensation) {
495
494
return impossibleCompensation ();
496
495
}
497
496
497
+ final Compensation unionedChildCompensation = getChildCompensation ().union (otherWithSelectCompensation .getChildCompensation ());
498
+ if (unionedChildCompensation .isImpossible () || !unionedChildCompensation .canBeDeferred ()) {
499
+ return Compensation .impossibleCompensation ();
500
+ }
501
+
498
502
final ResultCompensationFunction newResultResultCompensationFunction ;
499
503
final var resultCompensationFunction = getResultCompensationFunction ();
500
504
final var otherResultCompensationFunction = otherWithSelectCompensation .getResultCompensationFunction ();
@@ -508,7 +512,8 @@ default Compensation union(@Nonnull Compensation otherCompensation) {
508
512
newResultResultCompensationFunction = resultCompensationFunction ;
509
513
}
510
514
511
- final var otherCompensationMap = otherWithSelectCompensation .getPredicateCompensationMap ();
515
+ final var otherCompensationMap =
516
+ otherWithSelectCompensation .getPredicateCompensationMap ();
512
517
final var combinedPredicateMap = new LinkedIdentityMap <QueryPredicate , PredicateCompensationFunction >();
513
518
514
519
combinedPredicateMap .putAll (getPredicateCompensationMap ());
@@ -531,11 +536,6 @@ default Compensation union(@Nonnull Compensation otherCompensation) {
531
536
combinedPredicateMap .put (otherEntry .getKey (), otherEntry .getValue ());
532
537
}
533
538
534
- final Compensation unionedChildCompensation = getChildCompensation ().union (otherWithSelectCompensation .getChildCompensation ());
535
- if (unionedChildCompensation .isImpossible () || !unionedChildCompensation .canBeDeferred ()) {
536
- return Compensation .impossibleCompensation ();
537
- }
538
-
539
539
if (!unionedChildCompensation .isNeededForFiltering () &&
540
540
!newResultResultCompensationFunction .isNeeded () && combinedPredicateMap .isEmpty ()) {
541
541
return Compensation .noCompensation ();
@@ -551,7 +551,7 @@ default Compensation union(@Nonnull Compensation otherCompensation) {
551
551
ImmutableSet .of (),
552
552
Sets .union (getCompensatedAliases (), otherWithSelectCompensation .getCompensatedAliases ()),
553
553
newResultResultCompensationFunction ,
554
- ImmutableMap . of ());
554
+ AggregateMappings . empty ());
555
555
}
556
556
557
557
/**
@@ -572,6 +572,23 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
572
572
}
573
573
final var otherWithSelectCompensation = (WithSelectCompensation )otherCompensation ;
574
574
575
+ final Compensation childCompensation = getChildCompensation ();
576
+ Verify .verify (!(childCompensation instanceof WithSelectCompensation ) ||
577
+ ((WithSelectCompensation )childCompensation ).getUnmatchedForEachQuantifiers ().isEmpty ());
578
+
579
+ final Compensation intersectedChildCompensation =
580
+ childCompensation .intersect (otherWithSelectCompensation .getChildCompensation ());
581
+ if (intersectedChildCompensation .isImpossible () || !intersectedChildCompensation .canBeDeferred ()) {
582
+ return Compensation .impossibleCompensation ();
583
+ }
584
+
585
+ final var newMatchedAggregateMap =
586
+ Stream .concat (getAggregateMappings ().getMatchedAggregateMap ().entrySet ().stream (),
587
+ otherWithSelectCompensation .getAggregateMappings ().getMatchedAggregateMap ().entrySet ().stream ())
588
+ .collect (ImmutableMap .toImmutableMap (Map .Entry ::getKey ,
589
+ Map .Entry ::getValue ,
590
+ (l , r ) -> l ));
591
+
575
592
final ResultCompensationFunction newResultResultCompensationFunction ;
576
593
final var resultCompensationFunction = getResultCompensationFunction ();
577
594
final var otherResultCompensationFunction = otherWithSelectCompensation .getResultCompensationFunction ();
@@ -600,16 +617,6 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
600
617
}
601
618
}
602
619
603
- final Compensation childCompensation = getChildCompensation ();
604
- Verify .verify (!(childCompensation instanceof WithSelectCompensation ) ||
605
- ((WithSelectCompensation )childCompensation ).getUnmatchedForEachQuantifiers ().isEmpty ());
606
-
607
- final Compensation intersectedChildCompensation =
608
- childCompensation .intersect (otherWithSelectCompensation .getChildCompensation ());
609
- if (intersectedChildCompensation .isImpossible () || !intersectedChildCompensation .canBeDeferred ()) {
610
- return Compensation .impossibleCompensation ();
611
- }
612
-
613
620
if (!intersectedChildCompensation .isNeededForFiltering () &&
614
621
!newResultResultCompensationFunction .isNeeded () && combinedPredicateMap .isEmpty ()) {
615
622
return Compensation .noCompensation ();
@@ -619,13 +626,6 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
619
626
return intersectedChildCompensation ;
620
627
}
621
628
622
- final var newMatchedAggregateValueMap =
623
- Stream .concat (getMatchedAggregateValueMap ().entrySet ().stream (),
624
- otherWithSelectCompensation .getMatchedAggregateValueMap ().entrySet ().stream ())
625
- .collect (ImmutableMap .toImmutableMap (Map .Entry ::getKey ,
626
- Map .Entry ::getValue ,
627
- (l , r ) -> l ));
628
-
629
629
// Note that at the current time each side can only contribute at most one foreach quantifier, thus the
630
630
// intersection should also only contain at most one for each quantifier.
631
631
final Sets .SetView <Quantifier > intersectedMatchedQuantifiers =
@@ -647,7 +647,7 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
647
647
intersectedUnmatchedQuantifiers ,
648
648
getCompensatedAliases (), // both compensated aliases must be identical, but too expensive to check
649
649
newResultResultCompensationFunction ,
650
- newMatchedAggregateValueMap );
650
+ AggregateMappings . empty () );
651
651
}
652
652
}
653
653
@@ -677,7 +677,7 @@ class ForMatch implements WithSelectCompensation {
677
677
@ Nonnull
678
678
private final ResultCompensationFunction resultCompensationFunction ;
679
679
@ Nonnull
680
- private final Map < Value , Value > matchedAggregateValueMap ;
680
+ private final AggregateMappings aggregateMappings ;
681
681
682
682
@ Nonnull
683
683
private final Supplier <Set <Quantifier >> unmatchedForEachQuantifiersSupplier ;
@@ -689,7 +689,7 @@ private ForMatch(final boolean isImpossible,
689
689
@ Nonnull final Collection <? extends Quantifier > unmatchedQuantifiers ,
690
690
@ Nonnull final Set <CorrelationIdentifier > compensatedAliases ,
691
691
@ Nonnull final ResultCompensationFunction resultCompensationFunction ,
692
- @ Nonnull final Map < Value , Value > matchedAggregateValueMap ) {
692
+ @ Nonnull final AggregateMappings aggregateMappings ) {
693
693
this .isImpossible = isImpossible ;
694
694
this .childCompensation = childCompensation ;
695
695
this .predicateCompensationMap = new LinkedIdentityMap <>();
@@ -700,7 +700,7 @@ private ForMatch(final boolean isImpossible,
700
700
this .unmatchedQuantifiers .addAll (unmatchedQuantifiers );
701
701
this .compensatedAliases = ImmutableSet .copyOf (compensatedAliases );
702
702
this .resultCompensationFunction = resultCompensationFunction ;
703
- this .matchedAggregateValueMap = ImmutableMap . copyOf ( matchedAggregateValueMap ) ;
703
+ this .aggregateMappings = aggregateMappings ;
704
704
this .unmatchedForEachQuantifiersSupplier = Suppliers .memoize (this ::computeUnmatchedForEachQuantifiers );
705
705
}
706
706
@@ -721,7 +721,6 @@ public Set<Quantifier> getMatchedQuantifiers() {
721
721
return matchedQuantifiers ;
722
722
}
723
723
724
-
725
724
@ Nonnull
726
725
@ Override
727
726
public Set <Quantifier > getUnmatchedQuantifiers () {
@@ -761,8 +760,8 @@ public ResultCompensationFunction getResultCompensationFunction() {
761
760
762
761
@ Nonnull
763
762
@ Override
764
- public Map < Value , Value > getMatchedAggregateValueMap () {
765
- return matchedAggregateValueMap ;
763
+ public AggregateMappings getAggregateMappings () {
764
+ return aggregateMappings ;
766
765
}
767
766
768
767
/**
0 commit comments