use of com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan in project fdb-record-layer by FoundationDB.
the class FDBInQueryTest method testOneOfThemIn.
/**
* Verify that one-of-them queries work with IN.
*/
@Test
void testOneOfThemIn() 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);
RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.field("repeater").oneOfThem().in(ls)).build();
// Index(ind [EQUALS $__in_repeater__0]) | UnorderedPrimaryKeyDistinct() WHERE __in_repeater__0 IN [13, 22]
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(503365581, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(77841121, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(77839705, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
assertEquals(50, querySimpleRecordStore(recordMetaDataHook, plan, EvaluationContext::empty, record -> assertThat(record.getRecNo() % 4, anyOf(is(3L), is(2L))), TestHelpers::assertDiscardedNone));
}
use of com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan in project fdb-record-layer by FoundationDB.
the class FDBInQueryTest method testInWithNesting.
/**
* Verify that a query with an IN on the second nested field of a multi-index for which there is also a first nested
* field is translated into an appropriate index scan.
*/
@DualPlannerTest
void testInWithNesting() 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(RecordQueryPlanMatchers.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(ImmutableList.of("String6", "String1", "String25", "String11"))))));
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, cursor -> assertEquals(asList("_56", "_6", "_1", "_51", "_11", "_61"), cursor.map(TestRecordsWithHeaderProto.MyRecord.Builder::getStrValue).asList().get()), TestHelpers::assertDiscardedNone);
}
use of com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan in project fdb-record-layer by FoundationDB.
the class FDBInQueryTest method testRecordFunctionInUngrouped.
/**
* Verify that IN works with ungrouped rank indexes.
*/
@Test
void testRecordFunctionInUngrouped() throws Exception {
RecordMetaDataHook recordMetaDataHook = metadata -> metadata.addIndex("MySimpleRecord", new Index("rank", field("num_value_2").ungrouped(), IndexTypes.RANK));
setupSimpleRecordStore(recordMetaDataHook, (i, builder) -> builder.setRecNo(i).setStrValueIndexed("str" + i % 4).setNumValue2(i + 100));
List<Long> ls = Arrays.asList(1L, 3L, 5L);
RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.rank("num_value_2").in(ls)).build();
// Index(rank [EQUALS $__in_rank(Field { 'num_value_2' None} group 1)__0] BY_RANK) WHERE __in_rank(Field { 'num_value_2' None} group 1)__0 IN [1, 3, 5]
RecordQueryPlan plan = planner.plan(query);
assertMatchesExactly(plan, inValuesJoinPlan(indexPlan().where(indexName("rank")).and(RecordQueryPlanMatchers.indexScanType(IndexScanType.BY_RANK))).where(inValuesList(equalsObject(ls))));
assertEquals(1518925028, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(-1422629447, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(-1422660327, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
List<Long> recNos = new ArrayList<>();
querySimpleRecordStore(recordMetaDataHook, plan, EvaluationContext::empty, record -> recNos.add(record.getRecNo()), TestHelpers::assertDiscardedNone);
assertEquals(Arrays.asList(1L, 3L, 5L), recNos);
}
use of com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan in project fdb-record-layer by FoundationDB.
the class FDBInQueryTest method testInWithContinuation.
/**
* Verify that an IN join is executed correctly when continuations are used.
*/
@DualPlannerTest
void testInWithContinuation() 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("String1", "String6", "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 [String1, String6, 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(1075745133, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(1864571255, 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(559717093, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(-744622543, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(-1523769992, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
}
// result: [ "_1", "_51", "_56", "_6", "_11", "_61"]
final Holder<byte[]> continuation = new Holder<>();
queryRecordsWithHeader(recordMetaDataHook, plan, null, 10, cursor -> {
RecordCursorResult<TestRecordsWithHeaderProto.MyRecord.Builder> result = cursor.getNext();
assertEquals("_1", Objects.requireNonNull(result.get()).getStrValue());
continuation.value = result.getContinuation().toBytes();
}, TestHelpers::assertDiscardedNone);
queryRecordsWithHeader(recordMetaDataHook, planner.plan(query), continuation.value, 10, cursor -> {
RecordCursorResult<TestRecordsWithHeaderProto.MyRecord.Builder> result = cursor.getNext();
assertEquals("_51", Objects.requireNonNull(result.get()).getStrValue());
result = cursor.getNext();
assertEquals("_56", Objects.requireNonNull(result.get()).getStrValue());
continuation.value = result.getContinuation().toBytes();
}, TestHelpers::assertDiscardedNone);
RecordQuery query2 = RecordQuery.newBuilder().setRecordType("MyRecord").setFilter(Query.field("header").matches(Query.and(Query.field("rec_no").equalsValue(1L), Query.field("path").in(asList("String6", "String11"))))).build();
// we miss _6
// Note, Since we have two equals operands, the continuation ends up being relative to that
// and is just the id, so we want the id of the continuation point from before ("_56") to be greater than the
// first id of the new continuation ("_11")
queryRecordsWithHeader(recordMetaDataHook, planner.plan(query2), continuation.value, 10, cursor -> {
RecordCursorResult<TestRecordsWithHeaderProto.MyRecord.Builder> result = cursor.getNext();
assertEquals("_11", Objects.requireNonNull(result.get()).getStrValue());
result = cursor.getNext();
assertEquals("_61", Objects.requireNonNull(result.get()).getStrValue());
result = cursor.getNext();
assertFalse(result.hasNext());
}, TestHelpers::assertDiscardedNone);
}
use of com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan in project fdb-record-layer by FoundationDB.
the class FDBInQueryTest method testInQueryOr.
/**
* Verify that IN queries can be planned using index scans, then used in a UNION to implement OR with an inequality
* on the same field, and that the resulting union will be ordered by that field.
*/
@DualPlannerTest
void testInQueryOr() throws Exception {
complexQuerySetup(NO_HOOK);
RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.or(Query.field("num_value_unique").in(Arrays.asList(903, 905, 901)), Query.field("num_value_unique").greaterThan(950))).build();
RecordQueryPlan plan = planner.plan(query);
if (planner instanceof RecordQueryPlanner) {
// Index(MySimpleRecord$num_value_unique [EQUALS $__in_num_value_unique__0]) WHERE __in_num_value_unique__0 IN [901, 903, 905] SORTED ∪[Field { 'num_value_unique' None}, Field { 'rec_no' None}] Index(MySimpleRecord$num_value_unique ([950],>)
assertMatchesExactly(plan, unionPlan(indexPlan().where(indexName("MySimpleRecord$num_value_unique")), inValuesJoinPlan(indexPlan().where(indexName("MySimpleRecord$num_value_unique")).and(scanComparisons(range("[EQUALS $__in_num_value_unique__0]")))).where(inValuesList(equalsObject(Arrays.asList(901, 903, 905))))).where(comparisonKey(concat(field("num_value_unique"), primaryKey("MySimpleRecord")))));
assertEquals(1116661716, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(-924293640, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(713030732, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
} else {
assertMatchesExactly(plan, fetchFromPartialRecordPlan(unionPlan(coveringIndexPlan().where(indexPlanOf(indexPlan().where(indexName("MySimpleRecord$num_value_unique")))), inValuesJoinPlan(coveringIndexPlan().where(indexPlanOf(indexPlan().where(indexName("MySimpleRecord$num_value_unique")).and(scanComparisons(equalities(exactly(anyParameterComparison()))))))).where(inValuesList(equalsObject(Arrays.asList(901, 903, 905))))).where(comparisonKey(concat(field("num_value_unique"), primaryKey("MySimpleRecord"))))));
assertEquals(874214575, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(-1101003320, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(-1557397237, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
}
assertEquals(53, querySimpleRecordStore(NO_HOOK, plan, EvaluationContext::empty, record -> assertThat(record.getNumValueUnique(), anyOf(is(901), is(903), is(905), greaterThan(950))), TestHelpers::assertDiscardedNone));
}
Aggregations