Skip to content

Commit 4b2c8c1

Browse files
committed
adding tests
1 parent db5859d commit 4b2c8c1

File tree

10 files changed

+254
-39
lines changed

10 files changed

+254
-39
lines changed

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -238,19 +238,11 @@ default PredicateCompensationFunction computeCompensationFunction(@Nonnull final
238238
}
239239

240240
@Nonnull
241-
default PredicateCompensationFunction computeCompensationFunction(@Nonnull final PartialMatch partialMatch,
242-
@Nonnull final QueryPredicate originalQueryPredicate,
243-
@Nonnull final Map<CorrelationIdentifier, ComparisonRange> boundParameterPrefixMap,
244-
@Nonnull final List<PredicateCompensationFunction> childrenResults,
245-
@Nonnull final PullUp pullUp) {
246-
Verify.verify(this instanceof LeafQueryPredicate);
247-
Verify.verify(childrenResults.isEmpty());
248-
249-
return toResidualPredicate()
250-
.replaceValuesMaybe(pullUp::pullUpValueMaybe)
251-
.map(PredicateCompensationFunction::ofPredicate)
252-
.orElse(PredicateCompensationFunction.impossibleCompensation());
253-
}
241+
PredicateCompensationFunction computeCompensationFunction(@Nonnull PartialMatch partialMatch,
242+
@Nonnull QueryPredicate originalQueryPredicate,
243+
@Nonnull Map<CorrelationIdentifier, ComparisonRange> boundParameterPrefixMap,
244+
@Nonnull List<PredicateCompensationFunction> childrenResults,
245+
@Nonnull PullUp pullUp);
254246

