Search in sources :

Example 1 with FieldValue

use of com.apple.foundationdb.record.query.predicates.FieldValue in project fdb-record-layer by FoundationDB.

the class ValueIndexLikeExpansionVisitor method visitExpression.

@Nonnull
@Override
public GraphExpansion visitExpression(@Nonnull FieldKeyExpression fieldKeyExpression) {
    final String fieldName = fieldKeyExpression.getFieldName();
    final KeyExpression.FanType fanType = fieldKeyExpression.getFanType();
    final VisitorState state = getCurrentState();
    final List<String> fieldNamePrefix = state.getFieldNamePrefix();
    final CorrelationIdentifier baseAlias = state.getBaseAlias();
    final List<String> fieldNames = ImmutableList.<String>builder().addAll(fieldNamePrefix).add(fieldName).build();
    final Value value;
    final Placeholder predicate;
    switch(fanType) {
        case FanOut:
            // explode this field and prefixes of this field
            final Quantifier childBase = fieldKeyExpression.explodeField(baseAlias, fieldNamePrefix);
            value = state.registerValue(QuantifiedObjectValue.of(childBase.getAlias()));
            final GraphExpansion childExpansion;
            if (state.isKey()) {
                predicate = value.asPlaceholder(newParameterAlias());
                childExpansion = GraphExpansion.ofPlaceholder(value, predicate);
            } else {
                childExpansion = GraphExpansion.ofResultValue(value);
            }
            final SelectExpression selectExpression = childExpansion.buildSelectWithBase(childBase);
            final Quantifier childQuantifier = Quantifier.forEach(GroupExpressionRef.of(selectExpression));
            final GraphExpansion.Sealed sealedChildExpansion = childExpansion.seal();
            return sealedChildExpansion.derivedWithQuantifier(childQuantifier);
        case None:
            value = state.registerValue(new FieldValue(QuantifiedColumnValue.of(baseAlias, 0), fieldNames));
            if (state.isKey()) {
                predicate = value.asPlaceholder(newParameterAlias());
                return GraphExpansion.ofPlaceholder(value, predicate);
            }
            return GraphExpansion.ofResultValue(value);
        // TODO collect/concatenate function
        case Concatenate:
        default:
    }
    throw new UnsupportedOperationException();
}
Also used : Placeholder(com.apple.foundationdb.record.query.predicates.ValueComparisonRangePredicate.Placeholder) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) SelectExpression(com.apple.foundationdb.record.query.plan.temp.expressions.SelectExpression) VisitorState(com.apple.foundationdb.record.query.plan.temp.ValueIndexLikeExpansionVisitor.VisitorState) QuantifiedObjectValue(com.apple.foundationdb.record.query.predicates.QuantifiedObjectValue) Value(com.apple.foundationdb.record.query.predicates.Value) EmptyValue(com.apple.foundationdb.record.query.predicates.EmptyValue) KeyExpressionWithValue(com.apple.foundationdb.record.metadata.expressions.KeyExpressionWithValue) FieldValue(com.apple.foundationdb.record.query.predicates.FieldValue) QuantifiedColumnValue(com.apple.foundationdb.record.query.predicates.QuantifiedColumnValue) FieldValue(com.apple.foundationdb.record.query.predicates.FieldValue) Nonnull(javax.annotation.Nonnull)

Example 2 with FieldValue

use of com.apple.foundationdb.record.query.predicates.FieldValue in project fdb-record-layer by FoundationDB.

the class OrderingProperty method deriveForPredicatesFilterFromOrderings.

