Search in sources :

Example 6 with QuantifiedColumnValue

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

the class ImplementInJoinRule method getInSourcesForRequestedOrdering.

@Nonnull
@SuppressWarnings("unchecked")
private ImmutableList<InSource> getInSourcesForRequestedOrdering(@Nonnull final Map<CorrelationIdentifier, Quantifier> explodeAliasToQuantifierMap, @Nonnull final Set<CorrelationIdentifier> explodeAliases, @Nonnull final IdentityBiMap<Quantifier.ForEach, ExplodeExpression> quantifierToExplodeBiMap, @Nonnull final Ordering providedInnerOrdering, @Nonnull final RequestedOrdering requestedOrdering) {
    final var availableExplodeAliases = Sets.newLinkedHashSet(explodeAliases);
    final var requestedOrderingKeyParts = requestedOrdering.getOrderingKeyParts();
    final var sourcesBuilder = ImmutableList.<InSource>builder();
    final var resultOrderingKeyPartsBuilder = ImmutableList.<KeyPart>builder();
    final var innerOrderingKeyParts = providedInnerOrdering.getOrderingKeyParts();
    final var innerEqualityBoundKeyMap = providedInnerOrdering.getEqualityBoundKeyMap();
    final var resultOrderingEqualityBoundKeyMap = HashMultimap.create(innerEqualityBoundKeyMap);
    for (var i = 0; i < requestedOrderingKeyParts.size() && !availableExplodeAliases.isEmpty(); i++) {
        final var requestedOrderingKeyPart = requestedOrderingKeyParts.get(i);
        final var comparisons = innerEqualityBoundKeyMap.get(requestedOrderingKeyPart.getNormalizedKeyExpression());
        if (comparisons.isEmpty()) {
            return ImmutableList.of();
        }
        final var comparisonsCorrelatedTo = comparisons.stream().flatMap(comparison -> comparison.getCorrelatedTo().stream()).collect(ImmutableSet.toImmutableSet());
        if (comparisonsCorrelatedTo.size() > 1) {
            return ImmutableList.of();
        }
        if (Sets.intersection(comparisonsCorrelatedTo, explodeAliases).isEmpty()) {
            // 
            continue;
        }
        final var explodeAlias = Iterables.getOnlyElement(comparisonsCorrelatedTo);
        // 
        if (!availableExplodeAliases.contains(explodeAlias)) {
            return ImmutableList.of();
        }
        // 
        // We need to find the one quantifier over an explode expression that we can use to establish
        // the requested order.
        // 
        final var explodeQuantifier = Objects.requireNonNull(explodeAliasToQuantifierMap.get(explodeAlias));
        final var explodeExpression = Objects.requireNonNull(quantifierToExplodeBiMap.getUnwrapped(explodeQuantifier));
        // 
        // At this point we have a bound key expression that matches the requested order at this position,
        // and we have our hands on a particular explode expression leading us directly do the in source.
        // 
        final var explodeResultValues = explodeExpression.getResultValues();
        if (explodeResultValues.size() != 1) {
            return ImmutableList.of();
        }
        final var explodeValue = Iterables.getOnlyElement(explodeResultValues);
        final InSource inSource;
        if (explodeValue instanceof LiteralValue<?>) {
            final Object literalValue = ((LiteralValue<?>) explodeValue).getLiteralValue();
            if (literalValue instanceof List<?>) {
                inSource = new SortedInValuesSource(CORRELATION.bindingName(explodeQuantifier.getAlias().getId()), (List<Object>) literalValue, requestedOrderingKeyPart.isReverse());
            } else {
                return ImmutableList.of();
            }
        } else if (explodeValue instanceof QuantifiedColumnValue) {
            inSource = new SortedInParameterSource(CORRELATION.bindingName(explodeQuantifier.getAlias().getId()), ((QuantifiedColumnValue) explodeValue).getAlias().getId(), requestedOrderingKeyPart.isReverse());
        } else {
            return ImmutableList.of();
        }
        availableExplodeAliases.remove(explodeAlias);
        sourcesBuilder.add(inSource);
        resultOrderingEqualityBoundKeyMap.removeAll(requestedOrderingKeyPart.getNormalizedKeyExpression());
        resultOrderingKeyPartsBuilder.add(requestedOrderingKeyPart);
    }
    if (availableExplodeAliases.isEmpty()) {
        // 
        // All available explode aliases have been depleted. Create an ordering and check against the requested
        // ordering.
        // 
        resultOrderingKeyPartsBuilder.addAll(innerOrderingKeyParts);
        final var resultOrdering = new Ordering(resultOrderingEqualityBoundKeyMap, resultOrderingKeyPartsBuilder.build(), providedInnerOrdering.isDistinct());
        return Ordering.satisfiesRequestedOrdering(resultOrdering, requestedOrdering) ? sourcesBuilder.build() : ImmutableList.of();
    } else {
        // 
        for (final var explodeAlias : availableExplodeAliases) {
            final var explodeQuantifier = Objects.requireNonNull(explodeAliasToQuantifierMap.get(explodeAlias));
            final var explodeExpression = Objects.requireNonNull(quantifierToExplodeBiMap.getUnwrapped(explodeQuantifier));
            final var explodeResultValues = explodeExpression.getResultValues();
            if (explodeResultValues.size() != 1) {
                return ImmutableList.of();
            }
            final var explodeValue = Iterables.getOnlyElement(explodeResultValues);
            final InSource inSource;
            if (explodeValue instanceof LiteralValue<?>) {
                final Object literalValue = ((LiteralValue<?>) explodeValue).getLiteralValue();
                if (literalValue instanceof List<?>) {
                    inSource = new InValuesSource(CORRELATION.bindingName(explodeQuantifier.getAlias().getId()), (List<Object>) literalValue);
                } else {
                    return ImmutableList.of();
                }
            } else if (explodeValue instanceof QuantifiedColumnValue) {
                inSource = new InParameterSource(CORRELATION.bindingName(explodeQuantifier.getAlias().getId()), ((QuantifiedColumnValue) explodeValue).getAlias().getId());
            } else {
                return ImmutableList.of();
            }
            sourcesBuilder.add(inSource);
        }
    }
    // 
    return sourcesBuilder.build();
}
Also used : PlannerRuleCall(com.apple.foundationdb.record.query.plan.temp.PlannerRuleCall) OrderingAttribute(com.apple.foundationdb.record.query.plan.temp.OrderingAttribute) LinkedIdentitySet(com.apple.foundationdb.record.query.plan.temp.LinkedIdentitySet) GroupExpressionRef(com.apple.foundationdb.record.query.plan.temp.GroupExpressionRef) HashMultimap(com.google.common.collect.HashMultimap) Pair(org.apache.commons.lang3.tuple.Pair) RelationalExpressionMatchers.selectExpression(com.apple.foundationdb.record.query.plan.temp.matchers.RelationalExpressionMatchers.selectExpression) Map(java.util.Map) RequestedOrdering(com.apple.foundationdb.record.query.plan.temp.RequestedOrdering) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) Set(java.util.Set) SelectExpression(com.apple.foundationdb.record.query.plan.temp.expressions.SelectExpression) LiteralValue(com.apple.foundationdb.record.query.predicates.LiteralValue) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Value(com.apple.foundationdb.record.query.predicates.Value) List(java.util.List) RecordQueryInUnionPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryInUnionPlan) CorrelationIdentifier(com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier) OrderingProperty(com.apple.foundationdb.record.query.plan.temp.properties.OrderingProperty) Optional(java.util.Optional) API(com.apple.foundationdb.annotation.API) InParameterSource(com.apple.foundationdb.record.query.plan.plans.InParameterSource) CORRELATION(com.apple.foundationdb.record.Bindings.Internal.CORRELATION) Iterables(com.google.common.collect.Iterables) PlannerRule(com.apple.foundationdb.record.query.plan.temp.PlannerRule) Quantifier(com.apple.foundationdb.record.query.plan.temp.Quantifier) CollectionMatcher(com.apple.foundationdb.record.query.plan.temp.matchers.CollectionMatcher) Ordering(com.apple.foundationdb.record.query.plan.temp.Ordering) Quantifiers(com.apple.foundationdb.record.query.plan.temp.Quantifiers) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) RelationalExpressionMatchers.explodeExpression(com.apple.foundationdb.record.query.plan.temp.matchers.RelationalExpressionMatchers.explodeExpression) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) IdentityBiMap(com.apple.foundationdb.record.query.plan.temp.IdentityBiMap) PushInterestingOrderingThroughInLikeSelectRule.findInnerQuantifier(com.apple.foundationdb.record.query.plan.temp.rules.PushInterestingOrderingThroughInLikeSelectRule.findInnerQuantifier) InSource(com.apple.foundationdb.record.query.plan.plans.InSource) InValuesSource(com.apple.foundationdb.record.query.plan.plans.InValuesSource) MultiMatcher.some(com.apple.foundationdb.record.query.plan.temp.matchers.MultiMatcher.some) Nonnull(javax.annotation.Nonnull) SortedInValuesSource(com.apple.foundationdb.record.query.plan.plans.SortedInValuesSource) KeyPart(com.apple.foundationdb.record.query.plan.temp.KeyPart) SortedInParameterSource(com.apple.foundationdb.record.query.plan.plans.SortedInParameterSource) BindingMatcher(com.apple.foundationdb.record.query.plan.temp.matchers.BindingMatcher) QuantifierMatchers.forEachQuantifier(com.apple.foundationdb.record.query.plan.temp.matchers.QuantifierMatchers.forEachQuantifier) QuantifiedColumnValue(com.apple.foundationdb.record.query.predicates.QuantifiedColumnValue) ExplodeExpression(com.apple.foundationdb.record.query.plan.temp.expressions.ExplodeExpression) SortedInParameterSource(com.apple.foundationdb.record.query.plan.plans.SortedInParameterSource) KeyPart(com.apple.foundationdb.record.query.plan.temp.KeyPart) LiteralValue(com.apple.foundationdb.record.query.predicates.LiteralValue) InParameterSource(com.apple.foundationdb.record.query.plan.plans.InParameterSource) SortedInParameterSource(com.apple.foundationdb.record.query.plan.plans.SortedInParameterSource) QuantifiedColumnValue(com.apple.foundationdb.record.query.predicates.QuantifiedColumnValue) InSource(com.apple.foundationdb.record.query.plan.plans.InSource) RequestedOrdering(com.apple.foundationdb.record.query.plan.temp.RequestedOrdering) Ordering(com.apple.foundationdb.record.query.plan.temp.Ordering) InValuesSource(com.apple.foundationdb.record.query.plan.plans.InValuesSource) SortedInValuesSource(com.apple.foundationdb.record.query.plan.plans.SortedInValuesSource) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) SortedInValuesSource(com.apple.foundationdb.record.query.plan.plans.SortedInValuesSource) Nonnull(javax.annotation.Nonnull)

