Search in sources :

Example 1 with Bindings

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

the class FDBCollateQueryTest method queryNames.

protected List<String> queryNames(RecordQuery query, RecordMetaDataHook hook, String... bindings) throws Exception {
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, hook);
        Bindings.Builder builder = Bindings.newBuilder();
        for (int i = 0; i < bindings.length; i += 2) {
            builder.set(bindings[i], bindings[i + 1]);
        }
        return recordStore.planQuery(query).execute(recordStore, EvaluationContext.forBindings(builder.build())).map(r -> {
            TestRecords1Proto.MySimpleRecord record = TestRecords1Proto.MySimpleRecord.newBuilder().mergeFrom(r.getRecord()).build();
            return record.getStrValueIndexed();
        }).asList().join();
    }
}
Also used : TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) Bindings(com.apple.foundationdb.record.Bindings)

Example 2 with Bindings

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

the class FDBQueryCompatibilityTest method testParameterBindings.

@DualPlannerTest
public void testParameterBindings() throws Exception {
    RecordMetaDataHook hook = complexQuerySetupHook();
    complexQuerySetup(hook);
    BoundRecordQuery boundQuery = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.and(Query.field("str_value_indexed").equalsParameter("p1"), Query.field("str_value_indexed").equalsParameter("p2"), Query.field("num_value_3_indexed").equalsParameter("p3"), Query.field("num_value_2").equalsValue(0))).buildAndBind(recordStore, Bindings.newBuilder().set("p1", "whatever").set("p2", "whatever").set("p3", "-10").build());
    final RecordQueryPlan plan = planner.plan(boundQuery.getRecordQuery());
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, hook);
        final Bindings compatibleBindings = Bindings.newBuilder().set("p1", "even").set("p2", "even").set("p3", 3).build();
        assertTrue(boundQuery.isCompatible(recordStore, compatibleBindings));
        final Bindings incompatibleBindings = Bindings.newBuilder().set("p1", "even").set("p2", "odd").set("p3", 3).build();
        assertFalse(boundQuery.isCompatible(recordStore, incompatibleBindings));
        int i = 0;
        try (RecordCursorIterator<FDBQueriedRecord<Message>> cursor = plan.execute(recordStore, EvaluationContext.forBindings(compatibleBindings)).asIterator()) {
            while (cursor.hasNext()) {
                FDBQueriedRecord<Message> rec = cursor.next();
                TestRecords1Proto.MySimpleRecord.Builder myrec = TestRecords1Proto.MySimpleRecord.newBuilder();
                myrec.mergeFrom(Objects.requireNonNull(rec).getRecord());
                assertEquals("even", myrec.getStrValueIndexed());
                assertEquals(0, (myrec.getNumValue2() % 3));
                assertEquals(3, (myrec.getNumValue3Indexed() % 5));
                i++;
            }
        }
        assertEquals(3, i);
        TestHelpers.assertDiscardedNone(context);
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) BoundRecordQuery(com.apple.foundationdb.record.query.BoundRecordQuery) FDBQueriedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord) Message(com.google.protobuf.Message) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) Bindings(com.apple.foundationdb.record.Bindings)

Example 3 with Bindings

use of com.apple.foundationdb.record.Bindings 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 4 with Bindings

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

the class QueryExpressionTest method testParameterComparisonsSimple.

@Test
public void testParameterComparisonsSimple() throws Exception {
    final TestScalarFieldAccess rec = TestScalarFieldAccess.newBuilder().setField("abc").build();
    final QueryComponent equalsP1 = new FieldWithComparison("field", new Comparisons.ParameterComparison(Comparisons.Type.EQUALS, "p1"));
    final QueryComponent notEqualsP2 = new FieldWithComparison("field", new Comparisons.ParameterComparison(Comparisons.Type.NOT_EQUALS, "p2"));
    final Bindings b1 = Bindings.newBuilder().set("p1", "abc").set("p2", "xyz").build();
    assertTrue(evaluate(equalsP1, b1, rec));
    assertTrue(evaluate(notEqualsP2, b1, rec));
    final Bindings b2 = Bindings.newBuilder().set("p1", "foo").set("p2", "bar").build();
    assertFalse(evaluate(equalsP1, b2, rec));
    assertTrue(evaluate(notEqualsP2, b2, rec));
}
Also used : TestScalarFieldAccess(com.apple.foundationdb.record.metadata.ExpressionTestsProto.TestScalarFieldAccess) Bindings(com.apple.foundationdb.record.Bindings) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with Bindings

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

the class QueryExpressionTest method testParameterComparison.

protected void testParameterComparison(String name, String field, Object val1, Comparisons.Type type, Object val2, Boolean expected) throws Exception {
    if (type.isUnary()) {
        assertThrows(name, RecordCoreException.class, () -> new Comparisons.ParameterComparison(type, "fooParam"));
        return;
    }
    final TestScalarFieldAccess rec = createRecord(field, val1).build();
    try {
        final Comparisons.Comparison comparison = new Comparisons.ParameterComparison(type, "fooParam");
        final QueryComponent qc = new FieldWithComparison(field, comparison);
        final Bindings bindings = Bindings.newBuilder().set("fooParam", val2).build();
        if (val1 != null && val2 != null && (type == Comparisons.Type.IN && !(val2 instanceof List) || type.name().startsWith("TEXT_"))) {
            assertThrows(name, RecordCoreException.class, () -> evaluate(qc, bindings, rec));
        } else {
            assertEquals(expected, evaluate(qc, bindings, rec), name);
        }
    } catch (Exception e) {
        throw new AssertionError(name + " Threw: " + e.getMessage(), e);
    }
}
Also used : TestScalarFieldAccess(com.apple.foundationdb.record.metadata.ExpressionTestsProto.TestScalarFieldAccess) ArrayList(java.util.ArrayList) List(java.util.List) Bindings(com.apple.foundationdb.record.Bindings) RecordCoreException(com.apple.foundationdb.record.RecordCoreException)

Aggregations

Bindings (com.apple.foundationdb.record.Bindings)6 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)3 TestScalarFieldAccess (com.apple.foundationdb.record.metadata.ExpressionTestsProto.TestScalarFieldAccess)2 FDBQueriedRecord (com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord)2 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)2 Message (com.google.protobuf.Message)2 API (com.apple.foundationdb.annotation.API)1 EvaluationContext (com.apple.foundationdb.record.EvaluationContext)1 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)1 TestRecords1Proto (com.apple.foundationdb.record.TestRecords1Proto)1 BoundRecordQuery (com.apple.foundationdb.record.query.BoundRecordQuery)1 RecordQuery (com.apple.foundationdb.record.query.RecordQuery)1 CrossProduct.crossProduct (com.apple.foundationdb.record.query.combinatorics.CrossProduct.crossProduct)1 BooleanComponent.groupedComparisons (com.apple.foundationdb.record.query.expressions.BooleanComponent.groupedComparisons)1 Comparisons (com.apple.foundationdb.record.query.expressions.Comparisons)1 QueryComponent (com.apple.foundationdb.record.query.expressions.QueryComponent)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableNetwork (com.google.common.graph.ImmutableNetwork)1 Network (com.google.common.graph.Network)1