Search in sources :

Example 11 with CorrelationIdentifier

use of com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier in project fdb-record-layer by FoundationDB.

the class TopologicalSortTest method testTopologicalSortImpossibleDependencies.

@Test
public void testTopologicalSortImpossibleDependencies() {
    final CorrelationIdentifier a = CorrelationIdentifier.of("a");
    final CorrelationIdentifier b = CorrelationIdentifier.of("b");
    final CorrelationIdentifier c = CorrelationIdentifier.of("c");
    final Map<CorrelationIdentifier, Set<CorrelationIdentifier>> dependencies = ImmutableMap.of(b, ImmutableSet.of(a), c, ImmutableSet.of(b), a, ImmutableSet.of(c));
    final EnumeratingIterable<CorrelationIdentifier> topologicalPermutations = TopologicalSort.topologicalOrderPermutations(ImmutableSet.of(a, b, c), id -> dependencies.getOrDefault(id, ImmutableSet.of()));
    final EnumeratingIterator<CorrelationIdentifier> iterator = topologicalPermutations.iterator();
    assertFalse(iterator.hasNext());
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) CorrelationIdentifier(com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier) Test(org.junit.jupiter.api.Test)

Example 12 with CorrelationIdentifier

use of com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier in project fdb-record-layer by FoundationDB.

the class FindingMatcher method enumerate.

/**
 * Method to enumerate the permutations on this side against the permutation of the other side in order
 * to form matches (bijective mappings between the permutations). The match predicate is called for each pair of
 * elements (for a match attempt). If the match predicate returns {@code true} the pair is recorded
 * as a matching pair. We attempt to find a matching pair (one element from this side; one from the
 * other side) for each element identified by {@link #getAliases()}. For each individual new such pair that is found,
 * we continue in the matching attempt. Once a set of bindings is established for all aliases
 * in {@link #getAliases} this method then includes that {@link AliasMap} in the computable of {@link AliasMap}s that
 * form the result and proceeds to consume further permutations from the iterator that is passed in in order to
 * establish other matches between this and other.
 * @param iterator an enumerating iterable for the permutations on this side
 * @param otherOrdered one permutation (that is not violating dependencies, constraints, etc.) of the other side
 * @return an {@link Iterator} of match results (of type {@link AliasMap})
 */
@SuppressWarnings("java:S135")
@Nonnull
public Iterator<AliasMap> enumerate(@Nonnull final EnumeratingIterator<CorrelationIdentifier> iterator, @Nonnull final List<CorrelationIdentifier> otherOrdered) {
    final Set<CorrelationIdentifier> aliases = getAliases();
    final AliasMap boundAliasesMap = getBoundAliasesMap();
    if (otherOrdered.isEmpty()) {
        return ImmutableList.of(boundAliasesMap).iterator();
    }
    int size = otherOrdered.size();
    return new AbstractIterator<AliasMap>() {

        @Override
        protected AliasMap computeNext() {
            while (iterator.hasNext()) {
                final List<CorrelationIdentifier> ordered = iterator.next();
                final AliasMap.Builder aliasMapBuilder = AliasMap.builder(aliases.size());
                int i;
                for (i = 0; i < size; i++) {
                    final AliasMap aliasMap = aliasMapBuilder.build();
                    final CorrelationIdentifier alias = ordered.get(i);
                    final CorrelationIdentifier otherAlias = otherOrdered.get(i);
                    final Optional<AliasMap> dependsOnMapOptional = mapDependenciesToOther(aliasMap, alias, otherAlias);
                    if (!dependsOnMapOptional.isPresent()) {
                        break;
                    }
                    final AliasMap dependsOnMap = dependsOnMapOptional.get();
                    final T entity = Objects.requireNonNull(getAliasToElementMap().get(alias));
                    final T otherEntity = Objects.requireNonNull(getOtherAliasToElementMap().get(otherAlias));
                    if (!matchPredicate.test(entity, otherEntity, boundAliasesMap.combine(dependsOnMap))) {
                        break;
                    }
                    // We now amend the equivalences passed in by adding the already known bound aliases left
                    // of i and make them equivalent as well
                    aliasMapBuilder.put(alias, otherAlias);
                }
                if (i == size) {
                    iterator.skip(i - 1);
                    return boundAliasesMap.derived(ordered.size()).zip(ordered, otherOrdered, i).build();
                } else {
                    // we can skip all permutations where the i-th value is bound the way it currently is
                    iterator.skip(i);
                }
            }
            return endOfData();
        }
    };
}
Also used : CorrelationIdentifier(com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier) AliasMap(com.apple.foundationdb.record.query.plan.temp.AliasMap) AbstractIterator(com.google.common.collect.AbstractIterator) Nonnull(javax.annotation.Nonnull)

Example 13 with CorrelationIdentifier

use of com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier in project fdb-record-layer by FoundationDB.

the class PartialOrderTest method testEligibleSetsSomeDependencies2.

