use of com.apple.test.Tags in project fdb-record-layer by FoundationDB.
the class FDBNestedFieldQueryTest method nested.
/**
* Verify that nested field comparisons with fanout can scan indexes.
*/
@DualPlannerTest
public void nested() throws Exception {
try (FDBRecordContext context = openContext()) {
openNestedRecordStore(context);
TestRecords4Proto.RestaurantReviewer.Builder reviewerBuilder = TestRecords4Proto.RestaurantReviewer.newBuilder();
reviewerBuilder.setId(1);
reviewerBuilder.setName("Lemuel");
recordStore.saveRecord(reviewerBuilder.build());
reviewerBuilder.setId(2);
reviewerBuilder.setName("Gulliver");
recordStore.saveRecord(reviewerBuilder.build());
TestRecords4Proto.RestaurantRecord.Builder recBuilder = TestRecords4Proto.RestaurantRecord.newBuilder();
recBuilder.setRestNo(101);
recBuilder.setName("The Emperor's Three Tables");
TestRecords4Proto.RestaurantReview.Builder reviewBuilder = recBuilder.addReviewsBuilder();
reviewBuilder.setReviewer(1);
reviewBuilder.setRating(10);
reviewBuilder = recBuilder.addReviewsBuilder();
reviewBuilder.setReviewer(2);
reviewBuilder.setRating(3);
TestRecords4Proto.RestaurantTag.Builder tagBuilder = recBuilder.addTagsBuilder();
tagBuilder.setValue("Lilliput");
tagBuilder.setWeight(5);
recordStore.saveRecord(recBuilder.build());
recBuilder = TestRecords4Proto.RestaurantRecord.newBuilder();
recBuilder.setRestNo(102);
recBuilder.setName("Small Fry's Fried Victuals");
reviewBuilder = recBuilder.addReviewsBuilder();
reviewBuilder.setReviewer(1);
reviewBuilder.setRating(5);
reviewBuilder = recBuilder.addReviewsBuilder();
reviewBuilder.setReviewer(2);
reviewBuilder.setRating(5);
tagBuilder = recBuilder.addTagsBuilder();
tagBuilder.setValue("Lilliput");
tagBuilder.setWeight(1);
recordStore.saveRecord(recBuilder.build());
commit(context);
}
// TODO this was originally:
// QueryExpression.field("reviews").matches(QueryExpression.field("rating").greaterThan(5)),
// which should have failed validate
RecordQuery query = RecordQuery.newBuilder().setRecordType("RestaurantRecord").setFilter(Query.field("reviews").oneOfThem().matches(Query.field("rating").greaterThan(5))).build();
// Index(review_rating ([5],>) | UnorderedPrimaryKeyDistinct()
RecordQueryPlan plan = planner.plan(query);
if (planner instanceof RecordQueryPlanner) {
assertThat(plan, primaryKeyDistinct(indexScan(allOf(indexName("review_rating"), bounds(hasTupleString("([5],>"))))));
assertEquals(1378568952, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(-2085209333, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(2129300140, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
} else {
assertThat(plan, fetch(primaryKeyDistinct(coveringIndexScan(indexScan(allOf(indexName("review_rating"), bounds(hasTupleString("([5],>"))))))));
assertEquals(1060048085, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(-1589243362, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(-1669701185, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
}
assertEquals(Arrays.asList(101L), fetchResultValues(plan, TestRecords4Proto.RestaurantRecord.REST_NO_FIELD_NUMBER, this::openNestedRecordStore, TestHelpers::assertDiscardedNone));
query = RecordQuery.newBuilder().setRecordType("RestaurantRecord").setFilter(Query.field("tags").oneOfThem().matches(Query.and(Query.field("value").equalsValue("Lilliput"), Query.field("weight").greaterThanOrEquals(5)))).build();
// Index(tag [[Lilliput, 5],[Lilliput]]) | UnorderedPrimaryKeyDistinct()
plan = planner.plan(query);
if (planner instanceof RecordQueryPlanner) {
assertThat(plan, primaryKeyDistinct(indexScan(allOf(indexName("tag"), bounds(hasTupleString("[[Lilliput, 5],[Lilliput]]"))))));
assertEquals(-1197819382, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(1570485504, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(1584619812, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
} else {
assertThat(plan, fetch(primaryKeyDistinct(coveringIndexScan(indexScan(allOf(indexName("tag"), bounds(hasTupleString("[[Lilliput, 5],[Lilliput]]"))))))));
assertEquals(205198931, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(2066451475, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(2080585783, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
}
assertEquals(Collections.singletonList(101L), fetchResultValues(plan, TestRecords4Proto.RestaurantRecord.REST_NO_FIELD_NUMBER, this::openNestedRecordStore, TestHelpers::assertDiscardedNone));
QueryComponent reviewFilter = Query.field("reviews").oneOfThem().matches(Query.and(Query.field("rating").equalsValue(5), Query.field("reviewer").equalsValue(1L)));
query = RecordQuery.newBuilder().setRecordType("RestaurantRecord").setFilter(reviewFilter).build();
plan = planner.plan(query);
if (planner instanceof RecordQueryPlanner) {
assertThat(plan, filter(reviewFilter, primaryKeyDistinct(indexScan(allOf(indexName("review_rating"), bounds(hasTupleString("[[5],[5]]")))))));
assertEquals(1252155441, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(-2056078191, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(-1471222808, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
assertEquals(Collections.singletonList(102L), fetchResultValues(plan, TestRecords4Proto.RestaurantRecord.REST_NO_FIELD_NUMBER, this::openNestedRecordStore, TestHelpers::assertDiscardedNone));
} else {
assertThat(plan, filter(reviewFilter, primaryKeyDistinct(indexScan(allOf(indexName("review_rating"), bounds(hasTupleString("[[5],[5]]")))))));
assertEquals(1252155441, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(1947915247, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(-1762196666, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
assertEquals(Collections.singletonList(102L), fetchResultValues(plan, TestRecords4Proto.RestaurantRecord.REST_NO_FIELD_NUMBER, this::openNestedRecordStore, context -> TestHelpers.assertDiscardedAtMost(3, context)));
}
}
Aggregations