Skip to content

Commit 46804ab

Browse files
committed
save-point
1 parent 016b52b commit 46804ab

File tree

12 files changed

+551
-268
lines changed

12 files changed

+551
-268
lines changed

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/cascades/Compensation.java

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
import com.apple.foundationdb.record.query.plan.cascades.expressions.LogicalFilterExpression;
2727
import com.apple.foundationdb.record.query.plan.cascades.expressions.RelationalExpression;
2828
import com.apple.foundationdb.record.query.plan.cascades.predicates.QueryPredicate;
29-
import com.apple.foundationdb.record.query.plan.cascades.rules.DataAccessRule;
3029
import com.apple.foundationdb.record.query.plan.cascades.values.Value;
30+
import com.apple.foundationdb.record.query.plan.cascades.values.translation.TranslationMap;
3131
import com.google.common.base.Suppliers;
3232
import com.google.common.base.Verify;
3333
import com.google.common.collect.ImmutableBiMap;
@@ -41,6 +41,7 @@
4141
import java.util.Collection;
4242
import java.util.Map;
4343
import java.util.Set;
44+
import java.util.function.Function;
4445
import java.util.function.Supplier;
4546
import java.util.stream.Stream;
4647

@@ -107,9 +108,8 @@
107108
* <br>
108109
* Compensation is computed either during the matching process or is computed after a complete match has been found
109110
* 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.
113113
* <br>
114114
* A query sub graph can have multiple matches that could be utilized. In the example above, another index on {@code b}
115115
* 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) {
143143
@Nonnull
144144
@Override
145145
public RelationalExpression apply(@Nonnull final Memoizer memoizer,
146-
@Nonnull final RelationalExpression relationalExpression) {
146+
@Nonnull final RelationalExpression relationalExpression,
147+
@Nonnull Function<CorrelationIdentifier, TranslationMap> matchedToRealizedTranslationMapFunction) {
147148
throw new RecordCoreException("this method should not be called");
148149
}
149150

150151
@Nonnull
151152
@Override
152153
public RelationalExpression applyFinal(@Nonnull final Memoizer memoizer,
153-
@Nonnull final RelationalExpression relationalExpression) {
154+
@Nonnull final RelationalExpression relationalExpression,
155+
@Nonnull Function<CorrelationIdentifier, TranslationMap> matchedToRealizedTranslationMapFunction) {
154156
throw new RecordCoreException("this method should not be called");
155157
}
156158
};
@@ -187,26 +189,29 @@ public Compensation intersect(@Nonnull final Compensation otherCompensation) {
187189
@Nonnull
188190
@Override
189191
public RelationalExpression apply(@Nonnull final Memoizer memoizer,
190-
@Nonnull final RelationalExpression relationalExpression) {
192+
@Nonnull final RelationalExpression relationalExpression,
193+
@Nonnull Function<CorrelationIdentifier, TranslationMap> matchedToRealizedTranslationMapFunction) {
191194
throw new RecordCoreException("this method should not be called");
192195
}
193196

194197
@Nonnull
195198
@Override
196199
public RelationalExpression applyFinal(@Nonnull final Memoizer memoizer,
197-
@Nonnull final RelationalExpression relationalExpression) {
200+
@Nonnull final RelationalExpression relationalExpression,
201+
@Nonnull Function<CorrelationIdentifier, TranslationMap> matchedToRealizedTranslationMapFunction) {
198202
throw new RecordCoreException("this method should not be called");
199203
}
200204
};
201205

202206
@Nonnull
203207
default RelationalExpression applyAllNeededCompensations(@Nonnull final Memoizer memoizer,
204-
@Nonnull RelationalExpression relationalExpression) {
208+
@Nonnull RelationalExpression relationalExpression,
209+
@Nonnull Function<CorrelationIdentifier, TranslationMap> matchedToRealizedTranslationMapFunction) {
205210
if (isNeededForFiltering()) {
206-
relationalExpression = apply(memoizer, relationalExpression);
211+
relationalExpression = apply(memoizer, relationalExpression, matchedToRealizedTranslationMapFunction);
207212
}
208213
if (isFinalNeeded()) {
209-
relationalExpression = applyFinal(memoizer, relationalExpression);
214+
relationalExpression = applyFinal(memoizer, relationalExpression, matchedToRealizedTranslationMapFunction);
210215
}
211216

212217
return relationalExpression;
@@ -218,23 +223,31 @@ default RelationalExpression applyAllNeededCompensations(@Nonnull final Memoizer
218223
* {@link WithSelectCompensation}.
219224
* @param memoizer the memoizer for new {@link Reference}s
220225
* @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
221229
* @return a new relational expression that corrects the result of {@code reference} by applying appropriate
222230
* filters and/or transformations
223231
*/
224232
@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);
226235

227236
/**
228237
* When applied to a reference this method returns a {@link RelationalExpression} consuming the
229238
* reference passed in that applies a final shape correction as needed by e.g. the result compensation function in
230239
* {@link WithSelectCompensation}.
231240
* @param memoizer the memoizer for new {@link Reference}s
232241
* @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
233245
* @return a new relational expression that corrects the result of {@code reference} by applying a final shape
234246
* correction of the resulting records.
235247
*/
236248
@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);
238251

