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);
}
Aggregations