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