Search in sources :

Example 1 with Comparison

use of com.apple.foundationdb.record.query.expressions.Comparisons.Comparison in project fdb-record-layer by FoundationDB.

the class QueryToKeyMatcher method matches.

@Nonnull
private Match matches(@Nonnull AndComponent query, @Nonnull KeyExpression key, @Nonnull MatchingMode matchingMode, @Nullable FilterSatisfiedMask filterMask) {
    final List<QueryComponent> listOfQueries = query.getChildren();
    final Iterator<KeyExpression> keyChildIterator;
    final int keyChildSize = key.getColumnSize();
    if (key instanceof ThenKeyExpression) {
        List<KeyExpression> children = ((ThenKeyExpression) key).getChildren();
        keyChildIterator = children.iterator();
    } else {
        keyChildIterator = Iterators.singletonIterator(key);
    }
    // Keep a local mask so that if we can only partially fulfill queries we don't pollute the main
    // one with our state
    FilterSatisfiedMask localMask = FilterSatisfiedMask.of(query);
    localMask.setExpression(key);
    List<Comparison> comparisons = new ArrayList<>(keyChildSize);
    while (keyChildIterator.hasNext()) {
        KeyExpression exp = keyChildIterator.next();
        // Look for a query segment that matches this expression
        boolean found = false;
        boolean foundInequality = false;
        final Iterator<FilterSatisfiedMask> childMaskIterator = localMask.getChildren().iterator();
        for (QueryComponent querySegment : listOfQueries) {
            Match match = matches(querySegment, exp, matchingMode, childMaskIterator.next());
            if (match.getType() != MatchType.NO_MATCH) {
                found = true;
                comparisons.addAll(match.getComparisons());
                if (match.getType() == MatchType.INEQUALITY) {
                    foundInequality = true;
                }
                break;
            }
        }
        if (!found) {
            return Match.none();
        }
        // Only the last comparison in the list can be inequality.
        if (localMask.allSatisfied() || foundInequality) {
            break;
        }
    }
    if (matchingMode.equals(MatchingMode.SATISFY_QUERY) && !localMask.allSatisfied() || matchingMode.equals(MatchingMode.COVER_KEY) && comparisons.size() < keyChildSize) {
        // filters that have not yet been satisfied.
        return Match.none();
    }
    if (filterMask != null) {
        filterMask.mergeWith(localMask);
    }
    return new Match(comparisons);
}
Also used : QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) FunctionKeyExpression(com.apple.foundationdb.record.metadata.expressions.FunctionKeyExpression) LiteralKeyExpression(com.apple.foundationdb.record.metadata.expressions.LiteralKeyExpression) RecordTypeKeyExpression(com.apple.foundationdb.record.metadata.expressions.RecordTypeKeyExpression) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) BaseKeyExpression(com.apple.foundationdb.record.metadata.expressions.BaseKeyExpression) ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) ArrayList(java.util.ArrayList) Comparison(com.apple.foundationdb.record.query.expressions.Comparisons.Comparison) QueryKeyExpressionWithComparison(com.apple.foundationdb.record.query.expressions.QueryKeyExpressionWithComparison) FieldWithComparison(com.apple.foundationdb.record.query.expressions.FieldWithComparison) OneOfThemWithComparison(com.apple.foundationdb.record.query.expressions.OneOfThemWithComparison) RecordTypeKeyComparison(com.apple.foundationdb.record.query.expressions.RecordTypeKeyComparison) FilterSatisfiedMask(com.apple.foundationdb.record.query.plan.planning.FilterSatisfiedMask) Nonnull(javax.annotation.Nonnull)

Example 2 with Comparison

use of com.apple.foundationdb.record.query.expressions.Comparisons.Comparison in project fdb-record-layer by FoundationDB.

the class Ordering method intersectEqualityBoundKeys.

/**
 * Intersect the equality-bound keys of two orderings. This method is usually passed in as a method reference to
 * {@link #combineEqualityBoundKeys(List, BinaryOperator)} as the binary operator.
 * @param left multimap of equality-bound keys of the left ordering (and their bindings)
 * @param right multimap of equality-bound keys of the right ordering (and their bindings)
 * @return new combined multimap of equality-bound keys (and their bindings)
 */
@Nonnull
public static SetMultimap<KeyExpression, Comparison> intersectEqualityBoundKeys(@Nonnull SetMultimap<KeyExpression, Comparison> left, @Nonnull SetMultimap<KeyExpression, Comparison> right) {
    final ImmutableSetMultimap.Builder<KeyExpression, Comparison> resultBuilder = ImmutableSetMultimap.builder();
    for (final Map.Entry<KeyExpression, Collection<Comparison>> rightEntry : right.asMap().entrySet()) {
        final KeyExpression rightKey = rightEntry.getKey();
        if (left.containsKey(rightKey)) {
            // 
            // Left side contains the same key. We can only retain this key in the result, however, if at least
            // one actual comparison on right is in left as well.
            // 
            final Collection<Comparison> rightComparisons = rightEntry.getValue();
            final boolean anyMatchingComparison = rightComparisons.stream().anyMatch(rightComparison -> left.containsEntry(rightKey, rightComparison));
            if (anyMatchingComparison) {
                resultBuilder.putAll(rightKey, rightComparisons);
            }
        }
    }
    return resultBuilder.build();
}
Also used : ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) Comparison(com.apple.foundationdb.record.query.expressions.Comparisons.Comparison) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) Collection(java.util.Collection) Map(java.util.Map) Nonnull(javax.annotation.Nonnull)

Aggregations

KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)2 Comparison (com.apple.foundationdb.record.query.expressions.Comparisons.Comparison)2 Nonnull (javax.annotation.Nonnull)2 BaseKeyExpression (com.apple.foundationdb.record.metadata.expressions.BaseKeyExpression)1 EmptyKeyExpression (com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression)1 FieldKeyExpression (com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression)1 FunctionKeyExpression (com.apple.foundationdb.record.metadata.expressions.FunctionKeyExpression)1 GroupingKeyExpression (com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression)1 LiteralKeyExpression (com.apple.foundationdb.record.metadata.expressions.LiteralKeyExpression)1 NestingKeyExpression (com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression)1 RecordTypeKeyExpression (com.apple.foundationdb.record.metadata.expressions.RecordTypeKeyExpression)1 ThenKeyExpression (com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression)1 FieldWithComparison (com.apple.foundationdb.record.query.expressions.FieldWithComparison)1 OneOfThemWithComparison (com.apple.foundationdb.record.query.expressions.OneOfThemWithComparison)1 QueryComponent (com.apple.foundationdb.record.query.expressions.QueryComponent)1 QueryKeyExpressionWithComparison (com.apple.foundationdb.record.query.expressions.QueryKeyExpressionWithComparison)1 RecordTypeKeyComparison (com.apple.foundationdb.record.query.expressions.RecordTypeKeyComparison)1 FilterSatisfiedMask (com.apple.foundationdb.record.query.plan.planning.FilterSatisfiedMask)1 ImmutableSetMultimap (com.google.common.collect.ImmutableSetMultimap)1 ArrayList (java.util.ArrayList)1