use of com.apple.foundationdb.record.query.RecordQuery in project fdb-record-layer by FoundationDB.
the class FDBNestedFieldQueryTest method nestedThenWithAndPartial.
/**
* Verify that matching part of a nested field only uses part of the index.
*/
@DualPlannerTest
public void nestedThenWithAndPartial() throws Exception {
final RecordMetaDataHook hook = metaData -> {
metaData.addIndex("RestaurantReviewer", "hometownEmail", concat(field("stats").nest(concatenateFields("hometown", "school_name", "start_date")), field("email")));
};
nestedWithAndSetup(hook);
RecordQuery query = RecordQuery.newBuilder().setRecordType("RestaurantReviewer").setFilter(Query.and(Query.field("stats").matches(Query.and(Query.field("hometown").equalsValue("Home Town"), Query.field("school_name").equalsValue("University of Learning"))), Query.field("email").equalsValue("pmp@example.com"))).build();
RecordQueryPlan plan = planner.plan(query);
assertThat(plan, filter(Query.field("email").equalsValue("pmp@example.com"), indexScan(allOf(indexName("hometownEmail"), bounds(hasTupleString("[[Home Town, University of Learning],[Home Town, University of Learning]]"))))));
if (planner instanceof RecordQueryPlanner) {
assertEquals(895882018, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(146214617, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(1382626015, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
} else {
assertEquals(-1385621911, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(945827751, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(-2112728147, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
}
assertEquals(Collections.singletonList(1L), fetchResultValues(plan, TestRecords4Proto.RestaurantReviewer.ID_FIELD_NUMBER, context -> openNestedRecordStore(context, hook), TestHelpers::assertDiscardedNone));
}
use of com.apple.foundationdb.record.query.RecordQuery in project fdb-record-layer by FoundationDB.
the class FDBNestedFieldQueryTest method nestedThenWithAnd.
/**
* Verify that AND conditions involving nested non-repeated fields still work when index has non-nested fields,
* no matter which way the nested conditions are expressed.
*/
@DualPlannerTest
public void nestedThenWithAnd() throws Exception {
final RecordMetaDataHook hook = metaData -> {
metaData.addIndex("RestaurantReviewer", "emailHometown", concat(field("email"), field("stats").nest(concatenateFields("hometown", "start_date"))));
};
nestedWithAndSetup(hook);
RecordQuery query = RecordQuery.newBuilder().setRecordType("RestaurantReviewer").setFilter(Query.and(Query.field("stats").matches(Query.and(Query.field("start_date").lessThanOrEquals(0L), Query.field("hometown").equalsValue("Home Town"))), Query.field("email").equalsValue("pmp@example.com"))).build();
RecordQueryPlan plan = planner.plan(query);
assertThat(plan, indexScan(allOf(indexName("emailHometown"), bounds(hasTupleString("([pmp@example.com, Home Town, null],[pmp@example.com, Home Town, 0]]")))));
assertEquals(-688450117, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(-1159885451, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(608425592, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
assertEquals(Collections.singletonList(1L), fetchResultValues(plan, TestRecords4Proto.RestaurantReviewer.ID_FIELD_NUMBER, context -> openNestedRecordStore(context, hook), TestHelpers::assertDiscardedNone));
query = RecordQuery.newBuilder().setRecordType("RestaurantReviewer").setFilter(Query.and(Query.field("stats").matches(Query.field("start_date").lessThanOrEquals(0L)), Query.field("email").equalsValue("pmp@example.com"), Query.field("stats").matches(Query.field("hometown").equalsValue("Home Town")))).build();
assertEquals(plan, planner.plan(query));
}
use of com.apple.foundationdb.record.query.RecordQuery in project fdb-record-layer by FoundationDB.
the class FDBInQueryTest method inQueryWithSortBySecondFieldOfCompoundIndex.
/**
* Verify that an IN query with a sort can be implemented as an ordered union of compound indexes that can satisfy
* the sort once the equality predicates from the IN have been pushed onto the indexes.
* @see com.apple.foundationdb.record.query.plan.planning.InExtractor#asOr()
*/
@ParameterizedTest
@EnumSource(InAsOrUnionMode.class)
void inQueryWithSortBySecondFieldOfCompoundIndex(InAsOrUnionMode inAsOrMode) throws Exception {
RecordMetaDataHook hook = metaData -> metaData.addIndex("MySimpleRecord", "compoundIndex", concat(field("num_value_3_indexed"), field("str_value_indexed")));
complexQuerySetup(hook);
final List<Integer> inList = asList(1, 4, 2);
final QueryComponent filter = Query.field("num_value_3_indexed").in(inList);
RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(filter).setSort(field("str_value_indexed")).build();
// The configuration is planner-specific.
assertTrue(planner instanceof RecordQueryPlanner);
planner.setConfiguration(inAsOrMode.configure(planner.getConfiguration().asBuilder()).build());
// Index(MySimpleRecord$str_value_indexed <,>) | num_value_3_indexed IN [1, 4, 2]
// Index(compoundIndex [[1],[1]]) ∪[Field { 'str_value_indexed' None}, Field { 'rec_no' None}] Index(compoundIndex [[4],[4]]) ∪[Field { 'str_value_indexed' None}, Field { 'rec_no' None}] Index(compoundIndex [[2],[2]])
// ∪(__in_num_value_3_indexed__0 IN [1, 4, 2]) Index(compoundIndex [EQUALS $__in_num_value_3_indexed__0])
RecordQueryPlan plan = planner.plan(query);
if (inAsOrMode == InAsOrUnionMode.AS_OR) {
assertMatchesExactly(plan, unionPlan(inList.stream().map(number -> indexPlan().where(indexName("compoundIndex")).and(scanComparisons(range(String.format("[[%d],[%d]]", number, number))))).collect(ImmutableList.toImmutableList())).where(comparisonKey(concat(field("str_value_indexed"), primaryKey("MySimpleRecord")))));
assertEquals(-1813975352, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(-1188407258, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(2089555085, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
} else if (inAsOrMode == InAsOrUnionMode.AS_UNION) {
assertMatchesExactly(plan, inUnionPlan(indexPlan().where(indexName("compoundIndex")).and(scanComparisons(range("[EQUALS $__in_num_value_3_indexed__0]")))).where(inUnionComparisonKey(concat(field("str_value_indexed"), primaryKey("MySimpleRecord")))).and(inUnionValuesSources(exactly(inUnionInValues(equalsObject(inList)).and(inUnionBindingName("__in_num_value_3_indexed__0"))))));
assertEquals(406364040, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(494706693, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(-628125702, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
} else {
assertMatchesExactly(plan, filterPlan(indexPlan().where(indexName("MySimpleRecord$str_value_indexed")).and(scanComparisons(unbounded()))).where(queryComponents(exactly(equalsObject(filter)))));
assertEquals(1775865786, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(972267, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(212572525, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
}
assertEquals(60, querySimpleRecordStore(hook, plan, EvaluationContext::empty, record -> assertThat(record.getNumValue3Indexed(), anyOf(is(1), is(2), is(4))), context -> TestHelpers.assertDiscardedAtMost(40, context)));
}
use of com.apple.foundationdb.record.query.RecordQuery in project fdb-record-layer by FoundationDB.
the class FDBInQueryTest method testInQueryOrCompound.
/**
* Verify that a complex query involving IN, AND, and OR is planned using a union of scans and joins on a
* multi-field index, where the left subset has equality and the final field has an IN plus inequality on that same
* field.
*/
@DualPlannerTest
void testInQueryOrCompound() throws Exception {
RecordMetaDataHook hook = complexQuerySetupHook();
complexQuerySetup(hook);
RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.and(Query.field("str_value_indexed").equalsValue("odd"), Query.field("num_value_2").equalsValue(0), Query.or(Query.field("num_value_3_indexed").in(Arrays.asList(1, 3)), Query.field("num_value_3_indexed").greaterThanOrEquals(4)))).build();
// Index(multi_index [EQUALS odd, EQUALS 0, EQUALS $__in_num_value_3_indexed__0]) WHERE __in_num_value_3_indexed__0 IN [1, 3] SORTED ∪[Field { 'num_value_3_indexed' None}, Field { 'rec_no' None}] Index(multi_index [[odd, 0, 4],[odd, 0]])
RecordQueryPlan plan = planner.plan(query);
if (planner instanceof RecordQueryPlanner) {
assertMatchesExactly(plan, unionPlan(inValuesJoinPlan(indexPlan().where(indexName("multi_index")).and(scanComparisons(range("[EQUALS odd, EQUALS 0, EQUALS $__in_num_value_3_indexed__0]")))).where(inValuesList(equalsObject(Arrays.asList(1, 3)))), indexPlan().where(indexName("multi_index")).and(scanComparisons(range("[[odd, 0, 4],[odd, 0]]")))));
assertEquals(468569345, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(1312398381, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(1327693258, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
} else {
assertMatchesExactly(plan, fetchFromPartialRecordPlan(unionPlan(coveringIndexPlan().where(indexPlanOf(indexPlan().where(indexName("multi_index")))), inValuesJoinPlan(coveringIndexPlan().where(indexPlanOf(indexPlan().where(indexName("multi_index")).and(scanComparisons(equalities(exactly(equalsObject(new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, "odd")), equalsObject(new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, 0)), anyParameterComparison()))))))).where(inValuesList(equalsObject(Arrays.asList(1, 3))))).where(comparisonKey(concat(field("num_value_3_indexed"), primaryKey("MySimpleRecord"))))));
assertEquals(687944558, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(-913554911, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(-137232979, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
}
assertEquals(3 + 4 + 4, querySimpleRecordStore(hook, plan, EvaluationContext::empty, record -> {
assertThat(record.getStrValueIndexed(), is("odd"));
assertThat(record.getNumValue2(), is(0));
assertThat(record.getNumValue3Indexed(), anyOf(is(1), is(3), greaterThanOrEqualTo(4)));
}, TestHelpers::assertDiscardedNone));
}
use of com.apple.foundationdb.record.query.RecordQuery in project fdb-record-layer by FoundationDB.
the class FDBInQueryTest method enumIn.
/**
* Verify that enum field indexes are used to implement IN clauses.
*/
@DualPlannerTest
void enumIn() throws Exception {
RecordMetaDataHook hook = metaData -> {
final RecordTypeBuilder type = metaData.getRecordType("MyShapeRecord");
metaData.addIndex(type, new Index("color", field("color")));
};
setupEnumShapes(hook);
final var redBlue = asList(TestRecordsEnumProto.MyShapeRecord.Color.RED, TestRecordsEnumProto.MyShapeRecord.Color.BLUE);
RecordQuery query = RecordQuery.newBuilder().setRecordType("MyShapeRecord").setFilter(Query.field("color").in(redBlue)).build();
// Index(color [EQUALS $__in_color__0]) WHERE __in_color__0 IN [RED, BLUE]
RecordQueryPlan plan = planner.plan(query);
if (planner instanceof RecordQueryPlanner) {
assertMatchesExactly(plan, descendantPlans(indexPlan().where(indexName("color"))));
assertFalse(plan.hasRecordScan(), "should not use record scan");
assertEquals(-520431454, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(1447363737, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(1442809521, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
} else {
assertMatchesExactly(plan, fetchFromPartialRecordPlan(inValuesJoinPlan(coveringIndexPlan().where(indexPlanOf(indexPlan().where(indexName("color")).and(scanComparisons(equalities(exactly(anyParameterComparison()))))))).where(inValuesList(equalsObject(redBlue)))));
assertFalse(plan.hasRecordScan(), "should not use record scan");
assertEquals(1174038242, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(-1089300981, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(-1677589406, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
}
try (FDBRecordContext context = openContext()) {
openEnumRecordStore(context, hook);
int i = 0;
try (RecordCursorIterator<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan).asIterator()) {
while (cursor.hasNext()) {
FDBQueriedRecord<Message> rec = cursor.next();
TestRecordsEnumProto.MyShapeRecord.Builder shapeRec = TestRecordsEnumProto.MyShapeRecord.newBuilder();
shapeRec.mergeFrom(Objects.requireNonNull(rec).getRecord());
assertThat(shapeRec.getColor(), is(oneOf(TestRecordsEnumProto.MyShapeRecord.Color.RED, TestRecordsEnumProto.MyShapeRecord.Color.BLUE)));
i++;
}
}
assertEquals(18, i);
TestHelpers.assertDiscardedNone(context);
}
}
Aggregations