Search in sources :

Example 81 with RecordQueryPlan

use of com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan in project fdb-record-layer by FoundationDB.

the class TextIndexTest method queryMapDocumentsWithGroupedIndex.

@Nonnull
private List<Long> queryMapDocumentsWithGroupedIndex(@Nonnull String key, @Nonnull QueryComponent textFilter, long group, int planHash) throws InterruptedException, ExecutionException {
    if (!(textFilter instanceof ComponentWithComparison)) {
        throw new RecordCoreArgumentException("filter without comparison provided as text filter");
    }
    final QueryComponent filter = Query.and(Query.field("group").equalsValue(group), Query.field("entry").oneOfThem().matches(Query.and(Query.field("key").equalsValue(key), textFilter)));
    final Matcher<RecordQueryPlan> planMatcher = descendant(coveringIndexScan(textIndexScan(allOf(indexName(MAP_ON_VALUE_GROUPED_INDEX.getName()), groupingBounds(allOf(notNullValue(), hasTupleString("[[" + group + ", " + key + "],[" + group + ", " + key + "]]"))), textComparison(equalTo(((ComponentWithComparison) textFilter).getComparison()))))));
    return queryDocuments(Collections.singletonList(MAP_DOC), Collections.singletonList(field("doc_id")), filter, planHash, planMatcher).map(t -> t.getLong(0)).asList().get();
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) ComponentWithComparison(com.apple.foundationdb.record.query.expressions.ComponentWithComparison) RecordCoreArgumentException(com.apple.foundationdb.record.RecordCoreArgumentException) Nonnull(javax.annotation.Nonnull)

Example 82 with RecordQueryPlan

use of com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan in project fdb-record-layer by FoundationDB.

the class RankIndexTest method rankWithoutGroupRestriction.

@Test
public void rankWithoutGroupRestriction() throws Exception {
    // Grouped rank in filter but query results include all groups.
    RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank(Key.Expressions.field("score").groupBy(Key.Expressions.field("gender"))).equalsValue(1L)).build();
    RecordQueryPlan plan = planner.plan(query);
    assertFalse(plan.hasIndexScan("rank_by_gender"));
    assertTrue(plan.hasRecordScan());
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context);
        List<String> names = recordStore.executeQuery(plan).map(rec -> TestRecordsRankProto.BasicRankedRecord.newBuilder().mergeFrom(rec.getRecord()).getName()).asList().join();
        assertEquals(Arrays.asList("achilles", "laodice"), names);
        commit(context);
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) Matchers.hasToString(org.hamcrest.Matchers.hasToString) PlanMatchers.hasTupleString(com.apple.foundationdb.record.query.plan.match.PlanMatchers.hasTupleString) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Test(org.junit.jupiter.api.Test)

Example 83 with RecordQueryPlan

use of com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan in project fdb-record-layer by FoundationDB.

the class RankIndexTest method coveringScoreValues.

@Test
public void coveringScoreValues() throws Exception {
    RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.field("gender").equalsValue("M")).setRequiredResults(Arrays.asList(Key.Expressions.field("gender"), Key.Expressions.field("score"))).build();
    RecordQueryPlan plan = planner.plan(query);
    assertThat(plan, coveringIndexScan(indexScan(allOf(indexName("rank_by_gender"), indexScanType(IndexScanType.BY_VALUE), bounds(hasTupleString("[[M],[M]]"))))));
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context);
        List<Integer> scores = recordStore.executeQuery(plan).map(rec -> TestRecordsRankProto.BasicRankedRecord.newBuilder().mergeFrom(rec.getRecord()).getScore()).asList().join();
        assertEquals(Arrays.asList(75, 100), scores);
        commit(context);
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Test(org.junit.jupiter.api.Test)

Example 84 with RecordQueryPlan

use of com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan in project fdb-record-layer by FoundationDB.

the class RankIndexTest method containsNullScore.

