use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class FDBSortQueryIndexSelectionTest method sortOnlyUnique.
private void sortOnlyUnique(RecordMetaDataHook hook) {
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
for (int i = 0; i < 100; i++) {
TestRecords1Proto.MySimpleRecord.Builder recBuilder = TestRecords1Proto.MySimpleRecord.newBuilder();
// Carter-Wegman hash, with large enough prime
recBuilder.setRecNo((1096 * i + 722) % 1289);
recBuilder.setNumValueUnique(i);
recordStore.saveRecord(recBuilder.build());
}
commit(context);
}
RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setSort(field("num_value_unique")).build();
// Index(MySimpleRecord$num_value_unique <,>)
RecordQueryPlan plan = planner.plan(query);
assertThat(plan, indexScan(allOf(indexName("MySimpleRecord$num_value_unique"), unbounded())));
assertEquals(-1130465929, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(-491910604, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(-491910604, 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).asIterator()) {
while (cursor.hasNext()) {
FDBQueriedRecord<Message> rec = cursor.next();
TestRecords1Proto.MySimpleRecord.Builder myrec = TestRecords1Proto.MySimpleRecord.newBuilder();
myrec.mergeFrom(Objects.requireNonNull(rec).getRecord());
assertEquals(i++, myrec.getNumValueUnique());
}
}
assertEquals(100, i);
assertDiscardedNone(context);
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class FDBRepeatedFieldQueryTest method testOnlyRepeatIndex.
/**
* Verify that an index on a repeated field isn't used for normal scans.
*/
@DualPlannerTest
public void testOnlyRepeatIndex() throws Exception {
RecordMetaDataHook hook = metaData -> {
metaData.removeIndex("MySimpleRecord$str_value_indexed");
metaData.removeIndex("MySimpleRecord$num_value_unique");
metaData.removeIndex("MySimpleRecord$num_value_3_indexed");
metaData.addIndex("MySimpleRecord", "repeater$fanout", field("repeater", FanType.FanOut));
};
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
for (int i = 0; i < 3; i++) {
TestRecords1Proto.MySimpleRecord.Builder recBuilder = TestRecords1Proto.MySimpleRecord.newBuilder();
recBuilder.setRecNo(i);
recordStore.saveRecord(recBuilder.build());
}
commit(context);
}
RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").build();
RecordQueryPlan plan = planner.plan(query);
assertThat(plan, typeFilter(contains("MySimpleRecord"), scan(unbounded())));
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
assertEquals(LongStream.range(0, 3).mapToObj(Long::valueOf).collect(Collectors.toList()), recordStore.executeQuery(plan).map(FDBQueriedRecord::getRecord).map(message -> message.getField(message.getDescriptorForType().findFieldByNumber(1))).asList().join());
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class FDBRestrictedIndexQueryTest method queryAllowedIndexes.
/**
* Verify that queries do not use prohibited indexes.
*/
@DualPlannerTest
void queryAllowedIndexes() {
RecordMetaDataHook hook = metaData -> {
metaData.removeIndex("MySimpleRecord$str_value_indexed");
metaData.addIndex("MySimpleRecord", new Index("limited_str_value_index", field("str_value_indexed"), Index.EMPTY_VALUE, IndexTypes.VALUE, IndexOptions.NOT_ALLOWED_FOR_QUERY_OPTIONS));
};
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
recordStore.deleteAllRecords();
TestRecords1Proto.MySimpleRecord.Builder recBuilder = TestRecords1Proto.MySimpleRecord.newBuilder();
recBuilder.setRecNo(1);
recBuilder.setStrValueIndexed("abc");
recBuilder.setNumValueUnique(123);
recordStore.saveRecord(recBuilder.build());
recBuilder.setRecNo(2);
recBuilder.setStrValueIndexed("xyz");
recBuilder.setNumValueUnique(987);
recordStore.saveRecord(recBuilder.build());
commit(context);
}
RecordQuery query1 = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.field("str_value_indexed").equalsValue("abc")).build();
// Index(limited_str_value_index [[abc],[abc]])
// Scan(<,>) | [MySimpleRecord] | str_value_indexed EQUALS abc
RecordQueryPlan plan1 = planner.plan(query1);
assertThat("should not use prohibited index", plan1, hasNoDescendant(indexScan("limited_str_value_index")));
assertTrue(plan1.hasFullRecordScan(), "should use full record scan");
if (planner instanceof RecordQueryPlanner) {
assertEquals(-223683738, plan1.planHash(PlanHashable.PlanHashKind.LEGACY));
// TODO: Issue https://github.com/FoundationDB/fdb-record-layer/issues/1074
// assertEquals(1148834070, plan1.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
} else {
assertEquals(-2136471589, plan1.planHash(PlanHashable.PlanHashKind.LEGACY));
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
try (RecordCursor<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan1)) {
FDBQueriedRecord<Message> rec = cursor.getNext().get();
TestRecords1Proto.MySimpleRecord.Builder myrec = TestRecords1Proto.MySimpleRecord.newBuilder();
myrec.mergeFrom(Objects.requireNonNull(rec).getRecord());
assertEquals("abc", myrec.getStrValueIndexed());
assertFalse(cursor.getNext().hasNext());
}
TestHelpers.assertDiscardedExactly(1, context);
clearStoreCounter(context);
}
RecordQuery query2 = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.field("str_value_indexed").equalsValue("abc")).setAllowedIndex("limited_str_value_index").build();
// Index(limited_str_value_index [[abc],[abc]])
RecordQueryPlan plan2 = planner.plan(query2);
assertThat("explicitly use prohibited index", plan2, descendant(indexScan("limited_str_value_index")));
assertFalse(plan2.hasRecordScan(), "should not use record scan");
assertEquals(-1573180774, plan2.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(994464666, plan2.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(-1531627068, plan2.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
try (RecordCursor<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan2)) {
FDBQueriedRecord<Message> rec = cursor.getNext().get();
TestRecords1Proto.MySimpleRecord.Builder myrec = TestRecords1Proto.MySimpleRecord.newBuilder();
myrec.mergeFrom(Objects.requireNonNull(rec).getRecord());
assertEquals("abc", myrec.getStrValueIndexed());
assertFalse(cursor.getNext().hasNext());
}
TestHelpers.assertDiscardedNone(context);
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class FDBSortQueryIndexSelectionTest method testComplexQuery5r.
/**
* Verify that a sort can be implemented by traversing an index in the reverse order.
*/
@DualPlannerTest
public void testComplexQuery5r() throws Exception {
RecordMetaDataHook hook = complexQuerySetupHook();
complexQuerySetup(hook);
RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setSort(field("num_value_unique"), true).build();
// Index(MySimpleRecord$num_value_unique <,> REVERSE)
RecordQueryPlan plan = planner.plan(query);
assertThat(plan, indexScan(allOf(indexName("MySimpleRecord$num_value_unique"), unbounded())));
assertTrue(plan.isReverse(), "plan should have reversal");
assertEquals(-1130465928, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(-491910790, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(-491910790, 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).asIterator()) {
while (cursor.hasNext()) {
FDBQueriedRecord<Message> rec = cursor.next();
TestRecords1Proto.MySimpleRecord.Builder myrec = TestRecords1Proto.MySimpleRecord.newBuilder();
myrec.mergeFrom(Objects.requireNonNull(rec).getRecord());
assertEquals(1000 - i, myrec.getNumValueUnique());
i++;
}
}
assertEquals(100, i);
assertDiscardedNone(context);
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class FDBSortQueryIndexSelectionTest method testComplexQuery8x.
/**
* Verify that a query with a filter on one field and a sort on another uses the index of the sort preferentially,
* and falls back to filtering to implement the filter if an appropriate multi-field index is not available.
*/
@DualPlannerTest
public void testComplexQuery8x() throws Exception {
RecordMetaDataHook hook = complexQuerySetupHook();
complexQuerySetup(hook);
final QueryComponent filter = Query.field("str_value_indexed").equalsValue("even");
RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(filter).setSort(field("num_value_3_indexed")).build();
RecordQueryPlan plan = planner.plan(query);
assertThat(plan, filter(filter, indexScan(allOf(indexName("MySimpleRecord$num_value_3_indexed"), unbounded()))));
if (planner instanceof RecordQueryPlanner) {
assertEquals(-1429997503, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
// TODO: Issue https://github.com/FoundationDB/fdb-record-layer/issues/1074
// assertEquals(-1729416480, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
} else {
assertEquals(952181942, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
int i = 0;
try (RecordCursorIterator<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan).asIterator()) {
while (cursor.hasNext()) {
FDBQueriedRecord<Message> rec = cursor.next();
TestRecords1Proto.MySimpleRecord.Builder myrec = TestRecords1Proto.MySimpleRecord.newBuilder();
myrec.mergeFrom(Objects.requireNonNull(rec).getRecord());
assertEquals("even", myrec.getStrValueIndexed());
i++;
}
}
assertEquals(50, i);
assertDiscardedAtMost(50, context);
}
}
Aggregations