32
32
import com .google .common .base .Verify ;
33
33
import com .google .common .collect .ImmutableBiMap ;
34
34
import com .google .common .collect .ImmutableList ;
35
- import com .google .common .collect .ImmutableMap ;
36
35
import com .google .common .collect .ImmutableSet ;
37
36
import com .google .common .collect .Iterables ;
38
37
import com .google .common .collect .Sets ;
43
42
import java .util .Set ;
44
43
import java .util .function .Function ;
45
44
import java .util .function .Supplier ;
46
- import java .util .stream .Stream ;
47
45
48
46
/**
49
47
* Interface for all kinds of compensation. A compensation is the byproduct of expression DAG matching.
@@ -415,7 +413,7 @@ default ForMatch derived(final boolean isImpossible,
415
413
@ Nonnull final Set <? extends Quantifier > unmatchedQuantifiers ,
416
414
@ Nonnull final Set <CorrelationIdentifier > compensatedAliases ,
417
415
@ Nonnull final ResultCompensationFunction resultCompensationFunction ,
418
- @ Nonnull final AggregateMappings aggregateMappings ) {
416
+ @ Nonnull final GroupByMappings groupByMappings ) {
419
417
//
420
418
// At least one of these conditions must be true:
421
419
// - it is an impossible compensation (in which case the predicate compensation map may be empty)
@@ -428,7 +426,7 @@ default ForMatch derived(final boolean isImpossible,
428
426
!predicateCompensationMap .isEmpty () || resultCompensationFunction .isNeeded () || isNeededForFiltering ());
429
427
430
428
return new ForMatch (isImpossible , this , predicateCompensationMap , matchedQuantifiers ,
431
- unmatchedQuantifiers , compensatedAliases , resultCompensationFunction , aggregateMappings );
429
+ unmatchedQuantifiers , compensatedAliases , resultCompensationFunction , groupByMappings );
432
430
}
433
431
434
432
/**
@@ -479,7 +477,7 @@ default boolean isFinalNeeded() {
479
477
ResultCompensationFunction getResultCompensationFunction ();
480
478
481
479
@ Nonnull
482
- AggregateMappings getAggregateMappings ();
480
+ GroupByMappings getGroupByMappings ();
483
481
484
482
/**
485
483
* Specific implementation of union-ing two compensations both of type {@link WithSelectCompensation}.
@@ -575,7 +573,7 @@ default Compensation union(@Nonnull Compensation otherCompensation) {
575
573
ImmutableSet .of (),
576
574
Sets .union (getCompensatedAliases (), otherWithSelectCompensation .getCompensatedAliases ()),
577
575
newResultResultCompensationFunction ,
578
- AggregateMappings .empty ());
576
+ GroupByMappings .empty ());
579
577
}
580
578
581
579
/**
@@ -606,26 +604,40 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
606
604
return Compensation .impossibleCompensation ();
607
605
}
608
606
609
- final var newMatchedAggregateMap =
610
- Stream .concat (getAggregateMappings ().getMatchedAggregateMap ().entrySet ().stream (),
611
- otherWithSelectCompensation .getAggregateMappings ().getMatchedAggregateMap ().entrySet ().stream ())
612
- .collect (ImmutableMap .toImmutableMap (Map .Entry ::getKey ,
613
- Map .Entry ::getValue ,
614
- (l , r ) -> l ));
615
- final var newUnmatchedAggregateMapBuilder =
607
+ final var newMatchedGroupingsMapBuilder = ImmutableBiMap .<Value , Value >builder ();
608
+ final var matchedGroupingsMap = getGroupByMappings ().getMatchedGroupingsMap ();
609
+ newMatchedGroupingsMapBuilder .putAll (matchedGroupingsMap );
610
+ for (final var entry : otherWithSelectCompensation .getGroupByMappings ().getMatchedGroupingsMap ().entrySet ()) {
611
+ if (!matchedGroupingsMap .containsKey (entry .getKey ())) {
612
+ newMatchedGroupingsMapBuilder .put (entry );
613
+ }
614
+ }
615
+
616
+ final var newMatchedAggregatesMapBuilder = ImmutableBiMap .<Value , Value >builder ();
617
+ final var matchedAggregatesMap = getGroupByMappings ().getMatchedAggregatesMap ();
618
+ newMatchedAggregatesMapBuilder .putAll (matchedAggregatesMap );
619
+ for (final var entry : otherWithSelectCompensation .getGroupByMappings ().getMatchedAggregatesMap ().entrySet ()) {
620
+ if (!matchedAggregatesMap .containsKey (entry .getKey ())) {
621
+ newMatchedAggregatesMapBuilder .put (entry );
622
+ }
623
+ }
624
+ final var newMatchedAggregatesMap = newMatchedAggregatesMapBuilder .build ();
625
+ final var newUnmatchedAggregatesMapBuilder =
616
626
ImmutableBiMap .<CorrelationIdentifier , Value >builder ();
617
- final var unmatchedAggregateMap = getAggregateMappings ().getUnmatchedAggregateMap ();
627
+ final var unmatchedAggregateMap = getGroupByMappings ().getUnmatchedAggregatesMap ();
618
628
for (final var entry : unmatchedAggregateMap .entrySet ()) {
619
- if (!newMatchedAggregateMap .containsKey (entry .getValue ())) {
620
- newUnmatchedAggregateMapBuilder .put (entry );
629
+ if (!newMatchedAggregatesMap .containsKey (entry .getValue ())) {
630
+ newUnmatchedAggregatesMapBuilder .put (entry );
621
631
}
622
632
}
623
- for (final var entry : otherWithSelectCompensation .getAggregateMappings ().getUnmatchedAggregateMap ().entrySet ()) {
624
- if (!newMatchedAggregateMap .containsKey (entry .getValue ())) {
625
- newUnmatchedAggregateMapBuilder .put (entry );
633
+ for (final var entry : otherWithSelectCompensation .getGroupByMappings ().getUnmatchedAggregatesMap ().entrySet ()) {
634
+ if (!newMatchedAggregatesMap .containsKey (entry .getValue ()) &&
635
+ !unmatchedAggregateMap .inverse ().containsKey (entry .getValue ())) {
636
+ newUnmatchedAggregatesMapBuilder .put (entry );
626
637
}
627
638
}
628
- final var newAggregateMappings = AggregateMappings .of (newMatchedAggregateMap , newUnmatchedAggregateMapBuilder .build ());
639
+ final var newGroupByMappings = GroupByMappings .of (newMatchedGroupingsMapBuilder .build (),
640
+ newMatchedAggregatesMap , newUnmatchedAggregatesMapBuilder .build ());
629
641
630
642
final ResultCompensationFunction newResultResultCompensationFunction ;
631
643
final var resultCompensationFunction = getResultCompensationFunction ();
@@ -638,7 +650,7 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
638
650
Verify .verify (otherResultCompensationFunction .isNeeded ());
639
651
// pick the one from this side -- it does not matter as both candidates have the same shape
640
652
newResultResultCompensationFunction =
641
- resultCompensationFunction .amend (unmatchedAggregateMap , newMatchedAggregateMap );
653
+ resultCompensationFunction .amend (unmatchedAggregateMap , newMatchedAggregatesMap );
642
654
}
643
655
644
656
final var otherCompensationMap =
@@ -656,7 +668,7 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
656
668
// either compensation and let the planner figure out which one wins. We just pick one side here.
657
669
// 2. TODO.
658
670
combinedPredicateMap .put (entry .getKey (),
659
- entry .getValue ().amend (unmatchedAggregateMap , newMatchedAggregateMap ));
671
+ entry .getValue ().amend (unmatchedAggregateMap , newMatchedAggregatesMap ));
660
672
}
661
673
}
662
674
@@ -690,7 +702,7 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
690
702
intersectedUnmatchedQuantifiers ,
691
703
getCompensatedAliases (), // both compensated aliases must be identical, but too expensive to check
692
704
newResultResultCompensationFunction ,
693
- newAggregateMappings );
705
+ newGroupByMappings );
694
706
}
695
707
}
696
708
@@ -720,7 +732,7 @@ class ForMatch implements WithSelectCompensation {
720
732
@ Nonnull
721
733
private final ResultCompensationFunction resultCompensationFunction ;
722
734
@ Nonnull
723
- private final AggregateMappings aggregateMappings ;
735
+ private final GroupByMappings groupByMappings ;
724
736
725
737
@ Nonnull
726
738
private final Supplier <Set <Quantifier >> unmatchedForEachQuantifiersSupplier ;
@@ -732,7 +744,7 @@ private ForMatch(final boolean isImpossible,
732
744
@ Nonnull final Collection <? extends Quantifier > unmatchedQuantifiers ,
733
745
@ Nonnull final Set <CorrelationIdentifier > compensatedAliases ,
734
746
@ Nonnull final ResultCompensationFunction resultCompensationFunction ,
735
- @ Nonnull final AggregateMappings aggregateMappings ) {
747
+ @ Nonnull final GroupByMappings groupByMappings ) {
736
748
this .isImpossible = isImpossible ;
737
749
this .childCompensation = childCompensation ;
738
750
this .predicateCompensationMap = new LinkedIdentityMap <>();
@@ -743,7 +755,7 @@ private ForMatch(final boolean isImpossible,
743
755
this .unmatchedQuantifiers .addAll (unmatchedQuantifiers );
744
756
this .compensatedAliases = ImmutableSet .copyOf (compensatedAliases );
745
757
this .resultCompensationFunction = resultCompensationFunction ;
746
- this .aggregateMappings = aggregateMappings ;
758
+ this .groupByMappings = groupByMappings ;
747
759
this .unmatchedForEachQuantifiersSupplier = Suppliers .memoize (this ::computeUnmatchedForEachQuantifiers );
748
760
}
749
761
@@ -803,8 +815,8 @@ public ResultCompensationFunction getResultCompensationFunction() {
803
815
804
816
@ Nonnull
805
817
@ Override
806
- public AggregateMappings getAggregateMappings () {
807
- return aggregateMappings ;
818
+ public GroupByMappings getGroupByMappings () {
819
+ return groupByMappings ;
808
820
}
809
821
810
822
/**
0 commit comments