Skip to content

Commit 3c0d29e

Browse files
committed
save-point
1 parent 72c8f9e commit 3c0d29e

File tree

12 files changed

+554
-268
lines changed

12 files changed

+554
-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,
@@ -148,14 +148,16 @@ public Compensation intersect(@Nonnull final Compensation otherCompensation) {
148148
@Nonnull
149149
@Override
150150
public RelationalExpression apply(@Nonnull final Memoizer memoizer,
151-
@Nonnull final RelationalExpression relationalExpression) {
151+
@Nonnull final RelationalExpression relationalExpression,
152+
@Nonnull Function<CorrelationIdentifier, TranslationMap> matchedToRealizedTranslationMapFunction) {
152153
throw new RecordCoreException("this method should not be called");
153154
}
154155

155156
@Nonnull
156157
@Override
157158
public RelationalExpression applyFinal(@Nonnull final Memoizer memoizer,
158-
@Nonnull final RelationalExpression relationalExpression) {
159+
@Nonnull final RelationalExpression relationalExpression,
160+
@Nonnull Function<CorrelationIdentifier, TranslationMap> matchedToRealizedTranslationMapFunction) {
159161
throw new RecordCoreException("this method should not be called");
160162
}
161163
};
@@ -192,26 +194,29 @@ public Compensation intersect(@Nonnull final Compensation otherCompensation) {
192194
@Nonnull
193195
@Override
194196
public RelationalExpression apply(@Nonnull final Memoizer memoizer,
195-
@Nonnull final RelationalExpression relationalExpression) {
197+
@Nonnull final RelationalExpression relationalExpression,
198+
@Nonnull Function<CorrelationIdentifier, TranslationMap> matchedToRealizedTranslationMapFunction) {
196199
throw new RecordCoreException("this method should not be called");
197200
}
198201

199202
@Nonnull
200203
@Override
201204
public RelationalExpression applyFinal(@Nonnull final Memoizer memoizer,
202-
@Nonnull final RelationalExpression relationalExpression) {
205+
@Nonnull final RelationalExpression relationalExpression,
206+
@Nonnull Function<CorrelationIdentifier, TranslationMap> matchedToRealizedTranslationMapFunction) {
203207
throw new RecordCoreException("this method should not be called");
204208
}
205209
};
206210

207211
@Nonnull
208212
default RelationalExpression applyAllNeededCompensations(@Nonnull final Memoizer memoizer,
209-
@Nonnull RelationalExpression relationalExpression) {
213+
@Nonnull RelationalExpression relationalExpression,
214+
@Nonnull Function<CorrelationIdentifier, TranslationMap> matchedToRealizedTranslationMapFunction) {
210215
if (isNeededForFiltering()) {
211-
relationalExpression = apply(memoizer, relationalExpression);
216+
relationalExpression = apply(memoizer, relationalExpression, matchedToRealizedTranslationMapFunction);
212217
}
213218
if (isFinalNeeded()) {
214-
relationalExpression = applyFinal(memoizer, relationalExpression);
219+
relationalExpression = applyFinal(memoizer, relationalExpression, matchedToRealizedTranslationMapFunction);
215220
}
216221

217222
return relationalExpression;
@@ -223,23 +228,31 @@ default RelationalExpression applyAllNeededCompensations(@Nonnull final Memoizer
223228
* {@link WithSelectCompensation}.
224229
* @param memoizer the memoizer for new {@link Reference}s
225230
* @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
226234
* @return a new relational expression that corrects the result of {@code reference} by applying appropriate
227235
* filters and/or transformations
228236
*/
229237
@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);
231240

232241
/**
233242
* When applied to a reference this method returns a {@link RelationalExpression} consuming the
234243
* reference passed in that applies a final shape correction as needed by e.g. the result compensation function in
235244
* {@link WithSelectCompensation}.
236245
* @param memoizer the memoizer for new {@link Reference}s
237246
* @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
238250
* @return a new relational expression that corrects the result of {@code reference} by applying a final shape
239251
* correction of the resulting records.
240252
*/
241253
@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);
243256

