use of com.apple.foundationdb.record.query.expressions.FieldWithComparison in project fdb-record-layer by FoundationDB.
the class GroupingValidator method findGroupFieldFilter.
private static boolean findGroupFieldFilter(@Nonnull List<QueryComponent> filters, @Nonnull FieldKeyExpression groupField, @Nonnull List<QueryComponent> groupFilters, @Nonnull List<Comparisons.Comparison> groupComparisons) {
for (QueryComponent filter : filters) {
if (filter instanceof FieldWithComparison) {
FieldWithComparison comparisonFilter = (FieldWithComparison) filter;
if (comparisonFilter.getFieldName().equals(groupField.getFieldName()) && (comparisonFilter.getComparison().getType() == Comparisons.Type.EQUALS || comparisonFilter.getComparison().getType() == Comparisons.Type.IS_NULL)) {
groupFilters.add(filter);
groupComparisons.add(comparisonFilter.getComparison());
return true;
}
}
}
return false;
}
use of com.apple.foundationdb.record.query.expressions.FieldWithComparison in project fdb-record-layer by FoundationDB.
the class InExtractor method extractInClauses.
@SuppressWarnings("unchecked")
private QueryComponent extractInClauses() {
final AtomicInteger bindingIndex = new AtomicInteger();
return mapClauses(filter, (withComparison, fields) -> {
if (withComparison.getComparison().getType() == Comparisons.Type.IN) {
String bindingName = Bindings.Internal.IN.bindingName(withComparison.getName() + "__" + bindingIndex.getAndIncrement());
List<FieldKeyExpression> nestedFields = null;
if (fields != null && (withComparison instanceof FieldWithComparison || withComparison instanceof OneOfThemWithComparison)) {
nestedFields = new ArrayList<>(fields);
nestedFields.add(Key.Expressions.field(((BaseField) withComparison).getFieldName(), withComparison instanceof FieldWithComparison ? KeyExpression.FanType.None : KeyExpression.FanType.FanOut));
}
KeyExpression orderingKey = getOrderingKey(nestedFields);
if (withComparison.getComparison() instanceof Comparisons.ComparisonWithParameter) {
final String parameterName = ((Comparisons.ComparisonWithParameter) withComparison.getComparison()).getParameter();
inClauses.add(new InParameterClause(bindingName, parameterName, orderingKey));
} else {
final List<Object> comparand = (List<Object>) withComparison.getComparison().getComparand();
// ListComparison does not allow empty/null
if (comparand != null && comparand.size() == 1) {
return withComparison.withOtherComparison(new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, comparand.get(0)));
}
inClauses.add(new InValuesClause(bindingName, comparand, orderingKey));
}
return withComparison.withOtherComparison(new Comparisons.ParameterComparison(Comparisons.Type.EQUALS, bindingName, Bindings.Internal.IN));
} else {
return withComparison;
}
}, Collections.emptyList());
}
use of com.apple.foundationdb.record.query.expressions.FieldWithComparison in project fdb-record-layer by FoundationDB.
the class TextIndexTest method performQueryWithIndexScan.
@Nonnull
private Set<Long> performQueryWithIndexScan(@Nonnull RecordMetaDataHook hook, @Nonnull Index index, @Nonnull QueryComponent filter) throws Exception {
final ExecuteProperties executeProperties = ExecuteProperties.newBuilder().setTimeLimit(3000).build();
final RecordQuery query = RecordQuery.newBuilder().setRecordType(SIMPLE_DOC).setRequiredResults(Collections.singletonList(field("doc_id"))).setFilter(filter).build();
Set<Long> results = new HashSet<>();
RecordQueryPlan plan;
byte[] continuation;
try (FDBRecordContext context = openContext()) {
openRecordStore(context, hook);
RecordQueryPlanner planner = new RecordQueryPlanner(recordStore.getRecordMetaData(), recordStore.getRecordStoreState());
plan = planner.plan(query);
assertThat(filter, instanceOf(FieldWithComparison.class));
FieldWithComparison fieldFilter = (FieldWithComparison) filter;
if (fieldFilter.getComparison() instanceof Comparisons.TextContainsAllPrefixesComparison && ((Comparisons.TextContainsAllPrefixesComparison) fieldFilter.getComparison()).isStrict()) {
// Strict field field comparisons cannot be covering
assertThat(plan, descendant(textIndexScan(indexName(index.getName()))));
} else {
assertThat(plan, descendant(coveringIndexScan(textIndexScan(indexName(index.getName())))));
}
try (RecordCursor<Long> cursor = recordStore.executeQuery(plan, null, executeProperties).map(record -> record.getPrimaryKey().getLong(0))) {
cursor.forEach(results::add).get();
continuation = cursor.getNext().getContinuation().toBytes();
}
}
while (continuation != null) {
try (FDBRecordContext context = openContext()) {
openRecordStore(context, hook);
try (RecordCursor<Long> cursor = recordStore.executeQuery(plan, continuation, executeProperties).map(record -> record.getPrimaryKey().getLong(0))) {
cursor.forEach(results::add).get();
continuation = cursor.getNext().getContinuation().toBytes();
}
}
}
return results;
}
use of com.apple.foundationdb.record.query.expressions.FieldWithComparison in project fdb-record-layer by FoundationDB.
the class RankIndexTest method twoRankPredicates.
@Test
public void twoRankPredicates() throws Exception {
// Different rank predicates: at most one can be used in the scan.
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.and(Query.field("gender").equalsValue("M"), Query.rank(Key.Expressions.field("score").ungrouped()).lessThan(3L), Query.rank(Key.Expressions.field("score").groupBy(Key.Expressions.field("gender"))).equalsValue(1L))).build();
RecordQueryPlan plan = planner.plan(query);
assertThat(plan, scoreForRank(contains(hasToString("__rank_0 = BasicRankedRecord$score.score_for_rank_else_skip(3)")), fetch(filter(new FieldWithComparison("score", new Comparisons.ParameterComparison(Comparisons.Type.LESS_THAN, "__rank_0", Bindings.Internal.RANK)), coveringIndexScan(indexScan(allOf(indexName("rank_by_gender"), bounds(hasTupleString("[[M, 1],[M, 1]]")))))))));
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
List<String> names = recordStore.executeQuery(plan).map(rec -> TestRecordsRankProto.BasicRankedRecord.newBuilder().mergeFrom(rec.getRecord()).getName()).asList().join();
assertEquals(Arrays.asList("achilles"), names);
commit(context);
}
}
use of com.apple.foundationdb.record.query.expressions.FieldWithComparison in project fdb-record-layer by FoundationDB.
the class IndexAggregateFunctionCall method extractFieldPaths.
/**
* Helper method to extract a set of key expressions that are bound through some comparison in the
* query component passed in.
* @param queryComponent the query component
* @param predicate a predicate used for filtering each encountered {@link FieldWithComparison}
* @return a set of {@link KeyExpression}s where each element is a key expression of a field (i.e. a
* {@link com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression}) or a simple nesting field
* (i.e. a {@link com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression}) that is bound
* through some comparison
*/
@Nonnull
public static Set<KeyExpression> extractFieldPaths(@Nonnull QueryComponent queryComponent, @Nonnull final Predicate<FieldWithComparison> predicate) {
if (queryComponent instanceof BaseField) {
final BaseField baseField = (BaseField) queryComponent;
if (baseField instanceof NestedField) {
final NestedField nestedField = (NestedField) baseField;
final Set<KeyExpression> nestedExpressions = extractFieldPaths(nestedField.getChild(), predicate);
return nestedExpressions.stream().map(nestedExpression -> Key.Expressions.field(nestedField.getFieldName()).nest(nestedExpression)).collect(ImmutableSet.toImmutableSet());
}
if (baseField instanceof FieldWithComparison) {
final FieldWithComparison fieldWithComparison = (FieldWithComparison) baseField;
if (predicate.test(fieldWithComparison)) {
return ImmutableSet.of(Key.Expressions.field(fieldWithComparison.getFieldName()));
}
}
return ImmutableSet.of();
} else if (queryComponent instanceof AndComponent) {
final Set<KeyExpression> boundFields = Sets.newHashSet();
final AndComponent andComponent = (AndComponent) queryComponent;
andComponent.getChildren().forEach(child -> boundFields.addAll(extractEqualityBoundFields(child)));
return boundFields;
}
return ImmutableSet.of();
}
Aggregations