Skip to content

Commit e5c435f

Browse files
committed
Add Cascades version of test.
1 parent 1b370ed commit e5c435f

File tree

1 file changed

+71
-3
lines changed
  • fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/query

1 file changed

+71
-3
lines changed

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

+71-3
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,26 @@
3232
import com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext;
3333
import com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreBase;
3434
import com.apple.foundationdb.record.query.RecordQuery;
35+
import com.apple.foundationdb.record.query.expressions.Comparisons;
3536
import com.apple.foundationdb.record.query.expressions.Query;
3637
import com.apple.foundationdb.record.query.plan.AvailableFields;
3738
import com.apple.foundationdb.record.query.plan.QueryPlanner;
3839
import com.apple.foundationdb.record.query.plan.RecordQueryPlanner;
3940
import com.apple.foundationdb.record.query.plan.cascades.AliasMap;
41+
import com.apple.foundationdb.record.query.plan.cascades.CascadesPlanner;
4042
import com.apple.foundationdb.record.query.plan.cascades.CorrelationIdentifier;
43+
import com.apple.foundationdb.record.query.plan.cascades.GraphExpansion;
4144
import com.apple.foundationdb.record.query.plan.cascades.Quantifier;
4245
import com.apple.foundationdb.record.query.plan.cascades.Reference;
4346
import com.apple.foundationdb.record.query.plan.cascades.explain.PlannerGraph;
47+
import com.apple.foundationdb.record.query.plan.cascades.expressions.LogicalSortExpression;
48+
import com.apple.foundationdb.record.query.plan.cascades.expressions.RecursiveExpression;
4449
import com.apple.foundationdb.record.query.plan.cascades.expressions.RelationalExpression;
50+
import com.apple.foundationdb.record.query.plan.cascades.predicates.ValuePredicate;
51+
import com.apple.foundationdb.record.query.plan.cascades.values.FieldValue;
4552
import com.apple.foundationdb.record.query.plan.cascades.values.ObjectValue;
53+
import com.apple.foundationdb.record.query.plan.cascades.values.QuantifiedObjectValue;
54+
import com.apple.foundationdb.record.query.plan.cascades.values.RecursivePriorValue;
4655
import com.apple.foundationdb.record.query.plan.cascades.values.Value;
4756
import com.apple.foundationdb.record.query.plan.cascades.values.translation.TranslationMap;
4857
import com.apple.foundationdb.record.query.plan.plans.QueryResult;
@@ -51,13 +60,13 @@
5160
import com.apple.test.Tags;
5261
import com.google.protobuf.Message;
5362
import org.junit.jupiter.api.Tag;
54-
import org.junit.jupiter.api.Test;
5563

5664
import javax.annotation.Nonnull;
5765
import javax.annotation.Nullable;
5866
import java.util.List;
5967
import java.util.Set;
6068

69+
import static com.apple.foundationdb.record.provider.foundationdb.query.FDBSimpleQueryGraphTest.fullTypeScan;
6170
import static org.junit.jupiter.api.Assertions.assertEquals;
6271
import static org.junit.jupiter.api.Assertions.fail;
6372