244257
/**
245258
* 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) {
320333
@Nonnull
321334
@Override
322335
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);
325340
}
326341

327342
@Nonnull
328343
@Override
329344
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);
332350
}
333351
};
334352
}
@@ -348,17 +366,21 @@ default Compensation intersect(@Nonnull Compensation otherCompensation) {
348366
@Nonnull
349367
@Override
350368
public RelationalExpression apply(@Nonnull final Memoizer memoizer,
351-
@Nonnull final RelationalExpression relationalExpression) {
369+
@Nonnull final RelationalExpression relationalExpression,
370+
@Nonnull Function<CorrelationIdentifier, TranslationMap> matchedToRealizedTranslationMapFunction) {
352371
return Compensation.this.apply(memoizer,
353-
otherCompensation.apply(memoizer, relationalExpression));
372+
otherCompensation.apply(memoizer, relationalExpression,
373+
matchedToRealizedTranslationMapFunction), matchedToRealizedTranslationMapFunction);
354374
}
355375

356376
@Nonnull
357377
@Override
358378
public RelationalExpression applyFinal(@Nonnull final Memoizer memoizer,
359-
@Nonnull final RelationalExpression relationalExpression) {
379+
@Nonnull final RelationalExpression relationalExpression,
380+
@Nonnull Function<CorrelationIdentifier, TranslationMap> matchedToRealizedTranslationMapFunction) {
360381
return Compensation.this.applyFinal(memoizer,
361-
otherCompensation.applyFinal(memoizer, relationalExpression));
382+
otherCompensation.applyFinal(memoizer, relationalExpression,
383+
matchedToRealizedTranslationMapFunction), matchedToRealizedTranslationMapFunction);
362384
}
363385
};
364386
}
@@ -789,28 +811,35 @@ public AggregateMappings getAggregateMappings() {
789811
*
790812
* @param memoizer the memoizer for new {@link Reference}s
791813
* @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
792817
*
793818
* @return a new relational expression that corrects the result of {@code reference} by applying appropriate
794819
* filters and/or transformations
795820
*/
796821
@Nonnull
797822
@Override
798823
public RelationalExpression apply(@Nonnull final Memoizer memoizer,
799-
@Nonnull RelationalExpression relationalExpression) {
824+
@Nonnull RelationalExpression relationalExpression,
825+
@Nonnull final Function<CorrelationIdentifier, TranslationMap> matchedToRealizedTranslationMapFunction) {
800826
Verify.verify(!isImpossible());
801827

802828
// apply the child as needed
803829
if (childCompensation.isNeededForFiltering()) {
804-
relationalExpression = childCompensation.apply(memoizer, relationalExpression);
830+
relationalExpression = childCompensation.apply(memoizer, relationalExpression,
831+
matchedToRealizedTranslationMapFunction);
805832
}
806833

807834
final var matchedForEachAlias = getMatchedForEachAlias();
835+
final var matchedToRealizedTranslationMap = matchedToRealizedTranslationMapFunction.apply(matchedForEachAlias);
808836

809837
final var compensatedPredicates = new LinkedIdentitySet<QueryPredicate>();
810838
final var injectCompensationFunctions = predicateCompensationMap.values();
811839
for (final var predicateCompensationFunction : injectCompensationFunctions) {
812840
// TODO construct a translation map using matchedForEachAlias as target
813-
compensatedPredicates.addAll(predicateCompensationFunction.applyCompensationForPredicate(matchedForEachAlias));
841+
compensatedPredicates.addAll(
842+
predicateCompensationFunction.applyCompensationForPredicate(matchedToRealizedTranslationMap));
814843
}
815844

816845
final var compensatedPredicatesCorrelatedTo =
@@ -909,13 +938,16 @@ private CorrelationIdentifier getMatchedForEachAlias() {
909938
@Nonnull
910939
@Override
911940
public RelationalExpression applyFinal(@Nonnull final Memoizer memoizer,
912-
@Nonnull RelationalExpression relationalExpression) {
941+
@Nonnull RelationalExpression relationalExpression,
942+
@Nonnull Function<CorrelationIdentifier, TranslationMap> matchedToRealizedTranslationMapFunction) {
913943
Verify.verify(!isImpossible());
914944
Verify.verify(resultCompensationFunction.isNeeded());
915945

916946
final var matchedForEachAlias = getMatchedForEachAlias();
917947

918-
final var resultValue = resultCompensationFunction.applyCompensationForResult(matchedForEachAlias);
948+
final var resultValue =
949+
resultCompensationFunction.applyCompensationForResult(
950+
matchedToRealizedTranslationMapFunction.apply(matchedForEachAlias));
919951

920952
//
921953
// At this point we definitely need a new SELECT expression.

0 commit comments

Comments
 (0)