@Test
public void containsNullScore() throws Exception {
    fdb = FDBDatabaseFactory.instance().getDatabase();
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context);
        // Undo loadRecords().
        recordStore.deleteAllRecords();
        TestRecordsRankProto.BasicRankedRecord.Builder rec = TestRecordsRankProto.BasicRankedRecord.newBuilder();
        rec.setName("achilles");
        rec.setGender("m");
        recordStore.saveRecord(rec.build());
        rec.clear();
        rec.setName("helen");
        rec.setScore(1);
        rec.setGender("f");
        recordStore.saveRecord(rec.build());
        commit(context);
    }
    List<RecordQuery> queries = Arrays.asList(RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank("score").equalsValue(0L)).build(), RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank("score").equalsValue(1L)).build(), RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank("score").lessThanOrEquals(1L)).build(), RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank("score").lessThan(1L)).build(), RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank("score").greaterThanOrEquals(0L)).build(), RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank("score").greaterThan(0L)).build());
    List<String> planStrings = Arrays.asList("Index(BasicRankedRecord$score [[0],[0]] BY_RANK)", "Index(BasicRankedRecord$score [[1],[1]] BY_RANK)", "Index(BasicRankedRecord$score ([null],[1]] BY_RANK)", "Index(BasicRankedRecord$score ([null],[1]) BY_RANK)", "Index(BasicRankedRecord$score [[0],> BY_RANK)", "Index(BasicRankedRecord$score ([0],> BY_RANK)");
    List<RecordQueryPlan> plans = queries.stream().map(planner::plan).collect(Collectors.toList());
    for (int i = 0; i < plans.size(); i++) {
        assertEquals(planStrings.get(i), plans.get(i).toString());
    }
    List<List<String>> resultLists = Arrays.asList(Collections.singletonList("achilles"), Collections.singletonList("helen"), Arrays.asList("achilles", "helen"), Collections.singletonList("achilles"), Arrays.asList("achilles", "helen"), Collections.singletonList("helen"));
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context);
        for (int i = 0; i < plans.size(); i++) {
            List<String> names = recordStore.executeQuery(plans.get(i)).map(record -> TestRecordsRankProto.BasicRankedRecord.newBuilder().mergeFrom(record.getRecord()).getName()).asList().join();
            assertEquals(resultLists.get(i), names);
        }
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) List(java.util.List) ArrayList(java.util.ArrayList) Matchers.hasToString(org.hamcrest.Matchers.hasToString) PlanMatchers.hasTupleString(com.apple.foundationdb.record.query.plan.match.PlanMatchers.hasTupleString) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Test(org.junit.jupiter.api.Test)

Example 85 with RecordQueryPlan

use of com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan in project fdb-record-layer by FoundationDB.

the class RankIndexTest method writeOnlyRankQuery.

@Test
public void writeOnlyRankQuery() throws Exception {
    assertThrows(RecordCoreException.class, () -> {
        fdb = FDBDatabaseFactory.instance().getDatabase();
        try (FDBRecordContext context = openContext()) {
            openRecordStore(context);
            recordStore.markIndexWriteOnly("rank_by_gender").join();
            // Re-open to reload state.
            openRecordStore(context);
            RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank(Key.Expressions.field("score").groupBy(Key.Expressions.field("gender"))).equalsValue(1L)).build();
            RecordQueryPlan plan = recordStore.planQuery(query);
            assertEquals("Scan(<,>) | [BasicRankedRecord] | rank([Field { 'gender' None}, Field { 'score' None}] group 1) EQUALS 1", plan.toString());
            List<TestRecordsRankProto.BasicRankedRecord> records = recordStore.executeQuery(plan).map(rec -> TestRecordsRankProto.BasicRankedRecord.newBuilder().mergeFrom(rec.getRecord()).build()).asList().get();
        } catch (ExecutionException e) {
            throw (Exception) e.getCause();
        }
    });
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Aggregations

RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)373 RecordQuery (com.apple.foundationdb.record.query.RecordQuery)265 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)245 Test (org.junit.jupiter.api.Test)228 FDBQueriedRecord (com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord)174 Message (com.google.protobuf.Message)167 Query (com.apple.foundationdb.record.query.expressions.Query)123 Tags (com.apple.test.Tags)121 Tag (org.junit.jupiter.api.Tag)121 Index (com.apple.foundationdb.record.metadata.Index)119 RecordQueryPlanner (com.apple.foundationdb.record.query.plan.RecordQueryPlanner)118 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)118 QueryComponent (com.apple.foundationdb.record.query.expressions.QueryComponent)117 List (java.util.List)114 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)114 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)113 Expressions.field (com.apple.foundationdb.record.metadata.Key.Expressions.field)107 Arrays (java.util.Arrays)107 Collections (java.util.Collections)106 ArrayList (java.util.ArrayList)100