@@ -67,13 +76,15 @@
6776
@Tag(Tags.RequiresFDB)
6877
public class RecursiveQueryTest extends FDBRecordStoreQueryTestBase {
6978

70-
@Test
79+
@DualPlannerTest()
7180
public void testUp() throws Exception {
7281
loadRecords();
7382

7483
final RecordQueryPlan plan;
7584
if (planner instanceof RecordQueryPlanner) {
7685
plan = buildAncestorPlan((RecordQueryPlanner)planner);
86+
} else if (planner instanceof CascadesPlanner) {
87+
plan = buildAncestorPlan((CascadesPlanner)planner);
7788
} else {
7889
plan = null;
7990
fail("unsupported planner type");
@@ -84,6 +95,7 @@ public void testUp() throws Exception {
8495
assertEquals(expected, actual);
8596
}
8697

98+
@Nonnull
8799
private RecordQueryPlan buildAncestorPlan(@Nonnull RecordQueryPlanner planner) throws Exception {
88100
final RecordQuery rootQuery = RecordQuery.newBuilder()
89101
.setRecordType("MyChildRecord")
@@ -96,13 +108,42 @@ private RecordQueryPlan buildAncestorPlan(@Nonnull RecordQueryPlanner planner) t
96108
return recursiveQuery(planner, rootQuery, childQuery, "parent_rec_no");
97109
}
98110

99-
@Test
111+
@Nonnull
112+
private RecordQueryPlan buildAncestorPlan(@Nonnull CascadesPlanner cascadesPlanner) {
113+
return planGraph(
114+
() -> {
115+
var rootQuantifier = fullTypeScan(cascadesPlanner.getRecordMetaData(), "MyChildRecord");
116+
var graphExpansionBuilder = GraphExpansion.builder();
117+
graphExpansionBuilder.addQuantifier(rootQuantifier);
118+
graphExpansionBuilder.addPredicate(
119+
new ValuePredicate(FieldValue.ofFieldName(QuantifiedObjectValue.of(rootQuantifier.getAlias(), rootQuantifier.getFlowedObjectType()), "str_value"),
120+
new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, "three")));
121+
rootQuantifier = Quantifier.forEach(Reference.of(graphExpansionBuilder.build().buildSimpleSelectOverQuantifier((Quantifier.ForEach)rootQuantifier)));
122+
123+
var childQuantifier = fullTypeScan(cascadesPlanner.getRecordMetaData(), "MyChildRecord");
124+
graphExpansionBuilder = GraphExpansion.builder();
125+
graphExpansionBuilder.addQuantifier(childQuantifier);
126+
var aliasForPrior = CorrelationIdentifier.uniqueID();
127+
graphExpansionBuilder.addPredicate(
128+
new ValuePredicate(FieldValue.ofFieldName(QuantifiedObjectValue.of(childQuantifier.getAlias(), childQuantifier.getFlowedObjectType()), "rec_no"),
129+
new Comparisons.ValueComparison(Comparisons.Type.EQUALS, FieldValue.ofFieldName(RecursivePriorValue.of(aliasForPrior, childQuantifier.getFlowedObjectType()), "parent_rec_no"))));
130+
childQuantifier = Quantifier.forEach(Reference.of(graphExpansionBuilder.build().buildSimpleSelectOverQuantifier((Quantifier.ForEach)childQuantifier)), aliasForPrior);
131+
132+
final var resultStrValue = FieldValue.ofFieldName(QuantifiedObjectValue.of(childQuantifier.getAlias(), childQuantifier.getFlowedObjectType()), "str_value");
133+
final var recursive = new RecursiveExpression(resultStrValue, rootQuantifier, childQuantifier);
134+
return Reference.of(LogicalSortExpression.unsorted(Quantifier.forEach(Reference.of(recursive))));
135+
});
136+
}
137+
138+
@DualPlannerTest()
100139
public void testDown() throws Exception {
101140
loadRecords();
102141

103142
final RecordQueryPlan plan;
104143
if (planner instanceof RecordQueryPlanner) {
105144
plan = buildDescendantPlan((RecordQueryPlanner)planner);
145+
} else if (planner instanceof CascadesPlanner) {
146+
plan = buildDescendantPlan((CascadesPlanner)planner);
106147
} else {
107148
plan = null;
108149
fail("unsupported planner type");
@@ -126,6 +167,33 @@ private static RecordQueryPlan buildDescendantPlan(@Nonnull RecordQueryPlanner p
126167
return recursiveQuery(planner, rootQuery, childQuery, "rec_no");
127168
}
128169

170+
@Nonnull
171+
private RecordQueryPlan buildDescendantPlan(@Nonnull CascadesPlanner cascadesPlanner) {
172+
return planGraph(
173+
() -> {
174+
var rootQuantifier = fullTypeScan(cascadesPlanner.getRecordMetaData(), "MyChildRecord");
175+
var graphExpansionBuilder = GraphExpansion.builder();
176+
graphExpansionBuilder.addQuantifier(rootQuantifier);
177+
graphExpansionBuilder.addPredicate(
178+
new ValuePredicate(FieldValue.ofFieldName(QuantifiedObjectValue.of(rootQuantifier.getAlias(), rootQuantifier.getFlowedObjectType()), "parent_rec_no"),
179+
new Comparisons.NullComparison(Comparisons.Type.IS_NULL)));
180+
rootQuantifier = Quantifier.forEach(Reference.of(graphExpansionBuilder.build().buildSimpleSelectOverQuantifier((Quantifier.ForEach)rootQuantifier)));
181+
182+
var childQuantifier = fullTypeScan(cascadesPlanner.getRecordMetaData(), "MyChildRecord");
183+
graphExpansionBuilder = GraphExpansion.builder();
184+
graphExpansionBuilder.addQuantifier(childQuantifier);
185+
var aliasForPrior = CorrelationIdentifier.uniqueID();
186+
graphExpansionBuilder.addPredicate(
187+
new ValuePredicate(FieldValue.ofFieldName(QuantifiedObjectValue.of(childQuantifier.getAlias(), childQuantifier.getFlowedObjectType()), "parent_rec_no"),
188+
new Comparisons.ValueComparison(Comparisons.Type.EQUALS, FieldValue.ofFieldName(RecursivePriorValue.of(aliasForPrior, childQuantifier.getFlowedObjectType()), "rec_no"))));
189+
childQuantifier = Quantifier.forEach(Reference.of(graphExpansionBuilder.build().buildSimpleSelectOverQuantifier((Quantifier.ForEach)childQuantifier)), aliasForPrior);
190+
191+
final var resultStrValue = FieldValue.ofFieldName(QuantifiedObjectValue.of(childQuantifier.getAlias(), childQuantifier.getFlowedObjectType()), "str_value");
192+
final var recursive = new RecursiveExpression(resultStrValue, rootQuantifier, childQuantifier);
193+
return Reference.of(LogicalSortExpression.unsorted(Quantifier.forEach(Reference.of(recursive))));
194+
});
195+
}
196+
129197
private void openParentChildRecordStore(FDBRecordContext context) throws Exception {
130198
final RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecordsParentChildRelationshipProto.getDescriptor());
131199
createOrOpenRecordStore(context, metaDataBuilder.getRecordMetaData());

0 commit comments

Comments
 (0)