Search in sources :

Example 1 with ComponentWithComparison

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

the class InExtractor method mapClauses.

private QueryComponent mapClauses(QueryComponent filter, BiFunction<ComponentWithComparison, List<FieldKeyExpression>, QueryComponent> mapper, @Nullable List<FieldKeyExpression> fields) {
    if (filter instanceof ComponentWithComparison) {
        final ComponentWithComparison withComparison = (ComponentWithComparison) filter;
        return mapper.apply(withComparison, fields);
    } else if (filter instanceof ComponentWithChildren) {
        ComponentWithChildren componentWithChildren = (ComponentWithChildren) filter;
        return componentWithChildren.withOtherChildren(componentWithChildren.getChildren().stream().map(component -> mapClauses(component, mapper, fields)).collect(Collectors.toList()));
    } else if (filter instanceof ComponentWithSingleChild) {
        ComponentWithSingleChild componentWithSingleChild = (ComponentWithSingleChild) filter;
        List<FieldKeyExpression> nestedFields = null;
        if (fields != null && (componentWithSingleChild instanceof NestedField || componentWithSingleChild instanceof OneOfThemWithComponent)) {
            nestedFields = new ArrayList<>(fields);
            nestedFields.add(Key.Expressions.field(((BaseField) componentWithSingleChild).getFieldName(), componentWithSingleChild instanceof NestedField ? KeyExpression.FanType.None : KeyExpression.FanType.FanOut));
        }
        return componentWithSingleChild.withOtherChild(mapClauses(componentWithSingleChild.getChild(), mapper, nestedFields));
    } else if (filter instanceof ComponentWithNoChildren) {
        return filter;
    } else {
        throw new Query.InvalidExpressionException("Unsupported query type " + filter.getClass());
    }
}
Also used : BiFunction(java.util.function.BiFunction) Bindings(com.apple.foundationdb.record.Bindings) PlanOrderingKey(com.apple.foundationdb.record.query.plan.PlanOrderingKey) ComponentWithSingleChild(com.apple.foundationdb.record.query.expressions.ComponentWithSingleChild) ComponentWithComparison(com.apple.foundationdb.record.query.expressions.ComponentWithComparison) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) ArrayList(java.util.ArrayList) Key(com.apple.foundationdb.record.metadata.Key) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) ImmutableList(com.google.common.collect.ImmutableList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) ComponentWithNoChildren(com.apple.foundationdb.record.query.expressions.ComponentWithNoChildren) InSource(com.apple.foundationdb.record.query.plan.plans.InSource) InValuesSource(com.apple.foundationdb.record.query.plan.plans.InValuesSource) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) FieldWithComparison(com.apple.foundationdb.record.query.expressions.FieldWithComparison) Query(com.apple.foundationdb.record.query.expressions.Query) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) RecordQueryInParameterJoinPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryInParameterJoinPlan) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) List(java.util.List) NestedField(com.apple.foundationdb.record.query.expressions.NestedField) OneOfThemWithComponent(com.apple.foundationdb.record.query.expressions.OneOfThemWithComponent) BaseField(com.apple.foundationdb.record.query.expressions.BaseField) OneOfThemWithComparison(com.apple.foundationdb.record.query.expressions.OneOfThemWithComparison) RecordQueryInValuesJoinPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryInValuesJoinPlan) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) API(com.apple.foundationdb.annotation.API) InParameterSource(com.apple.foundationdb.record.query.plan.plans.InParameterSource) ComponentWithChildren(com.apple.foundationdb.record.query.expressions.ComponentWithChildren) Collections(java.util.Collections) BaseField(com.apple.foundationdb.record.query.expressions.BaseField) Query(com.apple.foundationdb.record.query.expressions.Query) ComponentWithNoChildren(com.apple.foundationdb.record.query.expressions.ComponentWithNoChildren) ComponentWithChildren(com.apple.foundationdb.record.query.expressions.ComponentWithChildren) NestedField(com.apple.foundationdb.record.query.expressions.NestedField) ComponentWithSingleChild(com.apple.foundationdb.record.query.expressions.ComponentWithSingleChild) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) ComponentWithComparison(com.apple.foundationdb.record.query.expressions.ComponentWithComparison) OneOfThemWithComponent(com.apple.foundationdb.record.query.expressions.OneOfThemWithComponent)

Example 2 with ComponentWithComparison

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

the class TextIndexTest method queryMapDocumentsWithIndex.

@Nonnull
private List<Long> queryMapDocumentsWithIndex(@Nonnull String key, @Nonnull QueryComponent textFilter, int planHash, boolean isCoveringIndexExpected) throws InterruptedException, ExecutionException {
    if (!(textFilter instanceof ComponentWithComparison)) {
        throw new RecordCoreArgumentException("filter without comparison provided as text filter");
    }
    final QueryComponent filter = Query.field("entry").oneOfThem().matches(Query.and(Query.field("key").equalsValue(key), textFilter));
    Matcher<RecordQueryPlan> indexMatcher = textIndexScan(allOf(indexName(MAP_ON_VALUE_INDEX.getName()), groupingBounds(allOf(notNullValue(), hasTupleString("[[" + key + "],[" + key + "]]"))), textComparison(equalTo(((ComponentWithComparison) textFilter).getComparison()))));
    if (isCoveringIndexExpected) {
        indexMatcher = coveringIndexScan(indexMatcher);
    }
    final Matcher<RecordQueryPlan> planMatcher = descendant(indexMatcher);
    return queryDocuments(Collections.singletonList(MAP_DOC), Collections.singletonList(field("doc_id")), filter, planHash, planMatcher).map(t -> t.getLong(0)).asList().get();
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) ComponentWithComparison(com.apple.foundationdb.record.query.expressions.ComponentWithComparison) RecordCoreArgumentException(com.apple.foundationdb.record.RecordCoreArgumentException) Nonnull(javax.annotation.Nonnull)

