Search in sources :

Example 16 with EvaluationContext

use of com.apple.foundationdb.record.EvaluationContext in project fdb-record-layer by FoundationDB.

the class FDBRecordStoreQueryTest method testParameterQuery1.

/**
 * Verify that index lookups work with parameterized queries.
 */
@DualPlannerTest
void testParameterQuery1() throws Exception {
    RecordMetaDataHook hook = complexQuerySetupHook();
    complexQuerySetup(hook);
    RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.and(Query.field("str_value_indexed").equalsParameter("1"), Query.field("num_value_2").equalsParameter("2"))).build();
    // Index(multi_index [EQUALS $1, EQUALS $2])
    RecordQueryPlan plan = planner.plan(query);
    assertMatchesExactly(plan, indexPlan().where(indexName("multi_index")).and(scanComparisons(range("[EQUALS $1, EQUALS $2]"))));
    assertEquals(584809367, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
    assertEquals(1148926968, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
    assertEquals(1148926968, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, hook);
        for (int attempt = 1; attempt <= 2; attempt++) {
            String strValue;
            int numValue2;
            switch(attempt) {
                case 1:
                    strValue = "even";
                    numValue2 = 1;
                    break;
                case 2:
                default:
                    strValue = "odd";
                    numValue2 = 2;
                    break;
            }
            Bindings.Builder bindings = Bindings.newBuilder();
            bindings.set("1", strValue);
            bindings.set("2", numValue2);
            EvaluationContext evaluationContext = EvaluationContext.forBindings(bindings.build());
            int i = 0;
            try (RecordCursorIterator<FDBQueriedRecord<Message>> cursor = plan.execute(recordStore, evaluationContext).asIterator()) {
                while (cursor.hasNext()) {
                    FDBQueriedRecord<Message> rec = cursor.next();
                    TestRecords1Proto.MySimpleRecord.Builder myrec = TestRecords1Proto.MySimpleRecord.newBuilder();
                    myrec.mergeFrom(rec.getRecord());
                    assertEquals(strValue, myrec.getStrValueIndexed());
                    assertTrue((myrec.getNumValue2() % 3) == numValue2);
                    i++;
                }
            }
            assertEquals(16, i);
        }
        assertDiscardedNone(context);
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) Message(com.google.protobuf.Message) Bindings(com.apple.foundationdb.record.Bindings) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) EvaluationContext(com.apple.foundationdb.record.EvaluationContext) RecordQuery(com.apple.foundationdb.record.query.RecordQuery)

Example 17 with EvaluationContext

use of com.apple.foundationdb.record.EvaluationContext in project fdb-record-layer by FoundationDB.

the class FDBRecordStoreQueryTestBase method querySimpleRecordStoreWithContinuation.

/**
 * A query execution utility that can handle continuations. This is very similar to the above {@link #querySimpleRecordStore}
 * with the additional support for {@link ExecuteProperties} and continuation.
 * This method returns the last result encountered. In the case where the row limit was encountered, this would be the one
 * result that contains the continuation that should be used on the next call.
 * @param recordMetaDataHook Metadata hook to invoke while opening store
 * @param plan the plan to execute
 * @param contextSupplier provider method to get execution context
 * @param continuation execution continuation
 * @param executeProperties execution properties to pass into the execute method
 * @param checkNumRecords Consumer that verifies correct number of records returned
 * @param checkRecord Consumer that asserts every record retrieved
 * @param checkDiscarded Consumer that asserts the number of discarded records
 * @return the last result from the cursor
 * @throws Throwable any thrown exception, or its cause if the exception is a {@link ExecutionException}
 */
