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,39 @@ 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
+ newUnmatchedAggregatesMapBuilder .put (entry );
621
631
}
622
632
}
623
- final var newAggregateMappings = AggregateMappings .of (newMatchedAggregateMap , newUnmatchedAggregateMapBuilder .build ());
633
+ final var newGroupByMappings = GroupByMappings .of (newMatchedGroupingsMapBuilder .build (),
634
+ newMatchedAggregatesMap , newUnmatchedAggregatesMapBuilder .build ());
624
635
625
636
final ResultCompensationFunction newResultResultCompensationFunction ;
626
637
final var resultCompensationFunction = getResultCompensationFunction ();
@@ -633,7 +644,7 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
633
644
Verify .verify (otherResultCompensationFunction .isNeeded ());
634
645
// pick the one from this side -- it does not matter as both candidates have the same shape
635
646
newResultResultCompensationFunction =
636
- resultCompensationFunction .amend (unmatchedAggregateMap , newMatchedAggregateMap );
647
+ resultCompensationFunction .amend (unmatchedAggregateMap , newMatchedAggregatesMap );
637
648
}
638
649
639
650
final var otherCompensationMap =
@@ -651,7 +662,7 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
651
662
// either compensation and let the planner figure out which one wins. We just pick one side here.
652
663
// 2. TODO.
653
664
combinedPredicateMap .put (entry .getKey (),
654
- entry .getValue ().amend (unmatchedAggregateMap , newMatchedAggregateMap ));
665
+ entry .getValue ().amend (unmatchedAggregateMap , newMatchedAggregatesMap ));
655
666
}
656
667
}
657
668
@@ -685,7 +696,7 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
685
696
intersectedUnmatchedQuantifiers ,
686
697
getCompensatedAliases (), // both compensated aliases must be identical, but too expensive to check
687
698
newResultResultCompensationFunction ,
688
- newAggregateMappings );
699
+ newGroupByMappings );
689
700
}
690
701
}
691
702
@@ -715,7 +726,7 @@ class ForMatch implements WithSelectCompensation {
715
726
@ Nonnull
716
727
private final ResultCompensationFunction resultCompensationFunction ;
717
728
@ Nonnull
718
- private final AggregateMappings aggregateMappings ;
729
+ private final GroupByMappings groupByMappings ;
719
730
720
731
@ Nonnull
721
732
private final Supplier <Set <Quantifier >> unmatchedForEachQuantifiersSupplier ;
@@ -727,7 +738,7 @@ private ForMatch(final boolean isImpossible,
727
738
@ Nonnull final Collection <? extends Quantifier > unmatchedQuantifiers ,
728
739
@ Nonnull final Set <CorrelationIdentifier > compensatedAliases ,
729
740
@ Nonnull final ResultCompensationFunction resultCompensationFunction ,
730
- @ Nonnull final AggregateMappings aggregateMappings ) {
741
+ @ Nonnull final GroupByMappings groupByMappings ) {
731
742
this .isImpossible = isImpossible ;
732
743
this .childCompensation = childCompensation ;
733
744
this .predicateCompensationMap = new LinkedIdentityMap <>();
@@ -738,7 +749,7 @@ private ForMatch(final boolean isImpossible,
738
749
this .unmatchedQuantifiers .addAll (unmatchedQuantifiers );
739
750
this .compensatedAliases = ImmutableSet .copyOf (compensatedAliases );
740
751
this .resultCompensationFunction = resultCompensationFunction ;
741
- this .aggregateMappings = aggregateMappings ;
752
+ this .groupByMappings = groupByMappings ;
742
753
this .unmatchedForEachQuantifiersSupplier = Suppliers .memoize (this ::computeUnmatchedForEachQuantifiers );
743
754
}
744
755
@@ -798,8 +809,8 @@ public ResultCompensationFunction getResultCompensationFunction() {
798
809
799
810
@ Nonnull
800
811
@ Override
801
- public AggregateMappings getAggregateMappings () {
802
- return aggregateMappings ;
812
+ public GroupByMappings getGroupByMappings () {
813
+ return groupByMappings ;
803
814
}
804
815
805
816
/**
0 commit comments