Search in sources :

Example 1 with RecordQuerySortPlan

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

the class RecordQueryPlanner method plan.

/**
 * Create a plan to get the results of the provided query.
 *
 * @param query a query for records on this planner's metadata
 * @param parameterRelationshipGraph a set of bindings and their relationships that provide additional information
 *        to the planner that may improve plan quality but may also tighten requirements imposed on the parameter
 *        bindings that are used to execute the query
 * @return a plan that will return the results of the provided query when executed
 * @throws com.apple.foundationdb.record.RecordCoreException if there is no index that matches the sort in the provided query
 */
@Nonnull
@Override
public RecordQueryPlan plan(@Nonnull RecordQuery query, @Nonnull ParameterRelationshipGraph parameterRelationshipGraph) {
    query.validate(metaData);
    final PlanContext planContext = getPlanContext(query);
    final BooleanNormalizer normalizer = BooleanNormalizer.forConfiguration(configuration);
    final QueryComponent queryFilter = query.getFilter();
    final QueryComponent filter = normalizer.normalizeIfPossible(queryFilter == null ? null : queryFilter.withParameterRelationshipMap(parameterRelationshipGraph));
    final KeyExpression sort = query.getSort();
    final boolean sortReverse = query.isSortReverse();
    RecordQueryPlan plan = plan(planContext, filter, sort, sortReverse);
    if (plan == null) {
        if (sort == null) {
            throw new RecordCoreException("Unexpected failure to plan without sort");
        }
        final RecordQueryPlannerSortConfiguration sortConfiguration = configuration.getSortConfiguration();
        if (sortConfiguration != null && sortConfiguration.shouldAllowNonIndexSort(query)) {
            final PlanContext withoutSort = new PlanContext(query.toBuilder().setSort(null).build(), planContext.indexes, planContext.commonPrimaryKey);
            plan = plan(withoutSort, filter, null, false);
            if (plan == null) {
                throw new RecordCoreException("Unexpected failure to plan without sort");
            }
            plan = new RecordQuerySortPlan(plan, sortConfiguration.getSortKey(sort, sortReverse));
        } else {
            throw new RecordCoreException("Cannot sort without appropriate index: " + sort);
        }
    }
    if (query.getRequiredResults() != null) {
        plan = tryToConvertToCoveringPlan(planContext, plan);
    }
    if (timer != null) {
        plan.logPlanStructure(timer);
    }
    if (plan.getComplexity() > configuration.getComplexityThreshold()) {
        throw new RecordQueryPlanComplexityException(plan);
    }
    if (logger.isTraceEnabled()) {
        logger.trace(KeyValueLogMessage.of("explain of plan", "explain", PlannerGraphProperty.explain(plan)));
    }
    return plan;
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) RecordQuerySortPlan(com.apple.foundationdb.record.query.plan.sorting.RecordQuerySortPlan) 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) BooleanNormalizer(com.apple.foundationdb.record.query.plan.planning.BooleanNormalizer) RecordQueryPlannerSortConfiguration(com.apple.foundationdb.record.query.plan.sorting.RecordQueryPlannerSortConfiguration) Nonnull(javax.annotation.Nonnull)

Aggregations

RecordCoreException (com.apple.foundationdb.record.RecordCoreException)1 EmptyKeyExpression (com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression)1 FieldKeyExpression (com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression)1 GroupingKeyExpression (com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression)1 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)1 NestingKeyExpression (com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression)1 RecordTypeKeyExpression (com.apple.foundationdb.record.metadata.expressions.RecordTypeKeyExpression)1 ThenKeyExpression (com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression)1 VersionKeyExpression (com.apple.foundationdb.record.metadata.expressions.VersionKeyExpression)1 QueryComponent (com.apple.foundationdb.record.query.expressions.QueryComponent)1 BooleanNormalizer (com.apple.foundationdb.record.query.plan.planning.BooleanNormalizer)1 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)1 RecordQueryPlannerSortConfiguration (com.apple.foundationdb.record.query.plan.sorting.RecordQueryPlannerSortConfiguration)1 RecordQuerySortPlan (com.apple.foundationdb.record.query.plan.sorting.RecordQuerySortPlan)1 Nonnull (javax.annotation.Nonnull)1