use of com.apple.foundationdb.record.query.expressions.QueryComponent in project fdb-record-layer by FoundationDB.
the class FunctionKeyIndexTest method testQueryFunctionIndex.
private void testQueryFunctionIndex(boolean functionQuery) throws Exception {
Index funcIndex = new Index("substr_index", function("substr", concat(field("str_value"), value(0), value(3))), IndexTypes.VALUE);
Index normalIndex = new Index("normal_index", field("str_value"), IndexTypes.VALUE);
Records records = Records.create();
for (int i = 0; i < 10; i++) {
String strValue = "ab" + Character.toString((char) ('c' + i)) + "_" + i;
records.add(i, strValue);
}
saveRecords(records, funcIndex, normalIndex);
QueryComponent filter;
if (functionQuery) {
filter = Query.and(Query.keyExpression(funcIndex.getRootExpression()).greaterThanOrEquals("abd"), Query.keyExpression(funcIndex.getRootExpression()).lessThanOrEquals("abg"));
} else {
filter = Query.and(Query.field("str_value").greaterThanOrEquals("abd"), Query.field("str_value").lessThanOrEquals("abg"));
}
RecordQuery query = RecordQuery.newBuilder().setRecordType("TypesRecord").setFilter(filter).build();
// Index(substr_index [[abd],[abg]])
RecordQueryPlan plan = planner.plan(query);
if (functionQuery) {
assertThat(plan, indexScan(allOf(indexName(funcIndex.getName()), bounds(hasTupleString("[[abd],[abg]]")))));
assertEquals(316561162, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(1660925199, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(33212272, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
} else {
// Here I'm really just making sure that (a) the substr_index is not selected, because the
// function call doesn't appear in the query anyway and (b) that the planner doesn't throw
// an exception or do something wonky as a result of the presence of this index.
assertThat(plan, indexScan(allOf(indexName(normalIndex.getName()), bounds(hasTupleString("[[abd],[abg]]")))));
assertEquals(1189784448, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(1463869061, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(-163843866, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
}
try (FDBRecordContext context = openContext()) {
openRecordStore(context, funcIndex, normalIndex);
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 = functionQuery ? record.getStrValue().substring(0, 3) : record.getStrValue();
assertThat(str, greaterThanOrEqualTo("abd"));
assertThat(str, lessThanOrEqualTo("abg"));
++count;
}
}
assertEquals(functionQuery ? 4 : 3, count);
assertDiscardedNone(context);
}
}
use of com.apple.foundationdb.record.query.expressions.QueryComponent in project fdb-record-layer by FoundationDB.
the class LucenePlanner method getScanForOrLucene.
private LuceneIndexQueryPlan getScanForOrLucene(@Nonnull Index index, final String parentFieldName, final OrComponent filter, final FilterSatisfiedMask filterMask, final ScanComparisons groupingComparisons) {
final Iterator<FilterSatisfiedMask> subFilterMasks = filterMask != null ? filterMask.getChildren().iterator() : null;
final List<QueryComponent> filters = filter.getChildren();
LuceneIndexQueryPlan combinedComparison = null;
for (QueryComponent subFilter : filters) {
final FilterSatisfiedMask childMask = subFilterMasks != null ? subFilterMasks.next() : null;
LuceneIndexQueryPlan childComparison = getComparisonsForLuceneFilter(index, parentFieldName, subFilter, childMask, groupingComparisons);
if (childComparison != null && childMask != null) {
childMask.setSatisfied(true);
combinedComparison = combinedComparison == null ? childComparison : LuceneIndexQueryPlan.merge(childComparison, combinedComparison, "OR");
}
}
if (filterMask != null && filterMask.getUnsatisfiedFilters().isEmpty()) {
filterMask.setSatisfied(true);
}
return combinedComparison;
}
use of com.apple.foundationdb.record.query.expressions.QueryComponent in project fdb-record-layer by FoundationDB.
the class CombineFilterRuleTest method combineFilter.
@Test
public void combineFilter() {
for (RecordQueryPlan basePlan : basePlans) {
QueryComponent filter1 = Query.field("testField").equalsValue(5);
QueryComponent filter2 = Query.field("testField2").equalsValue(10);
GroupExpressionRef<RelationalExpression> root = GroupExpressionRef.of(buildLogicalFilter(filter1, buildLogicalFilter(filter2, basePlan)));
TestRuleExecution execution = TestRuleExecution.applyRule(blankContext, rule, root);
assertTrue(execution.isRuleMatched());
assertTrue(execution.getResult().containsInMemo(buildLogicalFilter(Query.and(filter1, filter2), basePlan)));
}
}
use of com.apple.foundationdb.record.query.expressions.QueryComponent in project fdb-record-layer by FoundationDB.
the class CombineFilterRuleTest method doesNotCoalesce.
@Test
public void doesNotCoalesce() {
for (RecordQueryPlan basePlan : basePlans) {
QueryComponent filter1 = Query.field("testField").equalsValue(5);
GroupExpressionRef<RelationalExpression> root = GroupExpressionRef.of(buildLogicalFilter(filter1, buildLogicalFilter(filter1, basePlan)));
TestRuleExecution execution = TestRuleExecution.applyRule(blankContext, rule, root);
assertTrue(execution.isRuleMatched());
// this rule should not try to coalesce the two filters
assertTrue(root.containsInMemo(buildLogicalFilter(Query.and(filter1, filter1), basePlan)));
assertFalse(root.containsInMemo(buildLogicalFilter(filter1, basePlan)));
}
}
use of com.apple.foundationdb.record.query.expressions.QueryComponent in project fdb-record-layer by FoundationDB.
the class BooleanNormalizerTest method assertExpectedNormalization.
protected static void assertExpectedNormalization(@Nonnull final BooleanNormalizer normalizer, @Nonnull final QueryComponent expected, @Nonnull final QueryComponent given) {
final QueryComponent normalized = normalizer.normalize(given);
assertFilterEquals(expected, normalized);
if (!normalizer.isCheckForDuplicateConditions()) {
assertEquals(numberOfTerms(expected), normalizer.getNormalizedSize(given));
}
assertEquals(normalized, normalizer.normalize(normalized), "Normalized form should be stable");
}
Aggregations