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,
@@ -148,14 +148,16 @@ public Compensation intersect(@Nonnull final Compensation otherCompensation) {
148
148
@ Nonnull
149
149
@ Override
150
150
public RelationalExpression apply (@ Nonnull final Memoizer memoizer ,
151
- @ Nonnull final RelationalExpression relationalExpression ) {
151
+ @ Nonnull final RelationalExpression relationalExpression ,
152
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
152
153
throw new RecordCoreException ("this method should not be called" );
153
154
}
154
155
155
156
@ Nonnull
156
157
@ Override
157
158
public RelationalExpression applyFinal (@ Nonnull final Memoizer memoizer ,
158
- @ Nonnull final RelationalExpression relationalExpression ) {
159
+ @ Nonnull final RelationalExpression relationalExpression ,
160
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
159
161
throw new RecordCoreException ("this method should not be called" );
160
162
}
161
163
};
@@ -192,26 +194,29 @@ public Compensation intersect(@Nonnull final Compensation otherCompensation) {
192
194
@ Nonnull
193
195
@ Override
194
196
public RelationalExpression apply (@ Nonnull final Memoizer memoizer ,
195
- @ Nonnull final RelationalExpression relationalExpression ) {
197
+ @ Nonnull final RelationalExpression relationalExpression ,
198
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
196
199
throw new RecordCoreException ("this method should not be called" );
197
200
}
198
201
199
202
@ Nonnull
200
203
@ Override
201
204
public RelationalExpression applyFinal (@ Nonnull final Memoizer memoizer ,
202
- @ Nonnull final RelationalExpression relationalExpression ) {
205
+ @ Nonnull final RelationalExpression relationalExpression ,
206
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
203
207
throw new RecordCoreException ("this method should not be called" );
204
208
}
205
209
};
206
210
207
211
@ Nonnull
208
212
default RelationalExpression applyAllNeededCompensations (@ Nonnull final Memoizer memoizer ,
209
- @ Nonnull RelationalExpression relationalExpression ) {
213
+ @ Nonnull RelationalExpression relationalExpression ,
214
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
210
215
if (isNeededForFiltering ()) {
211
- relationalExpression = apply (memoizer , relationalExpression );
216
+ relationalExpression = apply (memoizer , relationalExpression , matchedToRealizedTranslationMapFunction );
212
217
}
213
218
if (isFinalNeeded ()) {
214
- relationalExpression = applyFinal (memoizer , relationalExpression );
219
+ relationalExpression = applyFinal (memoizer , relationalExpression , matchedToRealizedTranslationMapFunction );
215
220
}
216
221
217
222
return relationalExpression ;
@@ -223,23 +228,31 @@ default RelationalExpression applyAllNeededCompensations(@Nonnull final Memoizer
223
228
* {@link WithSelectCompensation}.
224
229
* @param memoizer the memoizer for new {@link Reference}s
225
230
* @param relationalExpression root of graph to apply compensation to
231
+ * @param matchedToRealizedTranslationMapFunction a function that given an alias for the quantifier over the
232
+ * realized compensation, returns a {@link TranslationMap} that is then used to translate
233
+ * from {@link QueryPredicate}s and {@link Value}s using matched aliases to the realized alias
226
234
* @return a new relational expression that corrects the result of {@code reference} by applying appropriate
227
235
* filters and/or transformations
228
236
*/
229
237
@ Nonnull
230
- RelationalExpression apply (@ Nonnull Memoizer memoizer , @ Nonnull RelationalExpression relationalExpression );
238
+ RelationalExpression apply (@ Nonnull Memoizer memoizer , @ Nonnull RelationalExpression relationalExpression ,
239
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction );
231
240
232
241
/**
233
242
* When applied to a reference this method returns a {@link RelationalExpression} consuming the
234
243
* reference passed in that applies a final shape correction as needed by e.g. the result compensation function in
235
244
* {@link WithSelectCompensation}.
236
245
* @param memoizer the memoizer for new {@link Reference}s
237
246
* @param relationalExpression root of graph to apply compensation to
247
+ * @param matchedToRealizedTranslationMapFunction a function that given an alias for the quantifier over the
248
+ * realized compensation, returns a {@link TranslationMap} that is then used to translate
249
+ * from {@link QueryPredicate}s and {@link Value}s using matched aliases to the realized alias
238
250
* @return a new relational expression that corrects the result of {@code reference} by applying a final shape
239
251
* correction of the resulting records.
240
252
*/
241
253
@ Nonnull
242
- RelationalExpression applyFinal (@ Nonnull Memoizer memoizer , @ Nonnull RelationalExpression relationalExpression );
254
+ RelationalExpression applyFinal (@ Nonnull Memoizer memoizer , @ Nonnull RelationalExpression relationalExpression ,
255
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction );
243
256
244
257
/**
245
258
* Returns if this compensation object needs to be applied in order to correct the result of a match.
@@ -320,15 +333,20 @@ default Compensation union(@Nonnull Compensation otherCompensation) {
320
333
@ Nonnull
321
334
@ Override
322
335
public RelationalExpression apply (@ Nonnull final Memoizer memoizer ,
323
- @ Nonnull final RelationalExpression relationalExpression ) {
324
- return Compensation .this .apply (memoizer , otherCompensation .apply (memoizer , relationalExpression ));
336
+ @ Nonnull final RelationalExpression relationalExpression ,
337
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
338
+ return Compensation .this .apply (memoizer , otherCompensation .apply (memoizer , relationalExpression ,
339
+ matchedToRealizedTranslationMapFunction ), matchedToRealizedTranslationMapFunction );
325
340
}
326
341
327
342
@ Nonnull
328
343
@ Override
329
344
public RelationalExpression applyFinal (@ Nonnull final Memoizer memoizer ,
330
- @ Nonnull final RelationalExpression relationalExpression ) {
331
- return Compensation .this .applyFinal (memoizer , otherCompensation .applyFinal (memoizer , relationalExpression ));
345
+ @ Nonnull final RelationalExpression relationalExpression ,
346
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
347
+ return Compensation .this .applyFinal (memoizer , otherCompensation .applyFinal (memoizer ,
348
+ relationalExpression , matchedToRealizedTranslationMapFunction ),
349
+ matchedToRealizedTranslationMapFunction );
332
350
}
333
351
};
334
352
}
@@ -348,17 +366,21 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
348
366
@ Nonnull
349
367
@ Override
350
368
public RelationalExpression apply (@ Nonnull final Memoizer memoizer ,
351
- @ Nonnull final RelationalExpression relationalExpression ) {
369
+ @ Nonnull final RelationalExpression relationalExpression ,
370
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
352
371
return Compensation .this .apply (memoizer ,
353
- otherCompensation .apply (memoizer , relationalExpression ));
372
+ otherCompensation .apply (memoizer , relationalExpression ,
373
+ matchedToRealizedTranslationMapFunction ), matchedToRealizedTranslationMapFunction );
354
374
}
355
375
356
376
@ Nonnull
357
377
@ Override
358
378
public RelationalExpression applyFinal (@ Nonnull final Memoizer memoizer ,
359
- @ Nonnull final RelationalExpression relationalExpression ) {
379
+ @ Nonnull final RelationalExpression relationalExpression ,
380
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
360
381
return Compensation .this .applyFinal (memoizer ,
361
- otherCompensation .applyFinal (memoizer , relationalExpression ));
382
+ otherCompensation .applyFinal (memoizer , relationalExpression ,
383
+ matchedToRealizedTranslationMapFunction ), matchedToRealizedTranslationMapFunction );
362
384
}
363
385
};
364
386
}
@@ -789,28 +811,35 @@ public AggregateMappings getAggregateMappings() {
789
811
*
790
812
* @param memoizer the memoizer for new {@link Reference}s
791
813
* @param relationalExpression root of graph to apply compensation to
814
+ * @param matchedToRealizedTranslationMapFunction a function that given an alias for the quantifier over the
815
+ * realized compensation, returns a {@link TranslationMap} that is then used to translate
816
+ * from {@link QueryPredicate}s and {@link Value}s using matched aliases to the realized alias
792
817
*
793
818
* @return a new relational expression that corrects the result of {@code reference} by applying appropriate
794
819
* filters and/or transformations
795
820
*/
796
821
@ Nonnull
797
822
@ Override
798
823
public RelationalExpression apply (@ Nonnull final Memoizer memoizer ,
799
- @ Nonnull RelationalExpression relationalExpression ) {
824
+ @ Nonnull RelationalExpression relationalExpression ,
825
+ @ Nonnull final Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
800
826
Verify .verify (!isImpossible ());
801
827
802
828
// apply the child as needed
803
829
if (childCompensation .isNeededForFiltering ()) {
804
- relationalExpression = childCompensation .apply (memoizer , relationalExpression );
830
+ relationalExpression = childCompensation .apply (memoizer , relationalExpression ,
831
+ matchedToRealizedTranslationMapFunction );
805
832
}
806
833
807
834
final var matchedForEachAlias = getMatchedForEachAlias ();
835
+ final var matchedToRealizedTranslationMap = matchedToRealizedTranslationMapFunction .apply (matchedForEachAlias );
808
836
809
837
final var compensatedPredicates = new LinkedIdentitySet <QueryPredicate >();
810
838
final var injectCompensationFunctions = predicateCompensationMap .values ();
811
839
for (final var predicateCompensationFunction : injectCompensationFunctions ) {
812
840
// TODO construct a translation map using matchedForEachAlias as target
813
- compensatedPredicates .addAll (predicateCompensationFunction .applyCompensationForPredicate (matchedForEachAlias ));
841
+ compensatedPredicates .addAll (
842
+ predicateCompensationFunction .applyCompensationForPredicate (matchedToRealizedTranslationMap ));
814
843
}
815
844
816
845
final var compensatedPredicatesCorrelatedTo =
@@ -909,13 +938,16 @@ private CorrelationIdentifier getMatchedForEachAlias() {
909
938
@ Nonnull
910
939
@ Override
911
940
public RelationalExpression applyFinal (@ Nonnull final Memoizer memoizer ,
912
- @ Nonnull RelationalExpression relationalExpression ) {
941
+ @ Nonnull RelationalExpression relationalExpression ,
942
+ @ Nonnull Function <CorrelationIdentifier , TranslationMap > matchedToRealizedTranslationMapFunction ) {
913
943
Verify .verify (!isImpossible ());
914
944
Verify .verify (resultCompensationFunction .isNeeded ());
915
945
916
946
final var matchedForEachAlias = getMatchedForEachAlias ();
917
947
918
- final var resultValue = resultCompensationFunction .applyCompensationForResult (matchedForEachAlias );
948
+ final var resultValue =
949
+ resultCompensationFunction .applyCompensationForResult (
950
+ matchedToRealizedTranslationMapFunction .apply (matchedForEachAlias ));
919
951
920
952
//
921
953
// At this point we definitely need a new SELECT expression.
0 commit comments