Search in sources :

Example 16 with ThenKeyExpression

use of com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression in project fdb-record-layer by FoundationDB.

the class RecordQueryPlanner method planOneOfThemWithComponent.

@Nullable
private ScoredPlan planOneOfThemWithComponent(@Nonnull CandidateScan candidateScan, @Nonnull KeyExpression indexExpr, @Nonnull OneOfThemWithComponent filter, @Nullable KeyExpression sort) {
    if (indexExpr instanceof FieldKeyExpression) {
        return null;
    } else if (indexExpr instanceof ThenKeyExpression) {
        ThenKeyExpression then = (ThenKeyExpression) indexExpr;
        return planOneOfThemWithComponent(candidateScan, then.getChildren().get(0), filter, sort);
    } else if (indexExpr instanceof NestingKeyExpression) {
        NestingKeyExpression indexNesting = (NestingKeyExpression) indexExpr;
        ScoredPlan plan = null;
        if (sort == null) {
            plan = planNesting(candidateScan, indexNesting, filter, null);
        } else if (sort instanceof FieldKeyExpression) {
            plan = null;
        } else if (sort instanceof ThenKeyExpression) {
            plan = null;
        } else if (sort instanceof NestingKeyExpression) {
            NestingKeyExpression sortNesting = (NestingKeyExpression) sort;
            plan = planNesting(candidateScan, indexNesting, filter, sortNesting);
        }
        if (plan != null) {
            List<QueryComponent> unsatisfied;
            if (!plan.unsatisfiedFilters.isEmpty()) {
                unsatisfied = Collections.singletonList(filter);
            } else {
                unsatisfied = Collections.emptyList();
            }
            // Right now it marks the whole nesting as unsatisfied, in theory there could be plans that handle that
            plan = new ScoredPlan(plan.score, plan.plan, unsatisfied, true);
        }
        return plan;
    }
    return null;
}
Also used : ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) Nullable(javax.annotation.Nullable)

Example 17 with ThenKeyExpression

use of com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression in project fdb-record-layer by FoundationDB.

the class RecordQueryPlanner method planVersion.

@Nullable
private ScoredPlan planVersion(@Nonnull CandidateScan candidateScan, @Nonnull KeyExpression indexExpr, @Nonnull QueryRecordFunctionWithComparison filter, @Nullable KeyExpression sort) {
    if (indexExpr instanceof VersionKeyExpression) {
        final Comparisons.Comparison comparison = filter.getComparison();
        final ScanComparisons comparisons = ScanComparisons.from(comparison);
        if (sort == null || sort.equals(VersionKeyExpression.VERSION)) {
            IndexScanParameters scanParameters = IndexScanComparisons.byValue(comparisons);
            RecordQueryPlan plan = new RecordQueryIndexPlan(candidateScan.index.getName(), scanParameters, candidateScan.reverse);
            return new ScoredPlan(1, plan, Collections.emptyList(), false);
        }
    } else if (indexExpr instanceof ThenKeyExpression) {
        ThenKeyExpression then = (ThenKeyExpression) indexExpr;
        if (sort == null) {
            // && !then.createsDuplicates()) {
            return planVersion(candidateScan, then.getChildren().get(0), filter, null);
        } else {
            return new AndWithThenPlanner(candidateScan, then, Collections.singletonList(filter), sort).plan();
        }
    }
    return null;
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) VersionKeyExpression(com.apple.foundationdb.record.metadata.expressions.VersionKeyExpression) IndexScanParameters(com.apple.foundationdb.record.provider.foundationdb.IndexScanParameters) ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) TimeWindowScanComparisons(com.apple.foundationdb.record.provider.foundationdb.leaderboard.TimeWindowScanComparisons) IndexScanComparisons(com.apple.foundationdb.record.provider.foundationdb.IndexScanComparisons) TimeWindowScanComparisons(com.apple.foundationdb.record.provider.foundationdb.leaderboard.TimeWindowScanComparisons) IndexScanComparisons(com.apple.foundationdb.record.provider.foundationdb.IndexScanComparisons) RankComparisons(com.apple.foundationdb.record.query.plan.planning.RankComparisons) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) RecordQueryIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan) Nullable(javax.annotation.Nullable)

