Search in sources :

Example 1 with LogicalProjectionExpression

use of com.apple.foundationdb.record.query.plan.temp.expressions.LogicalProjectionExpression 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 LogicalProjectionExpression

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

the class MergeProjectionAndFetchRule method onMatch.

@Override
public void onMatch(@Nonnull PlannerRuleCall call) {
    final LogicalProjectionExpression projectionExpression = call.get(root);
    // if the fetch is able to push all values we can eliminate the fetch as well
    final RecordQueryFetchFromPartialRecordPlan fetchPlan = call.get(innerPlanMatcher);
    final CorrelationIdentifier newInnerAlias = CorrelationIdentifier.uniqueID();
    final List<? extends Value> resultValues = projectionExpression.getResultValues();
    final boolean allPushable = resultValues.stream().allMatch(value -> fetchPlan.pushValue(value, newInnerAlias).isPresent());
    if (allPushable) {
        // all fields in the projection are already available underneath the fetch
        // we don't need the projection nor the fetch
        call.yield(call.ref(fetchPlan.getChild()));
    }
}
Also used : RecordQueryFetchFromPartialRecordPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryFetchFromPartialRecordPlan) CorrelationIdentifier(com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier) LogicalProjectionExpression(com.apple.foundationdb.record.query.plan.temp.expressions.LogicalProjectionExpression)

Aggregations

LogicalProjectionExpression (com.apple.foundationdb.record.query.plan.temp.expressions.LogicalProjectionExpression)2 RecordQueryFetchFromPartialRecordPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryFetchFromPartialRecordPlan)1 CorrelationIdentifier (com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier)1 FullUnorderedScanExpression (com.apple.foundationdb.record.query.plan.temp.expressions.FullUnorderedScanExpression)1 LogicalDistinctExpression (com.apple.foundationdb.record.query.plan.temp.expressions.LogicalDistinctExpression)1 LogicalSortExpression (com.apple.foundationdb.record.query.plan.temp.expressions.LogicalSortExpression)1 LogicalTypeFilterExpression (com.apple.foundationdb.record.query.plan.temp.expressions.LogicalTypeFilterExpression)1 SelectExpression (com.apple.foundationdb.record.query.plan.temp.expressions.SelectExpression)1 HashSet (java.util.HashSet)1 Nonnull (javax.annotation.Nonnull)1