Class Quantifiers
- java.lang.Object
-
- com.apple.foundationdb.record.query.plan.temp.Quantifiers
-
public class Quantifiers extends Object
Auxiliary class containing factory methods and helpers forQuantifier
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Quantifiers.AliasResolver
Resolver to resolve aliases to quantifiers.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Iterable<AliasMap>
enumerateConstraintAliases(AliasMap aliasMap, List<? extends Quantifier> quantifiers, Function<Quantifier,Collection<AliasMap>> constraintsFunction, Set<CorrelationIdentifier> eligibleAliases, Set<CorrelationIdentifier> otherEligibleAliases)
Method to enumerate the bound aliases due to constraints.static <E extends RelationalExpression>
List<Quantifier.Existential>existentialQuantifiers(Iterable<? extends ExpressionRef<E>> rangesOverPlans)
Create a list of existential quantifiers from a list of expression references these quantifiers should range over.static Iterable<AliasMap>
findMatches(AliasMap boundAliasesMap, Collection<? extends Quantifier> quantifiers, Collection<? extends Quantifier> otherQuantifiers, MatchPredicate<Quantifier> matchPredicate)
Method to find matches between the given set of quantifiers and the given set of other quantifiers using an alias map and amatchPredicate
.static <E extends RelationalExpression>
List<Quantifier.ForEach>forEachQuantifiers(Iterable<ExpressionRef<E>> rangesOverExpressions)
Create a list of for-each quantifiers from a list of references to range over.static <E extends RelationalExpression,Q extends Quantifier>
List<Q>fromExpressions(Iterable<? extends ExpressionRef<E>> rangesOverExpressions, Function<ExpressionRef<E>,Q> creator)
Create a list of quantifiers given a list of references these quantifiers should range over.static <E extends RecordQueryPlan>
List<Quantifier.Physical>fromPlans(Iterable<? extends ExpressionRef<E>> rangesOverPlans)
Create a list of physical quantifiers given a list of references these quantifiers should range over.static <M> GenericMatcher<BoundMatch<EnumeratingIterable<M>>>
genericMatcher(AliasMap boundAliasesMap, Collection<? extends Quantifier> quantifiers, Collection<? extends Quantifier> otherQuantifiers, MatchFunction<Quantifier,M> matchFunction)
Method to create a matcher between the given set of quantifiers and the given set of other quantifiers using an alias map and amatchFunction
.static <M> Iterable<BoundMatch<EnumeratingIterable<M>>>
match(AliasMap boundAliasesMap, Collection<? extends Quantifier> quantifiers, Collection<? extends Quantifier> otherQuantifiers, MatchFunction<Quantifier,M> matchFunction)
Method to match the given set of quantifiers and the given set of other quantifiers using an alias map and amatchFunction
.static <Q extends Quantifier>
List<Q>narrow(Class<Q> narrowedClass, List<? extends Quantifier> quantifiers)
static <Q extends Quantifier>
Set<Q>narrow(Class<Q> narrowedClass, Set<? extends Quantifier> quantifiers)
static AliasMap
toAliasMap(com.google.common.collect.BiMap<Quantifier,Quantifier> map)
Convenience helper to create an alias translation map based on a translation map using quantifiers.static com.google.common.collect.BiMap<CorrelationIdentifier,Quantifier>
toBiMap(Collection<? extends Quantifier> quantifiers)
Convenience helper to create an alias to quantifier map using a collection of quantifiers.static AliasMap
translate(Quantifier from, Quantifier to)
Convenience helper to create a single ID translation map from the alias of one quantifier to the alias of another quantifier.
-
-
-
Method Detail
-
forEachQuantifiers
@Nonnull public static <E extends RelationalExpression> List<Quantifier.ForEach> forEachQuantifiers(@Nonnull Iterable<ExpressionRef<E>> rangesOverExpressions)
Create a list of for-each quantifiers from a list of references to range over.- Type Parameters:
E
- type parameter to constrain expressions toRelationalExpression
- Parameters:
rangesOverExpressions
- iterableExpressionRef
s ofRelationalExpression
s- Returns:
- a list of for-each quantifiers where each quantifier ranges over one of the given references
-
existentialQuantifiers
@Nonnull public static <E extends RelationalExpression> List<Quantifier.Existential> existentialQuantifiers(@Nonnull Iterable<? extends ExpressionRef<E>> rangesOverPlans)
Create a list of existential quantifiers from a list of expression references these quantifiers should range over.- Type Parameters:
E
- type parameter to constrain expressions toRelationalExpression
- Parameters:
rangesOverPlans
- iterableExpressionRef
s of ofRelationalExpression
s.- Returns:
- a list of physical quantifiers where each quantifier ranges over one of the given references
-
fromPlans
@Nonnull public static <E extends RecordQueryPlan> List<Quantifier.Physical> fromPlans(@Nonnull Iterable<? extends ExpressionRef<E>> rangesOverPlans)
Create a list of physical quantifiers given a list of references these quantifiers should range over.- Type Parameters:
E
- type parameter to constrain expressions toRecordQueryPlan
- Parameters:
rangesOverPlans
- iterableExpressionRef
s ofRecordQueryPlan
- Returns:
- a list of physical quantifiers where each quantifier ranges over a reference contained in the given iterable
-
fromExpressions
@Nonnull public static <E extends RelationalExpression,Q extends Quantifier> List<Q> fromExpressions(@Nonnull Iterable<? extends ExpressionRef<E>> rangesOverExpressions, @Nonnull Function<ExpressionRef<E>,Q> creator)
Create a list of quantifiers given a list of references these quantifiers should range over.- Type Parameters:
E
- type parameter to constrain expressions toRelationalExpression
Q
- the type of the quantifier to be created- Parameters:
rangesOverExpressions
- iterable ofExpressionRef
s the quantifiers will be created to range overcreator
- lambda to to be called for each expression reference contained inrangesOverExpression
s to create the actual quantifier. This allows for callers to create different kinds of quantifier based on needs.- Returns:
- a list of quantifiers where each quantifier ranges over an reference contained in the given iterable
-
translate
@Nonnull public static AliasMap translate(@Nonnull Quantifier from, @Nonnull Quantifier to)
Convenience helper to create a single ID translation map from the alias of one quantifier to the alias of another quantifier.- Parameters:
from
- quantifierto
- quantifier- Returns:
- a new translation map mapping from
from.getAlias()
toto.getAlias()
-
toAliasMap
@Nonnull public static AliasMap toAliasMap(@Nonnull com.google.common.collect.BiMap<Quantifier,Quantifier> map)
Convenience helper to create an alias translation map based on a translation map using quantifiers. of another quantifier.- Parameters:
map
- quantifier to quantifier bi-map- Returns:
- a new
AliasMap
mapping fromfrom.getAlias()
toto.getAlias()
-
toBiMap
@Nonnull public static com.google.common.collect.BiMap<CorrelationIdentifier,Quantifier> toBiMap(@Nonnull Collection<? extends Quantifier> quantifiers)
Convenience helper to create an alias to quantifier map using a collection of quantifiers.- Parameters:
quantifiers
- collection of quantifiers- Returns:
- a new
BiMap
mapping fromq.getAlias()
toq
for everyq
inquantifiers
-
narrow
@Nonnull public static <Q extends Quantifier> List<Q> narrow(@Nonnull Class<Q> narrowedClass, @Nonnull List<? extends Quantifier> quantifiers)
-
narrow
@Nonnull public static <Q extends Quantifier> Set<Q> narrow(@Nonnull Class<Q> narrowedClass, @Nonnull Set<? extends Quantifier> quantifiers)
-
findMatches
@Nonnull public static Iterable<AliasMap> findMatches(@Nonnull AliasMap boundAliasesMap, @Nonnull Collection<? extends Quantifier> quantifiers, @Nonnull Collection<? extends Quantifier> otherQuantifiers, @Nonnull MatchPredicate<Quantifier> matchPredicate)
Method to find matches between the given set of quantifiers and the given set of other quantifiers using an alias map and amatchPredicate
. This method makes use ofpredicatedMatcher(com.apple.foundationdb.record.query.plan.temp.AliasMap, java.util.Collection<? extends com.apple.foundationdb.record.query.plan.temp.Quantifier>, java.util.Collection<? extends com.apple.foundationdb.record.query.plan.temp.Quantifier>, com.apple.foundationdb.record.query.plan.temp.matching.MatchPredicate<com.apple.foundationdb.record.query.plan.temp.Quantifier>)
to create a matcher that is then used to perform actual matching. Two quantifiers can only match if they are equal on kind and thematchPredicate
handed in is also satisfied.- Parameters:
boundAliasesMap
- aliases map of already bound quantifiersquantifiers
- collection of quantifiersotherQuantifiers
- collection of other quantifiersmatchPredicate
- that tests if two quantifiers and their graph they range over can be considered equivalent- Returns:
- an
Iterable
ofAliasMap
s containing a mapping from the quantifiers this expression owns to the quantifiers inotherQuantifiers
. Note that the mapping is bijective and can therefore be inverted
-
match
@Nonnull public static <M> Iterable<BoundMatch<EnumeratingIterable<M>>> match(@Nonnull AliasMap boundAliasesMap, @Nonnull Collection<? extends Quantifier> quantifiers, @Nonnull Collection<? extends Quantifier> otherQuantifiers, @Nonnull MatchFunction<Quantifier,M> matchFunction)
Method to match the given set of quantifiers and the given set of other quantifiers using an alias map and amatchFunction
. In contrast tofindMatches(com.apple.foundationdb.record.query.plan.temp.AliasMap, java.util.Collection<? extends com.apple.foundationdb.record.query.plan.temp.Quantifier>, java.util.Collection<? extends com.apple.foundationdb.record.query.plan.temp.Quantifier>, com.apple.foundationdb.record.query.plan.temp.matching.MatchPredicate<com.apple.foundationdb.record.query.plan.temp.Quantifier>)
this more generic version of the matching algorithm allows to compute anIterable
of results when a match is found which is then associated with the actualBoundMatch
. Two quantifiers can be considered a match if the givenMatchFunction
returns a non-emptyIterable
of some type. This method attempts to match each quantifier from the set of quantifiers this expression owns to a quantifier of the given other set of quantifiers such that the given match function is returns a non-empty iterable for each mapping. Note that there may be multiple distinct matches between the quantifier sets. This method returns anIterable
of such matches where each match is only computed when it is requested by an iterator'sIterator.hasNext()
. Thus it can be assumed that this method never pre-computes all possible matches. For expressions that cannot introduce correlations, this method's complexity is simplyO(n!)
wheren
is the number of quantifiers (which is equal for both sets). This path is taken by UNIONs and other set operations. For expressions that introduce correlations, the matching process is more complicated. First, we determine one topologically correct ordering. Then we iterate through all topologically correct orderings of the set of quantifiers this expression owns. For each element in the permutation of quantifiers that is returned by theEnumeratingIterator
we try to match the quantifiers between the sets topologically from left to right (usingmatchPredicate
). For each match we find we add a mapping between the alias of this and the other quantifier that matched into anAliasMap
that extends the alias map handed in.- Type Parameters:
M
- type that the match functionmatchFunction
produces- Parameters:
boundAliasesMap
- aliases map of already bound quantifiersquantifiers
- collection of quantifiersotherQuantifiers
- collection of other quantifiersmatchFunction
- that computes an non-emptyIterable
of match results if two quantifiers and their graph they range over can be considered matching and an emptyIterable
otherwise- Returns:
- an
Iterable
ofBoundMatch
es containing anAliasMap
mapping from the quantifiers this expression owns to the quantifiers inotherQuantifiers
together with the cross-product of all individual matches that were computed between during the matching of individual quantifiers.
-
genericMatcher
@Nonnull public static <M> GenericMatcher<BoundMatch<EnumeratingIterable<M>>> genericMatcher(@Nonnull AliasMap boundAliasesMap, @Nonnull Collection<? extends Quantifier> quantifiers, @Nonnull Collection<? extends Quantifier> otherQuantifiers, @Nonnull MatchFunction<Quantifier,M> matchFunction)
Method to create a matcher between the given set of quantifiers and the given set of other quantifiers using an alias map and amatchFunction
. In contrast tofindMatches(com.apple.foundationdb.record.query.plan.temp.AliasMap, java.util.Collection<? extends com.apple.foundationdb.record.query.plan.temp.Quantifier>, java.util.Collection<? extends com.apple.foundationdb.record.query.plan.temp.Quantifier>, com.apple.foundationdb.record.query.plan.temp.matching.MatchPredicate<com.apple.foundationdb.record.query.plan.temp.Quantifier>)
this more generic version of the matching algorithm allows to compute anIterable
of results when a match is found which is then associated with the actualBoundMatch
. Two quantifiers can be considered a match if the givenMatchFunction
returns a non-emptyIterable
of some type. This method attempts to match each quantifier from the set of quantifiers this expression owns to a quantifier of the given other set of quantifiers such that the given match function is returns a non-empty iterable for each mapping. Note that there may be multiple distinct matches between the quantifier sets. This method returns anIterable
of such matches where each match is only computed when it is requested by an iterator'sIterator.hasNext()
. Thus it can be assumed that this method never pre-computes all possible matches. For expressions that cannot introduce correlations, this method's complexity is simplyO(n!)
wheren
is the number of quantifiers (which is equal for both sets). This path is taken by UNIONs and other set operations. For expressions that introduce correlations, the matching process is more complicated. First, we determine one topologically correct ordering. Then we iterate through all topologically correct orderings of the set of quantifiers this expression owns. For each element in the permutation of quantifiers that is returned by theEnumeratingIterator
we try to match the quantifiers between the sets topologically from left to right (usingmatchPredicate
). For each match we find we add a mapping between the alias of this and the other quantifier that matched into anAliasMap
that extends the alias map handed in.- Type Parameters:
M
- type that the match functionmatchFunction
produces- Parameters:
boundAliasesMap
- aliases map of already bound quantifiersquantifiers
- collection of quantifiersotherQuantifiers
- collection of other quantifiersmatchFunction
- that computes an non-emptyIterable
of match results if two quantifiers and their graph they range over can be considered matching and an emptyIterable
otherwise- Returns:
- a new generic matcher
-
enumerateConstraintAliases
@Nonnull public static Iterable<AliasMap> enumerateConstraintAliases(@Nonnull AliasMap aliasMap, @Nonnull List<? extends Quantifier> quantifiers, @Nonnull Function<Quantifier,Collection<AliasMap>> constraintsFunction, @Nonnull Set<CorrelationIdentifier> eligibleAliases, @Nonnull Set<CorrelationIdentifier> otherEligibleAliases)
Method to enumerate the bound aliases due to constraints. This reduces the number of permutations the matching algorithm has to enumerate in a subsequent step. Consider the following example: Let's says we haveAliasMap
s for three given quantifiers:
All quantifiers have a constraintq1: Iterable.of((a11 -> aa, a2 -> ab), (a11 -> ab, a2 -> aa), (a11 -> ac, a2 -> ad)) q2: Iterable.of((a11 -> aa, a2 -> ab), (a11 -> ab, a2 -> aa)) q3: Iterable.of((a11 -> aa, a2 -> ab), (a11 -> ab, a2 -> aa), (a11 -> ac, a2 -> ad))
(a11 -> aa, a2 -> ab)
. This means that in this example, for that alias map inq1
we find exactly one compatible alias map forq2
andq3
. The same is true for(a11 -> ab, a2 -> aa)
. The map(a11 -> ac, a2 -> ad)
, however, is not one of the constraints inq2
. Therefore, that map can never produce a proper match for all three quantifiers as they cannot all use that mapping (q2
only uses incompatible mappings). Similarly if the filtered cross product of alias maps is empty we know that there cannot be any match between these quantifiers at all. If the list of quantifiers passed in is empty, this method returns a singleton iterable of the givenAliasMap
. This is equivalent to saying that the (empty) set of quantifiers are not imposing a constraint on matching. If the quantifiers that are passed in use only empty alias maps, we consider the empty alias map compatible with any other alias map.- Parameters:
aliasMap
- alias map of bound aliasesquantifiers
- list of quantifiersconstraintsFunction
- a function returning aCollection
ofAliasMap
s with potentially several alias maps. This function must be defined over th set ofquantifiers
also passed in.eligibleAliases
- a set of aliases to filter the aliases under consideration during enumeration on the source sideotherEligibleAliases
- a set of aliases to filter the aliases under consideration during enumeration on the target side- Returns:
- an
Iterable
of possibleAliasMap
s where each such map contains a set of compatible bindings over all quantifiers.
-
-