use of com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan in project fdb-record-layer by FoundationDB.
the class RankIndexTest method halfIntervalGroupedRankQuery.
@Test
public void halfIntervalGroupedRankQuery() throws Exception {
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.and(Query.field("gender").equalsValue("M"), Query.rank(Key.Expressions.field("score").groupBy(Key.Expressions.field("gender"))).lessThan(1L))).build();
RecordQueryPlan plan = planner.plan(query);
assertEquals("Index(rank_by_gender ([M, null],[M, 1]) BY_RANK)", plan.toString());
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
try (RecordCursor<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan)) {
FDBQueriedRecord<Message> rec = cursor.getNext().get();
TestRecordsRankProto.BasicRankedRecord.Builder myrec = TestRecordsRankProto.BasicRankedRecord.newBuilder();
myrec.mergeFrom(rec.getRecord());
assertEquals("hector", myrec.getName());
assertEquals(75, myrec.getScore());
assertFalse(cursor.getNext().hasNext());
}
}
}
use of com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan in project fdb-record-layer by FoundationDB.
the class RankIndexTest method queryWithRanks.
@Test
public void queryWithRanks() throws Exception {
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.field("gender").equalsValue("M")).setSort(Key.Expressions.field("score")).build();
RecordQueryPlan plan = planner.plan(query);
QueryRecordFunction<Long> ranker = Query.rank(Key.Expressions.field("score").groupBy(Key.Expressions.field("gender")));
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
try (RecordCursorIterator<? extends Pair<Message, Long>> cursor = recordStore.executeQuery(plan).mapPipelined(record -> ranker.eval(recordStore, EvaluationContext.EMPTY, record.getStoredRecord()).thenApply(rank -> new ImmutablePair<>(record.getRecord(), rank)), recordStore.getPipelineSize(PipelineOperation.RECORD_FUNCTION)).asIterator()) {
long rank = 0;
while (cursor.hasNext()) {
Pair<Message, Long> recWithRank = cursor.next();
TestRecordsRankProto.BasicRankedRecord.Builder myrec = TestRecordsRankProto.BasicRankedRecord.newBuilder();
myrec.mergeFrom(recWithRank.getLeft());
assertEquals((Long) rank++, recWithRank.getRight());
}
}
}
}
use of com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan in project fdb-record-layer by FoundationDB.
the class RankIndexTest method rankPlusRankIn2.
@Test
public void rankPlusRankIn2() throws Exception {
// Different rank predicates: at most one can be used in the scan.
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.and(Query.field("gender").equalsValue("M"), Query.rank(Key.Expressions.field("score").ungrouped()).lessThan(3L), Query.rank(Key.Expressions.field("score").groupBy(Key.Expressions.field("gender"))).in("mranks"))).build();
RecordQueryPlan plan = planner.plan(query);
assertThat(plan, inParameter(equalTo("mranks"), scoreForRank(containsInAnyOrder(hasToString("__rank_0 = BasicRankedRecord$score.score_for_rank_else_skip(3)")), fetch(filter(rankComparisonFor("score", Comparisons.Type.LESS_THAN, "__rank_0"), coveringIndexScan(indexScan(allOf(indexName("rank_by_gender"), bounds(hasTupleString("[EQUALS M, EQUALS $__in_rank([Field { 'gender' None}, Field { 'score' None}] group 1)__0]"))))))))));
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
EvaluationContext bound = EvaluationContext.forBinding("mranks", Arrays.asList(0L, 1L, 3L));
List<String> names = plan.execute(recordStore, bound).map(rec -> TestRecordsRankProto.BasicRankedRecord.newBuilder().mergeFrom(rec.getRecord()).getName()).asList().join();
assertEquals(Arrays.asList("hector", "achilles"), names);
commit(context);
}
}
use of com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan in project fdb-record-layer by FoundationDB.
the class RankIndexTest method rankScanContinuation.
@Test
public void rankScanContinuation() throws Exception {
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank("score").lessThan(100)).build();
RecordQueryPlan plan = planner.plan(query);
assertTrue(plan.hasIndexScan("BasicRankedRecord$score"));
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
Multiset<String> names = HashMultiset.create();
byte[] continuation = null;
do {
RecordCursor<FDBQueriedRecord<Message>> recs = recordStore.executeQuery(plan, continuation, ExecuteProperties.newBuilder().setReturnedRowLimit(2).build());
recs.forEach(rec -> names.add(TestRecordsRankProto.BasicRankedRecord.newBuilder().mergeFrom(rec.getRecord()).getName())).join();
continuation = recs.getNext().getContinuation().toBytes();
} while (continuation != null);
assertEquals(ImmutableMultiset.of("achilles", "hector", "helen", "penelope", "laodice"), names);
commit(context);
}
}
use of com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan in project fdb-record-layer by FoundationDB.
the class RankIndexTest method rankPlusRankIn1.
@Test
public void rankPlusRankIn1() throws Exception {
// Different rank predicates: at most one can be used in the scan.
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.and(Query.field("gender").equalsValue("M"), Query.rank(Key.Expressions.field("score").ungrouped()).in(Arrays.asList(0L, 2L)), Query.rank(Key.Expressions.field("score").groupBy(Key.Expressions.field("gender"))).lessThanOrEquals(1L))).build();
RecordQueryPlan plan = planner.plan(query);
assertThat(plan, inValues(equalTo(Arrays.asList(0L, 2L)), scoreForRank(containsInAnyOrder(hasToString("__rank_0 = BasicRankedRecord$score.score_for_rank($__in_rank(Field { 'score' None} group 1)__0)")), fetch(filter(rankComparisonFor("score", Comparisons.Type.EQUALS, "__rank_0"), coveringIndexScan(indexScan(allOf(indexName("rank_by_gender"), bounds(hasTupleString("([M, null],[M, 1]]"))))))))));
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
List<String> names = recordStore.executeQuery(plan).map(rec -> TestRecordsRankProto.BasicRankedRecord.newBuilder().mergeFrom(rec.getRecord()).getName()).asList().join();
assertEquals(Arrays.asList("hector"), names);
commit(context);
}
}
Aggregations