Aggregations

QuantifiedColumnValue (com.apple.foundationdb.record.query.predicates.QuantifiedColumnValue)6 Value (com.apple.foundationdb.record.query.predicates.Value)6 Nonnull (javax.annotation.Nonnull)6 ImmutableList (com.google.common.collect.ImmutableList)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 Iterables (com.google.common.collect.Iterables)4 Collection (java.util.Collection)4 List (java.util.List)4 API (com.apple.foundationdb.annotation.API)3 CORRELATION (com.apple.foundationdb.record.Bindings.Internal.CORRELATION)3 InParameterSource (com.apple.foundationdb.record.query.plan.plans.InParameterSource)3 InSource (com.apple.foundationdb.record.query.plan.plans.InSource)3 InValuesSource (com.apple.foundationdb.record.query.plan.plans.InValuesSource)3 RecordQueryInUnionPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryInUnionPlan)3 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)3 CorrelationIdentifier (com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier)3 GroupExpressionRef (com.apple.foundationdb.record.query.plan.temp.GroupExpressionRef)3 IdentityBiMap (com.apple.foundationdb.record.query.plan.temp.IdentityBiMap)3 KeyPart (com.apple.foundationdb.record.query.plan.temp.KeyPart)3 LinkedIdentitySet (com.apple.foundationdb.record.query.plan.temp.LinkedIdentitySet)3