use of com.apple.foundationdb.record.query.RecordQuery in project fdb-record-layer by FoundationDB.
the class RankIndexTest method leftHalfIntervalRankQuery.
@Test
public void leftHalfIntervalRankQuery() throws Exception {
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank("score").greaterThan(2L)).build();
RecordQueryPlan plan = planner.plan(query);
assertEquals("Index(BasicRankedRecord$score ([2],> 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("laodice", myrec.getName());
assertEquals(300, myrec.getScore());
assertFalse(cursor.getNext().hasNext());
}
}
}
use of com.apple.foundationdb.record.query.RecordQuery in project fdb-record-layer by FoundationDB.
the class RankIndexTest method rankPlusMatchingNonRankIndex.
@Test
public void rankPlusMatchingNonRankIndex() throws Exception {
RecordMetaDataHook hook = md -> md.addIndex("BasicRankedRecord", new Index("AaaSumIndex", Key.Expressions.field("score").ungrouped(), IndexTypes.SUM));
try (FDBRecordContext context = openContext()) {
openRecordStore(context, hook);
// Ordinarily the stable order is the persistent form; force new index to the front.
recordStore.getRecordMetaData().getRecordType("BasicRankedRecord").getIndexes().sort(Comparator.comparing(Index::getName));
recordStore.rebuildIndex(recordStore.getRecordMetaData().getIndex("AaaSumIndex")).join();
commit(context);
}
// New index should not interfere with score_for_rank choice.
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.and(Query.field("gender").equalsValue("F"), Query.rank(Key.Expressions.field("score").ungrouped()).equalsValue(2L))).build();
RecordQueryPlan plan = planner.plan(query);
assertEquals("Index(rank_by_gender [EQUALS F, EQUALS $__rank_0])" + " WHERE __rank_0 = BasicRankedRecord$score.score_for_rank(2)", plan.toString());
try (FDBRecordContext context = openContext()) {
openRecordStore(context, hook);
List<String> names = recordStore.executeQuery(plan).map(rec -> TestRecordsRankProto.BasicRankedRecord.newBuilder().mergeFrom(rec.getRecord()).getName()).asList().join();
assertEquals(Arrays.asList("helen", "penelope"), names);
assertEquals(875L, recordStore.evaluateAggregateFunction(Collections.singletonList("BasicRankedRecord"), new IndexAggregateFunction(FunctionNames.SUM, Key.Expressions.field("score"), null), Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join().getLong(0));
commit(context);
}
}
use of com.apple.foundationdb.record.query.RecordQuery in project fdb-record-layer by FoundationDB.
the class RankIndexTest method rankScanIntersection.
@Test
public void rankScanIntersection() throws Exception {
try (FDBRecordContext context = openContext()) {
openRecordStore(context, md -> {
md.removeIndex("rank_by_gender");
md.addIndex("BasicRankedRecord", "gender");
});
recordStore.rebuildIndex(recordStore.getRecordMetaData().getIndex("BasicRankedRecord$gender")).join();
// Laodice fails the rank test; need something that fails the gender test.
recordStore.saveRecord(TestRecordsRankProto.BasicRankedRecord.newBuilder().setName("patroclus").setScore(200).setGender("M").build());
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.and(Query.rank("score").equalsValue(2), Query.field("gender").equalsValue("F"))).build();
RecordQueryPlan plan = planner.plan(query);
assertEquals("Index(BasicRankedRecord$score [[2],[2]] BY_RANK) ∩ Index(BasicRankedRecord$gender [[F],[F]])", plan.toString());
Set<String> names = new HashSet<>();
Function<FDBQueriedRecord<Message>, String> name = rec -> TestRecordsRankProto.BasicRankedRecord.newBuilder().mergeFrom(rec.getRecord()).getName();
RecordCursor<String> cursor = recordStore.executeQuery(plan, null, ExecuteProperties.newBuilder().setReturnedRowLimit(1).build()).map(name);
RecordCursorResult<String> result = cursor.getNext();
assertTrue(result.hasNext());
names.add(result.get());
cursor = recordStore.executeQuery(plan, result.getContinuation().toBytes(), ExecuteProperties.newBuilder().setReturnedRowLimit(1).build()).map(name);
result = cursor.getNext();
assertTrue(result.hasNext());
names.add(result.get());
cursor = recordStore.executeQuery(plan, result.getContinuation().toBytes(), ExecuteProperties.newBuilder().setReturnedRowLimit(1).build()).map(name);
result = cursor.getNext();
assertFalse(result.hasNext());
assertEquals(Sets.newHashSet("penelope", "helen"), names);
commit(context);
}
}
use of com.apple.foundationdb.record.query.RecordQuery in project fdb-record-layer by FoundationDB.
the class BitmapValueIndexTest method filterIndexSelection.
@Test
void filterIndexSelection() {
try (FDBRecordContext context = openContext()) {
createOrOpenRecordStore(context, metaData(REC_NO_BY_STR_NUMS_HOOK));
saveRecords(100, 200);
commit(context);
}
try (FDBRecordContext context = openContext()) {
createOrOpenRecordStore(context, metaData(REC_NO_BY_STR_NUMS_HOOK));
setupPlanner(null);
final RecordQuery recordQuery = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.and(Query.field("str_value").equalsValue("odd"), Query.field("num_value_2").equalsValue(3))).setRequiredResults(Collections.singletonList(field("rec_no"))).build();
final RecordQueryPlan queryPlan = ComposedBitmapIndexAggregate.tryPlan((RecordQueryPlanner) planner, recordQuery, BITMAP_VALUE_REC_NO_BY_STR, IndexQueryabilityFilter.TRUE).orElseGet(() -> fail("Cannot plan query"));
assertThat(queryPlan, coveringIndexScan(indexScan(allOf(indexName("rec_no_by_str_num2"), indexScanType(IndexScanType.BY_GROUP), bounds(hasTupleString("[[odd, 3],[odd, 3]]"))))));
assertEquals(1188586655, queryPlan.planHash());
assertEquals(Optional.empty(), ComposedBitmapIndexAggregate.tryPlan((RecordQueryPlanner) planner, recordQuery, BITMAP_VALUE_REC_NO_BY_STR, IndexQueryabilityFilter.FALSE));
}
}
use of com.apple.foundationdb.record.query.RecordQuery in project fdb-record-layer by FoundationDB.
the class RecordTypeKeyTest method testScan.
@Test
public void testScan() throws Exception {
List<FDBStoredRecord<Message>> recs = saveSomeRecords(BASIC_HOOK);
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, BASIC_HOOK);
RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").build();
RecordQueryPlan plan = planner.plan(query);
assertEquals(recs.subList(0, 2), recordStore.executeQuery(query).map(FDBQueriedRecord::getStoredRecord).asList().join());
assertThat(plan, scan(bounds(hasTupleString("[IS MySimpleRecord]"))));
}
}
Aggregations