public static Optional<Ordering> deriveForPredicatesFilterFromOrderings(@Nonnull final List<Optional<Ordering>> orderingOptionals, @Nonnull final RecordQueryPredicatesFilterPlan predicatesFilterPlan) {
    final Optional<Ordering> childOrderingOptional = Iterables.getOnlyElement(orderingOptionals);
    if (childOrderingOptional.isPresent()) {
        final Ordering childOrdering = childOrderingOptional.get();
        final SetMultimap<KeyExpression, Comparisons.Comparison> equalityBoundFileKeyExpressions = predicatesFilterPlan.getPredicates().stream().flatMap(queryPredicate -> {
            if (!(queryPredicate instanceof ValuePredicate)) {
                return Stream.empty();
            }
            final ValuePredicate valuePredicate = (ValuePredicate) queryPredicate;
            if (!valuePredicate.getComparison().getType().isEquality()) {
                return Stream.empty();
            }
            if (!(valuePredicate.getValue() instanceof FieldValue)) {
                return Stream.of();
            }
            return Stream.of(Pair.of((FieldValue) valuePredicate.getValue(), valuePredicate.getComparison()));
        }).map(valueComparisonPair -> {
            final FieldValue fieldValue = valueComparisonPair.getLeft();
            final String fieldName = fieldValue.getFieldName();
            KeyExpression keyExpression = Key.Expressions.field(fieldName);
            final List<String> fieldPrefix = fieldValue.getFieldPrefix();
            for (int i = fieldPrefix.size() - 1; i >= 0; i--) {
                keyExpression = Key.Expressions.field(fieldPrefix.get(i)).nest(keyExpression);
            }
            return Pair.of(keyExpression, valueComparisonPair.getRight());
        }).collect(ImmutableSetMultimap.toImmutableSetMultimap(Pair::getLeft, Pair::getRight));
        final ImmutableList<KeyPart> resultOrderingKeyParts = childOrdering.getOrderingKeyParts().stream().filter(keyPart -> !equalityBoundFileKeyExpressions.containsKey(keyPart.getNormalizedKeyExpression())).collect(ImmutableList.toImmutableList());
        final SetMultimap<KeyExpression, Comparisons.Comparison> resultEqualityBoundKeyMap = HashMultimap.create(childOrdering.getEqualityBoundKeyMap());
        equalityBoundFileKeyExpressions.forEach(resultEqualityBoundKeyMap::put);
        return Optional.of(new Ordering(resultEqualityBoundKeyMap, resultOrderingKeyParts, childOrdering.isDistinct()));
    } else {
        return Optional.empty();
    }
}
Also used : RecordQueryIntersectionPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryIntersectionPlan) PlannerProperty(com.apple.foundationdb.record.query.plan.temp.PlannerProperty) HashMultimap(com.google.common.collect.HashMultimap) Pair(org.apache.commons.lang3.tuple.Pair) RecordQueryScanPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryScanPlan) RecordQueryUnionPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryUnionPlan) RecordQueryUnorderedUnionPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryUnorderedUnionPlan) PrimaryScanExpression(com.apple.foundationdb.record.query.plan.temp.expressions.PrimaryScanExpression) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) RequestedOrdering(com.apple.foundationdb.record.query.plan.temp.RequestedOrdering) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) RecordQueryPredicatesFilterPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPredicatesFilterPlan) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) RecordQueryCoveringIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryCoveringIndexPlan) List(java.util.List) Stream(java.util.stream.Stream) RecordQueryInUnionPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryInUnionPlan) CorrelationIdentifier(com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier) RecordQueryInJoinPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryInJoinPlan) PlanContext(com.apple.foundationdb.record.query.plan.temp.PlanContext) Optional(java.util.Optional) API(com.apple.foundationdb.annotation.API) CORRELATION(com.apple.foundationdb.record.Bindings.Internal.CORRELATION) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) Iterables(com.google.common.collect.Iterables) ValuePredicate(com.apple.foundationdb.record.query.predicates.ValuePredicate) Quantifier(com.apple.foundationdb.record.query.plan.temp.Quantifier) Ordering(com.apple.foundationdb.record.query.plan.temp.Ordering) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) LogicalSortExpression(com.apple.foundationdb.record.query.plan.temp.expressions.LogicalSortExpression) Key(com.apple.foundationdb.record.metadata.Key) ImmutableList(com.google.common.collect.ImmutableList) ExpressionRef(com.apple.foundationdb.record.query.plan.temp.ExpressionRef) FieldValue(com.apple.foundationdb.record.query.predicates.FieldValue) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) RecordQueryIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan) RecordQueryPlanWithIndex(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlanWithIndex) ScanComparisons(com.apple.foundationdb.record.query.plan.ScanComparisons) KeyPart(com.apple.foundationdb.record.query.plan.temp.KeyPart) SetMultimap(com.google.common.collect.SetMultimap) RelationalExpression(com.apple.foundationdb.record.query.plan.temp.RelationalExpression) ValueIndexExpansionVisitor(com.apple.foundationdb.record.query.plan.temp.ValueIndexExpansionVisitor) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) RecordType(com.apple.foundationdb.record.metadata.RecordType) Index(com.apple.foundationdb.record.metadata.Index) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) KeyPart(com.apple.foundationdb.record.query.plan.temp.KeyPart) ValuePredicate(com.apple.foundationdb.record.query.predicates.ValuePredicate) RequestedOrdering(com.apple.foundationdb.record.query.plan.temp.RequestedOrdering) Ordering(com.apple.foundationdb.record.query.plan.temp.Ordering) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) FieldValue(com.apple.foundationdb.record.query.predicates.FieldValue)

Example 3 with FieldValue

use of com.apple.foundationdb.record.query.predicates.FieldValue in project fdb-record-layer by FoundationDB.

the class PushReferencedFieldsThroughSelectRule method onMatch.