Example 18 with ThenKeyExpression

use of com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression in project fdb-record-layer by FoundationDB.

the class RecordQueryPlanner method planQueryKeyExpressionWithComparison.

@Nullable
private ScoredPlan planQueryKeyExpressionWithComparison(@Nonnull CandidateScan candidateScan, @Nonnull KeyExpression indexExpr, @Nonnull QueryKeyExpressionWithComparison queryKeyExpressionWithComparison, @Nullable KeyExpression sort) {
    if (indexExpr.equals(queryKeyExpressionWithComparison.getKeyExpression()) && (sort == null || sort.equals(indexExpr))) {
        final Comparisons.Comparison comparison = queryKeyExpressionWithComparison.getComparison();
        final ScanComparisons scanComparisons = ScanComparisons.from(comparison);
        if (scanComparisons == null) {
            return null;
        }
        // Must be equal.
        final boolean strictlySorted = sort != null;
        return new ScoredPlan(1, valueScan(candidateScan, scanComparisons, strictlySorted));
    } else if (indexExpr instanceof ThenKeyExpression) {
        return new AndWithThenPlanner(candidateScan, (ThenKeyExpression) indexExpr, Collections.singletonList(queryKeyExpressionWithComparison), sort).plan();
    }
    return null;
}
Also used : ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) TimeWindowScanComparisons(com.apple.foundationdb.record.provider.foundationdb.leaderboard.TimeWindowScanComparisons) IndexScanComparisons(com.apple.foundationdb.record.provider.foundationdb.IndexScanComparisons) TimeWindowScanComparisons(com.apple.foundationdb.record.provider.foundationdb.leaderboard.TimeWindowScanComparisons) IndexScanComparisons(com.apple.foundationdb.record.provider.foundationdb.IndexScanComparisons) RankComparisons(com.apple.foundationdb.record.query.plan.planning.RankComparisons) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) Nullable(javax.annotation.Nullable)

Example 19 with ThenKeyExpression

use of com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression 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 20 with ThenKeyExpression

use of com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression in project fdb-record-layer by FoundationDB.

the class ValueIndexLikeExpansionVisitor method visitExpression.

@Nonnull
@Override
public GraphExpansion visitExpression(@Nonnull final ThenKeyExpression thenKeyExpression) {
    final ImmutableList.Builder<GraphExpansion> expandedPredicatesBuilder = ImmutableList.builder();
    final VisitorState state = getCurrentState();
    int currentOrdinal = state.getCurrentOrdinal();
    for (KeyExpression child : thenKeyExpression.getChildren()) {
        final GraphExpansion graphExpansion = pop(child.expand(push(state.withCurrentOrdinal(currentOrdinal))));
        currentOrdinal += graphExpansion.getResultValues().size();
        expandedPredicatesBuilder.add(graphExpansion);
    }
    return GraphExpansion.ofOthers(expandedPredicatesBuilder.build());
}
Also used : VisitorState(com.apple.foundationdb.record.query.plan.temp.ValueIndexLikeExpansionVisitor.VisitorState) ImmutableList(com.google.common.collect.ImmutableList) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) Nonnull(javax.annotation.Nonnull)

Aggregations

ThenKeyExpression (com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression)20 FieldKeyExpression (com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression)13 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)11 Nullable (javax.annotation.Nullable)10 EmptyKeyExpression (com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression)9 GroupingKeyExpression (com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression)9 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)8 ArrayList (java.util.ArrayList)7 Nonnull (javax.annotation.Nonnull)7 NestingKeyExpression (com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression)6 Test (org.junit.jupiter.api.Test)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 FDBError (com.apple.foundationdb.FDBError)4 FDBException (com.apple.foundationdb.FDBException)4 Range (com.apple.foundationdb.Range)4 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)4 CloseableAsyncIterator (com.apple.foundationdb.async.CloseableAsyncIterator)4 EvaluationContext (com.apple.foundationdb.record.EvaluationContext)4 ExecuteProperties (com.apple.foundationdb.record.ExecuteProperties)4 FunctionNames (com.apple.foundationdb.record.FunctionNames)4