use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreQueryTestBase method fetchResultValues.
protected <T> List<T> fetchResultValues(RecordQueryPlan plan, Opener opener, Function<Message, T> rowHandler, TestHelpers.DangerousConsumer<FDBRecordContext> checkDiscarded) throws Exception {
List<T> result = new ArrayList<>();
try (FDBRecordContext context = openContext()) {
opener.open(context);
try (RecordCursorIterator<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan).asIterator()) {
while (cursor.hasNext()) {
FDBQueriedRecord<Message> rec = cursor.next();
result.add(rowHandler.apply(rec.getRecord()));
}
}
checkDiscarded.accept(context);
// TODO a hack until this gets refactored properly
clearStoreCounter(context);
}
return result;
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class FDBReturnedRecordLimitQueryTest method testComplexLimits7.
/**
* Verify that a returned record limit works properly when no filter is needed.
* Verify that a type filter that is trivially satisfied is elided from the final plan.
*/
@DualPlannerTest
void testComplexLimits7() throws Exception {
RecordMetaDataHook hook = complexQuerySetupHook();
complexQuerySetup(hook);
RecordQuery query = RecordQuery.newBuilder().setRecordTypes(Arrays.asList("MySimpleRecord", "MyOtherRecord")).build();
// Scan(<,>)
RecordQueryPlan plan = planner.plan(query);
assertThat(plan, scan(unbounded()));
assertEquals(2, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(-371672268, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(-371672268, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
int i = 0;
try (RecordCursorIterator<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan, null, ExecuteProperties.newBuilder().setReturnedRowLimit(10).build()).asIterator()) {
while (cursor.hasNext()) {
cursor.next();
i += 1;
}
}
assertEquals(10, i);
assertDiscardedNone(context);
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class FDBReturnedRecordLimitQueryTest method testComplexLimits2.
/**
* Verify that a returned record limit works properly against a query with a filter on one field and a sort on another,
* when the filter field is un-indexed and the sort is in reverse order.
*/
@ParameterizedTest
@BooleanSource
void testComplexLimits2(final boolean shouldOptimizeForIndexFilters) throws Exception {
RecordMetaDataHook hook = complexQuerySetupHook();
complexQuerySetup(hook);
final QueryComponent filter = Query.field("num_value_2").equalsValue(0);
RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(filter).setSort(field("str_value_indexed"), true).build();
setOptimizeForIndexFilters(shouldOptimizeForIndexFilters);
// Index(MySimpleRecord$str_value_indexed <,> REVERSE) | num_value_2 EQUALS 0
// Fetch(Covering(Index(multi_index <,> REVERSE) -> [num_value_2: KEY[1], num_value_3_indexed: KEY[2], rec_no: KEY[3], str_value_indexed: KEY[0]]) | num_value_2 EQUALS 0)
RecordQueryPlan plan = planner.plan(query);
if (shouldOptimizeForIndexFilters) {
assertThat(plan, fetch(filter(filter, coveringIndexScan(indexScan(allOf(indexName("multi_index"), unbounded()))))));
assertTrue(plan.isReverse());
assertEquals(-1143466156, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(915163788, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(-1279091452, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
} else {
assertThat(plan, filter(filter, indexScan(allOf(indexName("MySimpleRecord$str_value_indexed"), unbounded()))));
assertTrue(plan.isReverse());
assertEquals(-384998859, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(-1575402371, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(525309685, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
int i = 0;
try (RecordCursorIterator<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan, null, ExecuteProperties.newBuilder().setReturnedRowLimit(10).build()).asIterator()) {
while (cursor.hasNext()) {
FDBQueriedRecord<Message> rec = cursor.next();
TestRecords1Proto.MySimpleRecord.Builder myrec = TestRecords1Proto.MySimpleRecord.newBuilder();
myrec.mergeFrom(Objects.requireNonNull(rec).getRecord());
assertEquals(0, myrec.getNumValue2());
assertEquals("odd", myrec.getStrValueIndexed());
i += 1;
}
}
assertEquals(10, i);
assertDiscardedAtMost(34, context);
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class FDBSimpleJoinQueryTest method joinChildToParent.
/**
* Verify that simple binding joins in parent/child relationships work.
*/
@Test
public void joinChildToParent() throws Exception {
createJoinRecords(false);
RecordQuery parentQuery = RecordQuery.newBuilder().setRecordType("MyParentRecord").setFilter(Query.field("str_value_indexed").equalsValue("even")).build();
RecordQuery childQuery = RecordQuery.newBuilder().setRecordType("MyChildRecord").setFilter(Query.field("parent_rec_no").equalsParameter("parent")).build();
RecordQueryPlan parentPlan = planner.plan(parentQuery);
RecordQueryPlan childPlan = planner.plan(childQuery);
try (FDBRecordContext context = openContext()) {
openJoinRecordStore(context);
RecordCursor<FDBQueriedRecord<Message>> childCursor = RecordCursor.flatMapPipelined(ignore -> recordStore.executeQuery(parentPlan), (rec, ignore) -> {
TestRecordsParentChildRelationshipProto.MyParentRecord.Builder parentRec = TestRecordsParentChildRelationshipProto.MyParentRecord.newBuilder();
parentRec.mergeFrom(rec.getRecord());
EvaluationContext childContext = EvaluationContext.forBinding("parent", parentRec.getRecNo());
return childPlan.execute(recordStore, childContext);
}, null, 10);
RecordCursor<String> resultsCursor = childCursor.map(rec -> {
TestRecordsParentChildRelationshipProto.MyChildRecord.Builder childRec = TestRecordsParentChildRelationshipProto.MyChildRecord.newBuilder();
childRec.mergeFrom(rec.getRecord());
return childRec.getStrValue();
});
assertEquals(Arrays.asList("2.1", "2.2", "2.3", "4.1", "4.2", "4.3"), resultsCursor.asList().join());
assertDiscardedNone(context);
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class FDBSimpleJoinQueryTest method joinParentToChild.
/**
* Verify that simple binding joins in parent/child relationships work.
*/
@Test
public void joinParentToChild() throws Exception {
createJoinRecords(true);
RecordQuery parentQuery = RecordQuery.newBuilder().setRecordType("MyParentRecord").setFilter(Query.field("str_value_indexed").equalsValue("even")).build();
RecordQueryPlan parentPlan = planner.plan(parentQuery);
RecordQueryPlan childPlan = new RecordQueryLoadByKeysPlan("children");
try (FDBRecordContext context = openContext()) {
openJoinRecordStore(context);
RecordCursor<FDBQueriedRecord<Message>> parentCursor = recordStore.executeQuery(parentPlan);
RecordCursor<FDBQueriedRecord<Message>> childCursor = RecordCursor.flatMapPipelined(ignore -> recordStore.executeQuery(parentPlan), (rec, ignore) -> {
TestRecordsParentChildRelationshipProto.MyParentRecord.Builder parentRec = TestRecordsParentChildRelationshipProto.MyParentRecord.newBuilder();
parentRec.mergeFrom(rec.getRecord());
EvaluationContext childContext = EvaluationContext.forBinding("children", parentRec.getChildRecNosList().stream().map(Tuple::from).collect(Collectors.toList()));
return childPlan.execute(recordStore, childContext);
}, null, 10);
RecordCursor<String> resultsCursor = childCursor.map(rec -> {
TestRecordsParentChildRelationshipProto.MyChildRecord.Builder childRec = TestRecordsParentChildRelationshipProto.MyChildRecord.newBuilder();
childRec.mergeFrom(rec.getRecord());
return childRec.getStrValue();
});
assertEquals(Arrays.asList("2.1", "2.2", "2.3", "4.1", "4.2", "4.3"), resultsCursor.asList().join());
assertDiscardedNone(context);
}
}
Aggregations