Search in sources :

Example 36 with QueryComponent

use of com.apple.foundationdb.record.query.expressions.QueryComponent in project fdb-record-layer by FoundationDB.

the class FDBLuceneQueryTest method simpleLuceneScans.

@ParameterizedTest
@BooleanSource
public void simpleLuceneScans(boolean shouldDeferFetch) throws Exception {
    initializeFlat();
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context);
        final QueryComponent filter1 = new LuceneQueryComponent("civil blood makes civil hands unclean", Lists.newArrayList());
        // Query for full records
        RecordQuery query = RecordQuery.newBuilder().setRecordType(TextIndexTestUtils.SIMPLE_DOC).setFilter(filter1).build();
        setDeferFetchAfterUnionAndIntersection(shouldDeferFetch);
        Matcher<RecordQueryPlan> matcher = indexScan(allOf(indexScan("Complex$text_index"), indexScanType(IndexScanType.BY_LUCENE), bounds(hasTupleString("[[civil blood makes civil hands unclean],[civil blood makes civil hands unclean]]"))));
        RecordQueryPlan plan = planner.plan(query);
        // assertThat(plan, matcher);
        RecordCursor<FDBQueriedRecord<Message>> fdbQueriedRecordRecordCursor = recordStore.executeQuery(plan);
        RecordCursor<Tuple> map = fdbQueriedRecordRecordCursor.map(FDBQueriedRecord::getPrimaryKey);
        List<Long> primaryKeys = map.map(t -> t.getLong(0)).asList().get();
        assertEquals(ImmutableSet.of(2L, 4L), ImmutableSet.copyOf(primaryKeys));
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) LuceneQueryComponent(com.apple.foundationdb.record.query.expressions.LuceneQueryComponent) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) LuceneQueryComponent(com.apple.foundationdb.record.query.expressions.LuceneQueryComponent) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Tuple(com.apple.foundationdb.tuple.Tuple) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) BooleanSource(com.apple.test.BooleanSource)

Example 37 with QueryComponent

use of com.apple.foundationdb.record.query.expressions.QueryComponent in project fdb-record-layer by FoundationDB.

the class FDBLuceneQueryTest method testThenExpressionBeforeFieldExpression.

@ParameterizedTest
@BooleanSource
public void testThenExpressionBeforeFieldExpression(boolean shouldDeferFetch) throws Exception {
    initializeNestedWithField();
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context);
        final QueryComponent filter1 = new LuceneQueryComponent("*:*", Lists.newArrayList("doc_id"));
        RecordQuery query = RecordQuery.newBuilder().setRecordType(MAP_DOC).setFilter(filter1).build();
        setDeferFetchAfterUnionAndIntersection(shouldDeferFetch);
        RecordQueryPlan plan = planner.plan(query);
        RecordCursor<FDBQueriedRecord<Message>> fdbQueriedRecordRecordCursor = recordStore.executeQuery(plan);
        RecordCursor<Tuple> map = fdbQueriedRecordRecordCursor.map(FDBQueriedRecord::getPrimaryKey);
        List<Long> primaryKeys = map.map(t -> t.getLong(0)).asList().get();
        assertEquals(ImmutableSet.of(0L, 1L, 2L), ImmutableSet.copyOf(primaryKeys));
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) LuceneQueryComponent(com.apple.foundationdb.record.query.expressions.LuceneQueryComponent) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) LuceneQueryComponent(com.apple.foundationdb.record.query.expressions.LuceneQueryComponent) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Tuple(com.apple.foundationdb.tuple.Tuple) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) BooleanSource(com.apple.test.BooleanSource)

Example 38 with QueryComponent

use of com.apple.foundationdb.record.query.expressions.QueryComponent in project fdb-record-layer by FoundationDB.

the class FDBLuceneQueryTest method misMatchQueryShouldReturnNoResult.

@ParameterizedTest
@BooleanSource
public void misMatchQueryShouldReturnNoResult(boolean shouldDeferFetch) throws Exception {
    initializeFlat();
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context);
        final QueryComponent filter1 = new LuceneQueryComponent("doesNotExist", Lists.newArrayList("text"), true);
        // Query for full records
        RecordQuery query = RecordQuery.newBuilder().setRecordType(TextIndexTestUtils.SIMPLE_DOC).setFilter(filter1).build();
        setDeferFetchAfterUnionAndIntersection(shouldDeferFetch);
        RecordQueryPlan plan = planner.plan(query);
        Matcher<RecordQueryPlan> matcher = indexScan(allOf(indexScan("Complex$text_index"), indexScanType(IndexScanType.BY_LUCENE_FULL_TEXT), bounds(hasTupleString("[[doesNotExist],[doesNotExist]]"))));
        assertThat(plan, matcher);
        List<Long> primaryKeys = recordStore.executeQuery(plan).map(FDBQueriedRecord::getPrimaryKey).map(t -> t.getLong(0)).asList().get();
        assertEquals(ImmutableSet.of(), ImmutableSet.copyOf(primaryKeys));
        if (shouldDeferFetch) {
            assertLoadRecord(3, context);
        } else {
            assertLoadRecord(4, context);
        }
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) LuceneQueryComponent(com.apple.foundationdb.record.query.expressions.LuceneQueryComponent) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) LuceneQueryComponent(com.apple.foundationdb.record.query.expressions.LuceneQueryComponent) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) BooleanSource(com.apple.test.BooleanSource)

Example 39 with QueryComponent

use of com.apple.foundationdb.record.query.expressions.QueryComponent in project fdb-record-layer by FoundationDB.

the class FunctionKeyIndexTest method testOneOfQueryCoveringValueIndex.

