Search in sources :

Example 1 with ValuePredicate

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

the class InComparisonToExplodeRule method onMatch.

@Override
public void onMatch(@Nonnull PlannerRuleCall call) {
    final PlannerBindings bindings = call.getBindings();
    final SelectExpression selectExpression = bindings.get(root);
    // we don't need iteration stability
    final List<? extends ValuePredicate> inPredicatesList = bindings.getAll(inPredicateMatcher);
    if (inPredicatesList.isEmpty()) {
        return;
    }
    final Set<QueryPredicate> inPredicates = Sets.newIdentityHashSet();
    inPredicates.addAll(inPredicatesList);
    final ImmutableList.Builder<Quantifier> transformedQuantifiers = ImmutableList.builder();
    final ImmutableList.Builder<QueryPredicate> transformedPredicates = ImmutableList.builder();
    for (final QueryPredicate predicate : selectExpression.getPredicates()) {
        if (inPredicates.contains(predicate)) {
            final ValuePredicate valuePredicate = (ValuePredicate) predicate;
            final Comparisons.Comparison comparison = valuePredicate.getComparison();
            Verify.verify(comparison.getType() == Comparisons.Type.IN);
            final ExplodeExpression explodeExpression;
            if (comparison instanceof Comparisons.ListComparison) {
                explodeExpression = new ExplodeExpression(new LiteralValue<>(comparison.getComparand()));
            } else if (comparison instanceof Comparisons.ParameterComparison) {
                explodeExpression = new ExplodeExpression(QuantifiedColumnValue.of(CorrelationIdentifier.of(((Comparisons.ParameterComparison) comparison).getParameter()), 0));
            } else {
                throw new RecordCoreException("unknown in comparison " + comparison.getClass().getSimpleName());
            }
            final Quantifier.ForEach newQuantifier = Quantifier.forEach(GroupExpressionRef.of(explodeExpression));
            transformedPredicates.add(new ValuePredicate(((ValuePredicate) predicate).getValue(), new Comparisons.ParameterComparison(Comparisons.Type.EQUALS, Bindings.Internal.CORRELATION.bindingName(newQuantifier.getAlias().toString()), Bindings.Internal.CORRELATION)));
            transformedQuantifiers.add(newQuantifier);
        } else {
            transformedPredicates.add(predicate);
        }
    }
    transformedQuantifiers.addAll(bindings.getAll(innerQuantifierMatcher));
    call.yield(call.ref(new SelectExpression(selectExpression.getResultValues(), transformedQuantifiers.build(), transformedPredicates.build())));
}
Also used : QueryPredicate(com.apple.foundationdb.record.query.predicates.QueryPredicate) ImmutableList(com.google.common.collect.ImmutableList) LiteralValue(com.apple.foundationdb.record.query.predicates.LiteralValue) SelectExpression(com.apple.foundationdb.record.query.plan.temp.expressions.SelectExpression) ExplodeExpression(com.apple.foundationdb.record.query.plan.temp.expressions.ExplodeExpression) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) ValuePredicate(com.apple.foundationdb.record.query.predicates.ValuePredicate) PlannerBindings(com.apple.foundationdb.record.query.plan.temp.matchers.PlannerBindings) Quantifier(com.apple.foundationdb.record.query.plan.temp.Quantifier) QuantifierMatchers.forEachQuantifier(com.apple.foundationdb.record.query.plan.temp.matchers.QuantifierMatchers.forEachQuantifier)

Example 2 with ValuePredicate

use of com.apple.foundationdb.record.query.predicates.ValuePredicate 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 ValuePredicate

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

the class BooleanPredicateNormalizerTest method bigCnfThatWouldOverflow.

@Test
void bigCnfThatWouldOverflow() {
    // A CNF who's DNF size doesn't fit in an int.
    final List<QueryPredicate> conjuncts = new ArrayList<>();
    for (int i = 0; i < 32; i++) {
        final List<QueryPredicate> disjuncts = new ArrayList<>();
        for (int j = 0; j < 2; j++) {
            disjuncts.add(new ValuePredicate(F, new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, i * 100 + j)));
        }
        conjuncts.add(or(disjuncts));
    }
    final QueryPredicate cnf = and(conjuncts);
    final BooleanPredicateNormalizer normalizer = BooleanPredicateNormalizer.getDefaultInstanceForDnf();
    assertThrows(ArithmeticException.class, () -> normalizer.getNormalizedSize(cnf));
    assertThrows(BooleanPredicateNormalizer.NormalFormTooLargeException.class, () -> normalizer.normalize(cnf));
    assertEquals(cnf, normalizer.normalizeIfPossible(cnf).orElse(cnf));
}
Also used : QueryPredicate(com.apple.foundationdb.record.query.predicates.QueryPredicate) ValuePredicate(com.apple.foundationdb.record.query.predicates.ValuePredicate) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 4 with ValuePredicate

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

the class BooleanPredicateNormalizerTest method cnf.

@Test
void cnf() {
    final List<QueryPredicate> conjuncts = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        final List<QueryPredicate> disjuncts = new ArrayList<>();
        for (int j = 0; j < 5; j++) {
            disjuncts.add(new ValuePredicate(F, new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, i * 4 + j)));
        }
        conjuncts.add(or(disjuncts));
    }
    final QueryPredicate cnf = and(conjuncts);
    final BooleanPredicateNormalizer normalizer = BooleanPredicateNormalizer.getDefaultInstanceForDnf();
    assertNotEquals(cnf, normalizer.normalize(cnf).orElse(cnf));
    assertTrue(numberOfOrTerms(Objects.requireNonNull(normalizer.normalize(cnf).orElse(cnf))) <= normalizer.getNormalizedSize(cnf));
    final BooleanPredicateNormalizer lowLimitNormalizer = BooleanPredicateNormalizer.withLimit(DNF, 2);
    assertThrows(BooleanPredicateNormalizer.NormalFormTooLargeException.class, () -> lowLimitNormalizer.normalize(cnf));
    assertEquals(cnf, lowLimitNormalizer.normalizeIfPossible(cnf).orElse(cnf));
}
Also used : QueryPredicate(com.apple.foundationdb.record.query.predicates.QueryPredicate) ValuePredicate(com.apple.foundationdb.record.query.predicates.ValuePredicate) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 5 with ValuePredicate

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

Aggregations

ValuePredicate (com.apple.foundationdb.record.query.predicates.ValuePredicate)6 QueryPredicate (com.apple.foundationdb.record.query.predicates.QueryPredicate)5 Comparisons (com.apple.foundationdb.record.query.expressions.Comparisons)4 ImmutableList (com.google.common.collect.ImmutableList)4 CorrelationIdentifier (com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier)3 Quantifier (com.apple.foundationdb.record.query.plan.temp.Quantifier)3 API (com.apple.foundationdb.annotation.API)2 RelationalExpression (com.apple.foundationdb.record.query.plan.temp.RelationalExpression)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Objects (java.util.Objects)2 Collectors (java.util.stream.Collectors)2 Nonnull (javax.annotation.Nonnull)2 Test (org.junit.jupiter.api.Test)2 CORRELATION (com.apple.foundationdb.record.Bindings.Internal.CORRELATION)1 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)1 RecordMetaData (com.apple.foundationdb.record.RecordMetaData)1 Index (com.apple.foundationdb.record.metadata.Index)1 Key (com.apple.foundationdb.record.metadata.Key)1 RecordType (com.apple.foundationdb.record.metadata.RecordType)1