@Test
void testEligibleSetsSomeDependencies2() {
    final CorrelationIdentifier a = CorrelationIdentifier.of("a");
    final CorrelationIdentifier b = CorrelationIdentifier.of("b");
    final CorrelationIdentifier c = CorrelationIdentifier.of("c");
    final CorrelationIdentifier d = CorrelationIdentifier.of("d");
    final Map<CorrelationIdentifier, Set<CorrelationIdentifier>> dependencies = ImmutableMap.of(b, ImmutableSet.of(a), c, ImmutableSet.of(a), d, ImmutableSet.of(b, c));
    final var partialOrder = PartialOrder.of(ImmutableSet.of(a, b, c, d), id -> dependencies.getOrDefault(id, ImmutableSet.of()));
    var eligibleSet = partialOrder.eligibleSet();
    assertEquals(ImmutableSet.of(a), eligibleSet.eligibleElements());
    eligibleSet = eligibleSet.removeEligibleElements(eligibleSet.eligibleElements());
    assertEquals(ImmutableSet.of(b, c), eligibleSet.eligibleElements());
    eligibleSet = eligibleSet.removeEligibleElements(eligibleSet.eligibleElements());
    assertEquals(ImmutableSet.of(d), eligibleSet.eligibleElements());
    eligibleSet = eligibleSet.removeEligibleElements(eligibleSet.eligibleElements());
    assertTrue(eligibleSet.eligibleElements().isEmpty());
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) CorrelationIdentifier(com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier) Test(org.junit.jupiter.api.Test)

Example 14 with CorrelationIdentifier

use of com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier in project fdb-record-layer by FoundationDB.

the class PartialOrderTest method testEligibleSetsFullDependencies.

@Test
void testEligibleSetsFullDependencies() {
    final CorrelationIdentifier a = CorrelationIdentifier.of("a");
    final CorrelationIdentifier b = CorrelationIdentifier.of("b");
    final CorrelationIdentifier c = CorrelationIdentifier.of("c");
    final Map<CorrelationIdentifier, Set<CorrelationIdentifier>> dependencies = ImmutableMap.of(b, ImmutableSet.of(a), c, ImmutableSet.of(b));
    final var partialOrder = PartialOrder.of(ImmutableSet.of(a, b, c), id -> dependencies.getOrDefault(id, ImmutableSet.of()));
    var eligibleSet = partialOrder.eligibleSet();
    assertEquals(ImmutableSet.of(a), eligibleSet.eligibleElements());
    eligibleSet = eligibleSet.removeEligibleElements(eligibleSet.eligibleElements());
    assertEquals(ImmutableSet.of(b), eligibleSet.eligibleElements());
    eligibleSet = eligibleSet.removeEligibleElements(eligibleSet.eligibleElements());
    assertEquals(ImmutableSet.of(c), eligibleSet.eligibleElements());
    eligibleSet = eligibleSet.removeEligibleElements(eligibleSet.eligibleElements());
    assertTrue(eligibleSet.eligibleElements().isEmpty());
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) CorrelationIdentifier(com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier) Test(org.junit.jupiter.api.Test)

Example 15 with CorrelationIdentifier

use of com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier in project fdb-record-layer by FoundationDB.

the class PartialOrderTest method testEligibleSetsSomeDependencies.

@Test
void testEligibleSetsSomeDependencies() {
    final CorrelationIdentifier a = CorrelationIdentifier.of("a");
    final CorrelationIdentifier b = CorrelationIdentifier.of("b");
    final CorrelationIdentifier c = CorrelationIdentifier.of("c");
    final Map<CorrelationIdentifier, Set<CorrelationIdentifier>> dependencies = ImmutableMap.of(c, ImmutableSet.of(a));
    final var partialOrder = PartialOrder.of(ImmutableSet.of(a, b, c), id -> dependencies.getOrDefault(id, ImmutableSet.of()));
    var eligibleSet = partialOrder.eligibleSet();
    assertEquals(ImmutableSet.of(a, b), eligibleSet.eligibleElements());
    eligibleSet = eligibleSet.removeEligibleElements(eligibleSet.eligibleElements());
    assertEquals(ImmutableSet.of(c), eligibleSet.eligibleElements());
    eligibleSet = eligibleSet.removeEligibleElements(eligibleSet.eligibleElements());
    assertTrue(eligibleSet.eligibleElements().isEmpty());
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) CorrelationIdentifier(com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier) Test(org.junit.jupiter.api.Test)

Aggregations

CorrelationIdentifier (com.apple.foundationdb.record.query.plan.temp.CorrelationIdentifier)31 Set (java.util.Set)19 ImmutableSet (com.google.common.collect.ImmutableSet)18 Test (org.junit.jupiter.api.Test)17 List (java.util.List)15 ImmutableList (com.google.common.collect.ImmutableList)14 Nonnull (javax.annotation.Nonnull)11 Quantifier (com.apple.foundationdb.record.query.plan.temp.Quantifier)10 API (com.apple.foundationdb.annotation.API)8 AliasMap (com.apple.foundationdb.record.query.plan.temp.AliasMap)8 Value (com.apple.foundationdb.record.query.predicates.Value)7 GroupExpressionRef (com.apple.foundationdb.record.query.plan.temp.GroupExpressionRef)5 MatchInfo (com.apple.foundationdb.record.query.plan.temp.MatchInfo)5 PartialMatch (com.apple.foundationdb.record.query.plan.temp.PartialMatch)5 RelationalExpression (com.apple.foundationdb.record.query.plan.temp.RelationalExpression)5 QueryPredicate (com.apple.foundationdb.record.query.predicates.QueryPredicate)5 Verify (com.google.common.base.Verify)5 Optional (java.util.Optional)5 RecordQueryFetchFromPartialRecordPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryFetchFromPartialRecordPlan)4 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)4