Search in sources :

Example 1 with BaseField

use of com.apple.foundationdb.record.query.expressions.BaseField 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 2 with BaseField

use of com.apple.foundationdb.record.query.expressions.BaseField 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 3 with BaseField

use of com.apple.foundationdb.record.query.expressions.BaseField 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)

Example 4 with BaseField

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

the class GroupingValidator method matchNestingField.

private static boolean matchNestingField(@Nonnull QueryComponent filter, @Nonnull ComponentWithSingleChild nestingComponent, @Nonnull NestingKeyExpression nesting, @Nonnull List<QueryComponent> groupFilters, @Nonnull List<Comparisons.Comparison> groupComparisons) {
    if (nesting.getChild() instanceof NestingKeyExpression) {
        NestingKeyExpression childNesting = (NestingKeyExpression) nesting.getChild();
        QueryComponent childComponent = nestingComponent.getChild();
        if (childComponent instanceof NestedField || childComponent instanceof OneOfThemWithComponent) {
            if (childNesting.getParent().getFieldName().equals(((BaseField) childComponent).getFieldName()) && matchNestingField(filter, (ComponentWithSingleChild) childComponent, childNesting, groupFilters, groupComparisons)) {
                return true;
            }
        }
    } else if (nesting.getChild() instanceof FieldKeyExpression) {
        FieldKeyExpression childField = (FieldKeyExpression) nesting.getChild();
        if (nestingComponent.getChild() instanceof FieldWithComparison) {
            FieldWithComparison comparisonFilter = (FieldWithComparison) nestingComponent.getChild();
            if (comparisonFilter.getFieldName().equals(childField.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 : NestedField(com.apple.foundationdb.record.query.expressions.NestedField) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) BaseField(com.apple.foundationdb.record.query.expressions.BaseField) ComponentWithSingleChild(com.apple.foundationdb.record.query.expressions.ComponentWithSingleChild) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) OneOfThemWithComponent(com.apple.foundationdb.record.query.expressions.OneOfThemWithComponent) FieldWithComparison(com.apple.foundationdb.record.query.expressions.FieldWithComparison)

Aggregations

BaseField (com.apple.foundationdb.record.query.expressions.BaseField)4 FieldWithComparison (com.apple.foundationdb.record.query.expressions.FieldWithComparison)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 Comparisons (com.apple.foundationdb.record.query.expressions.Comparisons)3 NestedField (com.apple.foundationdb.record.query.expressions.NestedField)3 QueryComponent (com.apple.foundationdb.record.query.expressions.QueryComponent)3 List (java.util.List)3 API (com.apple.foundationdb.annotation.API)2 ComponentWithSingleChild (com.apple.foundationdb.record.query.expressions.ComponentWithSingleChild)2 OneOfThemWithComparison (com.apple.foundationdb.record.query.expressions.OneOfThemWithComparison)2 OneOfThemWithComponent (com.apple.foundationdb.record.query.expressions.OneOfThemWithComponent)2 ImmutableList (com.google.common.collect.ImmutableList)2 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Nonnull (javax.annotation.Nonnull)2 Nullable (javax.annotation.Nullable)2 Bindings (com.apple.foundationdb.record.Bindings)1 Key (com.apple.foundationdb.record.metadata.Key)1