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();
}
}
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);
}
}
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);
}
}
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));
}
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);
}
}
Aggregations