use of com.apple.foundationdb.record.TestHelpers in project fdb-record-layer by FoundationDB.
the class FDBSortQueryIndexSelectionTest method sortByPrimaryKey.
/**
* Verify that we can sort by the primary key if possible.
*/
@DualPlannerTest
@ParameterizedTest(name = "sortByPrimaryKey() [{0}]")
@BooleanSource
void sortByPrimaryKey(boolean reverse) throws Exception {
setupSimpleRecordStore(NO_HOOK, (i, builder) -> builder.setRecNo(i).setNumValue2(i % 2));
RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setSort(field("rec_no"), reverse).build();
RecordQueryPlan plan = planner.plan(query);
assertThat(plan, typeFilter(contains("MySimpleRecord"), scan(unbounded())));
AtomicLong lastId = new AtomicLong(reverse ? 99L : 0L);
int returned = querySimpleRecordStore(NO_HOOK, plan, EvaluationContext::empty, builder -> assertThat(builder.getRecNo(), equalTo(reverse ? lastId.getAndDecrement() : lastId.getAndIncrement())), TestHelpers::assertDiscardedNone);
assertEquals(100, returned);
}
use of com.apple.foundationdb.record.TestHelpers in project fdb-record-layer by FoundationDB.
the class FDBInQueryTest method testInQueryIndexSorted.
/**
* Verify that an IN against an unsorted list with an index is implemented as an index scan, with an IN join on
* a sorted copy of the list.
*/
@DualPlannerTest
void testInQueryIndexSorted() throws Exception {
complexQuerySetup(NO_HOOK);
RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.field("num_value_3_indexed").in(asList(1, 4, 2))).setSort(field("num_value_3_indexed")).build();
// Index(MySimpleRecord$num_value_3_indexed [EQUALS $__in_num_value_3_indexed__0]) WHERE __in_num_value_3_indexed__0 IN [1, 2, 4] SORTED
RecordQueryPlan plan = planner.plan(query);
if (planner instanceof RecordQueryPlanner) {
assertMatchesExactly(plan, inValuesJoinPlan(indexPlan().where(indexName("MySimpleRecord$num_value_3_indexed")).and(scanComparisons(range("[EQUALS $__in_num_value_3_indexed__0]")))).where(inValuesList(equalsObject(asList(1, 2, 4)))));
assertEquals(-2004060309, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(571226247, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(571195399, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
} else {
assertMatchesExactly(plan, fetchFromPartialRecordPlan(inValuesJoinPlan(coveringIndexPlan().where(indexPlanOf(indexPlan().where(indexName("MySimpleRecord$num_value_3_indexed")).and(scanComparisons(equalities(only(anyParameterComparison()))))))).where(inValuesList(equalsObject(asList(1, 2, 4))))));
assertEquals(-2068499040, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(-992959779, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(-1998042418, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
}
assertEquals(60, querySimpleRecordStore(NO_HOOK, plan, EvaluationContext::empty, record -> assertThat(record.getNumValue3Indexed(), anyOf(is(1), is(2), is(4))), TestHelpers::assertDiscardedNone));
}
use of com.apple.foundationdb.record.TestHelpers in project fdb-record-layer by FoundationDB.
the class FDBInQueryTest method testInWithLimit.
/**
* Verify that an IN join is executed correctly when the number of records to retrieve is limited.
*/
@DualPlannerTest
void testInWithLimit() throws Exception {
final RecordMetaDataHook recordMetaDataHook = metaData -> {
metaData.getRecordType("MyRecord").setPrimaryKey(field("str_value"));
metaData.addIndex("MyRecord", "ind", field("header").nest(field("rec_no"), field("path")));
};
setupRecordsWithHeader(recordMetaDataHook, (i, record) -> {
record.setStrValue("_" + i);
record.getHeaderBuilder().setRecNo(i % 5).setPath("String" + i % 50).setNum(i);
});
List<String> ls = asList("String6", "String1", "String25", "String11");
RecordQuery query = RecordQuery.newBuilder().setRecordType("MyRecord").setFilter(Query.field("header").matches(Query.and(Query.field("rec_no").equalsValue(1L), Query.field("path").in(ls)))).build();
// Index(ind [EQUALS 1, EQUALS $__in_path__0]) WHERE __in_path__0 IN [String6, String1, String25, String11]
RecordQueryPlan plan = planner.plan(query);
if (planner instanceof RecordQueryPlanner) {
assertMatchesExactly(plan, inValuesJoinPlan(indexPlan().where(indexName("ind")).and(scanComparisons(range("[EQUALS 1, EQUALS $__in_path__0]")))).where(inValuesList(equalsObject(ls))));
assertEquals(1075889283, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(1864715405, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(-847163347, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
} else {
assertMatchesExactly(plan, fetchFromPartialRecordPlan(inValuesJoinPlan(coveringIndexPlan().where(indexPlanOf(indexPlan().where(indexName("ind")).and(scanComparisons(equalities(exactly(equalsObject(new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, 1L)), anyParameterComparison()))))))).where(inValuesList(equalsObject(ls)))));
assertEquals(590997643, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(-740153893, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(-1523769992, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
}
queryRecordsWithHeader(recordMetaDataHook, plan, null, 3, cursor -> assertEquals(asList("_56", "_6", "_1"), cursor.map(TestRecordsWithHeaderProto.MyRecord.Builder::getStrValue).asList().get()), TestHelpers::assertDiscardedNone);
}
use of com.apple.foundationdb.record.TestHelpers in project fdb-record-layer by FoundationDB.
the class FDBInQueryTest method testMultipleInQueryIndexSorted.
/**
* Verify that a query with multiple INs is translated into an index scan within multiple IN joins, when the query
* sort order is compatible with the nesting of the IN joins.
*/
@DualPlannerTest
void testMultipleInQueryIndexSorted() throws Exception {
final RecordMetaDataHook recordMetaDataHook = metaData -> {
metaData.getRecordType("MyRecord").setPrimaryKey(field("str_value"));
metaData.addIndex("MyRecord", "ind", field("header").nest(field("rec_no"), field("path")));
};
setupRecordsWithHeader(recordMetaDataHook, (i, record) -> {
record.setStrValue("_" + i);
record.getHeaderBuilder().setRecNo(i % 5).setPath("String" + i % 50).setNum(i);
});
RecordQuery query = RecordQuery.newBuilder().setRecordType("MyRecord").setFilter(Query.field("header").matches(Query.and(Query.field("path").in(asList("String6", "String25", "String1", "String34")), Query.field("rec_no").in(asList(4L, 1L))))).setSort(field("header").nest(field("rec_no"), field("path"))).build();
// Index(ind [EQUALS $__in_rec_no__1, EQUALS $__in_path__0]) WHERE __in_path__0 IN [String1, String25, String34, String6] SORTED WHERE __in_rec_no__1 IN [1, 4] SORTED
RecordQueryPlan plan = planner.plan(query);
List<String> sortedStringList = asList("String1", "String25", "String34", "String6");
List<Long> sortedLongList = asList(1L, 4L);
if (planner instanceof RecordQueryPlanner) {
assertMatchesExactly(plan, inValuesJoinPlan(inValuesJoinPlan(indexPlan().where(indexName("ind")).and(scanComparisons(range("[EQUALS $__in_rec_no__1, EQUALS $__in_path__0]")))).where(inValuesList(equalsObject(sortedStringList)))).where(inValuesList(equalsObject(sortedLongList))));
assertEquals(303286809, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(-1661991116, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(1396696428, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
} else {
assertMatchesExactly(plan, fetchFromPartialRecordPlan(inValuesJoinPlan(inValuesJoinPlan(coveringIndexPlan().where(indexPlanOf(indexPlan().where(indexName("ind")).and(scanComparisons(equalities(exactly(anyParameterComparison(), anyParameterComparison()))))))).where(inValuesList(equalsObject(sortedStringList)))).where(inValuesList(equalsObject(sortedLongList)))));
assertEquals(457372810, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(-30052367, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(959442763, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
}
queryRecordsWithHeader(recordMetaDataHook, plan, cursor -> assertEquals(asList("1:String1", "1:String1", "1:String6", "1:String6", "4:String34", "4:String34"), cursor.map(m -> m.getHeader().getRecNo() + ":" + m.getHeader().getPath()).asList().get()), TestHelpers::assertDiscardedNone);
}
use of com.apple.foundationdb.record.TestHelpers in project fdb-record-layer by FoundationDB.
the class FDBInQueryTest method testOneOfThemInSorted.
/**
* Verify that one-of-them queries work with IN when sorted on the repeated field.
*/
@Test
void testOneOfThemInSorted() throws Exception {
RecordMetaDataHook recordMetaDataHook = metadata -> metadata.addIndex("MySimpleRecord", "ind", field("repeater", FanType.FanOut));
setupSimpleRecordStore(recordMetaDataHook, (i, builder) -> builder.setRecNo(i).addAllRepeater(Arrays.asList(10 + i % 4, 20 + i % 4)));
List<Integer> ls = Arrays.asList(13, 22);
List<Integer> reversed = new ArrayList<>(ls);
Collections.reverse(reversed);
RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.field("repeater").oneOfThem().in(reversed)).setSort(field("repeater", FanType.FanOut)).build();
RecordQueryPlan plan = planner.plan(query);
assertMatchesExactly(plan, inValuesJoinPlan(unorderedPrimaryKeyDistinctPlan(indexPlan().where(indexName("ind")).and(scanComparisons(range("[EQUALS $__in_repeater__0]"))))).where(inValuesList(equalsObject(ls))));
assertEquals(503365582, plan.planHash());
assertEquals(50, querySimpleRecordStore(recordMetaDataHook, plan, EvaluationContext::empty, record -> assertThat(record.getRecNo() % 4, anyOf(is(3L), is(2L))), TestHelpers::assertDiscardedNone));
}
Aggregations