Search in sources :

Example 96 with QueryComponent

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

the class JoinedRecordPlanner method buildQuery.

@Nonnull
private RecordQuery buildQuery(@Nonnull PendingType pendingType) {
    final List<QueryComponent> conditions = new ArrayList<>();
    for (PendingJoin pendingJoin : pendingType.pendingJoins) {
        final boolean bound;
        final KeyExpression expression;
        if (pendingJoin.pendingLeft == pendingType) {
            bound = pendingJoin.rightBound;
            expression = pendingJoin.join.getLeftExpression();
        } else if (pendingJoin.pendingRight == pendingType) {
            bound = pendingJoin.leftBound;
            expression = pendingJoin.join.getRightExpression();
        } else {
            throw notFoundEitherSide();
        }
        if (bound) {
            final Comparisons.Comparison comparison = new Comparisons.ParameterComparison(pendingJoin.singleton ? Comparisons.Type.EQUALS : Comparisons.Type.IN, pendingJoin.bindingName);
            conditions.add(buildCondition(expression, comparison));
            pendingJoins.remove(pendingJoin);
        }
    }
    RecordQuery.Builder builder = RecordQuery.newBuilder();
    builder.setRecordType(pendingType.joinConstituent.getRecordType().getName());
    if (!conditions.isEmpty()) {
        builder.setFilter(conditions.size() > 1 ? Query.and(conditions) : conditions.get(0));
    }
    // An alternative would be to add duplicate elimination on the result of the join before forming the synthetic records.
    // But until the planner is able to push that down / eliminate it when the scan does not produce duplicates, it's better to give it here,
    // since the planner at least understands the simple cases of the elimination.
    builder.setRemoveDuplicates(true);
    return builder.build();
}
Also used : QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) ArrayList(java.util.ArrayList) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Nonnull(javax.annotation.Nonnull)

Aggregations

QueryComponent (com.apple.foundationdb.record.query.expressions.QueryComponent)96 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)58 RecordQuery (com.apple.foundationdb.record.query.RecordQuery)44 Test (org.junit.jupiter.api.Test)41 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)37 ArrayList (java.util.ArrayList)30 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)29 FDBQueriedRecord (com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord)25 Index (com.apple.foundationdb.record.metadata.Index)24 Nonnull (javax.annotation.Nonnull)23 Comparisons (com.apple.foundationdb.record.query.expressions.Comparisons)22 Message (com.google.protobuf.Message)22 Query (com.apple.foundationdb.record.query.expressions.Query)20 RecordQueryPlanner (com.apple.foundationdb.record.query.plan.RecordQueryPlanner)20 List (java.util.List)20 Collections (java.util.Collections)19 Nullable (javax.annotation.Nullable)18 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)18 PlanHashable (com.apple.foundationdb.record.PlanHashable)17 Expressions.field (com.apple.foundationdb.record.metadata.Key.Expressions.field)17