Search in sources :

Example 1 with RankComparisons

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

the class RecordQueryPlanner method planFilterForInJoin.

private ScoredPlan planFilterForInJoin(@Nonnull PlanContext planContext, @Nonnull QueryComponent filter, boolean needOrdering) {
    planContext.rankComparisons = new RankComparisons(filter, planContext.indexes);
    List<ScoredPlan> intersectionCandidates = new ArrayList<>();
    ScoredPlan bestPlan = null;
    Index bestIndex = null;
    if (planContext.commonPrimaryKey != null) {
        bestPlan = planIndex(planContext, filter, null, planContext.commonPrimaryKey, intersectionCandidates);
    }
    for (Index index : planContext.indexes) {
        KeyExpression indexKeyExpression = indexKeyExpressionForPlan(planContext.commonPrimaryKey, index);
        ScoredPlan p = planIndex(planContext, filter, index, indexKeyExpression, intersectionCandidates);
        if (p != null) {
            // * need for type filtering if row scan with multiple types.
            if (isBetterThanOther(planContext, p, index, bestPlan, bestIndex)) {
                bestPlan = p;
                bestIndex = index;
            }
        }
    }
    if (bestPlan != null) {
        if (bestPlan.getNumNonSargables() > 0) {
            bestPlan = handleNonSargables(bestPlan, intersectionCandidates, planContext);
        }
        if (needOrdering) {
            bestPlan.planOrderingKey = PlanOrderingKey.forPlan(metaData, bestPlan.plan, planContext.commonPrimaryKey);
        }
    }
    return bestPlan;
}
Also used : RankComparisons(com.apple.foundationdb.record.query.plan.planning.RankComparisons) ArrayList(java.util.ArrayList) RecordTypeKeyExpression(com.apple.foundationdb.record.metadata.expressions.RecordTypeKeyExpression) VersionKeyExpression(com.apple.foundationdb.record.metadata.expressions.VersionKeyExpression) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) RecordQueryPlanWithIndex(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlanWithIndex) Index(com.apple.foundationdb.record.metadata.Index)

Example 2 with RankComparisons

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

the class RecordQueryPlanner method planCoveringAggregateIndex.

@Nullable
public RecordQueryCoveringIndexPlan planCoveringAggregateIndex(@Nonnull RecordQuery query, @Nonnull Index index, @Nonnull KeyExpression indexExpr) {
    final Collection<RecordType> recordTypes = metaData.recordTypesForIndex(index);
    if (recordTypes.size() != 1) {
        // Unfortunately, since we materialize partial records, we need a unique type for them.
        return null;
    }
    final RecordType recordType = recordTypes.iterator().next();
    final PlanContext planContext = getPlanContext(query);
    planContext.rankComparisons = new RankComparisons(query.getFilter(), planContext.indexes);
    // Repeated fields will be scanned one at a time by covering aggregate, so there is no issue with fan out.
    planContext.allowDuplicates = true;
    final CandidateScan candidateScan = new CandidateScan(planContext, index, query.isSortReverse());
    final ScoredPlan scoredPlan = planCandidateScan(candidateScan, indexExpr, BooleanNormalizer.forConfiguration(configuration).normalizeIfPossible(query.getFilter()), query.getSort());
    // It would be possible to handle unsatisfiedFilters if they, too, only involved group key (covering) fields.
    if (scoredPlan == null || !scoredPlan.unsatisfiedFilters.isEmpty() || !(scoredPlan.plan instanceof RecordQueryIndexPlan)) {
        return null;
    }
    final IndexKeyValueToPartialRecord.Builder builder = IndexKeyValueToPartialRecord.newBuilder(recordType);
    final List<KeyExpression> keyFields = index.getRootExpression().normalizeKeyForPositions();
    final List<KeyExpression> valueFields = Collections.emptyList();
    for (KeyExpression resultField : query.getRequiredResults()) {
        if (!addCoveringField(resultField, builder, keyFields, valueFields)) {
            return null;
        }
    }
    builder.addRequiredMessageFields();
    if (!builder.isValid(true)) {
        return null;
    }
    RecordQueryIndexPlan plan = (RecordQueryIndexPlan) scoredPlan.plan;
    IndexScanParameters scanParameters = new IndexScanComparisons(IndexScanType.BY_GROUP, plan.getComparisons());
    plan = new RecordQueryIndexPlan(plan.getIndexName(), scanParameters, plan.isReverse());
    return new RecordQueryCoveringIndexPlan(plan, recordType.getName(), AvailableFields.NO_FIELDS, builder.build());
}
Also used : IndexScanParameters(com.apple.foundationdb.record.provider.foundationdb.IndexScanParameters) RankComparisons(com.apple.foundationdb.record.query.plan.planning.RankComparisons) IndexScanComparisons(com.apple.foundationdb.record.provider.foundationdb.IndexScanComparisons) RecordQueryIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan) RecordTypeKeyExpression(com.apple.foundationdb.record.metadata.expressions.RecordTypeKeyExpression) VersionKeyExpression(com.apple.foundationdb.record.metadata.expressions.VersionKeyExpression) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) RecordQueryCoveringIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryCoveringIndexPlan) RecordType(com.apple.foundationdb.record.metadata.RecordType) Nullable(javax.annotation.Nullable)

Aggregations

EmptyKeyExpression (com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression)2 FieldKeyExpression (com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression)2 GroupingKeyExpression (com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression)2 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)2 NestingKeyExpression (com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression)2 RecordTypeKeyExpression (com.apple.foundationdb.record.metadata.expressions.RecordTypeKeyExpression)2 ThenKeyExpression (com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression)2 VersionKeyExpression (com.apple.foundationdb.record.metadata.expressions.VersionKeyExpression)2 RankComparisons (com.apple.foundationdb.record.query.plan.planning.RankComparisons)2 Index (com.apple.foundationdb.record.metadata.Index)1 RecordType (com.apple.foundationdb.record.metadata.RecordType)1 IndexScanComparisons (com.apple.foundationdb.record.provider.foundationdb.IndexScanComparisons)1 IndexScanParameters (com.apple.foundationdb.record.provider.foundationdb.IndexScanParameters)1 RecordQueryCoveringIndexPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryCoveringIndexPlan)1 RecordQueryIndexPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan)1 RecordQueryPlanWithIndex (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlanWithIndex)1 ArrayList (java.util.ArrayList)1 Nullable (javax.annotation.Nullable)1