Search in sources :

Example 16 with RecordQueryIndexPlan

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

the class ExpressionMatcherTest method nestedTypeMatchers.

@Test
public void nestedTypeMatchers() {
    BindingMatcher<RecordQueryIndexPlan> childMatcher1 = RecordQueryPlanMatchers.indexPlan();
    BindingMatcher<RecordQueryScanPlan> childMatcher2 = RecordQueryPlanMatchers.scanPlan();
    BindingMatcher<RecordQueryUnionPlan> parentMatcher = RecordQueryPlanMatchers.union(ListMatcher.exactly(QuantifierMatchers.physicalQuantifier(childMatcher1), QuantifierMatchers.physicalQuantifier(childMatcher2)));
    IndexScanParameters fullValueScan = IndexScanComparisons.byValue();
    RecordQueryIndexPlan child1 = new RecordQueryIndexPlan("an_index", fullValueScan, true);
    RecordQueryScanPlan child2 = new RecordQueryScanPlan(ScanComparisons.EMPTY, true);
    // check matches if the children are in the right order
    RelationalExpression root = // union with arbitrary comparison key
    RecordQueryUnionPlan.from(child1, child2, EmptyKeyExpression.EMPTY, false);
    Optional<PlannerBindings> possibleBindings = parentMatcher.bindMatches(PlannerBindings.empty(), root).findFirst();
    assertTrue(possibleBindings.isPresent());
    PlannerBindings allBindings = possibleBindings.get().mergedWith(getExistingBindings());
    assertExistingBindingsSurvived(allBindings);
    assertEquals(root, allBindings.get(parentMatcher));
    assertEquals(child1, allBindings.get(childMatcher1));
    assertEquals(child2, allBindings.get(childMatcher2));
    // check that we fail to match if the children are in the wrong order
    root = // union with arbitrary comparison key
    RecordQueryUnionPlan.from(child2, child1, EmptyKeyExpression.EMPTY, false);
    assertFalse(parentMatcher.bindMatches(PlannerBindings.empty(), root).findFirst().isPresent());
}
Also used : IndexScanParameters(com.apple.foundationdb.record.provider.foundationdb.IndexScanParameters) RelationalExpression(com.apple.foundationdb.record.query.plan.temp.RelationalExpression) RecordQueryUnionPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryUnionPlan) RecordQueryScanPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryScanPlan) RecordQueryIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan) Test(org.junit.jupiter.api.Test)

Example 17 with RecordQueryIndexPlan

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

the class ExpressionMatcherTest method matchChildOrder.

@Test
public void matchChildOrder() {
    BindingMatcher<RecordQueryUnionPlan> parentMatcher = RecordQueryPlanMatchers.union(ListMatcher.exactly(QuantifierMatchers.physicalQuantifier(RecordQueryPlanMatchers.indexPlan()), QuantifierMatchers.physicalQuantifier(RecordQueryPlanMatchers.scanPlan())));
    IndexScanParameters fullValueScan = IndexScanComparisons.byValue();
    RecordQueryIndexPlan child1 = new RecordQueryIndexPlan("an_index", fullValueScan, true);
    RecordQueryScanPlan child2 = new RecordQueryScanPlan(ScanComparisons.EMPTY, true);
    RelationalExpression root = // union with arbitrary comparison key
    RecordQueryUnionPlan.from(child1, child2, EmptyKeyExpression.EMPTY, false);
    assertTrue(parentMatcher.bindMatches(PlannerBindings.empty(), root).findFirst().isPresent());
    root = // union with arbitrary comparison key
    RecordQueryUnionPlan.from(child2, child1, EmptyKeyExpression.EMPTY, false);
    assertFalse(parentMatcher.bindMatches(PlannerBindings.empty(), root).findFirst().isPresent());
}
Also used : IndexScanParameters(com.apple.foundationdb.record.provider.foundationdb.IndexScanParameters) RelationalExpression(com.apple.foundationdb.record.query.plan.temp.RelationalExpression) RecordQueryUnionPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryUnionPlan) RecordQueryScanPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryScanPlan) RecordQueryIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan) Test(org.junit.jupiter.api.Test)

Example 18 with RecordQueryIndexPlan

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

the class ExpressionMatcherTest method matchChildrenAsReferences.

