Search in sources :

Example 1 with LogicalSortExpression

use of com.apple.foundationdb.record.query.plan.temp.expressions.LogicalSortExpression in project fdb-record-layer by FoundationDB.

the class RelationalExpression method fromRecordQuery.

@Nonnull
static RelationalExpression fromRecordQuery(@Nonnull PlanContext context, @Nonnull RecordQuery query) {
    query.validate(context.getMetaData());
    Quantifier.ForEach quantifier = Quantifier.forEach(GroupExpressionRef.of(new FullUnorderedScanExpression(context.getMetaData().getRecordTypes().keySet())));
    if (!context.getRecordTypes().isEmpty()) {
        quantifier = Quantifier.forEach(GroupExpressionRef.of(new LogicalTypeFilterExpression(new HashSet<>(context.getRecordTypes()), quantifier)));
    }
    final SelectExpression selectExpression;
    if (query.getFilter() != null) {
        selectExpression = query.getFilter().expand(quantifier.getAlias()).buildSelectWithBase(quantifier);
    } else {
        selectExpression = GraphExpansion.empty().buildSelectWithBase(quantifier);
    }
    quantifier = Quantifier.forEach(GroupExpressionRef.of(selectExpression));
    if (query.removesDuplicates()) {
        quantifier = Quantifier.forEach(GroupExpressionRef.of(new LogicalDistinctExpression(quantifier)));
    }
    if (query.getSort() != null) {
        quantifier = Quantifier.forEach(GroupExpressionRef.of(new LogicalSortExpression(query.getSort(), query.isSortReverse(), quantifier)));
    } else {
        quantifier = Quantifier.forEach(GroupExpressionRef.of(new LogicalSortExpression(null, false, quantifier)));
    }
    if (query.getRequiredResults() != null) {
        final List<? extends Value> projectedValues = Value.fromKeyExpressions(query.getRequiredResults().stream().flatMap(keyExpression -> keyExpression.normalizeKeyForPositions().stream()).collect(ImmutableList.toImmutableList()), quantifier.getAlias());
        quantifier = Quantifier.forEach(GroupExpressionRef.of(new LogicalProjectionExpression(projectedValues, quantifier)));
    }
    return quantifier.getRangesOver().get();
}
Also used : LogicalTypeFilterExpression(com.apple.foundationdb.record.query.plan.temp.expressions.LogicalTypeFilterExpression) LogicalProjectionExpression(com.apple.foundationdb.record.query.plan.temp.expressions.LogicalProjectionExpression) FullUnorderedScanExpression(com.apple.foundationdb.record.query.plan.temp.expressions.FullUnorderedScanExpression) SelectExpression(com.apple.foundationdb.record.query.plan.temp.expressions.SelectExpression) LogicalDistinctExpression(com.apple.foundationdb.record.query.plan.temp.expressions.LogicalDistinctExpression) HashSet(java.util.HashSet) LogicalSortExpression(com.apple.foundationdb.record.query.plan.temp.expressions.LogicalSortExpression) Nonnull(javax.annotation.Nonnull)

Example 2 with LogicalSortExpression

use of com.apple.foundationdb.record.query.plan.temp.expressions.LogicalSortExpression in project fdb-record-layer by FoundationDB.

the class PushInterestingOrderingThroughSortRule method onMatch.

@Override
public void onMatch(@Nonnull PlannerRuleCall call) {
    final PlannerBindings bindings = call.getBindings();
    final LogicalSortExpression logicalSortExpression = bindings.get(root);
    final ExpressionRef<? extends RelationalExpression> lowerRef = bindings.get(lowerRefMatcher);
    final KeyExpression sortKeyExpression = logicalSortExpression.getSort();
    if (sortKeyExpression == null) {
        call.pushRequirement(lowerRef, OrderingAttribute.ORDERING, ImmutableSet.of(RequestedOrdering.preserve()));
    } else {
        final List<KeyExpression> normalizedSortKeys = sortKeyExpression.normalizeKeyForPositions();
        final ImmutableList.Builder<KeyPart> keyPartBuilder = ImmutableList.builder();
        for (final KeyExpression keyExpression : normalizedSortKeys) {
            keyPartBuilder.add(KeyPart.of(keyExpression, logicalSortExpression.isReverse()));
        }
        final var orderings = ImmutableSet.of(new RequestedOrdering(keyPartBuilder.build(), RequestedOrdering.Distinctness.PRESERVE_DISTINCTNESS));
        call.pushRequirement(lowerRef, OrderingAttribute.ORDERING, orderings);
    }
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) PlannerBindings(com.apple.foundationdb.record.query.plan.temp.matchers.PlannerBindings) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) KeyPart(com.apple.foundationdb.record.query.plan.temp.KeyPart) RequestedOrdering(com.apple.foundationdb.record.query.plan.temp.RequestedOrdering) LogicalSortExpression(com.apple.foundationdb.record.query.plan.temp.expressions.LogicalSortExpression)

