Search in sources :

Example 6 with FilterSatisfiedMask

use of com.apple.foundationdb.record.query.plan.planning.FilterSatisfiedMask in project fdb-record-layer by FoundationDB.

the class ComposedBitmapIndexAggregate method separateGroupFilters.

private static boolean separateGroupFilters(@Nonnull QueryComponent filter, @Nonnull IndexAggregateFunctionCall indexAggregateFunctionCall, @Nonnull List<QueryComponent> commonFilters, @Nonnull List<QueryComponent> indexFilters) {
    QueryToKeyMatcher matcher = new QueryToKeyMatcher(filter);
    FilterSatisfiedMask filterMask = FilterSatisfiedMask.of(filter);
    QueryToKeyMatcher.Match match = matcher.matchesCoveringKey(indexAggregateFunctionCall.getGroupingKeyExpression().getGroupingSubKey(), filterMask);
    if (match.getType() != QueryToKeyMatcher.MatchType.EQUALITY) {
        // Did not manage to fully restrict the grouping key.
        return false;
    }
    // The position key(s) can also be constrained with inequalities and those go among the group filters.
    matcher.matchesCoveringKey(indexAggregateFunctionCall.getGroupedExpression(), filterMask);
    if (filterMask.allSatisfied()) {
        // Not enough conditions left over.
        return false;
    }
    for (FilterSatisfiedMask child : filterMask.getChildren()) {
        // Any left-over filter not matching either of those must match some per-index key.
        if (child.allSatisfied()) {
            commonFilters.add(child.getFilter());
        } else {
            indexFilters.add(child.getFilter());
        }
    }
    return true;
}
Also used : FilterSatisfiedMask(com.apple.foundationdb.record.query.plan.planning.FilterSatisfiedMask) QueryToKeyMatcher(com.apple.foundationdb.record.query.QueryToKeyMatcher)

Example 7 with FilterSatisfiedMask

use of com.apple.foundationdb.record.query.plan.planning.FilterSatisfiedMask 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 8 with FilterSatisfiedMask

use of com.apple.foundationdb.record.query.plan.planning.FilterSatisfiedMask in project fdb-record-layer by FoundationDB.

the class QueryToKeyMatcher method matches.

@Nonnull
private Match matches(@Nonnull OneOfThemWithComponent query, @Nonnull NestingKeyExpression key, @Nonnull MatchingMode matchingMode, @Nullable FilterSatisfiedMask filterMask) {
    if (key.getParent().getFanType() != KeyExpression.FanType.FanOut) {
        return Match.none();
    } else {
        if (Objects.equals(query.getFieldName(), key.getParent().getFieldName())) {
            FilterSatisfiedMask childMask = filterMask != null ? filterMask.getChild(query.getChild()) : null;
            Match childMatch = matches(query.getChild(), key.getChild(), matchingMode, childMask);
            if (childMask != null && childMask.isSatisfied() && filterMask.getExpression() == null) {
                filterMask.setExpression(key);
            }
            return childMatch;
        } else {
            return Match.none();
        }
    }
}
Also used : FilterSatisfiedMask(com.apple.foundationdb.record.query.plan.planning.FilterSatisfiedMask) Nonnull(javax.annotation.Nonnull)

Aggregations

FilterSatisfiedMask (com.apple.foundationdb.record.query.plan.planning.FilterSatisfiedMask)8 QueryComponent (com.apple.foundationdb.record.query.expressions.QueryComponent)3 Nonnull (javax.annotation.Nonnull)3 GroupingKeyExpression (com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression)2 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)2 QueryToKeyMatcher (com.apple.foundationdb.record.query.QueryToKeyMatcher)2 LuceneQueryComponent (com.apple.foundationdb.record.query.expressions.LuceneQueryComponent)2 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)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 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 IndexScanComparisons (com.apple.foundationdb.record.provider.foundationdb.IndexScanComparisons)1 TimeWindowScanComparisons (com.apple.foundationdb.record.provider.foundationdb.leaderboard.TimeWindowScanComparisons)1 Comparisons (com.apple.foundationdb.record.query.expressions.Comparisons)1 Comparison (com.apple.foundationdb.record.query.expressions.Comparisons.Comparison)1