Search in sources :

Example 1 with FieldWithComparison

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

the class GroupingValidator method findGroupFieldFilter.

private static boolean findGroupFieldFilter(@Nonnull List<QueryComponent> filters, @Nonnull FieldKeyExpression groupField, @Nonnull List<QueryComponent> groupFilters, @Nonnull List<Comparisons.Comparison> groupComparisons) {
    for (QueryComponent filter : filters) {
        if (filter instanceof FieldWithComparison) {
            FieldWithComparison comparisonFilter = (FieldWithComparison) filter;
            if (comparisonFilter.getFieldName().equals(groupField.getFieldName()) && (comparisonFilter.getComparison().getType() == Comparisons.Type.EQUALS || comparisonFilter.getComparison().getType() == Comparisons.Type.IS_NULL)) {
                groupFilters.add(filter);
                groupComparisons.add(comparisonFilter.getComparison());
                return true;
            }
        }
    }
    return false;
}
Also used : QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) FieldWithComparison(com.apple.foundationdb.record.query.expressions.FieldWithComparison)

Example 2 with FieldWithComparison

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

the class InExtractor method extractInClauses.

@SuppressWarnings("unchecked")
private QueryComponent extractInClauses() {
    final AtomicInteger bindingIndex = new AtomicInteger();
    return mapClauses(filter, (withComparison, fields) -> {
        if (withComparison.getComparison().getType() == Comparisons.Type.IN) {
            String bindingName = Bindings.Internal.IN.bindingName(withComparison.getName() + "__" + bindingIndex.getAndIncrement());
            List<FieldKeyExpression> nestedFields = null;
            if (fields != null && (withComparison instanceof FieldWithComparison || withComparison instanceof OneOfThemWithComparison)) {
                nestedFields = new ArrayList<>(fields);
                nestedFields.add(Key.Expressions.field(((BaseField) withComparison).getFieldName(), withComparison instanceof FieldWithComparison ? KeyExpression.FanType.None : KeyExpression.FanType.FanOut));
            }
            KeyExpression orderingKey = getOrderingKey(nestedFields);
            if (withComparison.getComparison() instanceof Comparisons.ComparisonWithParameter) {
                final String parameterName = ((Comparisons.ComparisonWithParameter) withComparison.getComparison()).getParameter();
                inClauses.add(new InParameterClause(bindingName, parameterName, orderingKey));
            } else {
                final List<Object> comparand = (List<Object>) withComparison.getComparison().getComparand();
                // ListComparison does not allow empty/null
                if (comparand != null && comparand.size() == 1) {
                    return withComparison.withOtherComparison(new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, comparand.get(0)));
                }
                inClauses.add(new InValuesClause(bindingName, comparand, orderingKey));
            }
            return withComparison.withOtherComparison(new Comparisons.ParameterComparison(Comparisons.Type.EQUALS, bindingName, Bindings.Internal.IN));
        } else {
            return withComparison;
        }
    }, Collections.emptyList());
}
Also used : BaseField(com.apple.foundationdb.record.query.expressions.BaseField) OneOfThemWithComparison(com.apple.foundationdb.record.query.expressions.OneOfThemWithComparison) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) FieldWithComparison(com.apple.foundationdb.record.query.expressions.FieldWithComparison) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 3 with FieldWithComparison

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

the class TextIndexTest method performQueryWithIndexScan.

