Search in sources :

Example 6 with ValuePredicate

use of com.apple.foundationdb.record.query.predicates.ValuePredicate in project fdb-record-layer by FoundationDB.

the class SelectExpression method partitionPredicates.

private static List<? extends QueryPredicate> partitionPredicates(final List<? extends QueryPredicate> predicates) {
    final ImmutableList<QueryPredicate> flattenedAndPredicates = predicates.stream().flatMap(predicate -> flattenAndPredicate(predicate).stream()).collect(ImmutableList.toImmutableList());
    // partition predicates in value-based predicates and non-value-based predicates
    final ImmutableList.Builder<PredicateWithValue> predicateWithValuesBuilder = ImmutableList.builder();
    final ImmutableList.Builder<QueryPredicate> resultPredicatesBuilder = ImmutableList.builder();
    for (final QueryPredicate flattenedAndPredicate : flattenedAndPredicates) {
        if (flattenedAndPredicate instanceof PredicateWithValue) {
            predicateWithValuesBuilder.add((PredicateWithValue) flattenedAndPredicate);
        } else {
            resultPredicatesBuilder.add(flattenedAndPredicate);
        }
    }
    final ImmutableList<PredicateWithValue> predicateWithValues = predicateWithValuesBuilder.build();
    final AliasMap boundIdentitiesMap = AliasMap.identitiesFor(flattenedAndPredicates.stream().flatMap(predicate -> predicate.getCorrelatedTo().stream()).collect(ImmutableSet.toImmutableSet()));
    final BoundEquivalence boundEquivalence = new BoundEquivalence(boundIdentitiesMap);
    final HashMultimap<Equivalence.Wrapper<Value>, PredicateWithValue> partitionedPredicatesWithValues = predicateWithValues.stream().collect(Multimaps.toMultimap(predicate -> boundEquivalence.wrap(predicate.getValue()), Function.identity(), HashMultimap::create));
    partitionedPredicatesWithValues.asMap().forEach((valueWrapper, predicatesOnValue) -> {
        final Value value = Objects.requireNonNull(valueWrapper.get());
        ComparisonRange resultRange = ComparisonRange.EMPTY;
        for (final PredicateWithValue predicateOnValue : predicatesOnValue) {
            if (predicateOnValue instanceof ValuePredicate) {
                final Comparisons.Comparison comparison = ((ValuePredicate) predicateOnValue).getComparison();
                final ComparisonRange.MergeResult mergeResult = resultRange.merge(comparison);
                resultRange = mergeResult.getComparisonRange();
                mergeResult.getResidualComparisons().forEach(residualComparison -> resultPredicatesBuilder.add(value.withComparison(residualComparison)));
            } else if (predicateOnValue instanceof Sargable) {
                final Sargable valueComparisonRangePredicate = (Sargable) predicateOnValue;
                final ComparisonRange comparisonRange = valueComparisonRangePredicate.getComparisonRange();
                final ComparisonRange.MergeResult mergeResult = resultRange.merge(comparisonRange);
                resultRange = mergeResult.getComparisonRange();
                mergeResult.getResidualComparisons().forEach(residualComparison -> resultPredicatesBuilder.add(value.withComparison(residualComparison)));
            } else {
                resultPredicatesBuilder.add(predicateOnValue);
            }
        }
        if (!resultRange.isEmpty()) {
            resultPredicatesBuilder.add(ValueComparisonRangePredicate.sargable(value, resultRange));
        }
    });
    return resultPredicatesBuilder.build();
}
Also used : ValuePredicate(com.apple.foundationdb.record.query.predicates.ValuePredicate) RelationalExpressionWithPredicates(com.apple.foundationdb.record.query.plan.temp.RelationalExpressionWithPredicates) Quantifier(com.apple.foundationdb.record.query.plan.temp.Quantifier) PredicateWithValue(com.apple.foundationdb.record.query.predicates.PredicateWithValue) ValueComparisonRangePredicate(com.apple.foundationdb.record.query.predicates.ValueComparisonRangePredicate) Function(java.util.function.Function) PredicateMap(com.apple.foundationdb.record.query.plan.temp.PredicateMap) Multimaps(com.google.common.collect.Multimaps) PartialMatch(com.apple.foundationdb.record.query.plan.temp.PartialMatch) HashMultimap(com.google.common.collect.HashMultimap) ImmutableList(com.google.common.collect.ImmutableList) ComparisonRange(com.apple.foundationdb.record.query.plan.temp.ComparisonRange) IterableHelpers(com.apple.foundationdb.record.query.plan.temp.IterableHelpers) Map(java.util.Map) Compensation(com.apple.foundationdb.record.query.plan.temp.Compensation) IdentityBiMap(com.apple.foundationdb.record.query.plan.temp.IdentityBiMap) Placeholder(com.apple.foundationdb.record.query.predicates.ValueComparisonRangePredicate.Placeholder) AliasMap(com.apple.foundationdb.record.query.plan.temp.AliasMap) Nonnull(javax.annotation.Nonnull) PredicateMapping(com.apple.foundationdb.record.query.plan.temp.PredicateMultiMap.PredicateMapping) Verify(com.google.common.base.Verify) ImmutableSet(com.google.common.collect.ImmutableSet) Equivalence(com.google.common.base.Equivalence) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) InternalPlannerGraphRewritable(com.apple.foundationdb.record.query.plan.temp.explain.InternalPlannerGraphRewritable) QueryPredicate(com.apple.foundationdb.record.query.predicates.QueryPredicate) EnumeratingIterable(com.apple.foundationdb.record.query.combinatorics.EnumeratingIterable) Streams(com.google.common.collect.Streams) AndPredicate(com.apple.foundationdb.record.query.predicates.AndPredicate) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) RelationalExpression(com.apple.foundationdb.record.query.plan.temp.RelationalExpression) Objects(java.util.Objects) Value(com.apple.foundationdb.record.query.predicates.Value) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) List(java.util.List) Stream(java.util.stream.Stream) Sargable(com.apple.foundationdb.record.query.predicates.ValueComparisonRangePredicate.Sargable) CorrelationIdentifier(com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier) CrossProduct(com.apple.foundationdb.record.query.combinatorics.CrossProduct) MatchInfo(com.apple.foundationdb.record.query.plan.temp.MatchInfo) Optional(java.util.Optional) API(com.apple.foundationdb.annotation.API) PlannerGraph(com.apple.foundationdb.record.query.plan.temp.explain.PlannerGraph) Sargable(com.apple.foundationdb.record.query.predicates.ValueComparisonRangePredicate.Sargable) QueryPredicate(com.apple.foundationdb.record.query.predicates.QueryPredicate) ImmutableList(com.google.common.collect.ImmutableList) PredicateWithValue(com.apple.foundationdb.record.query.predicates.PredicateWithValue) AliasMap(com.apple.foundationdb.record.query.plan.temp.AliasMap) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) ValuePredicate(com.apple.foundationdb.record.query.predicates.ValuePredicate) PredicateWithValue(com.apple.foundationdb.record.query.predicates.PredicateWithValue) Value(com.apple.foundationdb.record.query.predicates.Value) ComparisonRange(com.apple.foundationdb.record.query.plan.temp.ComparisonRange)

Aggregations

ValuePredicate (com.apple.foundationdb.record.query.predicates.ValuePredicate)6 QueryPredicate (com.apple.foundationdb.record.query.predicates.QueryPredicate)5 Comparisons (com.apple.foundationdb.record.query.expressions.Comparisons)4 ImmutableList (com.google.common.collect.ImmutableList)4 CorrelationIdentifier (com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier)3 Quantifier (com.apple.foundationdb.record.query.plan.temp.Quantifier)3 API (com.apple.foundationdb.annotation.API)2 RelationalExpression (com.apple.foundationdb.record.query.plan.temp.RelationalExpression)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Objects (java.util.Objects)2 Collectors (java.util.stream.Collectors)2 Nonnull (javax.annotation.Nonnull)2 Test (org.junit.jupiter.api.Test)2 CORRELATION (com.apple.foundationdb.record.Bindings.Internal.CORRELATION)1 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)1 RecordMetaData (com.apple.foundationdb.record.RecordMetaData)1 Index (com.apple.foundationdb.record.metadata.Index)1 Key (com.apple.foundationdb.record.metadata.Key)1 RecordType (com.apple.foundationdb.record.metadata.RecordType)1