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