protected RecordCursorResult<FDBQueriedRecord<Message>> querySimpleRecordStoreWithContinuation(@Nonnull RecordMetaDataHook recordMetaDataHook, @Nonnull RecordQueryPlan plan, @Nonnull Supplier<EvaluationContext> contextSupplier, @Nullable byte[] continuation, @Nonnull ExecuteProperties executeProperties, @Nonnull Consumer<Integer> checkNumRecords, @Nonnull Consumer<TestRecords1Proto.MySimpleRecord.Builder> checkRecord, @Nonnull Consumer<FDBRecordContext> checkDiscarded) throws Throwable {
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, recordMetaDataHook);
        AtomicInteger i = new AtomicInteger(0);
        CompletableFuture<RecordCursorResult<FDBQueriedRecord<Message>>> lastResult;
        RecordCursor<FDBQueriedRecord<Message>> cursor = plan.execute(recordStore, contextSupplier.get(), continuation, executeProperties);
        lastResult = cursor.forEachResult(result -> {
            TestRecords1Proto.MySimpleRecord.Builder myrec = TestRecords1Proto.MySimpleRecord.newBuilder();
            myrec.mergeFrom(result.get().getRecord());
            checkRecord.accept(myrec);
            i.incrementAndGet();
        });
        lastResult.get();
        checkNumRecords.accept(i.get());
        checkDiscarded.accept(context);
        // TODO a hack until this gets refactored properly
        clearStoreCounter(context);
        return lastResult.get();
    } catch (ExecutionException ex) {
        throw ex.getCause();
    }
}
Also used : IntStream(java.util.stream.IntStream) Assertions.fail(org.junit.jupiter.api.Assertions.fail) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) TestRecords3Proto(com.apple.foundationdb.record.TestRecords3Proto) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) CompletableFuture(java.util.concurrent.CompletableFuture) TestRecordsTupleFieldsProto(com.apple.foundationdb.record.TestRecordsTupleFieldsProto) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) RecordQueryPlanner(com.apple.foundationdb.record.query.plan.RecordQueryPlanner) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) RecordCursorResult(com.apple.foundationdb.record.RecordCursorResult) TestHelpers(com.apple.foundationdb.record.TestHelpers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Expressions.concatenateFields(com.apple.foundationdb.record.metadata.Key.Expressions.concatenateFields) RecordCursorIterator(com.apple.foundationdb.record.RecordCursorIterator) BiConsumer(java.util.function.BiConsumer) Expressions.concat(com.apple.foundationdb.record.metadata.Key.Expressions.concat) Nonnull(javax.annotation.Nonnull) Expressions.field(com.apple.foundationdb.record.metadata.Key.Expressions.field) Nullable(javax.annotation.Nullable) TestRecords4Proto(com.apple.foundationdb.record.TestRecords4Proto) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) RecordTypeBuilder(com.apple.foundationdb.record.metadata.RecordTypeBuilder) TupleFieldsHelper(com.apple.foundationdb.record.metadata.expressions.TupleFieldsHelper) FanType(com.apple.foundationdb.record.metadata.expressions.KeyExpression.FanType) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) FDBRecordStoreTestBase(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreTestBase) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) List(java.util.List) BindingMatcher(com.apple.foundationdb.record.query.plan.temp.matchers.BindingMatcher) TestRecords5Proto(com.apple.foundationdb.record.TestRecords5Proto) Index(com.apple.foundationdb.record.metadata.Index) EvaluationContext(com.apple.foundationdb.record.EvaluationContext) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TestRecordsEnumProto(com.apple.foundationdb.record.TestRecordsEnumProto) Message(com.google.protobuf.Message) RecordCursor(com.apple.foundationdb.record.RecordCursor) TestRecordsWithHeaderProto(com.apple.foundationdb.record.TestRecordsWithHeaderProto) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) Message(com.google.protobuf.Message) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) RecordTypeBuilder(com.apple.foundationdb.record.metadata.RecordTypeBuilder) RecordCursorResult(com.apple.foundationdb.record.RecordCursorResult) ExecutionException(java.util.concurrent.ExecutionException)

Example 18 with EvaluationContext

use of com.apple.foundationdb.record.EvaluationContext in project fdb-record-layer by FoundationDB.

the class FDBSimpleJoinQueryTest method joinChildToParent.

/**
 * Verify that simple binding joins in parent/child relationships work.
 */
@Test
public void joinChildToParent() throws Exception {
    createJoinRecords(false);
    RecordQuery parentQuery = RecordQuery.newBuilder().setRecordType("MyParentRecord").setFilter(Query.field("str_value_indexed").equalsValue("even")).build();
    RecordQuery childQuery = RecordQuery.newBuilder().setRecordType("MyChildRecord").setFilter(Query.field("parent_rec_no").equalsParameter("parent")).build();
    RecordQueryPlan parentPlan = planner.plan(parentQuery);
    RecordQueryPlan childPlan = planner.plan(childQuery);
    try (FDBRecordContext context = openContext()) {
        openJoinRecordStore(context);
        RecordCursor<FDBQueriedRecord<Message>> childCursor = RecordCursor.flatMapPipelined(ignore -> recordStore.executeQuery(parentPlan), (rec, ignore) -> {
            TestRecordsParentChildRelationshipProto.MyParentRecord.Builder parentRec = TestRecordsParentChildRelationshipProto.MyParentRecord.newBuilder();
            parentRec.mergeFrom(rec.getRecord());
            EvaluationContext childContext = EvaluationContext.forBinding("parent", parentRec.getRecNo());
            return childPlan.execute(recordStore, childContext);
        }, null, 10);
        RecordCursor<String> resultsCursor = childCursor.map(rec -> {
            TestRecordsParentChildRelationshipProto.MyChildRecord.Builder childRec = TestRecordsParentChildRelationshipProto.MyChildRecord.newBuilder();
            childRec.mergeFrom(rec.getRecord());
            return childRec.getStrValue();
        });
        assertEquals(Arrays.asList("2.1", "2.2", "2.3", "4.1", "4.2", "4.3"), resultsCursor.asList().join());
        assertDiscardedNone(context);
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) EvaluationContext(com.apple.foundationdb.record.EvaluationContext) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Test(org.junit.jupiter.api.Test)

Example 19 with EvaluationContext

use of com.apple.foundationdb.record.EvaluationContext in project fdb-record-layer by FoundationDB.

the class FDBSimpleJoinQueryTest method joinParentToChild.

/**
 * Verify that simple binding joins in parent/child relationships work.
 */
@Test
public void joinParentToChild() throws Exception {
    createJoinRecords(true);
    RecordQuery parentQuery = RecordQuery.newBuilder().setRecordType("MyParentRecord").setFilter(Query.field("str_value_indexed").equalsValue("even")).build();
    RecordQueryPlan parentPlan = planner.plan(parentQuery);
    RecordQueryPlan childPlan = new RecordQueryLoadByKeysPlan("children");
    try (FDBRecordContext context = openContext()) {
        openJoinRecordStore(context);
        RecordCursor<FDBQueriedRecord<Message>> parentCursor = recordStore.executeQuery(parentPlan);
        RecordCursor<FDBQueriedRecord<Message>> childCursor = RecordCursor.flatMapPipelined(ignore -> recordStore.executeQuery(parentPlan), (rec, ignore) -> {
            TestRecordsParentChildRelationshipProto.MyParentRecord.Builder parentRec = TestRecordsParentChildRelationshipProto.MyParentRecord.newBuilder();
            parentRec.mergeFrom(rec.getRecord());
            EvaluationContext childContext = EvaluationContext.forBinding("children", parentRec.getChildRecNosList().stream().map(Tuple::from).collect(Collectors.toList()));
            return childPlan.execute(recordStore, childContext);
        }, null, 10);
        RecordCursor<String> resultsCursor = childCursor.map(rec -> {
            TestRecordsParentChildRelationshipProto.MyChildRecord.Builder childRec = TestRecordsParentChildRelationshipProto.MyChildRecord.newBuilder();
            childRec.mergeFrom(rec.getRecord());
            return childRec.getStrValue();
        });
        assertEquals(Arrays.asList("2.1", "2.2", "2.3", "4.1", "4.2", "4.3"), resultsCursor.asList().join());
        assertDiscardedNone(context);
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) RecordQueryLoadByKeysPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryLoadByKeysPlan) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) EvaluationContext(com.apple.foundationdb.record.EvaluationContext) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Tuple(com.apple.foundationdb.tuple.Tuple) Test(org.junit.jupiter.api.Test)