Example 3 with LogicalSortExpression

use of com.apple.foundationdb.record.query.plan.temp.expressions.LogicalSortExpression in project fdb-record-layer by FoundationDB.

the class RemoveSortRule method onMatch.

@Override
public void onMatch(@Nonnull PlannerRuleCall call) {
    final LogicalSortExpression sortExpression = call.get(root);
    final RecordQueryPlan innerPlan = call.get(innerPlanMatcher);
    final KeyExpression sortKeyExpression = sortExpression.getSort();
    if (sortKeyExpression == null) {
        call.yield(call.ref(innerPlan));
        return;
    }
    final Optional<Ordering> orderingOptional = OrderingProperty.evaluate(innerPlan, call.getContext());
    if (orderingOptional.isEmpty()) {
        return;
    }
    final Ordering ordering = orderingOptional.get();
    final Set<KeyExpression> equalityBoundKeys = ordering.getEqualityBoundKeys();
    int equalityBoundUnsorted = equalityBoundKeys.size();
    final List<KeyPart> orderingKeys = ordering.getOrderingKeyParts();
    final Iterator<KeyPart> orderingKeysIterator = orderingKeys.iterator();
    final List<KeyExpression> normalizedSortExpressions = sortKeyExpression.normalizeKeyForPositions();
    for (final KeyExpression normalizedSortExpression : normalizedSortExpressions) {
        if (equalityBoundKeys.contains(normalizedSortExpression)) {
            equalityBoundUnsorted--;
            continue;
        }
        if (!orderingKeysIterator.hasNext()) {
            return;
        }
        final KeyPart currentOrderingKeyPart = orderingKeysIterator.next();
        if (!normalizedSortExpression.equals(currentOrderingKeyPart.getNormalizedKeyExpression())) {
            return;
        }
    }
    final boolean strictOrdered = // If we have exhausted the ordering info's keys, too, then its constituents are strictly ordered.
    !orderingKeysIterator.hasNext() || // Also a unique index if have gone through declared fields.
    strictlyOrderedIfUnique(innerPlan, call.getContext()::getIndexByName, normalizedSortExpressions.size() + equalityBoundUnsorted);
    call.yield(call.ref(strictOrdered ? innerPlan.strictlySorted() : innerPlan));
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) Ordering(com.apple.foundationdb.record.query.plan.temp.Ordering) KeyPart(com.apple.foundationdb.record.query.plan.temp.KeyPart) LogicalSortExpression(com.apple.foundationdb.record.query.plan.temp.expressions.LogicalSortExpression)

Aggregations

LogicalSortExpression (com.apple.foundationdb.record.query.plan.temp.expressions.LogicalSortExpression)3 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)2 KeyPart (com.apple.foundationdb.record.query.plan.temp.KeyPart)2 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)1 Ordering (com.apple.foundationdb.record.query.plan.temp.Ordering)1 RequestedOrdering (com.apple.foundationdb.record.query.plan.temp.RequestedOrdering)1 FullUnorderedScanExpression (com.apple.foundationdb.record.query.plan.temp.expressions.FullUnorderedScanExpression)1 LogicalDistinctExpression (com.apple.foundationdb.record.query.plan.temp.expressions.LogicalDistinctExpression)1 LogicalProjectionExpression (com.apple.foundationdb.record.query.plan.temp.expressions.LogicalProjectionExpression)1 LogicalTypeFilterExpression (com.apple.foundationdb.record.query.plan.temp.expressions.LogicalTypeFilterExpression)1 SelectExpression (com.apple.foundationdb.record.query.plan.temp.expressions.SelectExpression)1 PlannerBindings (com.apple.foundationdb.record.query.plan.temp.matchers.PlannerBindings)1 ImmutableList (com.google.common.collect.ImmutableList)1 HashSet (java.util.HashSet)1 Nonnull (javax.annotation.Nonnull)1