Class AliasMap
- java.lang.Object
-
- com.apple.foundationdb.record.query.plan.temp.AliasMap
-
public class AliasMap extends Object
This class encapsulates mappings betweenCorrelationIdentifier
s and helpers to create and maintain these mappings. While various operations or in general algorithms traverse the query graph(s) it is important to keep track of what alias (of a quantifier) should be considered to what other alias (of another quantifier). A quantifier (Quantifier
is referred to viaCorrelationIdentifier
. In this way there are no direct pointers to objects which is desirable while the graph is mutated through transformations. At various points during the planning of a query we may want to determine e.g. if a sub-graph would produce the same result as some other sub-graph. Similarly, we may want to determine whether a sub-graph is already properly memoized or not. In general, we need to determine if and how two sub-graphs are related to each other (usually equality but also subsumption, etc.) All of that is rather straightforward if both sub-graphs are completely uncorrelated and therefore independent of each other, or if they are correlated to a mutually external parameter. In these cases theAliasMap
implemented here would effectively be trivial, i.e. the map would be conceptually mapping identifiers to themselves. There are cases, however, that need some care to establish more complex relationships such as those mentioned above. Example 1: sub graph 1SELECT T.* FROM T WHERE T.x = c1
SELECT T.* FROM T WHERE T.x = c2
c1
andc2
.SELECT * FROM R AS c1, (SELECT T.* FROM T WHERE T.x = c1)
SELECT * FROM R AS c2, (SELECT T.* FROM T WHERE T.x = c2)
SELECT * FROM R AS c1, (SELECT T.* FROM T WHERE T.x = c1)
SELECT * FROM S AS c2, (SELECT T.* FROM T WHERE T.x = c2)
SELECT T.* FROM T WHERE T.x = c1 AND T.y = c2
SELECT T.* FROM T WHERE T.x = c2 AND T.y = c1
SELECT * FROM R AS c1, S AS c2, (SELECT T.* FROM T WHERE T.x = c1 AND T.y = c2)
SELECT * FROM R AS c2, S AS c1, (SELECT T.* FROM T WHERE T.x = c2 AND T.y = c1)
SELECT * FROM R AS c1, S AS c2, (SELECT T.* FROM T WHERE T.x = c2 AND T.y = c1)
Correlated.semanticEquals(java.lang.Object, com.apple.foundationdb.record.query.plan.temp.AliasMap)
we need to have all outside references bound and mapped in some way: should we treatc1
on the left asc2
on the right or the other way around? It is the job of an object of this class to facilitate the construction and the lookup of these mappings. Algorithms walking the graph make use of this map, construct and reconstruct assumed equality while traversing variation (such as permutations) of possible mappings. This class is immutable, all perceived "mutations" cause a new object to be created.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AliasMap.Builder
Builder class for theAliasMap
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static AliasMap.Builder
builder()
Create a new empty builder.static AliasMap.Builder
builder(int expectedSize)
Create a new empty builder using an expected size that is passed in.AliasMap
combine(AliasMap other)
Combine two compatibleAliasMap
s.Optional<AliasMap>
combineMaybe(AliasMap other)
Combine twoAliasMap
s if possibleAliasMap
compose(AliasMap other)
Compute the composition of twoAliasMap
s.boolean
containsMapping(CorrelationIdentifier source, CorrelationIdentifier target)
boolean
containsSource(CorrelationIdentifier alias)
boolean
containsTarget(CorrelationIdentifier alias)
static AliasMap
copyOf(com.google.common.collect.BiMap<CorrelationIdentifier,CorrelationIdentifier> map)
Factory method to create an alias map that is a copy of anBiMap
passed in.AliasMap.Builder
derived()
Create a builder for a new alias map using the bindings of this map.AliasMap.Builder
derived(int expectedAdditionalElements)
Create a builder for a new alias map using the bindings of this map.static AliasMap
emptyMap()
Factory method to create an empty alias map.Set<Map.Entry<CorrelationIdentifier,CorrelationIdentifier>>
entrySet()
Returns the set of entries in this map.boolean
equals(Object o)
Define equality based on the equality of the backing bimap.AliasMap
filterMappings(BiPredicate<CorrelationIdentifier,CorrelationIdentifier> predicate)
Filter the bindings of this alias map using some bi-predicate that is handed in.Iterable<AliasMap>
findMatches(Set<CorrelationIdentifier> aliases, Function<CorrelationIdentifier,Set<CorrelationIdentifier>> dependsOnFn, Set<CorrelationIdentifier> otherAliases, Function<CorrelationIdentifier,Set<CorrelationIdentifier>> otherDependsOnFn, MatchPredicate<CorrelationIdentifier> matchPredicate)
Find matches between two sets of aliases, given their depends-on sets and aMatchPredicate
.void
forEachMapping(BiConsumer<CorrelationIdentifier,CorrelationIdentifier> action)
Call an action for each mapping contained in this alias map.CorrelationIdentifier
getSource(CorrelationIdentifier target)
Get the source for a target passed in.CorrelationIdentifier
getSourceOrDefault(CorrelationIdentifier target, CorrelationIdentifier defaultValue)
Get the source for a target passed in or a default in case there is no mapping for the target alias.CorrelationIdentifier
getSourceOrThrow(CorrelationIdentifier target)
Get the source for a target passed in or throw an exception in case there is no mapping for the target alias.CorrelationIdentifier
getTarget(CorrelationIdentifier source)
Get the target for a source passed in.CorrelationIdentifier
getTargetOrDefault(CorrelationIdentifier source, CorrelationIdentifier defaultValue)
Get the target for a source passed in or a default in case there is no mapping for the source alias.CorrelationIdentifier
getTargetOrThrow(CorrelationIdentifier source)
Get the target for a source passed in or throw an exception in case there is no mapping for the source alias.int
hashCode()
Define equality based on the equality of the backing bimap.static AliasMap
identitiesFor(Set<CorrelationIdentifier> aliases)
Factory method to create an alias map that contains entriesa -> a, b -> b, ...
for each alias contained in the set ofCorrelationIdentifier
s passed in.boolean
isCompatible(AliasMap other)
Determine if twoAliasMap
s are compatible.PredicatedMatcher
matcher(Set<CorrelationIdentifier> aliases, Function<CorrelationIdentifier,Set<CorrelationIdentifier>> dependsOnFn, Set<CorrelationIdentifier> otherAliases, Function<CorrelationIdentifier,Set<CorrelationIdentifier>> otherDependsOnFn, MatchPredicate<CorrelationIdentifier> matchPredicate)
static AliasMap
of(CorrelationIdentifier source, CorrelationIdentifier target)
Factory method to create an alias map based on a given binding.int
size()
Returns the size of the alias map, i.e., the number of contained bindings.Set<CorrelationIdentifier>
sources()
Returns the set ofCorrelationIdentifier
s that are mapped by thisAliasMap
.Set<CorrelationIdentifier>
targets()
Returns the set of allCorrelationIdentifier
s that this map maps to using the set ofCorrelationIdentifier
s returned bysources()
.String
toString()
static AliasMap
zip(List<CorrelationIdentifier> left, List<CorrelationIdentifier> right)
Factory method to create an alias map based on azip
of two parallel lists of aliases.
-
-
-
Method Detail
-
equals
public boolean equals(Object o)
Define equality based on the equality of the backing bimap.
-
hashCode
public int hashCode()
Define equality based on the equality of the backing bimap.
-
containsSource
public boolean containsSource(@Nonnull CorrelationIdentifier alias)
-
containsTarget
public boolean containsTarget(@Nonnull CorrelationIdentifier alias)
-
containsMapping
public boolean containsMapping(@Nonnull CorrelationIdentifier source, @Nonnull CorrelationIdentifier target)
-
sources
@Nonnull public Set<CorrelationIdentifier> sources()
Returns the set ofCorrelationIdentifier
s that are mapped by thisAliasMap
.- Returns:
- a set of
CorrelationIdentifier
s that this map contains mappings for
-
targets
@Nonnull public Set<CorrelationIdentifier> targets()
Returns the set of allCorrelationIdentifier
s that this map maps to using the set ofCorrelationIdentifier
s returned bysources()
.- Returns:
- a set of
CorrelationIdentifier
s that this map maps to.
-
size
public int size()
Returns the size of the alias map, i.e., the number of contained bindings.- Returns:
- the size of the alias map.
-
derived
@Nonnull public AliasMap.Builder derived()
Create a builder for a new alias map using the bindings of this map.- Returns:
- a new builder derived from the contents of this map.
-
derived
@Nonnull public AliasMap.Builder derived(int expectedAdditionalElements)
Create a builder for a new alias map using the bindings of this map.- Parameters:
expectedAdditionalElements
- the number of additional elements that the caller expects to add before build is called.- Returns:
- a new builder derived from the contents of this map.
-
entrySet
@Nonnull public Set<Map.Entry<CorrelationIdentifier,CorrelationIdentifier>> entrySet()
Returns the set of entries in this map.- Returns:
- the set of entries
-
getSource
@Nullable public CorrelationIdentifier getSource(CorrelationIdentifier target)
Get the source for a target passed in.- Parameters:
target
- the target to return the source for- Returns:
- the source that target is bound to in this alias map or
null
if there is no binding totarget
in this alias map
-
getSourceOrDefault
@Nonnull public CorrelationIdentifier getSourceOrDefault(@Nonnull CorrelationIdentifier target, @Nonnull CorrelationIdentifier defaultValue)
Get the source for a target passed in or a default in case there is no mapping for the target alias.- Parameters:
target
- the target to return the source fordefaultValue
- default value to return is there is no mapping in this map fortarget
- Returns:
- the source that target is bound to in this alias map or
null
if there is no binding totarget
in this alias map
-
getSourceOrThrow
@Nonnull public CorrelationIdentifier getSourceOrThrow(@Nonnull CorrelationIdentifier target)
Get the source for a target passed in or throw an exception in case there is no mapping for the target alias.- Parameters:
target
- the target to return the source for- Returns:
- the source that target is bound to in this alias map or
null
if there is no binding totarget
in this alias map
-
getTarget
@Nullable public CorrelationIdentifier getTarget(CorrelationIdentifier source)
Get the target for a source passed in.- Parameters:
source
- the source to return the target for- Returns:
- the target that source is bound to in this alias map or
null
if there is no binding fromsource
in this alias map
-
getTargetOrDefault
@Nonnull public CorrelationIdentifier getTargetOrDefault(@Nonnull CorrelationIdentifier source, @Nonnull CorrelationIdentifier defaultValue)
Get the target for a source passed in or a default in case there is no mapping for the source alias.- Parameters:
source
- the source to return the target fordefaultValue
- default value to return is there is no mapping in this map forsource
- Returns:
- the target that source is bound to in this alias map or
null
if there is no binding fromsource
in this alias map
-
getTargetOrThrow
@Nonnull public CorrelationIdentifier getTargetOrThrow(@Nonnull CorrelationIdentifier source)
Get the target for a source passed in or throw an exception in case there is no mapping for the source alias.- Parameters:
source
- the source to return the target for- Returns:
- the target that source is bound to in this alias map or
null
if there is no binding fromsource
in this alias map
-
forEachMapping
public void forEachMapping(@Nonnull BiConsumer<CorrelationIdentifier,CorrelationIdentifier> action)
Call an action for each mapping contained in this alias map.- Parameters:
action
- a bi-consumer that is called for each (source, target) pair
-
filterMappings
@Nonnull public AliasMap filterMappings(@Nonnull BiPredicate<CorrelationIdentifier,CorrelationIdentifier> predicate)
Filter the bindings of this alias map using some bi-predicate that is handed in. This filter method works by immediately filtering the contents of this map and then creating a new map which is quite different to what e.g. operations onStream
would do where the result is computed when the collect happens.- Parameters:
predicate
- a bi-predicate that is called for each (source, target) pair- Returns:
- a new alias map that only retains bindings that where accepted by
predicate
, i.e., for which the predicate returnedtrue
.
-
compose
@Nonnull public AliasMap compose(@Nonnull AliasMap other)
Compute the composition of twoAliasMap
s.- Parameters:
other
- second alias map- Returns:
- a alias map that maps
a -> c
for alla, b, c
ifa -> b
is contained inthis
andb -> c
is contained inother
,a -> b
fora, b
ifa -> b
inthis
and nob -> x
for anyx
is contained inother
, andb -> c
forb, c
ifa -> x
for anyx
is not contained inthis
andb -> c
is contained inother
-
isCompatible
public boolean isCompatible(@Nonnull AliasMap other)
Determine if twoAliasMap
s are compatible.- Parameters:
other
- second alias map- Returns:
true
if thisAliasMap
is compatible toother
, that is there are no conflicting mappings in a sense that a union of the mappings ofthis
andother
can form aBiMap
,false
otherwise.
-
combine
@Nonnull public AliasMap combine(@Nonnull AliasMap other)
Combine two compatibleAliasMap
s.- Parameters:
other
- second alias map- Returns:
- a combined translation map (see
combineMaybe(com.apple.foundationdb.record.query.plan.temp.AliasMap)
)
-
combineMaybe
@Nonnull public Optional<AliasMap> combineMaybe(@Nonnull AliasMap other)
Combine twoAliasMap
s if possible- Parameters:
other
- second alias map- Returns:
Optional
containing a translation map that mapsa -> b
for alla, b
ifa -> b
is contained inthis
and there is no c withb != c
such thata -> c
is contained inother
. EmptyOptional
, otherwise.
-
builder
@Nonnull public static AliasMap.Builder builder()
Create a new empty builder.- Returns:
- a builder for a new
AliasMap
-
builder
@Nonnull public static AliasMap.Builder builder(int expectedSize)
Create a new empty builder using an expected size that is passed in.
-
emptyMap
@Nonnull public static AliasMap emptyMap()
Factory method to create an empty alias map.- Returns:
- a new empty
AliasMap
-
of
@Nonnull public static AliasMap of(@Nonnull CorrelationIdentifier source, @Nonnull CorrelationIdentifier target)
Factory method to create an alias map based on a given binding.- Parameters:
source
- an alias of a sourcetarget
- an alias of a target- Returns:
- a new
AliasMap
containing exactly the bindingsource -> target
-
copyOf
@Nonnull public static AliasMap copyOf(@Nonnull com.google.common.collect.BiMap<CorrelationIdentifier,CorrelationIdentifier> map)
Factory method to create an alias map that is a copy of anBiMap
passed in. Note that no actual copy takes place if the bi-map that is passed in is of typeImmutableBiMap
.- Parameters:
map
- a bi0map containing bindings- Returns:
- a new
AliasMap
-
identitiesFor
@Nonnull public static AliasMap identitiesFor(Set<CorrelationIdentifier> aliases)
Factory method to create an alias map that contains entriesa -> a, b -> b, ...
for each alias contained in the set ofCorrelationIdentifier
s passed in.- Parameters:
aliases
- set of aliases that this method should create identity-bindings for- Returns:
- a new
AliasMap
-
zip
@Nonnull public static AliasMap zip(@Nonnull List<CorrelationIdentifier> left, @Nonnull List<CorrelationIdentifier> right)
Factory method to create an alias map based on azip
of two parallel lists of aliases.- Parameters:
left
- one listright
- other list- Returns:
- a new
AliasMap
-
findMatches
public Iterable<AliasMap> findMatches(@Nonnull Set<CorrelationIdentifier> aliases, @Nonnull Function<CorrelationIdentifier,Set<CorrelationIdentifier>> dependsOnFn, @Nonnull Set<CorrelationIdentifier> otherAliases, @Nonnull Function<CorrelationIdentifier,Set<CorrelationIdentifier>> otherDependsOnFn, @Nonnull MatchPredicate<CorrelationIdentifier> matchPredicate)
Find matches between two sets of aliases, given their depends-on sets and aMatchPredicate
. This method creates an underlyingPredicatedMatcher
to do the work.- Parameters:
aliases
- a set of aliasesdependsOnFn
- a function that returns the set of dependencies for a given alias withinaliases
otherAliases
- a set of other aliasesotherDependsOnFn
- a function that returns the set of dependencies for a given alias withinotherAliases
matchPredicate
- match predicate. seeMatchPredicate
for more info- Returns:
- an iterable of
AliasMap
s where each individualAliasMap
is considered one match
-
matcher
public PredicatedMatcher matcher(@Nonnull Set<CorrelationIdentifier> aliases, @Nonnull Function<CorrelationIdentifier,Set<CorrelationIdentifier>> dependsOnFn, @Nonnull Set<CorrelationIdentifier> otherAliases, @Nonnull Function<CorrelationIdentifier,Set<CorrelationIdentifier>> otherDependsOnFn, @Nonnull MatchPredicate<CorrelationIdentifier> matchPredicate)
-
-