@Nonnull
private Set<Long> performQueryWithIndexScan(@Nonnull RecordMetaDataHook hook, @Nonnull Index index, @Nonnull QueryComponent filter) throws Exception {
    final ExecuteProperties executeProperties = ExecuteProperties.newBuilder().setTimeLimit(3000).build();
    final RecordQuery query = RecordQuery.newBuilder().setRecordType(SIMPLE_DOC).setRequiredResults(Collections.singletonList(field("doc_id"))).setFilter(filter).build();
    Set<Long> results = new HashSet<>();
    RecordQueryPlan plan;
    byte[] continuation;
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context, hook);
        RecordQueryPlanner planner = new RecordQueryPlanner(recordStore.getRecordMetaData(), recordStore.getRecordStoreState());
        plan = planner.plan(query);
        assertThat(filter, instanceOf(FieldWithComparison.class));
        FieldWithComparison fieldFilter = (FieldWithComparison) filter;
        if (fieldFilter.getComparison() instanceof Comparisons.TextContainsAllPrefixesComparison && ((Comparisons.TextContainsAllPrefixesComparison) fieldFilter.getComparison()).isStrict()) {
            // Strict field field comparisons cannot be covering
            assertThat(plan, descendant(textIndexScan(indexName(index.getName()))));
        } else {
            assertThat(plan, descendant(coveringIndexScan(textIndexScan(indexName(index.getName())))));
        }
        try (RecordCursor<Long> cursor = recordStore.executeQuery(plan, null, executeProperties).map(record -> record.getPrimaryKey().getLong(0))) {
            cursor.forEach(results::add).get();
            continuation = cursor.getNext().getContinuation().toBytes();
        }
    }
    while (continuation != null) {
        try (FDBRecordContext context = openContext()) {
            openRecordStore(context, hook);
            try (RecordCursor<Long> cursor = recordStore.executeQuery(plan, continuation, executeProperties).map(record -> record.getPrimaryKey().getLong(0))) {
                cursor.forEach(results::add).get();
                continuation = cursor.getNext().getContinuation().toBytes();
            }
        }
    }
    return results;
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) FieldWithComparison(com.apple.foundationdb.record.query.expressions.FieldWithComparison) RecordQueryPlanner(com.apple.foundationdb.record.query.plan.RecordQueryPlanner) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) HashSet(java.util.HashSet) Nonnull(javax.annotation.Nonnull)

Example 4 with FieldWithComparison

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

the class RankIndexTest method twoRankPredicates.

@Test
public void twoRankPredicates() throws Exception {
    // Different rank predicates: at most one can be used in the scan.
    RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.and(Query.field("gender").equalsValue("M"), Query.rank(Key.Expressions.field("score").ungrouped()).lessThan(3L), Query.rank(Key.Expressions.field("score").groupBy(Key.Expressions.field("gender"))).equalsValue(1L))).build();
    RecordQueryPlan plan = planner.plan(query);
    assertThat(plan, scoreForRank(contains(hasToString("__rank_0 = BasicRankedRecord$score.score_for_rank_else_skip(3)")), fetch(filter(new FieldWithComparison("score", new Comparisons.ParameterComparison(Comparisons.Type.LESS_THAN, "__rank_0", Bindings.Internal.RANK)), coveringIndexScan(indexScan(allOf(indexName("rank_by_gender"), bounds(hasTupleString("[[M, 1],[M, 1]]")))))))));
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context);
        List<String> names = recordStore.executeQuery(plan).map(rec -> TestRecordsRankProto.BasicRankedRecord.newBuilder().mergeFrom(rec.getRecord()).getName()).asList().join();
        assertEquals(Arrays.asList("achilles"), names);
        commit(context);
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) FieldWithComparison(com.apple.foundationdb.record.query.expressions.FieldWithComparison) Matchers.hasToString(org.hamcrest.Matchers.hasToString) PlanMatchers.hasTupleString(com.apple.foundationdb.record.query.plan.match.PlanMatchers.hasTupleString) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Test(org.junit.jupiter.api.Test)

Example 5 with FieldWithComparison

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

the class IndexAggregateFunctionCall method extractFieldPaths.

/**
 * Helper method to extract a set of key expressions that are bound through some comparison in the
 * query component passed in.
 * @param queryComponent the query component
 * @param predicate a predicate used for filtering each encountered {@link FieldWithComparison}
 * @return a set of {@link KeyExpression}s where each element is a key expression of a field (i.e. a
 * {@link com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression}) or a simple nesting field
 * (i.e. a {@link com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression}) that is bound
 * through some comparison
 */
