use of com.apple.foundationdb.record.query.RecordQuery in project fdb-record-layer by FoundationDB.
the class RankIndexTest method rankWithoutGroupRestriction.
@Test
public void rankWithoutGroupRestriction() throws Exception {
// Grouped rank in filter but query results include all groups.
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank(Key.Expressions.field("score").groupBy(Key.Expressions.field("gender"))).equalsValue(1L)).build();
RecordQueryPlan plan = planner.plan(query);
assertFalse(plan.hasIndexScan("rank_by_gender"));
assertTrue(plan.hasRecordScan());
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("achilles", "laodice"), names);
commit(context);
}
}
use of com.apple.foundationdb.record.query.RecordQuery in project fdb-record-layer by FoundationDB.
the class RankIndexTest method coveringScoreValues.
@Test
public void coveringScoreValues() throws Exception {
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.field("gender").equalsValue("M")).setRequiredResults(Arrays.asList(Key.Expressions.field("gender"), Key.Expressions.field("score"))).build();
RecordQueryPlan plan = planner.plan(query);
assertThat(plan, coveringIndexScan(indexScan(allOf(indexName("rank_by_gender"), indexScanType(IndexScanType.BY_VALUE), bounds(hasTupleString("[[M],[M]]"))))));
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
List<Integer> scores = recordStore.executeQuery(plan).map(rec -> TestRecordsRankProto.BasicRankedRecord.newBuilder().mergeFrom(rec.getRecord()).getScore()).asList().join();
assertEquals(Arrays.asList(75, 100), scores);
commit(context);
}
}
use of com.apple.foundationdb.record.query.RecordQuery in project fdb-record-layer by FoundationDB.
the class RankIndexTest method containsNullScore.
@Test
public void containsNullScore() throws Exception {
fdb = FDBDatabaseFactory.instance().getDatabase();
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
// Undo loadRecords().
recordStore.deleteAllRecords();
TestRecordsRankProto.BasicRankedRecord.Builder rec = TestRecordsRankProto.BasicRankedRecord.newBuilder();
rec.setName("achilles");
rec.setGender("m");
recordStore.saveRecord(rec.build());
rec.clear();
rec.setName("helen");
rec.setScore(1);
rec.setGender("f");
recordStore.saveRecord(rec.build());
commit(context);
}
List<RecordQuery> queries = Arrays.asList(RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank("score").equalsValue(0L)).build(), RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank("score").equalsValue(1L)).build(), RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank("score").lessThanOrEquals(1L)).build(), RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank("score").lessThan(1L)).build(), RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank("score").greaterThanOrEquals(0L)).build(), RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank("score").greaterThan(0L)).build());
List<String> planStrings = Arrays.asList("Index(BasicRankedRecord$score [[0],[0]] BY_RANK)", "Index(BasicRankedRecord$score [[1],[1]] BY_RANK)", "Index(BasicRankedRecord$score ([null],[1]] BY_RANK)", "Index(BasicRankedRecord$score ([null],[1]) BY_RANK)", "Index(BasicRankedRecord$score [[0],> BY_RANK)", "Index(BasicRankedRecord$score ([0],> BY_RANK)");
List<RecordQueryPlan> plans = queries.stream().map(planner::plan).collect(Collectors.toList());
for (int i = 0; i < plans.size(); i++) {
assertEquals(planStrings.get(i), plans.get(i).toString());
}
List<List<String>> resultLists = Arrays.asList(Collections.singletonList("achilles"), Collections.singletonList("helen"), Arrays.asList("achilles", "helen"), Collections.singletonList("achilles"), Arrays.asList("achilles", "helen"), Collections.singletonList("helen"));
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
for (int i = 0; i < plans.size(); i++) {
List<String> names = recordStore.executeQuery(plans.get(i)).map(record -> TestRecordsRankProto.BasicRankedRecord.newBuilder().mergeFrom(record.getRecord()).getName()).asList().join();
assertEquals(resultLists.get(i), names);
}
}
}
use of com.apple.foundationdb.record.query.RecordQuery in project fdb-record-layer by FoundationDB.
the class RankIndexTest method writeOnlyRankQuery.
@Test
public void writeOnlyRankQuery() throws Exception {
assertThrows(RecordCoreException.class, () -> {
fdb = FDBDatabaseFactory.instance().getDatabase();
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
recordStore.markIndexWriteOnly("rank_by_gender").join();
// Re-open to reload state.
openRecordStore(context);
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank(Key.Expressions.field("score").groupBy(Key.Expressions.field("gender"))).equalsValue(1L)).build();
RecordQueryPlan plan = recordStore.planQuery(query);
assertEquals("Scan(<,>) | [BasicRankedRecord] | rank([Field { 'gender' None}, Field { 'score' None}] group 1) EQUALS 1", plan.toString());
List<TestRecordsRankProto.BasicRankedRecord> records = recordStore.executeQuery(plan).map(rec -> TestRecordsRankProto.BasicRankedRecord.newBuilder().mergeFrom(rec.getRecord()).build()).asList().get();
} catch (ExecutionException e) {
throw (Exception) e.getCause();
}
});
}
use of com.apple.foundationdb.record.query.RecordQuery 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());
}
}
}
Aggregations