Class Mappings
- java.lang.Object
-
- org.apache.calcite.util.mapping.Mappings
-
public abstract class Mappings extends java.lang.Object
Utility functions related to mappings.- See Also:
MappingType
,Mapping
,Permutation
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Mappings.AbstractMapping
Abstract implementation ofMapping
.static interface
Mappings.CoreMapping
Core interface of all mappings.static class
Mappings.FiniteAbstractMapping
Abstract implementation of mapping where both source and target domains are finite.static interface
Mappings.FunctionMapping
Mapping where every source has a target.(package private) static class
Mappings.FunctionMappingIter
Iterator that yields the (source, target) values in aMappings.FunctionMapping
.static class
Mappings.IdentityMapping
The identity mapping, of a given size, or infinite.private static class
Mappings.InverseMapping
Decorator which converts anyMapping
into the inverse of itself.static class
Mappings.NoElementException
Thrown when a mapping is expected to return one element but returns none.static class
Mappings.OverridingSourceMapping
Source mapping that returns the same result as a parentMappings.SourceMapping
except for specific overriding elements.static class
Mappings.OverridingTargetMapping
Target mapping that returns the same result as a parentMappings.TargetMapping
except for specific overriding elements.private static class
Mappings.PartialFunctionImpl
Implementation ofMapping
where a source can have at most one target, and a target can have any number of sources.static class
Mappings.PartialMapping
A mapping where a source has at most one target, and every target has at most one source.static interface
Mappings.SourceMapping
Mapping suitable for sourcing columns.(package private) static class
Mappings.SurjectionWithInverse
A surjection with inverse has precisely one source for each target.static interface
Mappings.TargetMapping
Mapping suitable for mapping columns to a target.static class
Mappings.TooManyElementsException
Thrown when a mapping is expected to return one element but returns several.
-
Constructor Summary
Constructors Modifier Constructor Description private
Mappings()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Mappings.TargetMapping
append(Mappings.TargetMapping mapping0, Mappings.TargetMapping mapping1)
Creates a mapping by appending two mappings.static java.util.BitSet
apply(Mapping mapping, java.util.BitSet bitSet)
Applies a mapping to a BitSet.static <T> java.util.List<T>
apply(Mapping mapping, java.util.List<T> list)
Applies a mapping to a list.static ImmutableBitSet
apply(Mapping mapping, ImmutableBitSet bitSet)
Applies a mapping to anImmutableBitSet
.static int
apply(Mappings.TargetMapping mapping, int i)
Applies a mapping to an optional integer, returning an optional result.static com.google.common.collect.ImmutableList<ImmutableBitSet>
apply2(Mapping mapping, java.lang.Iterable<ImmutableBitSet> bitSets)
Applies a mapping to a collection ofImmutableBitSet
s.static java.util.List<java.lang.Integer>
apply2(Mapping mapping, java.util.List<java.lang.Integer> list)
static <T> java.util.List<T>
apply3(Mapping mapping, java.util.List<T> list)
Creates a view of a list, permuting according to a mapping.static java.util.List<java.lang.Integer>
asList(Mappings.TargetMapping mapping)
Returns a mapping as a list such thatlist.get(source)
ismapping.getTarget(source)
andlist.size()
ismapping.getSourceCount()
.static Mapping
bijection(java.util.List<java.lang.Integer> targets)
Creates a bijection.static Mapping
bijection(java.util.Map<java.lang.Integer,java.lang.Integer> targets)
Creates a bijection.static Mapping
create(MappingType mappingType, int sourceCount, int targetCount)
Creates a mapping with required properties.static Mappings.IdentityMapping
createIdentity(int fieldCount)
Creates the identity mapping.static Mappings.TargetMapping
createShiftMapping(int sourceCount, int... ints)
Creates a mapping that consists of a set of contiguous ranges.static Mapping
divide(Mapping mapping1, Mapping mapping2)
Divides one mapping by another.static java.lang.Iterable<IntPair>
invert(java.lang.Iterable<IntPair> pairs)
Inverts anIterable
overIntPair
s.static java.util.Iterator<IntPair>
invert(java.util.Iterator<IntPair> pairs)
Inverts anIterator
overIntPair
s.static Mapping
invert(Mapping mapping)
Converts a mapping to its inverse.static boolean
isIdentity(java.util.List<java.lang.Integer> list, int count)
Returns whether a list of integers is the identity mapping [0, ..., n - 1].static boolean
isIdentity(Mappings.TargetMapping mapping)
Returns whether a mapping is the identity.static Mappings.TargetMapping
merge(Mappings.TargetMapping mapping0, Mappings.TargetMapping mapping1)
Creates a mapping by merging two mappings.static Mapping
multiply(Mapping mapping1, Mapping mapping2)
Multiplies one mapping by another.static Mappings.TargetMapping
offset(Mappings.TargetMapping mapping, int offset, int sourceCount)
Returns a mapping that shifts a given mapping's source and target by a given offset.static Mappings.TargetMapping
offsetSource(Mappings.TargetMapping mapping, int offset)
Returns a mapping that shifts a given mapping's source by a given offset, incrementing the number of sources by the minimum possible.static Mappings.TargetMapping
offsetSource(Mappings.TargetMapping mapping, int offset, int sourceCount)
Returns a mapping that shifts a given mapping's source by a given offset.static Mappings.TargetMapping
offsetTarget(Mappings.TargetMapping mapping, int offset)
Returns a mapping that shifts a given mapping's target by a given offset, incrementing the number of targets by the minimum possible.static Mappings.TargetMapping
offsetTarget(Mappings.TargetMapping mapping, int offset, int targetCount)
Returns a mapping that shifts a given mapping's target by a given offset.static <T> java.util.List<T>
permute(java.util.List<T> list, Mappings.TargetMapping mapping)
Creates a view of a list, permuting according to a target mapping.static Mapping
source(java.util.List<java.lang.Integer> targets, int targetCount)
static Mapping
target(java.lang.Iterable<IntPair> pairs, int sourceCount, int targetCount)
static Mappings.TargetMapping
target(java.util.function.IntFunction<java.lang.Integer> function, int sourceCount, int targetCount)
static Mapping
target(java.util.List<java.lang.Integer> sources, int sourceCount)
static Mappings.TargetMapping
target(java.util.Map<java.lang.Integer,java.lang.Integer> map, int sourceCount, int targetCount)
Converts aMap
of integers to aMappings.TargetMapping
.
-
-
-
Method Detail
-
create
public static Mapping create(MappingType mappingType, int sourceCount, int targetCount)
Creates a mapping with required properties.
-
createIdentity
public static Mappings.IdentityMapping createIdentity(int fieldCount)
Creates the identity mapping.For example,
createIdentity(2)
returns the mapping {0:0, 1:1, 2:2}.- Parameters:
fieldCount
- Number of sources/targets- Returns:
- Identity mapping
-
divide
public static Mapping divide(Mapping mapping1, Mapping mapping2)
Divides one mapping by another.divide(A, B)
returns a mapping C such that B . C (the mapping B followed by the mapping C) is equivalent to A.- Parameters:
mapping1
- First mappingmapping2
- Second mapping- Returns:
- Mapping mapping3 such that mapping1 = mapping2 . mapping3
-
multiply
public static Mapping multiply(Mapping mapping1, Mapping mapping2)
Multiplies one mapping by another.divide(A, B)
returns a mapping C such that B . C (the mapping B followed by the mapping C) is equivalent to A.- Parameters:
mapping1
- First mappingmapping2
- Second mapping- Returns:
- Mapping mapping3 such that mapping1 = mapping2 . mapping3
-
apply
public static java.util.BitSet apply(Mapping mapping, java.util.BitSet bitSet)
Applies a mapping to a BitSet.If the mapping does not affect the bit set, returns the original. Never changes the original.
- Parameters:
mapping
- MappingbitSet
- Bit set- Returns:
- Bit set with mapping applied
-
apply
public static ImmutableBitSet apply(Mapping mapping, ImmutableBitSet bitSet)
Applies a mapping to anImmutableBitSet
.If the mapping does not affect the bit set, returns the original. Never changes the original.
- Parameters:
mapping
- MappingbitSet
- Bit set- Returns:
- Bit set with mapping applied
-
apply2
public static com.google.common.collect.ImmutableList<ImmutableBitSet> apply2(Mapping mapping, java.lang.Iterable<ImmutableBitSet> bitSets)
Applies a mapping to a collection ofImmutableBitSet
s.- Parameters:
mapping
- MappingbitSets
- Collection of bit sets- Returns:
- Bit sets with mapping applied
-
apply
public static <T> java.util.List<T> apply(Mapping mapping, java.util.List<T> list)
Applies a mapping to a list.- Type Parameters:
T
- Element type- Parameters:
mapping
- Mappinglist
- List- Returns:
- List with elements permuted according to mapping
-
apply2
public static java.util.List<java.lang.Integer> apply2(Mapping mapping, java.util.List<java.lang.Integer> list)
-
apply3
public static <T> java.util.List<T> apply3(Mapping mapping, java.util.List<T> list)
Creates a view of a list, permuting according to a mapping.- Type Parameters:
T
- Element type- Parameters:
mapping
- Mappinglist
- List- Returns:
- Permuted view of list
-
permute
public static <T> java.util.List<T> permute(java.util.List<T> list, Mappings.TargetMapping mapping)
Creates a view of a list, permuting according to a target mapping.- Type Parameters:
T
- Element type- Parameters:
mapping
- Mappinglist
- List- Returns:
- Permuted view of list
-
asList
public static java.util.List<java.lang.Integer> asList(Mappings.TargetMapping mapping)
Returns a mapping as a list such thatlist.get(source)
ismapping.getTarget(source)
andlist.size()
ismapping.getSourceCount()
.Converse of
target(List, int)
-
target
public static Mappings.TargetMapping target(java.util.Map<java.lang.Integer,java.lang.Integer> map, int sourceCount, int targetCount)
Converts aMap
of integers to aMappings.TargetMapping
.
-
target
public static Mappings.TargetMapping target(java.util.function.IntFunction<java.lang.Integer> function, int sourceCount, int targetCount)
-
target
public static Mapping target(java.lang.Iterable<IntPair> pairs, int sourceCount, int targetCount)
-
source
public static Mapping source(java.util.List<java.lang.Integer> targets, int targetCount)
-
target
public static Mapping target(java.util.List<java.lang.Integer> sources, int sourceCount)
-
bijection
public static Mapping bijection(java.util.List<java.lang.Integer> targets)
Creates a bijection.Throws if sources and targets are not one to one.
-
bijection
public static Mapping bijection(java.util.Map<java.lang.Integer,java.lang.Integer> targets)
Creates a bijection.Throws if sources and targets are not one to one.
-
isIdentity
public static boolean isIdentity(Mappings.TargetMapping mapping)
Returns whether a mapping is the identity.
-
createShiftMapping
public static Mappings.TargetMapping createShiftMapping(int sourceCount, int... ints)
Creates a mapping that consists of a set of contiguous ranges.For example,
createShiftMapping(60, 100, 0, 3, 200, 50, 5);
creates
Example mapping Source Target 0 100 1 101 2 102 3 -1 ... -1 50 200 51 201 52 202 53 203 54 204 55 -1 ... -1 59 -1 - Parameters:
sourceCount
- Maximum value ofsource
ints
- Collection of ranges, each(target, source, count)
- Returns:
- Mapping that maps from source ranges to target ranges
-
append
public static Mappings.TargetMapping append(Mappings.TargetMapping mapping0, Mappings.TargetMapping mapping1)
Creates a mapping by appending two mappings.Sources and targets of the second mapping are shifted to the right.
For example,
append({0:0, 1:1}, {0:0, 1:1, 2:2})
yields{0:0, 1:1, 2:2, 3:3, 4:4}
.
-
merge
public static Mappings.TargetMapping merge(Mappings.TargetMapping mapping0, Mappings.TargetMapping mapping1)
Creates a mapping by merging two mappings. There must be no clashes.Unlike
append(org.apache.calcite.util.mapping.Mappings.TargetMapping, org.apache.calcite.util.mapping.Mappings.TargetMapping)
, sources and targets are not shifted.For example,
merge({0:0, 1:1}, {2:2, 3:3, 4:4})
yields{0:0, 1:1, 2:2, 3:3, 4:4}
.merge({0:0, 1:1}, {1:2, 2:3})
throws, because there are two entries with source=1.
-
offsetSource
public static Mappings.TargetMapping offsetSource(Mappings.TargetMapping mapping, int offset)
Returns a mapping that shifts a given mapping's source by a given offset, incrementing the number of sources by the minimum possible.- Parameters:
mapping
- Input mappingoffset
- Offset to be applied to each source- Returns:
- Shifted mapping
-
offsetSource
public static Mappings.TargetMapping offsetSource(Mappings.TargetMapping mapping, int offset, int sourceCount)
Returns a mapping that shifts a given mapping's source by a given offset.For example, given
mapping
with sourceCount=2, targetCount=8, and (source, target) entries {[0: 5], [1: 7]}, offsetSource(mapping, 3) returns a mapping with sourceCount=5, targetCount=8, and (source, target) entries {[3: 5], [4: 7]}.- Parameters:
mapping
- Input mappingoffset
- Offset to be applied to each sourcesourceCount
- New source count; must be at leastmapping
's source count plusoffset
- Returns:
- Shifted mapping
-
offsetTarget
public static Mappings.TargetMapping offsetTarget(Mappings.TargetMapping mapping, int offset)
Returns a mapping that shifts a given mapping's target by a given offset, incrementing the number of targets by the minimum possible.- Parameters:
mapping
- Input mappingoffset
- Offset to be applied to each target- Returns:
- Shifted mapping
-
offsetTarget
public static Mappings.TargetMapping offsetTarget(Mappings.TargetMapping mapping, int offset, int targetCount)
Returns a mapping that shifts a given mapping's target by a given offset.For example, given
mapping
with sourceCount=2, targetCount=8, and (source, target) entries {[0: 5], [1: 7]}, offsetTarget(mapping, 3) returns a mapping with sourceCount=2, targetCount=11, and (source, target) entries {[0: 8], [1: 10]}.- Parameters:
mapping
- Input mappingoffset
- Offset to be applied to each targettargetCount
- New target count; must be at leastmapping
's target count plusoffset
- Returns:
- Shifted mapping
-
offset
public static Mappings.TargetMapping offset(Mappings.TargetMapping mapping, int offset, int sourceCount)
Returns a mapping that shifts a given mapping's source and target by a given offset.For example, given
mapping
with sourceCount=2, targetCount=8, and (source, target) entries {[0: 5], [1: 7]}, offsetSource(mapping, 3) returns a mapping with sourceCount=5, targetCount=8, and (source, target) entries {[3: 8], [4: 10]}.- Parameters:
mapping
- Input mappingoffset
- Offset to be applied to each sourcesourceCount
- New source count; must be at leastmapping
's source count plusoffset
- Returns:
- Shifted mapping
-
isIdentity
public static boolean isIdentity(java.util.List<java.lang.Integer> list, int count)
Returns whether a list of integers is the identity mapping [0, ..., n - 1].
-
invert
public static java.lang.Iterable<IntPair> invert(java.lang.Iterable<IntPair> pairs)
Inverts anIterable
overIntPair
s.
-
invert
public static java.util.Iterator<IntPair> invert(java.util.Iterator<IntPair> pairs)
Inverts anIterator
overIntPair
s.
-
apply
public static int apply(Mappings.TargetMapping mapping, int i)
Applies a mapping to an optional integer, returning an optional result.
-
-