@Nonnull
public static Set<KeyExpression> extractFieldPaths(@Nonnull QueryComponent queryComponent, @Nonnull final Predicate<FieldWithComparison> predicate) {
    if (queryComponent instanceof BaseField) {
        final BaseField baseField = (BaseField) queryComponent;
        if (baseField instanceof NestedField) {
            final NestedField nestedField = (NestedField) baseField;
            final Set<KeyExpression> nestedExpressions = extractFieldPaths(nestedField.getChild(), predicate);
            return nestedExpressions.stream().map(nestedExpression -> Key.Expressions.field(nestedField.getFieldName()).nest(nestedExpression)).collect(ImmutableSet.toImmutableSet());
        }
        if (baseField instanceof FieldWithComparison) {
            final FieldWithComparison fieldWithComparison = (FieldWithComparison) baseField;
            if (predicate.test(fieldWithComparison)) {
                return ImmutableSet.of(Key.Expressions.field(fieldWithComparison.getFieldName()));
            }
        }
        return ImmutableSet.of();
    } else if (queryComponent instanceof AndComponent) {
        final Set<KeyExpression> boundFields = Sets.newHashSet();
        final AndComponent andComponent = (AndComponent) queryComponent;
        andComponent.getChildren().forEach(child -> boundFields.addAll(extractEqualityBoundFields(child)));
        return boundFields;
    }
    return ImmutableSet.of();
}
Also used : NestedField(com.apple.foundationdb.record.query.expressions.NestedField) FieldWithComparison(com.apple.foundationdb.record.query.expressions.FieldWithComparison) Iterables(com.google.common.collect.Iterables) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) ImmutableSet(com.google.common.collect.ImmutableSet) Predicate(java.util.function.Predicate) Set(java.util.Set) EnumeratingIterable(com.apple.foundationdb.record.query.combinatorics.EnumeratingIterable) AndComponent(com.apple.foundationdb.record.query.expressions.AndComponent) Sets(com.google.common.collect.Sets) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) List(java.util.List) NestedField(com.apple.foundationdb.record.query.expressions.NestedField) Stream(java.util.stream.Stream) BaseField(com.apple.foundationdb.record.query.expressions.BaseField) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) StreamSupport(java.util.stream.StreamSupport) API(com.apple.foundationdb.annotation.API) Objects(com.google.common.base.Objects) Nonnull(javax.annotation.Nonnull) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) TopologicalSort(com.apple.foundationdb.record.query.combinatorics.TopologicalSort) Nullable(javax.annotation.Nullable) BaseField(com.apple.foundationdb.record.query.expressions.BaseField) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) AndComponent(com.apple.foundationdb.record.query.expressions.AndComponent) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) FieldWithComparison(com.apple.foundationdb.record.query.expressions.FieldWithComparison) Nonnull(javax.annotation.Nonnull)

Aggregations

FieldWithComparison (com.apple.foundationdb.record.query.expressions.FieldWithComparison)8 Comparisons (com.apple.foundationdb.record.query.expressions.Comparisons)5 QueryComponent (com.apple.foundationdb.record.query.expressions.QueryComponent)5 NestedField (com.apple.foundationdb.record.query.expressions.NestedField)4 FieldKeyExpression (com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression)3 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)3 NestingKeyExpression (com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression)3 BaseField (com.apple.foundationdb.record.query.expressions.BaseField)3 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)3 List (java.util.List)3 Nonnull (javax.annotation.Nonnull)3 Nullable (javax.annotation.Nullable)3 API (com.apple.foundationdb.annotation.API)2 GroupingKeyExpression (com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression)2 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)2 RecordQuery (com.apple.foundationdb.record.query.RecordQuery)2 AndComponent (com.apple.foundationdb.record.query.expressions.AndComponent)2 Set (java.util.Set)2 Bindings (com.apple.foundationdb.record.Bindings)1 ExecuteProperties (com.apple.foundationdb.record.ExecuteProperties)1