26
26
import com .apple .foundationdb .record .query .plan .cascades .expressions .LogicalFilterExpression ;
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
- import com .apple .foundationdb .record .query .plan .cascades .rules .DataAccessRule ;
30
29
import com .apple .foundationdb .record .query .plan .cascades .values .Value ;
30
+ import com .apple .foundationdb .record .query .plan .cascades .values .translation .TranslationMap ;
31
31
import com .google .common .base .Suppliers ;
32
32
import com .google .common .base .Verify ;
33
33
import com .google .common .collect .ImmutableBiMap ;
41
41
import java .util .Collection ;
42
42
import java .util .Map ;
43
43
import java .util .Set ;
44
+ import java .util .function .Function ;
44
45
import java .util .function .Supplier ;
45
46
import java .util .stream .Stream ;
46
47
107
108
* <br>
108
109
* Compensation is computed either during the matching process or is computed after a complete match has been found
109
110
* utilizing helper structures such as {@link PartialMatch} and {@link MatchInfo}, which are themselves
110
- * built during matching. Logic in
111
- * {@link DataAccessRule} computes and applies compensation
112
- * as needed when a complete index match has been found.
111
+ * built during matching. Logic in the data access rules computes and applies compensation as needed when a complete
112
+ * index match has been found.
113
113
* <br>
114
114
* A query sub graph can have multiple matches that could be utilized. In the example above, another index on {@code b}
115
115
* would also match but use {@code b} for the index scan and a predicate {@code WHERE a = 3}. Both match the query,
@@ -143,14 +143,16 @@ public Compensation intersect(@Nonnull final Compensation otherCompensation) {
143
143
@ Nonnull
144
144
@ Override
145
145
public RelationalExpression apply (@ Nonnull final Memoizer memoizer ,
146
- @ Nonnull final RelationalExpression relationalExpression ) {
146
+ @ Nonnull final RelationalExpression relationalExpression ,
147
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
147
148
throw new RecordCoreException ("this method should not be called" );
148
149
}
149
150
150
151
@ Nonnull
151
152
@ Override
152
153
public RelationalExpression applyFinal (@ Nonnull final Memoizer memoizer ,
153
- @ Nonnull final RelationalExpression relationalExpression ) {
154
+ @ Nonnull final RelationalExpression relationalExpression ,
155
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
154
156
throw new RecordCoreException ("this method should not be called" );
155
157
}
156
158
};
@@ -187,26 +189,29 @@ public Compensation intersect(@Nonnull final Compensation otherCompensation) {
187
189
@ Nonnull
188
190
@ Override
189
191
public RelationalExpression apply (@ Nonnull final Memoizer memoizer ,
190
- @ Nonnull final RelationalExpression relationalExpression ) {
192
+ @ Nonnull final RelationalExpression relationalExpression ,
193
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
191
194
throw new RecordCoreException ("this method should not be called" );
192
195
}
193
196
194
197
@ Nonnull
195
198
@ Override
196
199
public RelationalExpression applyFinal (@ Nonnull final Memoizer memoizer ,
197
- @ Nonnull final RelationalExpression relationalExpression ) {
200
+ @ Nonnull final RelationalExpression relationalExpression ,
201
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
198
202
throw new RecordCoreException ("this method should not be called" );
199
203
}
200
204
};
201
205
202
206
@ Nonnull
203
207
default RelationalExpression applyAllNeededCompensations (@ Nonnull final Memoizer memoizer ,
204
- @ Nonnull RelationalExpression relationalExpression ) {
208
+ @ Nonnull RelationalExpression relationalExpression ,
209
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
205
210
if (isNeededForFiltering ()) {
206
- relationalExpression = apply (memoizer , relationalExpression );
211
+ relationalExpression = apply (memoizer , relationalExpression , matchedToRealizedTranslationMapFunction );
207
212
}
208
213
if (isFinalNeeded ()) {
209
- relationalExpression = applyFinal (memoizer , relationalExpression );
214
+ relationalExpression = applyFinal (memoizer , relationalExpression , matchedToRealizedTranslationMapFunction );
210
215
}
211
216
212
217
return relationalExpression ;
@@ -218,23 +223,31 @@ default RelationalExpression applyAllNeededCompensations(@Nonnull final Memoizer
218
223
* {@link WithSelectCompensation}.
219
224
* @param memoizer the memoizer for new {@link Reference}s
220
225
* @param relationalExpression root of graph to apply compensation to
226
+ * @param matchedToRealizedTranslationMapFunction a function that given an alias for the quantifier over the
227
+ * realized compensation, returns a {@link TranslationMap} that is then used to translate
228
+ * from {@link QueryPredicate}s and {@link Value}s using matched aliases to the realized alias
221
229
* @return a new relational expression that corrects the result of {@code reference} by applying appropriate
222
230
* filters and/or transformations
223
231
*/
224
232
@ Nonnull
225
- RelationalExpression apply (@ Nonnull Memoizer memoizer , @ Nonnull RelationalExpression relationalExpression );
233
+ RelationalExpression apply (@ Nonnull Memoizer memoizer , @ Nonnull RelationalExpression relationalExpression ,
234
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction );
226
235
227
236
/**
228
237
* When applied to a reference this method returns a {@link RelationalExpression} consuming the
229
238
* reference passed in that applies a final shape correction as needed by e.g. the result compensation function in
230
239
* {@link WithSelectCompensation}.
231
240
* @param memoizer the memoizer for new {@link Reference}s
232
241
* @param relationalExpression root of graph to apply compensation to
242
+ * @param matchedToRealizedTranslationMapFunction a function that given an alias for the quantifier over the
243
+ * realized compensation, returns a {@link TranslationMap} that is then used to translate
244
+ * from {@link QueryPredicate}s and {@link Value}s using matched aliases to the realized alias
233
245
* @return a new relational expression that corrects the result of {@code reference} by applying a final shape
234
246
* correction of the resulting records.
235
247
*/
236
248
@ Nonnull
237
- RelationalExpression applyFinal (@ Nonnull Memoizer memoizer , @ Nonnull RelationalExpression relationalExpression );
249
+ RelationalExpression applyFinal (@ Nonnull Memoizer memoizer , @ Nonnull RelationalExpression relationalExpression ,
250
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction );
238
251
239
252
/**
240
253
* Returns if this compensation object needs to be applied in order to correct the result of a match.
@@ -315,15 +328,20 @@ default Compensation union(@Nonnull Compensation otherCompensation) {
315
328
@ Nonnull
316
329
@ Override
317
330
public RelationalExpression apply (@ Nonnull final Memoizer memoizer ,
318
- @ Nonnull final RelationalExpression relationalExpression ) {
319
- return Compensation .this .apply (memoizer , otherCompensation .apply (memoizer , relationalExpression ));
331
+ @ Nonnull final RelationalExpression relationalExpression ,
332
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
333
+ return Compensation .this .apply (memoizer , otherCompensation .apply (memoizer , relationalExpression ,
334
+ matchedToRealizedTranslationMapFunction ), matchedToRealizedTranslationMapFunction );
320
335
}
321
336
322
337
@ Nonnull
323
338
@ Override
324
339
public RelationalExpression applyFinal (@ Nonnull final Memoizer memoizer ,
325
- @ Nonnull final RelationalExpression relationalExpression ) {
326
- return Compensation .this .applyFinal (memoizer , otherCompensation .applyFinal (memoizer , relationalExpression ));
340
+ @ Nonnull final RelationalExpression relationalExpression ,
341
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
342
+ return Compensation .this .applyFinal (memoizer , otherCompensation .applyFinal (memoizer ,
343
+ relationalExpression , matchedToRealizedTranslationMapFunction ),
344
+ matchedToRealizedTranslationMapFunction );
327
345
}
328
346
};
329
347
}
@@ -343,17 +361,21 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
343
361
@ Nonnull
344
362
@ Override
345
363
public RelationalExpression apply (@ Nonnull final Memoizer memoizer ,
346
- @ Nonnull final RelationalExpression relationalExpression ) {
364
+ @ Nonnull final RelationalExpression relationalExpression ,
365
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
347
366
return Compensation .this .apply (memoizer ,
348
- otherCompensation .apply (memoizer , relationalExpression ));
367
+ otherCompensation .apply (memoizer , relationalExpression ,
368
+ matchedToRealizedTranslationMapFunction ), matchedToRealizedTranslationMapFunction );
349
369
}
350
370
351
371
@ Nonnull
352
372
@ Override
353
373
public RelationalExpression applyFinal (@ Nonnull final Memoizer memoizer ,
354
- @ Nonnull final RelationalExpression relationalExpression ) {
374
+ @ Nonnull final RelationalExpression relationalExpression ,
375
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
355
376
return Compensation .this .applyFinal (memoizer ,
356
- otherCompensation .applyFinal (memoizer , relationalExpression ));
377
+ otherCompensation .applyFinal (memoizer , relationalExpression ,
378
+ matchedToRealizedTranslationMapFunction ), matchedToRealizedTranslationMapFunction );
357
379
}
358
380
};
359
381
}
@@ -784,28 +806,35 @@ public AggregateMappings getAggregateMappings() {
784
806
*
785
807
* @param memoizer the memoizer for new {@link Reference}s
786
808
* @param relationalExpression root of graph to apply compensation to
809
+ * @param matchedToRealizedTranslationMapFunction a function that given an alias for the quantifier over the
810
+ * realized compensation, returns a {@link TranslationMap} that is then used to translate
811
+ * from {@link QueryPredicate}s and {@link Value}s using matched aliases to the realized alias
787
812
*
788
813
* @return a new relational expression that corrects the result of {@code reference} by applying appropriate
789
814
* filters and/or transformations
790
815
*/
791
816
@ Nonnull
792
817
@ Override
793
818
public RelationalExpression apply (@ Nonnull final Memoizer memoizer ,
794
- @ Nonnull RelationalExpression relationalExpression ) {
819
+ @ Nonnull RelationalExpression relationalExpression ,
820
+ @ Nonnull final Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
795
821
Verify .verify (!isImpossible ());
796
822
797
823
// apply the child as needed
798
824
if (childCompensation .isNeededForFiltering ()) {
799
- relationalExpression = childCompensation .apply (memoizer , relationalExpression );
825
+ relationalExpression = childCompensation .apply (memoizer , relationalExpression ,
826
+ matchedToRealizedTranslationMapFunction );
800
827
}
801
828
802
829
final var matchedForEachAlias = getMatchedForEachAlias ();
830
+ final var matchedToRealizedTranslationMap = matchedToRealizedTranslationMapFunction .apply (matchedForEachAlias );
803
831
804
832
final var compensatedPredicates = new LinkedIdentitySet <QueryPredicate >();
805
833
final var injectCompensationFunctions = predicateCompensationMap .values ();
806
834
for (final var predicateCompensationFunction : injectCompensationFunctions ) {
807
835
// TODO construct a translation map using matchedForEachAlias as target
808
- compensatedPredicates .addAll (predicateCompensationFunction .applyCompensationForPredicate (matchedForEachAlias ));
836
+ compensatedPredicates .addAll (
837
+ predicateCompensationFunction .applyCompensationForPredicate (matchedToRealizedTranslationMap ));
809
838
}
810
839
811
840
final var compensatedPredicatesCorrelatedTo =
@@ -904,13 +933,16 @@ private CorrelationIdentifier getMatchedForEachAlias() {
904
933
@ Nonnull
905
934
@ Override
906
935
public RelationalExpression applyFinal (@ Nonnull final Memoizer memoizer ,
907
- @ Nonnull RelationalExpression relationalExpression ) {
936
+ @ Nonnull RelationalExpression relationalExpression ,
937
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
908
938
Verify .verify (!isImpossible ());
909
939
Verify .verify (resultCompensationFunction .isNeeded ());
910
940
911
941
final var matchedForEachAlias = getMatchedForEachAlias ();
912
942
913
- final var resultValue = resultCompensationFunction .applyCompensationForResult (matchedForEachAlias );
943
+ final var resultValue =
944
+ resultCompensationFunction .applyCompensationForResult (
945
+ matchedToRealizedTranslationMapFunction .apply (matchedForEachAlias ));
914
946
915
947
//
916
948
// At this point we definitely need a new SELECT expression.
0 commit comments