@Override
public void onMatch(@Nonnull PlannerRuleCall call) {
    final PlannerBindings bindings = call.getBindings();
    final SelectExpression selectExpression = bindings.get(root);
    final List<? extends QueryPredicate> predicates = bindings.getAll(predicateMatcher);
    final Set<FieldValue> fieldValuesFromPredicates = RelationalExpressionWithPredicates.fieldValuesFromPredicates(predicates);
    final Set<FieldValue> fieldValuesFromResultValues = selectExpression.getFieldValuesFromResultValues();
    final ExpressionRef<? extends RelationalExpression> lowerRef = bindings.get(lowerRefMatcher);
    final ImmutableSet<FieldValue> allReferencedValues = ImmutableSet.<FieldValue>builder().addAll(call.getInterestingProperty(ReferencedFieldsAttribute.REFERENCED_FIELDS).map(ReferencedFields::getReferencedFieldValues).orElse(ImmutableSet.of())).addAll(fieldValuesFromPredicates).addAll(fieldValuesFromResultValues).build();
    call.pushRequirement(lowerRef, ReferencedFieldsAttribute.REFERENCED_FIELDS, new ReferencedFields(allReferencedValues));
}
Also used : ReferencedFields(com.apple.foundationdb.record.query.plan.temp.ReferencedFieldsAttribute.ReferencedFields) PlannerBindings(com.apple.foundationdb.record.query.plan.temp.matchers.PlannerBindings) SelectExpression(com.apple.foundationdb.record.query.plan.temp.expressions.SelectExpression) FieldValue(com.apple.foundationdb.record.query.predicates.FieldValue)

Example 4 with FieldValue

use of com.apple.foundationdb.record.query.predicates.FieldValue in project fdb-record-layer by FoundationDB.

the class BooleanPredicateNormalizerTest method bigNonCnf.

@Test
void bigNonCnf() {
    final QueryPredicate cnf = and(IntStream.rangeClosed(1, 9).boxed().map(i -> or(IntStream.rangeClosed(1, 9).boxed().map(j -> and(new ValuePredicate(new FieldValue(QuantifiedColumnValue.of(CorrelationIdentifier.UNGROUNDED, 0), ImmutableList.of("num_value_3_indexed")), new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, i * 9 + j)), new ValuePredicate(new FieldValue(QuantifiedColumnValue.of(CorrelationIdentifier.UNGROUNDED, 0), ImmutableList.of("str_value_indexed")), new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, "foo")))).collect(Collectors.toList()))).collect(Collectors.toList()));
    final BooleanPredicateNormalizer plannerNormalizer = BooleanPredicateNormalizer.forConfiguration(DNF, RecordQueryPlannerConfiguration.builder().build());
    assertThrows(BooleanPredicateNormalizer.NormalFormTooLargeException.class, () -> plannerNormalizer.normalize(cnf));
    assertEquals(cnf, plannerNormalizer.normalizeIfPossible(cnf).orElse(cnf));
}
Also used : IntStream(java.util.stream.IntStream) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) DNF(com.apple.foundationdb.record.query.plan.planning.BooleanPredicateNormalizer.Mode.DNF) ValuePredicate(com.apple.foundationdb.record.query.predicates.ValuePredicate) RecordQueryPlannerConfiguration(com.apple.foundationdb.record.query.plan.RecordQueryPlannerConfiguration) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) QueryPredicate(com.apple.foundationdb.record.query.predicates.QueryPredicate) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) List(java.util.List) OrPredicate.or(com.apple.foundationdb.record.query.predicates.OrPredicate.or) ImmutableList(com.google.common.collect.ImmutableList) CorrelationIdentifier(com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier) OrPredicate(com.apple.foundationdb.record.query.predicates.OrPredicate) AndPredicate.and(com.apple.foundationdb.record.query.predicates.AndPredicate.and) NotPredicate.not(com.apple.foundationdb.record.query.predicates.NotPredicate.not) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FieldValue(com.apple.foundationdb.record.query.predicates.FieldValue) QuantifiedColumnValue(com.apple.foundationdb.record.query.predicates.QuantifiedColumnValue) Nonnull(javax.annotation.Nonnull) QueryPredicate(com.apple.foundationdb.record.query.predicates.QueryPredicate) ValuePredicate(com.apple.foundationdb.record.query.predicates.ValuePredicate) FieldValue(com.apple.foundationdb.record.query.predicates.FieldValue) Test(org.junit.jupiter.api.Test)

Example 5 with FieldValue

use of com.apple.foundationdb.record.query.predicates.FieldValue in project fdb-record-layer by FoundationDB.

the class ValueIndexScanMatchCandidate method tryFetchCoveringIndexScan.

