Skip to content

Commit 49e025c

Browse files
authored
add minimize to RecordQueryPlan (#3356)
1 parent fba36f6 commit 49e025c

File tree

16 files changed

+551
-320
lines changed

16 files changed

+551
-320
lines changed

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/bitmap/ComposedBitmapIndexQueryPlan.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.apple.foundationdb.record.query.plan.plans.RecordQueryCoveringIndexPlan;
4848
import com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan;
4949
import com.apple.foundationdb.record.query.plan.plans.RecordQueryPlanWithNoChildren;
50+
import com.google.common.base.Verify;
5051
import com.google.common.collect.ImmutableList;
5152
import com.google.protobuf.Message;
5253

@@ -206,6 +207,28 @@ public ComposedBitmapIndexQueryPlan translateCorrelations(@Nonnull final Transla
206207
return new ComposedBitmapIndexQueryPlan(translatedIndexPlansBuilder.build(), composer);
207208
}
208209

210+
@Override
211+
public boolean canBeMinimized() {
212+
return indexPlans.stream().anyMatch(RecordQueryCoveringIndexPlan::canBeMinimized);
213+
}
214+
215+
@Nonnull
216+
@Override
217+
public RecordQueryPlan minimize(@Nonnull final List<Quantifier.Physical> newQuantifiers) {
218+
Verify.verify(newQuantifiers.isEmpty());
219+
220+
final var minimizedIndexPlansBuilder = ImmutableList.<RecordQueryCoveringIndexPlan>builder();
221+
for (final var indexPlan : indexPlans) {
222+
if (indexPlan.canBeMinimized()) {
223+
minimizedIndexPlansBuilder.add(indexPlan.minimize(newQuantifiers));
224+
} else {
225+
minimizedIndexPlansBuilder.add(indexPlan);
226+
}
227+
}
228+
229+
return new ComposedBitmapIndexQueryPlan(minimizedIndexPlansBuilder.build(), composer);
230+
}
231+
209232
@Nonnull
210233
@Override
211234
public Value getResultValue() {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* This source file is part of the FoundationDB open source project
55
*
6-
* Copyright 2015-2019 Apple Inc. and the FoundationDB project authors
6+
* Copyright 2015-2025 Apple Inc. and the FoundationDB project authors
77
*
88
* Licensed under the Apache License, Version 2.0 (the "License");
99
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
package com.apple.foundationdb.record.query.plan.cascades;
2222

2323
import com.apple.foundationdb.annotation.API;
24+
import com.apple.foundationdb.annotation.SpotBugsSuppressWarnings;
2425
import com.apple.foundationdb.record.PlanHashable;
2526
import com.apple.foundationdb.record.query.expressions.Comparisons;
2627
import com.apple.foundationdb.record.query.plan.QueryPlanner.IndexScanPreference;
@@ -62,6 +63,7 @@
6263
*/
6364
@API(API.Status.EXPERIMENTAL)
6465
@SuppressWarnings("PMD.TooManyStaticImports")
66+
@SpotBugsSuppressWarnings("SE_COMPARATOR_SHOULD_BE_SERIALIZABLE")
6567
public class PlanningCostModel implements CascadesCostModel {
6668
@Nonnull
6769
private static final Set<Class<? extends RelationalExpression>> interestingPlanClasses =

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package com.apple.foundationdb.record.query.plan.cascades;
2222

2323
import com.apple.foundationdb.annotation.API;
24+
import com.apple.foundationdb.annotation.SpotBugsSuppressWarnings;
2425
import com.apple.foundationdb.record.query.plan.RecordQueryPlannerConfiguration;
2526
import com.apple.foundationdb.record.query.plan.cascades.expressions.RelationalExpression;
2627

@@ -30,6 +31,7 @@
3031
* Cost model for {@link PlannerPhase#REWRITING}. TODO To be fleshed out whe we have actual rules.
3132
*/
3233
@API(API.Status.EXPERIMENTAL)
34+
@SpotBugsSuppressWarnings("SE_COMPARATOR_SHOULD_BE_SERIALIZABLE")
3335
public class RewritingCostModel implements CascadesCostModel {
3436
@Nonnull
3537
private final RecordQueryPlannerConfiguration configuration;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* StableSelectorCostModel.java
3+
*
4+
* This source file is part of the FoundationDB open source project
5+
*
6+
* Copyright 2015-2025 Apple Inc. and the FoundationDB project authors
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
package com.apple.foundationdb.record.query.plan.cascades;
22+
23+
import com.apple.foundationdb.annotation.API;
24+
import com.apple.foundationdb.annotation.SpotBugsSuppressWarnings;
25+
import com.apple.foundationdb.record.PlanHashable;
26+
import com.apple.foundationdb.record.query.plan.RecordQueryPlannerConfiguration;
27+
import com.apple.foundationdb.record.query.plan.cascades.expressions.RelationalExpression;
28+
29+
import javax.annotation.Nonnull;
30+
31+
/**
32+
* A comparator implementing a simple cost model for the {@link CascadesPlanner} to choose the plan with the smallest
33+
* plan hash.
34+
*/
35+
@API(API.Status.EXPERIMENTAL)
36+
@SpotBugsSuppressWarnings("SE_COMPARATOR_SHOULD_BE_SERIALIZABLE")
37+
public class StableSelectorCostModel implements CascadesCostModel {
38+
@Nonnull
39+
@Override
40+
public RecordQueryPlannerConfiguration getConfiguration() {
41+
return RecordQueryPlannerConfiguration.defaultPlannerConfiguration();
42+
}
43+
44+
@Override
45+
public int compare(@Nonnull final RelationalExpression a, @Nonnull final RelationalExpression b) {
46+
//
47+
// If plans are indistinguishable from a cost perspective, select one by planHash. This makes the cost model
48+
// stable (select the same plan on subsequent plannings).
49+
//
50+
if ((a instanceof PlanHashable) && (b instanceof PlanHashable)) {
51+
int hA = ((PlanHashable)a).planHash(PlanHashable.CURRENT_FOR_CONTINUATION);
52+
int hB = ((PlanHashable)b).planHash(PlanHashable.CURRENT_FOR_CONTINUATION);
53+
return Integer.compare(hA, hB);
54+
}
55+
56+
return 0;
57+
}
58+
}

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/plans/RecordQueryAggregateIndexPlan.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,19 @@ public RecordQueryAggregateIndexPlan translateCorrelations(@Nonnull final Transl
271271
return this;
272272
}
273273

274+
@Override
275+
public boolean canBeMinimized() {
276+
return indexPlan.canBeMinimized();
277+
}
278+
279+
@Nonnull
280+
@Override
281+
public RecordQueryPlan minimize(@Nonnull final List<Quantifier.Physical> newQuantifiers) {
282+
Verify.verify(newQuantifiers.isEmpty());
283+
return new RecordQueryAggregateIndexPlan(indexPlan.minimize(newQuantifiers), recordTypeName, toRecord,
284+
resultValue, groupByResultValue, constraint);
285+
}
286+
274287
@Override
275288
@SuppressWarnings("PMD.CompareObjectsWithEquals")
276289
public boolean equalsWithoutChildren(@Nonnull RelationalExpression otherExpression,

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/plans/RecordQueryCoveringIndexPlan.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,19 @@ public RecordQueryCoveringIndexPlan translateCorrelations(@Nonnull final Transla
241241
return this;
242242
}
243243

244+
@Override
245+
public boolean canBeMinimized() {
246+
return indexPlan.canBeMinimized();
247+
}
248+
249+
@Nonnull
250+
@Override
251+
public RecordQueryCoveringIndexPlan minimize(@Nonnull final List<Quantifier.Physical> newQuantifiers) {
252+
Verify.verify(newQuantifiers.isEmpty());
253+
return new RecordQueryCoveringIndexPlan((RecordQueryPlanWithIndex)indexPlan.minimize(newQuantifiers),
254+
recordTypeName, getAvailableFields(), toRecord);
255+
}
256+
244257
@Nonnull
245258
public Optional<Value> pushValueThroughFetch(@Nonnull Value value,
246259
@Nonnull CorrelationIdentifier sourceAlias,

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/plans/RecordQueryIndexPlan.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -475,20 +475,26 @@ public RecordQueryIndexPlan translateCorrelations(@Nonnull final TranslationMap
475475
return withIndexScanParameters(scanParameters.translateCorrelations(translationMap, shouldSimplifyValues));
476476
}
477477

478+
@Override
479+
public boolean canBeMinimized() {
480+
return matchCandidateOptional.isPresent();
481+
}
482+
478483
@Nonnull
479-
protected RecordQueryIndexPlan withIndexScanParameters(@Nonnull final IndexScanParameters newIndexScanParameters) {
480-
return new RecordQueryIndexPlan(indexName,
481-
commonPrimaryKey,
482-
newIndexScanParameters,
483-
indexFetchMethod,
484-
fetchIndexRecords,
485-
reverse,
486-
strictlySorted,
487-
matchCandidateOptional,
488-
resultType,
484+
@Override
485+
public RecordQueryIndexPlan minimize(@Nonnull final List<Quantifier.Physical> newQuantifiers) {
486+
Verify.verify(newQuantifiers.isEmpty());
487+
return new RecordQueryIndexPlan(indexName, commonPrimaryKey, scanParameters, indexFetchMethod,
488+
fetchIndexRecords, reverse, strictlySorted, Optional.empty(), resultType,
489489
constraint);
490490
}
491491

492+
@Nonnull
493+
protected RecordQueryIndexPlan withIndexScanParameters(@Nonnull final IndexScanParameters newIndexScanParameters) {
494+
return new RecordQueryIndexPlan(indexName, commonPrimaryKey, newIndexScanParameters, indexFetchMethod,
495+
fetchIndexRecords, reverse, strictlySorted, matchCandidateOptional, resultType, constraint);
496+
}
497+
492498
@Nonnull
493499
@Override
494500
public AvailableFields getAvailableFields() {

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/plans/RecordQueryPlan.java

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import com.apple.foundationdb.annotation.API;
2424
import com.apple.foundationdb.annotation.GenerateVisitor;
25-
import com.apple.foundationdb.record.query.plan.HeuristicPlanner;
2625
import com.apple.foundationdb.record.EvaluationContext;
2726
import com.apple.foundationdb.record.ExecuteProperties;
2827
import com.apple.foundationdb.record.PlanSerializable;
@@ -33,7 +32,9 @@
3332
import com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord;
3433
import com.apple.foundationdb.record.provider.foundationdb.FDBRecordStore;
3534
import com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreBase;
35+
import com.apple.foundationdb.record.query.combinatorics.TopologicalSort;
3636
import com.apple.foundationdb.record.query.plan.AvailableFields;
37+
import com.apple.foundationdb.record.query.plan.HeuristicPlanner;
3738
import com.apple.foundationdb.record.query.plan.cascades.AliasMap;
3839
import com.apple.foundationdb.record.query.plan.cascades.FinalMemoizer;
3940
import com.apple.foundationdb.record.query.plan.cascades.Quantifier;
@@ -44,13 +45,18 @@
4445
import com.apple.foundationdb.record.query.plan.serialization.PlanSerialization;
4546
import com.apple.foundationdb.record.query.plan.visitor.RecordQueryPlannerSubstitutionVisitor;
4647
import com.google.common.base.Verify;
48+
import com.google.common.collect.ImmutableList;
49+
import com.google.common.collect.Maps;
4750
import com.google.protobuf.Message;
4851

4952
import javax.annotation.Nonnull;
5053
import javax.annotation.Nullable;
5154
import java.util.List;
55+
import java.util.Map;
5256
import java.util.Objects;
5357

58+
import static com.apple.foundationdb.record.query.plan.cascades.properties.ReferencesAndDependenciesProperty.referencesAndDependencies;
59+
5460
/**
5561
* An executable query plan for producing records.
5662
* <br>
@@ -163,6 +169,87 @@ default RecordQueryPlan strictlySorted(@Nonnull FinalMemoizer memoizer) {
163169
return this;
164170
}
165171

172+
/**
173+
* Returns whether a plan with a smaller footprint can be generated by removing references to e.g. planning
174+
* bread crumbs such as match candidates, etc.
175+
* @return {@code true} iff the plan can be minimized.
176+
*/
177+
default boolean canBeMinimized() {
178+
return false;
179+
}
180+
181+
/**
182+
* Minimizes the plan by removing extraneous references to objects that are not strictly needed to execute the
183+
* plan.
184+
* @return a minimized plan
185+
*/
186+
@Nonnull
187+
@SuppressWarnings("PMD.CompareObjectsWithEquals")
188+
default RecordQueryPlan minimize() {
189+
//
190+
// We need to order all references in a way that only references occurring later can be dependent on references
191+
// occurring earlier.
192+
//
193+
final var referenceDependencyOrder =
194+
referencesAndDependencies().evaluate(this);
195+
196+
final var references =
197+
TopologicalSort.anyTopologicalOrderPermutation(referenceDependencyOrder)
198+
.orElseThrow(() -> new RecordCoreException("graph has cycles"));
199+
200+
//
201+
// We keep this cache to make sure that CSEs prior to minimization end up being CSEs after. We don't want to
202+
// break them as would be the case with a straightforward recursive approach.
203+
//
204+
final var minimizationCache =
205+
Maps.<Reference, Reference>newIdentityHashMap();
206+
207+
for (final var reference : references) {
208+
final var plan = reference.getOnlyElementAsPlan();
209+
210+
final var minimizedPlan =
211+
minimizePlanOverMinimizedReferences(minimizationCache, reference.getOnlyElementAsPlan());
212+
213+
if (minimizedPlan != plan) {
214+
minimizationCache.put(reference, Reference.plannedOf(minimizedPlan));
215+
}
216+
}
217+
218+
return minimizePlanOverMinimizedReferences(minimizationCache, this);
219+
}
220+
221+
@Nonnull
222+
default RecordQueryPlan minimize(@Nonnull final List<Quantifier.Physical> newQuantifiers) {
223+
throw new UnsupportedOperationException("RecordQueryPlan by default cannot be minimized");
224+
}
225+
226+
@Nonnull
227+
private static RecordQueryPlan minimizePlanOverMinimizedReferences(@Nonnull final Map<Reference, Reference> minimizationCache,
228+
@Nonnull final RecordQueryPlan plan) {
229+
var allMinimizedChildrenSame = true;
230+
final var newQuantifiersBuilder = ImmutableList.<Quantifier.Physical>builder();
231+
for (final var quantifier : plan.getQuantifiers()) {
232+
final var physicalQuantifier = quantifier.narrow(Quantifier.Physical.class);
233+
final var childReference = physicalQuantifier.getRangesOver();
234+
235+
final Reference translatedChildReference = minimizationCache.get(childReference);
236+
if (translatedChildReference != null) {
237+
newQuantifiersBuilder.add(physicalQuantifier.overNewReference(translatedChildReference));
238+
allMinimizedChildrenSame = false;
239+
} else {
240+
newQuantifiersBuilder.add(physicalQuantifier);
241+
}
242+
}
243+
244+
if (plan.canBeMinimized()) {
245+
return plan.minimize(newQuantifiersBuilder.build());
246+
}
247+
if (allMinimizedChildrenSame) {
248+
return plan;
249+
}
250+
return (RecordQueryPlan)plan.withQuantifiers(newQuantifiersBuilder.build());
251+
}
252+
166253
// we know the type of the group, even though the compiler doesn't, intentional use of reference equality
167254
@HeuristicPlanner
168255
@Nonnull

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/plans/RecordQueryScanPlan.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,19 @@ public RecordQueryScanPlan translateCorrelations(@Nonnull final TranslationMap t
315315
matchCandidateOptional);
316316
}
317317

318+
@Override
319+
public boolean canBeMinimized() {
320+
return matchCandidateOptional.isPresent();
321+
}
322+
323+
@Nonnull
324+
@Override
325+
public RecordQueryPlan minimize(@Nonnull final List<Quantifier.Physical> newQuantifiers) {
326+
Verify.verify(newQuantifiers.isEmpty());
327+
return new RecordQueryScanPlan(recordTypes, flowedType, commonPrimaryKey, comparisons, reverse, strictlySorted,
328+
Optional.empty());
329+
}
330+
318331
@Nonnull
319332
@Override
320333
public Value getResultValue() {

fdb-relational-core/src/main/java/com/apple/foundationdb/relational/recordlayer/metadata/RecordLayerInvokedRoutine.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.apple.foundationdb.relational.recordlayer.query.functions.CompiledSqlFunction;
2626
import com.apple.foundationdb.relational.util.Assert;
2727
import com.google.common.base.Supplier;
28-
import com.google.common.base.Suppliers;
2928

3029
import javax.annotation.Nonnull;
3130
import java.util.Objects;
@@ -46,7 +45,8 @@ public RecordLayerInvokedRoutine(@Nonnull final String description,
4645
@Nonnull final Supplier<CompiledSqlFunction> compilableSqlFunctionSupplier) {
4746
this.description = description;
4847
this.name = name;
49-
this.compilableSqlFunctionSupplier = Suppliers.memoize(compilableSqlFunctionSupplier);
48+
// TODO this used to be memoized
49+
this.compilableSqlFunctionSupplier = compilableSqlFunctionSupplier;
5050
}
5151

5252
@Nonnull

0 commit comments

Comments
 (0)