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.
@@ -410,7 +408,7 @@ default ForMatch derived(final boolean isImpossible,
410
408
@ Nonnull final Set <? extends Quantifier > unmatchedQuantifiers ,
411
409
@ Nonnull final Set <CorrelationIdentifier > compensatedAliases ,
412
410
@ Nonnull final ResultCompensationFunction resultCompensationFunction ,
413
- @ Nonnull final AggregateMappings aggregateMappings ) {
411
+ @ Nonnull final GroupByMappings groupByMappings ) {
414
412
//
415
413
// At least one of these conditions must be true:
416
414
// - it is an impossible compensation (in which case the predicate compensation map may be empty)
@@ -423,7 +421,7 @@ default ForMatch derived(final boolean isImpossible,
423
421
!predicateCompensationMap .isEmpty () || resultCompensationFunction .isNeeded () || isNeededForFiltering ());
424
422
425
423
return new ForMatch (isImpossible , this , predicateCompensationMap , matchedQuantifiers ,
426
- unmatchedQuantifiers , compensatedAliases , resultCompensationFunction , aggregateMappings );
424
+ unmatchedQuantifiers , compensatedAliases , resultCompensationFunction , groupByMappings );
427
425
}
428
426
429
427
/**
@@ -474,7 +472,7 @@ default boolean isFinalNeeded() {
474
472
ResultCompensationFunction getResultCompensationFunction ();
475
473
476
474
@ Nonnull
477
- AggregateMappings getAggregateMappings ();
475
+ GroupByMappings getGroupByMappings ();
478
476
479
477
/**
480
478
* Specific implementation of union-ing two compensations both of type {@link WithSelectCompensation}.
@@ -570,7 +568,7 @@ default Compensation union(@Nonnull Compensation otherCompensation) {
570
568
ImmutableSet .of (),
571
569
Sets .union (getCompensatedAliases (), otherWithSelectCompensation .getCompensatedAliases ()),
572
570
newResultResultCompensationFunction ,
573
- AggregateMappings .empty ());
571
+ GroupByMappings .empty ());
574
572
}
575
573
576
574
/**
@@ -601,26 +599,40 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
601
599
return Compensation .impossibleCompensation ();
602
600
}
603
601
604
- final var newMatchedAggregateMap =
605
- Stream .concat (getAggregateMappings ().getMatchedAggregateMap ().entrySet ().stream (),
606
- otherWithSelectCompensation .getAggregateMappings ().getMatchedAggregateMap ().entrySet ().stream ())
607
- .collect (ImmutableMap .toImmutableMap (Map .Entry ::getKey ,
608
- Map .Entry ::getValue ,
609
- (l , r ) -> l ));
610
- final var newUnmatchedAggregateMapBuilder =
602
+ final var newMatchedGroupingsMapBuilder = ImmutableBiMap .<Value , Value >builder ();
603
+ final var matchedGroupingsMap = getGroupByMappings ().getMatchedGroupingsMap ();
604
+ newMatchedGroupingsMapBuilder .putAll (matchedGroupingsMap );
605
+ for (final var entry : otherWithSelectCompensation .getGroupByMappings ().getMatchedGroupingsMap ().entrySet ()) {
606
+ if (!matchedGroupingsMap .containsKey (entry .getKey ())) {
607
+ newMatchedGroupingsMapBuilder .put (entry );
608
+ }
609
+ }
610
+
611
+ final var newMatchedAggregatesMapBuilder = ImmutableBiMap .<Value , Value >builder ();
612
+ final var matchedAggregatesMap = getGroupByMappings ().getMatchedAggregatesMap ();
613
+ newMatchedAggregatesMapBuilder .putAll (matchedAggregatesMap );
614
+ for (final var entry : otherWithSelectCompensation .getGroupByMappings ().getMatchedAggregatesMap ().entrySet ()) {
615
+ if (!matchedAggregatesMap .containsKey (entry .getKey ())) {
616
+ newMatchedAggregatesMapBuilder .put (entry );
617
+ }
618
+ }
619
+ final var newMatchedAggregatesMap = newMatchedAggregatesMapBuilder .build ();
620
+ final var newUnmatchedAggregatesMapBuilder =
611
621
ImmutableBiMap .<CorrelationIdentifier , Value >builder ();
612
- final var unmatchedAggregateMap = getAggregateMappings ().getUnmatchedAggregateMap ();
622
+ final var unmatchedAggregateMap = getGroupByMappings ().getUnmatchedAggregatesMap ();
613
623
for (final var entry : unmatchedAggregateMap .entrySet ()) {
614
- if (!newMatchedAggregateMap .containsKey (entry .getValue ())) {
615
- newUnmatchedAggregateMapBuilder .put (entry );
624
+ if (!newMatchedAggregatesMap .containsKey (entry .getValue ())) {
625
+ newUnmatchedAggregatesMapBuilder .put (entry );
616
626
}
617
627
}
618
- for (final var entry : otherWithSelectCompensation .getAggregateMappings ().getUnmatchedAggregateMap ().entrySet ()) {
619
- if (!newMatchedAggregateMap .containsKey (entry .getValue ())) {
620
- newUnmatchedAggregateMapBuilder .put (entry );
628
+ for (final var entry : otherWithSelectCompensation .getGroupByMappings ().getUnmatchedAggregatesMap ().entrySet ()) {
629
+ if (!newMatchedAggregatesMap .containsKey (entry .getValue ()) &&
630
+ !unmatchedAggregateMap .inverse ().containsKey (entry .getValue ())) {
631
+ newUnmatchedAggregatesMapBuilder .put (entry );
621
632
}
622
633
}
623
- final var newAggregateMappings = AggregateMappings .of (newMatchedAggregateMap , newUnmatchedAggregateMapBuilder .build ());
634
+ final var newGroupByMappings = GroupByMappings .of (newMatchedGroupingsMapBuilder .build (),
635
+ newMatchedAggregatesMap , newUnmatchedAggregatesMapBuilder .build ());
624
636
625
637
final ResultCompensationFunction newResultResultCompensationFunction ;
626
638
final var resultCompensationFunction = getResultCompensationFunction ();
@@ -633,7 +645,7 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
633
645
Verify .verify (otherResultCompensationFunction .isNeeded ());
634
646
// pick the one from this side -- it does not matter as both candidates have the same shape
635
647
newResultResultCompensationFunction =
636
- resultCompensationFunction .amend (unmatchedAggregateMap , newMatchedAggregateMap );
648
+ resultCompensationFunction .amend (unmatchedAggregateMap , newMatchedAggregatesMap );
637
649
}
638
650
639
651
final var otherCompensationMap =
@@ -651,7 +663,7 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
651
663
// either compensation and let the planner figure out which one wins. We just pick one side here.
652
664
// 2. TODO.
653
665
combinedPredicateMap .put (entry .getKey (),
654
- entry .getValue ().amend (unmatchedAggregateMap , newMatchedAggregateMap ));
666
+ entry .getValue ().amend (unmatchedAggregateMap , newMatchedAggregatesMap ));
655
667
}
656
668
}
657
669
@@ -685,7 +697,7 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
685
697
intersectedUnmatchedQuantifiers ,
686
698
getCompensatedAliases (), // both compensated aliases must be identical, but too expensive to check
687
699
newResultResultCompensationFunction ,
688
- newAggregateMappings );
700
+ newGroupByMappings );
689
701
}
690
702
}
691
703
@@ -715,7 +727,7 @@ class ForMatch implements WithSelectCompensation {
715
727
@ Nonnull
716
728
private final ResultCompensationFunction resultCompensationFunction ;
717
729
@ Nonnull
718
- private final AggregateMappings aggregateMappings ;
730
+ private final GroupByMappings groupByMappings ;
719
731
720
732
@ Nonnull
721
733
private final Supplier <Set <Quantifier >> unmatchedForEachQuantifiersSupplier ;
@@ -727,7 +739,7 @@ private ForMatch(final boolean isImpossible,
727
739
@ Nonnull final Collection <? extends Quantifier > unmatchedQuantifiers ,
728
740
@ Nonnull final Set <CorrelationIdentifier > compensatedAliases ,
729
741
@ Nonnull final ResultCompensationFunction resultCompensationFunction ,
730
- @ Nonnull final AggregateMappings aggregateMappings ) {
742
+ @ Nonnull final GroupByMappings groupByMappings ) {
731
743
this .isImpossible = isImpossible ;
732
744
this .childCompensation = childCompensation ;
733
745
this .predicateCompensationMap = new LinkedIdentityMap <>();
@@ -738,7 +750,7 @@ private ForMatch(final boolean isImpossible,
738
750
this .unmatchedQuantifiers .addAll (unmatchedQuantifiers );
739
751
this .compensatedAliases = ImmutableSet .copyOf (compensatedAliases );
740
752
this .resultCompensationFunction = resultCompensationFunction ;
741
- this .aggregateMappings = aggregateMappings ;
753
+ this .groupByMappings = groupByMappings ;
742
754
this .unmatchedForEachQuantifiersSupplier = Suppliers .memoize (this ::computeUnmatchedForEachQuantifiers );
743
755
}
744
756
@@ -798,8 +810,8 @@ public ResultCompensationFunction getResultCompensationFunction() {
798
810
799
811
@ Nonnull
800
812
@ Override
801
- public AggregateMappings getAggregateMappings () {
802
- return aggregateMappings ;
813
+ public GroupByMappings getGroupByMappings () {
814
+ return groupByMappings ;
803
815
}
804
816
805
817
/**
0 commit comments