Example 3 with ComponentWithComparison

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

the class TextIndexTest method queryMapDocumentsWithGroupedIndex.

@Nonnull
private List<Long> queryMapDocumentsWithGroupedIndex(@Nonnull String key, @Nonnull QueryComponent textFilter, long group, int planHash) throws InterruptedException, ExecutionException {
    if (!(textFilter instanceof ComponentWithComparison)) {
        throw new RecordCoreArgumentException("filter without comparison provided as text filter");
    }
    final QueryComponent filter = Query.and(Query.field("group").equalsValue(group), Query.field("entry").oneOfThem().matches(Query.and(Query.field("key").equalsValue(key), textFilter)));
    final Matcher<RecordQueryPlan> planMatcher = descendant(coveringIndexScan(textIndexScan(allOf(indexName(MAP_ON_VALUE_GROUPED_INDEX.getName()), groupingBounds(allOf(notNullValue(), hasTupleString("[[" + group + ", " + key + "],[" + group + ", " + key + "]]"))), textComparison(equalTo(((ComponentWithComparison) textFilter).getComparison()))))));
    return queryDocuments(Collections.singletonList(MAP_DOC), Collections.singletonList(field("doc_id")), filter, planHash, planMatcher).map(t -> t.getLong(0)).asList().get();
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) ComponentWithComparison(com.apple.foundationdb.record.query.expressions.ComponentWithComparison) RecordCoreArgumentException(com.apple.foundationdb.record.RecordCoreArgumentException) Nonnull(javax.annotation.Nonnull)

Example 4 with ComponentWithComparison

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

the class TextIndexTest method queryComplexDocumentsWithIndex.

@Nonnull
private List<Tuple> queryComplexDocumentsWithIndex(@Nonnull QueryComponent textFilter, @Nullable QueryComponent additionalFilter, boolean skipFilterCheck, long group, int planHash) throws InterruptedException, ExecutionException {
    if (!(textFilter instanceof ComponentWithComparison)) {
        throw new RecordCoreArgumentException("filter without comparison provided as text filter");
    }
    final Matcher<RecordQueryPlan> textScanMatcher = textIndexScan(allOf(indexName(COMPLEX_TEXT_BY_GROUP.getName()), groupingBounds(allOf(notNullValue(), hasTupleString("[[" + group + "],[" + group + "]]"))), textComparison(equalTo(((ComponentWithComparison) textFilter).getComparison()))));
    // Don't care whether it's covering or not
    final Matcher<RecordQueryPlan> textPlanMatcher = anyOf(textScanMatcher, coveringIndexScan(textScanMatcher));
    final Matcher<RecordQueryPlan> planMatcher;
    final QueryComponent filter;
    if (additionalFilter != null) {
        if (skipFilterCheck) {
            planMatcher = descendant(textPlanMatcher);
        } else {
            planMatcher = descendant(filter(additionalFilter, descendant(textPlanMatcher)));
        }
        filter = Query.and(textFilter, additionalFilter, Query.field("group").equalsValue(group));
    } else {
        planMatcher = descendant(textPlanMatcher);
        filter = Query.and(textFilter, Query.field("group").equalsValue(group));
    }
    return queryComplexDocumentsWithPlan(filter, planHash, planMatcher);
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) ComponentWithComparison(com.apple.foundationdb.record.query.expressions.ComponentWithComparison) RecordCoreArgumentException(com.apple.foundationdb.record.RecordCoreArgumentException) Nonnull(javax.annotation.Nonnull)

Aggregations

ComponentWithComparison (com.apple.foundationdb.record.query.expressions.ComponentWithComparison)4 QueryComponent (com.apple.foundationdb.record.query.expressions.QueryComponent)4 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)4 Nonnull (javax.annotation.Nonnull)4 RecordCoreArgumentException (com.apple.foundationdb.record.RecordCoreArgumentException)3 API (com.apple.foundationdb.annotation.API)1 Bindings (com.apple.foundationdb.record.Bindings)1 Key (com.apple.foundationdb.record.metadata.Key)1 FieldKeyExpression (com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression)1 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)1 NestingKeyExpression (com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression)1 BaseField (com.apple.foundationdb.record.query.expressions.BaseField)1 Comparisons (com.apple.foundationdb.record.query.expressions.Comparisons)1 ComponentWithChildren (com.apple.foundationdb.record.query.expressions.ComponentWithChildren)1 ComponentWithNoChildren (com.apple.foundationdb.record.query.expressions.ComponentWithNoChildren)1 ComponentWithSingleChild (com.apple.foundationdb.record.query.expressions.ComponentWithSingleChild)1 FieldWithComparison (com.apple.foundationdb.record.query.expressions.FieldWithComparison)1 NestedField (com.apple.foundationdb.record.query.expressions.NestedField)1 OneOfThemWithComparison (com.apple.foundationdb.record.query.expressions.OneOfThemWithComparison)1 OneOfThemWithComponent (com.apple.foundationdb.record.query.expressions.OneOfThemWithComponent)1