@Nonnull
private Optional<RelationalExpression> tryFetchCoveringIndexScan(@Nonnull final PartialMatch partialMatch, @Nonnull final List<ComparisonRange> comparisonRanges, final boolean isReverse) {
    if (recordTypes.size() > 1) {
        return Optional.empty();
    }
    final RecordType recordType = Iterables.getOnlyElement(recordTypes);
    final IndexKeyValueToPartialRecord.Builder builder = IndexKeyValueToPartialRecord.newBuilder(recordType);
    for (int i = 0; i < indexKeyValues.size(); i++) {
        final Value keyValue = indexKeyValues.get(i);
        if (keyValue instanceof FieldValue && keyValue.isFunctionallyDependentOn(recordValue)) {
            final AvailableFields.FieldData fieldData = AvailableFields.FieldData.of(IndexKeyValueToPartialRecord.TupleSource.KEY, i);
            addCoveringField(builder, (FieldValue) keyValue, fieldData);
        }
    }
    for (int i = 0; i < indexValueValues.size(); i++) {
        final Value valueValue = indexValueValues.get(i);
        if (valueValue instanceof FieldValue && valueValue.isFunctionallyDependentOn(recordValue)) {
            final AvailableFields.FieldData fieldData = AvailableFields.FieldData.of(IndexKeyValueToPartialRecord.TupleSource.VALUE, i);
            addCoveringField(builder, (FieldValue) valueValue, fieldData);
        }
    }
    if (!builder.isValid()) {
        return Optional.empty();
    }
    final IndexScanParameters scanParameters = IndexScanComparisons.byValue(toScanComparisons(comparisonRanges));
    final RecordQueryPlanWithIndex indexPlan = new RecordQueryIndexPlan(index.getName(), scanParameters, isReverse, false, (ValueIndexScanMatchCandidate) partialMatch.getMatchCandidate());
    final RecordQueryCoveringIndexPlan coveringIndexPlan = new RecordQueryCoveringIndexPlan(indexPlan, recordType.getName(), // not used except for old planner properties
    AvailableFields.NO_FIELDS, builder.build());
    return Optional.of(new RecordQueryFetchFromPartialRecordPlan(coveringIndexPlan, coveringIndexPlan::pushValueThroughFetch));
}
Also used : IndexScanParameters(com.apple.foundationdb.record.provider.foundationdb.IndexScanParameters) IndexKeyValueToPartialRecord(com.apple.foundationdb.record.query.plan.IndexKeyValueToPartialRecord) RecordQueryIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan) RecordQueryCoveringIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryCoveringIndexPlan) AvailableFields(com.apple.foundationdb.record.query.plan.AvailableFields) RecordQueryFetchFromPartialRecordPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryFetchFromPartialRecordPlan) RecordType(com.apple.foundationdb.record.metadata.RecordType) QuantifiedValue(com.apple.foundationdb.record.query.predicates.QuantifiedValue) FieldValue(com.apple.foundationdb.record.query.predicates.FieldValue) Value(com.apple.foundationdb.record.query.predicates.Value) QuantifiedColumnValue(com.apple.foundationdb.record.query.predicates.QuantifiedColumnValue) RecordQueryPlanWithIndex(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlanWithIndex) FieldValue(com.apple.foundationdb.record.query.predicates.FieldValue) Nonnull(javax.annotation.Nonnull)

Aggregations

FieldValue (com.apple.foundationdb.record.query.predicates.FieldValue)6 Nonnull (javax.annotation.Nonnull)4 RecordType (com.apple.foundationdb.record.metadata.RecordType)2 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)2 Comparisons (com.apple.foundationdb.record.query.expressions.Comparisons)2 RecordQueryCoveringIndexPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryCoveringIndexPlan)2 RecordQueryIndexPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan)2 RecordQueryPlanWithIndex (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlanWithIndex)2 CorrelationIdentifier (com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier)2 ReferencedFields (com.apple.foundationdb.record.query.plan.temp.ReferencedFieldsAttribute.ReferencedFields)2 SelectExpression (com.apple.foundationdb.record.query.plan.temp.expressions.SelectExpression)2 QuantifiedColumnValue (com.apple.foundationdb.record.query.predicates.QuantifiedColumnValue)2 API (com.apple.foundationdb.annotation.API)1 CORRELATION (com.apple.foundationdb.record.Bindings.Internal.CORRELATION)1 RecordMetaData (com.apple.foundationdb.record.RecordMetaData)1 Index (com.apple.foundationdb.record.metadata.Index)1 Key (com.apple.foundationdb.record.metadata.Key)1 EmptyKeyExpression (com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression)1 FieldKeyExpression (com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression)1 KeyExpressionWithValue (com.apple.foundationdb.record.metadata.expressions.KeyExpressionWithValue)1