239252
/**
240253
* 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) {
315328
@Nonnull
316329
@Override
317330
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);
320335
}
321336

322337
@Nonnull
323338
@Override
324339
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);
327345
}
328346
};
329347
}
@@ -343,17 +361,21 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
343361
@Nonnull
344362
@Override
345363
public RelationalExpression apply(@Nonnull final Memoizer memoizer,
346-
@Nonnull final RelationalExpression relationalExpression) {
364+
@Nonnull final RelationalExpression relationalExpression,
365+
@Nonnull Function<CorrelationIdentifier, TranslationMap> matchedToRealizedTranslationMapFunction) {
347366
return Compensation.this.apply(memoizer,
348-
otherCompensation.apply(memoizer, relationalExpression));
367+
otherCompensation.apply(memoizer, relationalExpression,
368+
matchedToRealizedTranslationMapFunction), matchedToRealizedTranslationMapFunction);
349369
}
350370

351371
@Nonnull
352372
@Override
353373
public RelationalExpression applyFinal(@Nonnull final Memoizer memoizer,
354-
@Nonnull final RelationalExpression relationalExpression) {
374+
@Nonnull final RelationalExpression relationalExpression,
375+
@Nonnull Function<CorrelationIdentifier, TranslationMap> matchedToRealizedTranslationMapFunction) {
355376
return Compensation.this.applyFinal(memoizer,
356-
otherCompensation.applyFinal(memoizer, relationalExpression));
377+
otherCompensation.applyFinal(memoizer, relationalExpression,
378+
matchedToRealizedTranslationMapFunction), matchedToRealizedTranslationMapFunction);
357379
}
358380
};
359381
}
@@ -784,28 +806,35 @@ public AggregateMappings getAggregateMappings() {
784806
*
785807
* @param memoizer the memoizer for new {@link Reference}s
786808
* @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
787812
*
788813
* @return a new relational expression that corrects the result of {@code reference} by applying appropriate
789814
* filters and/or transformations
790815
*/
791816
@Nonnull
792817
@Override
793818
public RelationalExpression apply(@Nonnull final Memoizer memoizer,
794-
@Nonnull RelationalExpression relationalExpression) {
819+
@Nonnull RelationalExpression relationalExpression,
820+
@Nonnull final Function<CorrelationIdentifier, TranslationMap> matchedToRealizedTranslationMapFunction) {
795821
Verify.verify(!isImpossible());
796822

797823
// apply the child as needed
798824
if (childCompensation.isNeededForFiltering()) {
799-
relationalExpression = childCompensation.apply(memoizer, relationalExpression);
825+
relationalExpression = childCompensation.apply(memoizer, relationalExpression,
826+
matchedToRealizedTranslationMapFunction);
800827
}
801828

802829
final var matchedForEachAlias = getMatchedForEachAlias();
830+
final var matchedToRealizedTranslationMap = matchedToRealizedTranslationMapFunction.apply(matchedForEachAlias);
803831

804832
final var compensatedPredicates = new LinkedIdentitySet<QueryPredicate>();
805833
final var injectCompensationFunctions = predicateCompensationMap.values();
806834
for (final var predicateCompensationFunction : injectCompensationFunctions) {
807835
// TODO construct a translation map using matchedForEachAlias as target
808-
compensatedPredicates.addAll(predicateCompensationFunction.applyCompensationForPredicate(matchedForEachAlias));
836+
compensatedPredicates.addAll(
837+
predicateCompensationFunction.applyCompensationForPredicate(matchedToRealizedTranslationMap));
809838
}
810839

811840
final var compensatedPredicatesCorrelatedTo =
@@ -904,13 +933,16 @@ private CorrelationIdentifier getMatchedForEachAlias() {
904933
@Nonnull
905934
@Override
906935
public RelationalExpression applyFinal(@Nonnull final Memoizer memoizer,
907-
@Nonnull RelationalExpression relationalExpression) {
936+
@Nonnull RelationalExpression relationalExpression,
937+
@Nonnull Function<CorrelationIdentifier, TranslationMap> matchedToRealizedTranslationMapFunction) {
908938
Verify.verify(!isImpossible());
909939
Verify.verify(resultCompensationFunction.isNeeded());
910940

911941
final var matchedForEachAlias = getMatchedForEachAlias();
912942

913-
final var resultValue = resultCompensationFunction.applyCompensationForResult(matchedForEachAlias);
943+
final var resultValue =
944+
resultCompensationFunction.applyCompensationForResult(
945+
matchedToRealizedTranslationMapFunction.apply(matchedForEachAlias));
914946

915947
//
916948
// At this point we definitely need a new SELECT expression.

0 commit comments

Comments
 (0)