@Test
public void matchChildrenAsReferences() {
    BindingMatcher<? extends ExpressionRef<? extends RelationalExpression>> childMatcher1 = ReferenceMatchers.anyRef();
    BindingMatcher<? extends ExpressionRef<? extends RelationalExpression>> childMatcher2 = ReferenceMatchers.anyRef();
    BindingMatcher<RecordQueryUnionPlan> matcher = RecordQueryPlanMatchers.union(ListMatcher.exactly(QuantifierMatchers.physicalQuantifierOverRef(childMatcher1), QuantifierMatchers.physicalQuantifierOverRef(childMatcher2)));
    IndexScanParameters fullValueScan = IndexScanComparisons.byValue();
    RecordQueryIndexPlan child1 = new RecordQueryIndexPlan("an_index", fullValueScan, true);
    RecordQueryScanPlan child2 = new RecordQueryScanPlan(ScanComparisons.EMPTY, true);
    RelationalExpression root = // union with arbitrary comparison key
    RecordQueryUnionPlan.from(child1, child2, EmptyKeyExpression.EMPTY, false);
    Optional<PlannerBindings> possibleBindings = matcher.bindMatches(PlannerBindings.empty(), root).findFirst();
    assertTrue(possibleBindings.isPresent());
    PlannerBindings newBindings = possibleBindings.get().mergedWith(getExistingBindings());
    assertExistingBindingsSurvived(newBindings);
    // check that root matches
    assertEquals(root, newBindings.get(matcher));
    // check that children are behind references
    assertEquals(child1, newBindings.get(childMatcher1).get());
    assertEquals(child2, newBindings.get(childMatcher2).get());
}
Also used : IndexScanParameters(com.apple.foundationdb.record.provider.foundationdb.IndexScanParameters) RelationalExpression(com.apple.foundationdb.record.query.plan.temp.RelationalExpression) RecordQueryUnionPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryUnionPlan) RecordQueryScanPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryScanPlan) RecordQueryIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan) Test(org.junit.jupiter.api.Test)

Example 19 with RecordQueryIndexPlan

use of com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan 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)

Example 20 with RecordQueryIndexPlan

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

the class RecordQueryPlanner method planScan.

@Nonnull
private RecordQueryPlan planScan(@Nonnull CandidateScan candidateScan, @Nonnull IndexScanComparisons indexScanComparisons, boolean strictlySorted) {
    RecordQueryPlan plan;
    Set<String> possibleTypes;
    if (candidateScan.index == null) {
        final ScanComparisons scanComparisons = indexScanComparisons.getComparisons();
        if (primaryKeyHasRecordTypePrefix && RecordTypeKeyComparison.hasRecordTypeKeyComparison(scanComparisons)) {
            possibleTypes = RecordTypeKeyComparison.recordTypeKeyComparisonTypes(scanComparisons);
        } else {
            possibleTypes = metaData.getRecordTypes().keySet();
        }
        plan = new RecordQueryScanPlan(possibleTypes, scanComparisons, candidateScan.reverse, strictlySorted);
    } else {
        plan = new RecordQueryIndexPlan(candidateScan.index.getName(), indexScanComparisons, candidateScan.reverse, strictlySorted);
        possibleTypes = getPossibleTypes(candidateScan.index);
    }
    // Add a type filter if the query plan might return records of more types than the query specified
    plan = addTypeFilterIfNeeded(candidateScan, plan, possibleTypes);
    return plan;
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) TimeWindowScanComparisons(com.apple.foundationdb.record.provider.foundationdb.leaderboard.TimeWindowScanComparisons) IndexScanComparisons(com.apple.foundationdb.record.provider.foundationdb.IndexScanComparisons) RecordQueryScanPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryScanPlan) RecordQueryIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan) Nonnull(javax.annotation.Nonnull)

Aggregations

RecordQueryIndexPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan)24 IndexScanParameters (com.apple.foundationdb.record.provider.foundationdb.IndexScanParameters)14 Test (org.junit.jupiter.api.Test)13 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)12 Comparisons (com.apple.foundationdb.record.query.expressions.Comparisons)7 RecordQueryScanPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryScanPlan)7 Index (com.apple.foundationdb.record.metadata.Index)5 IndexScanComparisons (com.apple.foundationdb.record.provider.foundationdb.IndexScanComparisons)5 ScanComparisons (com.apple.foundationdb.record.query.plan.ScanComparisons)5 RelationalExpression (com.apple.foundationdb.record.query.plan.temp.RelationalExpression)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)4 RecordQueryCoveringIndexPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryCoveringIndexPlan)4 RecordQueryUnionPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryUnionPlan)4 RecordType (com.apple.foundationdb.record.metadata.RecordType)3 ThenKeyExpression (com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression)3 QueryComponent (com.apple.foundationdb.record.query.expressions.QueryComponent)3 Nonnull (javax.annotation.Nonnull)3 Nullable (javax.annotation.Nullable)3 IndexScanType (com.apple.foundationdb.record.IndexScanType)2