@ParameterizedTest
@BooleanSource
public void testOneOfQueryCoveringValueIndex(boolean negated) throws Exception {
    Index coveringIndex = new Index("int_str_index", "int_value", "str_value");
    Records records = Records.create();
    for (int i = 0; i < 10; i++) {
        String strValue = (char) ('a' + i) + "_" + (char) ('b' + i);
        records.add(i, strValue);
    }
    saveRecords(records, coveringIndex);
    QueryComponent funcFilter = Query.keyExpression(function("chars", field("str_value"))).oneOfThem().equalsValue("c");
    if (negated) {
        funcFilter = Query.not(funcFilter);
    }
    RecordQuery query = RecordQuery.newBuilder().setRecordType("TypesRecord").setFilter(Query.and(Query.field("int_value").greaterThan(1), funcFilter)).setRequiredResults(Collections.singletonList(field("int_value"))).build();
    // Covering(Index(int_str_index ([1],>) -> [int_value: KEY[0], long_value: KEY[2], str_value: KEY[1]]) | ANY chars(Field { 'str_value' None}) EQUALS c
    RecordQueryPlan plan = planner.plan(query);
    assertThat(plan, PlanMatchers.filter(funcFilter, coveringIndexScan(indexScan(allOf(indexName(coveringIndex.getName()), bounds(hasTupleString("([1],>")))))));
    if (negated) {
        assertEquals(-1785473858, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
        assertEquals(-842591344, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
        assertEquals(-1235952736, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
    } else {
        assertEquals(-1785473859, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
        assertEquals(-1937041998, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
        assertEquals(1964563906, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
    }
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context, coveringIndex);
        List<Integer> results = recordStore.executeQuery(plan).map(r -> fromMessage(r.getRecord()).getIntValue()).asList().join();
        // 0 - 1 indexed out; 2 returned; 3 - 9 filtered out; last two reversed when negated.
        if (negated) {
            assertEquals(Arrays.asList(3, 4, 5, 6, 7, 8, 9), results);
            assertDiscardedExactly(1, context);
        } else {
            assertEquals(Collections.singletonList(2), results);
            assertDiscardedExactly(10 - 2 - 1, context);
        }
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) Index(com.apple.foundationdb.record.metadata.Index) PlanMatchers.hasTupleString(com.apple.foundationdb.record.query.plan.match.PlanMatchers.hasTupleString) Matchers.containsString(org.hamcrest.Matchers.containsString) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) BooleanSource(com.apple.test.BooleanSource)

Example 40 with QueryComponent

use of com.apple.foundationdb.record.query.expressions.QueryComponent in project fdb-record-layer by FoundationDB.

the class FunctionKeyIndexTest method testOneOfQueryFunctionIndex.

@Test
public void testOneOfQueryFunctionIndex() throws Exception {
    Index funcIndex = new Index("chars_index", function("chars", field("str_value")), IndexTypes.VALUE);
    Records records = Records.create();
    for (int i = 0; i < 10; i++) {
        String strValue = (char) ('a' + i) + "_" + (char) ('b' + i);
        records.add(i, strValue);
    }
    saveRecords(records, funcIndex);
    QueryComponent filter = Query.keyExpression(funcIndex.getRootExpression()).oneOfThem().equalsValue("c");
    RecordQuery query = RecordQuery.newBuilder().setRecordType("TypesRecord").setFilter(filter).build();
    // Index(chars_index [[c],[c]])
    RecordQueryPlan plan = planner.plan(query);
    assertThat(plan, primaryKeyDistinct(indexScan(allOf(indexName(funcIndex.getName()), bounds(hasTupleString("[[c],[c]]"))))));
    assertEquals(-76945989, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
    assertEquals(1921601810, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
    assertEquals(-2031924515, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context, funcIndex);
        int count = 0;
        try (RecordCursorIterator<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan).asIterator()) {
            while (cursor.hasNext()) {
                FDBQueriedRecord<Message> queriedRecord = cursor.next();
                TypesRecord record = fromMessage(queriedRecord.getRecord());
                assertTrue(records.contains(record));
                String str = record.getStrValue();
                assertThat(str, containsString("c"));
                ++count;
            }
        }
        assertEquals(2, count);
        assertDiscardedNone(context);
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) Message(com.google.protobuf.Message) Index(com.apple.foundationdb.record.metadata.Index) TypesRecord(com.apple.foundationdb.record.TestDataTypesProto.TypesRecord) PlanMatchers.hasTupleString(com.apple.foundationdb.record.query.plan.match.PlanMatchers.hasTupleString) Matchers.containsString(org.hamcrest.Matchers.containsString) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

QueryComponent (com.apple.foundationdb.record.query.expressions.QueryComponent)96 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)58 RecordQuery (com.apple.foundationdb.record.query.RecordQuery)44 Test (org.junit.jupiter.api.Test)41 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)37 ArrayList (java.util.ArrayList)30 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)29 FDBQueriedRecord (com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord)25 Index (com.apple.foundationdb.record.metadata.Index)24 Nonnull (javax.annotation.Nonnull)23 Comparisons (com.apple.foundationdb.record.query.expressions.Comparisons)22 Message (com.google.protobuf.Message)22 Query (com.apple.foundationdb.record.query.expressions.Query)20 RecordQueryPlanner (com.apple.foundationdb.record.query.plan.RecordQueryPlanner)20 List (java.util.List)20 Collections (java.util.Collections)19 Nullable (javax.annotation.Nullable)18 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)18 PlanHashable (com.apple.foundationdb.record.PlanHashable)17 Expressions.field (com.apple.foundationdb.record.metadata.Key.Expressions.field)17