Example 20 with EvaluationContext

use of com.apple.foundationdb.record.EvaluationContext in project fdb-record-layer by FoundationDB.

the class FDBSortQueryIndexSelectionTest method sortWithScannableFilterOnIndex.

/**
 * Verify that if the sort matches an index that can satisfy a filter that the index is used.
 */
@ParameterizedTest
@MethodSource("hooks")
void sortWithScannableFilterOnIndex(RecordMetaDataHook hook, PlannableIndexTypes indexTypes) throws Exception {
    setupSimpleRecordStore(hook, (i, builder) -> builder.setRecNo(i).setNumValue2(i % 2).setNumValue3Indexed(i % 3));
    RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.field("num_value_3_indexed").greaterThanOrEquals(2)).setSort(field("num_value_3_indexed")).build();
    setupPlanner(indexTypes);
    // Index(MySimpleRecord$num_value_3_indexed [[2],>)
    RecordQueryPlan plan = planner.plan(query);
    assertThat(plan, indexScan(allOf(indexName("MySimpleRecord$num_value_3_indexed"), bounds(hasTupleString("[[2],>")))));
    assertEquals(1008857205, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
    assertEquals(-2059045225, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
    assertEquals(-1347749581, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
    AtomicInteger lastNumValue3 = new AtomicInteger(Integer.MIN_VALUE);
    int returned = querySimpleRecordStore(hook, plan, EvaluationContext::empty, builder -> {
        assertThat(builder.getNumValue3Indexed(), greaterThanOrEqualTo(2));
        assertThat(builder.getNumValue3Indexed(), greaterThanOrEqualTo(lastNumValue3.get()));
        lastNumValue3.set(builder.getNumValue3Indexed());
    }, TestHelpers::assertDiscardedNone);
    assertEquals(33, returned);
    // reset planner
    setupPlanner(null);
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestHelpers(com.apple.foundationdb.record.TestHelpers) EvaluationContext(com.apple.foundationdb.record.EvaluationContext) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

EvaluationContext (com.apple.foundationdb.record.EvaluationContext)43 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)33 RecordQuery (com.apple.foundationdb.record.query.RecordQuery)31 Message (com.google.protobuf.Message)28 FDBQueriedRecord (com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord)27 List (java.util.List)27 PlanHashable (com.apple.foundationdb.record.PlanHashable)25 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)25 ImmutableList (com.google.common.collect.ImmutableList)24 Objects (java.util.Objects)24 Set (java.util.Set)23 Test (org.junit.jupiter.api.Test)23 ArrayList (java.util.ArrayList)22 Collections (java.util.Collections)22 Index (com.apple.foundationdb.record.metadata.Index)21 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)20 TestHelpers (com.apple.foundationdb.record.TestHelpers)20 Comparisons (com.apple.foundationdb.record.query.expressions.Comparisons)20 Query (com.apple.foundationdb.record.query.expressions.Query)20 Tags (com.apple.test.Tags)20