255247
/**
256248
* Create a {@link QueryPredicate} that is equivalent to {@code this} but which is evaluated as a residual

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ protected Set<? extends RelationalExpression> dataAccessForMatchPartition(@Nonnu
319319
return LinkedIdentitySet.of();
320320
}
321321

322+
if (logger.isTraceEnabled()) {
323+
for (Vectored<SingleMatchedAccess> bestMaximumCoverageMatch : bestMaximumCoverageMatches) {
324+
logger.trace("single access = {}", bestMaximumCoverageMatch.getElement());
325+
}
326+
}
327+
322328
// create scans for all best matches
323329
final var bestMatchToPlanMap =
324330
createScansForMatches(call.getContext(), call, bestMaximumCoverageMatches);
@@ -433,6 +439,11 @@ protected Set<? extends RelationalExpression> dataAccessForMatchPartition(@Nonnu
433439
bestMatchToDistinctPlanMap,
434440
binaryPartition,
435441
requestedOrderings);
442+
443+
if (logger.isTraceEnabled()) {
444+
logger.trace("binary intersection result = {}", binaryIntersections);
445+
}
446+
436447
if (binaryIntersections.hasViableIntersection()) {
437448
updateIntersectionInfoMap(intersectionInfoMap, binaryPartition, binaryIntersections);
438449
} else {
@@ -491,6 +502,10 @@ protected Set<? extends RelationalExpression> dataAccessForMatchPartition(@Nonnu
491502
kPartition,
492503
requestedOrderings);
493504

505+
if (logger.isTraceEnabled()) {
506+
logger.trace("intersection result = {}", intersectionResult);
507+
}
508+
494509
if (!intersectionResult.hasViableIntersection()) {
495510
continue;
496511
}

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreBase;
3232
import com.apple.foundationdb.record.provider.foundationdb.cursors.IntersectionMultiCursor;
3333
import com.apple.foundationdb.record.query.plan.HeuristicPlanner;
34-
import com.apple.foundationdb.record.query.plan.cascades.AliasMap;
3534
import com.apple.foundationdb.record.query.plan.cascades.CorrelationIdentifier;
3635
import com.apple.foundationdb.record.query.plan.cascades.FinalMemoizer;
3736
import com.apple.foundationdb.record.query.plan.cascades.OrderingPart.ProvidedOrderingPart;
@@ -43,7 +42,6 @@
4342
import com.apple.foundationdb.record.query.plan.cascades.explain.PlannerGraph;
4443
import com.apple.foundationdb.record.query.plan.cascades.typing.Type;
4544
import com.apple.foundationdb.record.query.plan.cascades.values.Value;
46-
import com.apple.foundationdb.record.query.plan.cascades.values.simplification.DefaultValueSimplificationRuleSet;
4745
import com.apple.foundationdb.record.query.plan.cascades.values.translation.TranslationMap;
4846
import com.google.auto.service.AutoService;
4947
import com.google.common.base.Verify;
@@ -107,12 +105,7 @@ public ComparisonKeyFunction.OnValues getComparisonKeyFunction() {
107105
@Override
108106
public List<? extends Value> getRequiredValues(@Nonnull final CorrelationIdentifier newBaseAlias,
109107
@Nonnull final Type inputType) {
110-
final var ruleSet = DefaultValueSimplificationRuleSet.instance();
111-
return getComparisonKeyValues().stream()
112-
.map(comparisonKeyValue ->
113-
comparisonKeyValue.rebase(AliasMap.ofAliases(Quantifier.current(), newBaseAlias))
114-
.simplify(ruleSet, EvaluationContext.empty(), AliasMap.emptyMap(), getCorrelatedTo()))
115-
.collect(ImmutableList.toImmutableList());
108+
throw new UnsupportedOperationException();
116109
}
117110

118111
@HeuristicPlanner

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,12 @@ public RecordQueryScanPlan(@Nullable Set<String> recordTypes,
157157
*/
158158
@VisibleForTesting
159159
public RecordQueryScanPlan(@Nullable Set<String> recordTypes,
160-
@Nonnull Type flowedType,
161-
@Nullable KeyExpression commonPrimaryKey,
162-
@Nonnull ScanComparisons comparisons,
163-
boolean reverse,
164-
boolean strictlySorted,
165-
@Nonnull final Optional<? extends WithPrimaryKeyMatchCandidate> matchCandidateOptional) {
160+
@Nonnull Type flowedType,
161+
@Nullable KeyExpression commonPrimaryKey,
162+
@Nonnull ScanComparisons comparisons,
163+
boolean reverse,
164+
boolean strictlySorted,
165+
@Nonnull final Optional<? extends WithPrimaryKeyMatchCandidate> matchCandidateOptional) {
166166
this.recordTypes = recordTypes == null ? null : ImmutableSet.copyOf(recordTypes);
167167
this.flowedType = flowedType;
168168
this.commonPrimaryKey = commonPrimaryKey;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* RootLogLevelExtension.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.provider.foundationdb;
22+
23+
import org.apache.logging.log4j.Level;
24+
import org.apache.logging.log4j.LogManager;
25+
import org.apache.logging.log4j.core.LoggerContext;
26+
import org.apache.logging.log4j.core.config.Configuration;
27+
import org.apache.logging.log4j.core.config.Configurator;
28+
import org.junit.jupiter.api.extension.AfterEachCallback;
29+
import org.junit.jupiter.api.extension.BeforeEachCallback;
30+
import org.junit.jupiter.api.extension.ExtensionContext;
31+
32+
public class RootLogLevelExtension implements BeforeEachCallback, AfterEachCallback {
33+
private final Level tempLevel;
34+
private Level original;
35+
36+
public RootLogLevelExtension(Level tempLevel) {
37+
this.tempLevel = tempLevel;
38+
}
39+
40+
@Override
41+
public void beforeEach(ExtensionContext context) {
42+
LoggerContext ctx = (LoggerContext)LogManager.getContext(false);
43+
Configuration cfg = ctx.getConfiguration();
44+
original = cfg.getLoggerConfig(LogManager.ROOT_LOGGER_NAME).getLevel();
45+
Configurator.setRootLevel(tempLevel);
46+
}
47+
48+
@Override
49+
public void afterEach(ExtensionContext context) {
50+
Configurator.setRootLevel(original);
51+
}
52+
}

fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/query/FDBSimpleQueryGraphTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.apple.foundationdb.record.metadata.Key;
3131
import com.apple.foundationdb.record.metadata.expressions.KeyExpression;
3232
import com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext;
33+
import com.apple.foundationdb.record.provider.foundationdb.RootLogLevelExtension;
3334
import com.apple.foundationdb.record.query.IndexQueryabilityFilter;
3435
import com.apple.foundationdb.record.query.expressions.Comparisons;
3536
import com.apple.foundationdb.record.query.plan.cascades.AccessHints;
@@ -61,11 +62,13 @@
6162
import com.google.common.collect.ImmutableList;
6263
import com.google.common.collect.ImmutableMap;
6364
import com.google.common.collect.ImmutableSet;
65+
import org.apache.logging.log4j.Level;
6466
import org.hamcrest.Matchers;
6567
import org.junit.jupiter.api.Assertions;
6668
import org.junit.jupiter.api.Assumptions;
6769
import org.junit.jupiter.api.Disabled;
6870
import org.junit.jupiter.api.Tag;
71+
import org.junit.jupiter.api.extension.RegisterExtension;
6972
import org.junit.jupiter.params.ParameterizedTest;
7073

7174
import javax.annotation.Nonnull;
@@ -130,6 +133,9 @@
130133
*/
131134
@Tag(Tags.RequiresFDB)
132135
public class FDBSimpleQueryGraphTest extends FDBRecordStoreQueryTestBase {
136+
@RegisterExtension
137+
static RootLogLevelExtension logLevel = new RootLogLevelExtension(Level.TRACE);
138+
133139
@DualPlannerTest(planner = DualPlannerTest.Planner.CASCADES)
134140
void testSimplePlanGraph() {
135141
CascadesPlanner cascadesPlanner = setUp();
Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,11 @@
1818
* limitations under the License.
1919
*/
2020

21-
package com.apple.foundationdb.record.provider.foundationdb.query.plan.cascades;
21+
package com.apple.foundationdb.record.query.plan.cascades;
2222

2323
import com.apple.foundationdb.record.query.expressions.Comparisons;
24-
import com.apple.foundationdb.record.query.plan.cascades.AccessHints;
25-
import com.apple.foundationdb.record.query.plan.cascades.Compensation;
26-
import com.apple.foundationdb.record.query.plan.cascades.CorrelationIdentifier;
27-
import com.apple.foundationdb.record.query.plan.cascades.GraphExpansion;
28-
import com.apple.foundationdb.record.query.plan.cascades.GroupByMappings;
29-
import com.apple.foundationdb.record.query.plan.cascades.LinkedIdentityMap;
30-
import com.apple.foundationdb.record.query.plan.cascades.Memoizer;
31-
import com.apple.foundationdb.record.query.plan.cascades.PlannerStage;
3224
import com.apple.foundationdb.record.query.plan.cascades.PredicateMultiMap.PredicateCompensationFunction;
3325
import com.apple.foundationdb.record.query.plan.cascades.PredicateMultiMap.ResultCompensationFunction;
34-
import com.apple.foundationdb.record.query.plan.cascades.Quantifier;
35-
import com.apple.foundationdb.record.query.plan.cascades.RuleTestHelper;
3626
import com.apple.foundationdb.record.query.plan.cascades.expressions.FullUnorderedScanExpression;
3727
import com.apple.foundationdb.record.query.plan.cascades.expressions.LogicalFilterExpression;
3828
import com.apple.foundationdb.record.query.plan.cascades.expressions.RelationalExpression;
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* MatchInfoTests.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.record.query.plan.cascades.expressions.FullUnorderedScanExpression;
24+
import com.apple.foundationdb.record.query.plan.cascades.typing.Type;
25+
import com.apple.foundationdb.record.query.plan.cascades.values.translation.TranslationMap;
26+
import com.google.common.collect.ImmutableSet;
27+
import com.google.common.collect.Iterables;
28+
import org.assertj.core.api.Assertions;
29+
import org.junit.jupiter.api.Test;
30+
31+
import javax.annotation.Nonnull;
32+
33+
public class MatchInfoTests {
34+
@Test
35+
void testCollectPredicates() {
36+
final var memoizer = Memoizer.noMemoization(PlannerStage.PLANNED);
37+
38+
final var fuseQuery = fuse();
39+
final var fuseCandidate = fuse();
40+
final var aCandidate = CorrelationIdentifier.of("a'");
41+
final var quantifierOverFuseCandidate =
42+
Quantifier.forEach(memoizer.memoizeExploratoryExpression(fuseCandidate), aCandidate);
43+
44+
final var selectOverFuseCandidate =
45+
GraphExpansion.builder()
46+
.addQuantifier(quantifierOverFuseCandidate)
47+
.build().buildSimpleSelectOverQuantifier(quantifierOverFuseCandidate);
48+
49+
final var matchInfos =
50+
fuseQuery.exactlySubsumedBy(fuseCandidate, AliasMap.emptyMap(), IdentityBiMap.create(), TranslationMap.empty());
51+
Assertions.assertThat(matchInfos).hasSizeGreaterThan(0);
52+
final var matchInfo = Iterables.getFirst(matchInfos, null);
53+
final var maxMatchMap = matchInfo.getMaxMatchMap();
54+
final var adjustedMaxMatchMapOptional =
55+
maxMatchMap.adjustMaybe(aCandidate, selectOverFuseCandidate.getResultValue(), ImmutableSet.of(aCandidate));
56+
Assertions.assertThat(adjustedMaxMatchMapOptional.isPresent()).isTrue();
57+
58+
final var adjustedMatch =
59+
matchInfo.adjustedBuilder()
60+
.setMaxMatchMap(adjustedMaxMatchMapOptional.get())
61+
.build();
62+
63+
Assertions.assertThat(adjustedMatch.collectPulledUpPredicateMappings(selectOverFuseCandidate, ImmutableSet.of()))
64+
.isEmpty();
65+
}
66+
67+
@Nonnull
68+
private static FullUnorderedScanExpression fuse() {
69+
return new FullUnorderedScanExpression(ImmutableSet.of("someType"),
70+
someRecordType(), new AccessHints());
71+
}
72+
73+
@Nonnull
74+
private static Type.Record someRecordType() {
75+
return RuleTestHelper.TYPE_S;
76+
}
77+
78+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* RecordQueryMultiIntersectionOnValuesPlanTests.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.plans;
22+
23+
import com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression;
24+
import com.apple.foundationdb.record.query.plan.ScanComparisons;
25+
import com.apple.foundationdb.record.query.plan.cascades.CorrelationIdentifier;
26+
import com.apple.foundationdb.record.query.plan.cascades.Memoizer;
27+
import com.apple.foundationdb.record.query.plan.cascades.PlannerStage;
28+
import com.apple.foundationdb.record.query.plan.cascades.Quantifier;
29+
import com.apple.foundationdb.record.query.plan.cascades.RuleTestHelper;
30+
import com.apple.foundationdb.record.query.plan.cascades.typing.Type;
31+
import com.apple.foundationdb.record.query.plan.cascades.values.QuantifiedObjectValue;
32+
import com.google.common.collect.ImmutableList;
33+
import com.google.common.collect.ImmutableSet;
34+
import org.assertj.core.api.Assertions;
35+
import org.junit.jupiter.api.Test;
36+
37+
import javax.annotation.Nonnull;
38+
import java.util.Optional;
39+
40+
public class RecordQueryMultiIntersectionOnValuesPlanTests {
41+
@Test
42+
public void testHeuristicPlannerEntryPoints() {
43+
final var memoizer = Memoizer.noMemoization(PlannerStage.PLANNED);
44+
final var scanA = memoizer.memoizePlan(scan());
45+
final var qunA = Quantifier.physical(scanA, CorrelationIdentifier.of("a"));
46+
final var scanB = memoizer.memoizePlan(scan());
47+
final var qunB = Quantifier.physical(scanB, CorrelationIdentifier.of("b"));
48+
49+
final var intersectionPLan =
50+
RecordQueryMultiIntersectionOnValuesPlan.intersection(ImmutableList.of(qunA, qunB),
51+
ImmutableList.of(),
52+
QuantifiedObjectValue.of(qunA),
53+
false);
54+
55+
Assertions.assertThat(intersectionPLan)
56+
.satisfies(plan ->
57+
Assertions.assertThatThrownBy(() -> plan.getRequiredValues(CorrelationIdentifier.uniqueId(), new Type.Any()))
58+
.isInstanceOf(UnsupportedOperationException.class))
59+
.satisfies(plan ->
60+
Assertions.assertThatThrownBy(plan::getRequiredFields)
61+
.isInstanceOf(UnsupportedOperationException.class))
62+
.satisfies(plan -> {
63+
final var scanANew = memoizer.memoizePlan(scan());
64+
final var scanBNew = memoizer.memoizePlan(scan());
65+
66+
Assertions.assertThat(plan.withChildrenReferences(ImmutableList.of(scanANew, scanBNew)))
67+
.isEqualTo(intersectionPLan);
68+
})
69+
.satisfies(plan ->
70+
Assertions.assertThatThrownBy(() -> plan.strictlySorted(memoizer))
71+
.isInstanceOf(UnsupportedOperationException.class));
72+
}
73+
74+
@Nonnull
75+
private static RecordQueryScanPlan scan() {
76+
return new RecordQueryScanPlan(ImmutableSet.of("someType"),
77+
someRecordType(),
78+
EmptyKeyExpression.EMPTY,
79+
ScanComparisons.EMPTY,
80+
false,
81+
false,
82+
Optional.empty());
83+
}
84+
85+
@Nonnull
86+
private static Type.Record someRecordType() {
87+
return RuleTestHelper.TYPE_S;
88+
}
89+
}

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ spotbugs-annotations = { module = "com.github.spotbugs:spotbugs-annotations", ve
144144

145145
[bundles]
146146

147-
test-impl = [ "assertj", "hamcrest", "junit-api", "junit-params", "log4j-core", "mockito" ]
147+
test-impl = [ "assertj", "hamcrest", "junit-api", "junit-params", "log4j-core", "mockito", "bndtools" ]
148148
test-runtime = [ "junit-engine", "log4j-slf4jBinding"]
149149
test-compileOnly = [ "autoService", "jsr305" ]
150150

0 commit comments

Comments
 (0)