Search in sources :

Example 1 with PlanOrderingKey

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

the class InExtractor method adjustOrdering.

/**
 * Adjust plan ordering for the result of handling these {@code IN} clauses.
 * @param ordering the base plan ordering
 * @param asPrefix {@code true} if the {@code IN} conditions become part of an equality prefix (because the elements are merged in order),
 * {@code false} if a separate ordering beforehand (because the elements are concatenated in turn)
 * @return a new plan ordering
 */
@Nullable
public PlanOrderingKey adjustOrdering(@Nullable PlanOrderingKey ordering, boolean asPrefix) {
    if (ordering == null || inClauses.isEmpty()) {
        return ordering;
    }
    // All the ordering keys from the IN joins look like non-prefix ordering and come before the others.
    final List<KeyExpression> keys = new ArrayList<>(ordering.getKeys());
    int prefixSize = ordering.getPrefixSize();
    final int primaryKeyStart = ordering.getPrimaryKeyStart();
    final int primaryKeyTailFromEnd = keys.size() - ordering.getPrimaryKeyTail();
    for (int i = 0; i < inClauses.size(); i++) {
        final KeyExpression inOrdering = inClauses.get(i).orderingKey;
        if (inOrdering == null) {
            return null;
        }
        final int position = keys.indexOf(inOrdering);
        if (position >= 0) {
            if (position < prefixSize) {
                // No longer an equality.
                prefixSize--;
            }
            keys.remove(position);
        }
        keys.add(prefixSize + i, inOrdering);
        if (asPrefix) {
            prefixSize++;
        }
    }
    return new PlanOrderingKey(keys, prefixSize, primaryKeyStart, keys.size() - primaryKeyTailFromEnd);
}
Also used : PlanOrderingKey(com.apple.foundationdb.record.query.plan.PlanOrderingKey) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) ArrayList(java.util.ArrayList) Nullable(javax.annotation.Nullable)

Aggregations

FieldKeyExpression (com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression)1 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)1 NestingKeyExpression (com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression)1 PlanOrderingKey (com.apple.foundationdb.record.query.plan.PlanOrderingKey)1 ArrayList (java.util.ArrayList)1 Nullable (javax.annotation.Nullable)1