Search in sources :

Example 1 with ValueComparisonRangePredicate

use of com.apple.foundationdb.record.query.predicates.ValueComparisonRangePredicate 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

API (com.apple.foundationdb.annotation.API)1 CrossProduct (com.apple.foundationdb.record.query.combinatorics.CrossProduct)1 EnumeratingIterable (com.apple.foundationdb.record.query.combinatorics.EnumeratingIterable)1 Comparisons (com.apple.foundationdb.record.query.expressions.Comparisons)1 AliasMap (com.apple.foundationdb.record.query.plan.temp.AliasMap)1 ComparisonRange (com.apple.foundationdb.record.query.plan.temp.ComparisonRange)1 Compensation (com.apple.foundationdb.record.query.plan.temp.Compensation)1 CorrelationIdentifier (com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier)1 IdentityBiMap (com.apple.foundationdb.record.query.plan.temp.IdentityBiMap)1 IterableHelpers (com.apple.foundationdb.record.query.plan.temp.IterableHelpers)1 MatchInfo (com.apple.foundationdb.record.query.plan.temp.MatchInfo)1 PartialMatch (com.apple.foundationdb.record.query.plan.temp.PartialMatch)1 PredicateMap (com.apple.foundationdb.record.query.plan.temp.PredicateMap)1 PredicateMapping (com.apple.foundationdb.record.query.plan.temp.PredicateMultiMap.PredicateMapping)1 Quantifier (com.apple.foundationdb.record.query.plan.temp.Quantifier)1 RelationalExpression (com.apple.foundationdb.record.query.plan.temp.RelationalExpression)1 RelationalExpressionWithPredicates (com.apple.foundationdb.record.query.plan.temp.RelationalExpressionWithPredicates)1 InternalPlannerGraphRewritable (com.apple.foundationdb.record.query.plan.temp.explain.InternalPlannerGraphRewritable)1 PlannerGraph (com.apple.foundationdb.record.query.plan.temp.explain.PlannerGraph)1 AndPredicate (com.apple.foundationdb.record.query.predicates.AndPredicate)1