Package org.apache.calcite.rel.metadata
Class RelMdUtil
- java.lang.Object
-
- org.apache.calcite.rel.metadata.RelMdUtil
-
public class RelMdUtil extends java.lang.Object
RelMdUtil provides utility methods used by the metadata provider methods.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
RelMdUtil.CardOfProjExpr
Visitor that walks over a scalar expression and computes the cardinality of its result.
-
Field Summary
Fields Modifier and Type Field Description static SqlFunction
ARTIFICIAL_SELECTIVITY_FUNC
-
Constructor Summary
Constructors Modifier Constructor Description private
RelMdUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
areColumnsDefinitelyUnique(RelMetadataQuery mq, RelNode rel, java.util.List<RexInputRef> columnRefs)
static boolean
areColumnsDefinitelyUnique(RelMetadataQuery mq, RelNode rel, ImmutableBitSet colMask)
Returns true if the columns represented in a bit mask are definitely known to form a unique column set.static boolean
areColumnsDefinitelyUniqueWhenNullsFiltered(RelMetadataQuery mq, RelNode rel, java.util.List<RexInputRef> columnRefs)
static boolean
areColumnsDefinitelyUniqueWhenNullsFiltered(RelMetadataQuery mq, RelNode rel, ImmutableBitSet colMask)
Returns true if the columns represented in a bit mask are definitely known to form a unique column set, when nulls have been filtered from the columns.static java.lang.Boolean
areColumnsUnique(RelMetadataQuery mq, RelNode rel, java.util.List<RexInputRef> columnRefs)
static java.lang.Boolean
areColumnsUniqueWhenNullsFiltered(RelMetadataQuery mq, RelNode rel, java.util.List<RexInputRef> columnRefs)
static double
capInfinity(java.lang.Double d)
Caps a double value at Double.MAX_VALUE if it's currently infinitystatic java.lang.Double
cardOfProjExpr(RelMetadataQuery mq, Project rel, RexNode expr)
Computes the cardinality of a particular expression from the projection list.static boolean
checkInputForCollationAndLimit(RelMetadataQuery mq, RelNode input, RelCollation collation, RexNode offset, RexNode fetch)
Returns whether a relational expression is already sorted and has fewer rows than the sum of offset and limit.static double
computeSemiJoinSelectivity(RelMetadataQuery mq, SemiJoin rel)
Computes the selectivity of a semijoin filter if it is applied on a fact table.static double
computeSemiJoinSelectivity(RelMetadataQuery mq, RelNode factRel, RelNode dimRel, java.util.List<java.lang.Integer> factKeyList, java.util.List<java.lang.Integer> dimKeyList)
Computes the selectivity of a semijoin filter if it is applied on a fact table.static double
computeSemiJoinSelectivity(RelMetadataQuery mq, RelNode factRel, RelNode dimRel, SemiJoin rel)
Computes the selectivity of a semijoin filter if it is applied on a fact table.static double
estimateFilteredRows(RelNode child, RexNode condition, RelMetadataQuery mq)
static double
estimateFilteredRows(RelNode child, RexProgram program, RelMetadataQuery mq)
static java.lang.Double
getJoinDistinctRowCount(RelMetadataQuery mq, RelNode joinRel, JoinRelType joinType, ImmutableBitSet groupKey, RexNode predicate, boolean useMaxNdv)
Computes the number of distinct rows for a set of keys returned from a join.static java.lang.Double
getJoinPopulationSize(RelMetadataQuery mq, RelNode joinRel, ImmutableBitSet groupKey)
Computes the population size for a set of keys returned from a joinstatic java.lang.Double
getJoinRowCount(RelMetadataQuery mq, Join join, RexNode condition)
Returns an estimate of the number of rows returned by aJoin
.static double
getMinusRowCount(RelMetadataQuery mq, Minus minus)
Returns an estimate of the number of rows returned by aMinus
.static double
getSelectivityValue(RexNode artificialSelectivityFuncNode)
Returns the selectivity value stored in a call.static java.lang.Double
getSemiJoinRowCount(RelMetadataQuery mq, RelNode left, RelNode right, JoinRelType joinType, RexNode condition)
Returns an estimate of the number of rows returned by aSemiJoin
.static double
getUnionAllRowCount(RelMetadataQuery mq, Union rel)
Returns an estimate of the number of rows returned by aUnion
(before duplicates are eliminated).static double
guessSelectivity(RexNode predicate)
Returns default estimates for selectivities, in the absence of stats.static double
guessSelectivity(RexNode predicate, boolean artificialOnly)
Returns default estimates for selectivities, in the absence of stats.static double
linear(int x, int minX, int maxX, double minY, double maxY)
Returns a point on a line.static RexNode
makeSemiJoinSelectivityRexNode(RelMetadataQuery mq, SemiJoin rel)
Creates a RexNode that stores a selectivity value corresponding to the selectivity of a semijoin.static RexNode
minusPreds(RexBuilder rexBuilder, RexNode pred1, RexNode pred2)
Takes the difference between two predicates, removing from the first any predicates also in the secondstatic java.lang.Double
numDistinctVals(java.lang.Double domainSize, java.lang.Double numSelected)
Returns the number of distinct values provided numSelected are selected where there are domainSize distinct values.static void
setAggChildKeys(ImmutableBitSet groupKey, Aggregate aggRel, ImmutableBitSet.Builder childKey)
Takes a bitmap representing a set of input references and extracts the ones that reference the group by columns in an aggregate.static void
setLeftRightBitmaps(ImmutableBitSet groupKey, ImmutableBitSet.Builder leftMask, ImmutableBitSet.Builder rightMask, int nFieldsOnLeft)
Separates a bit-mask representing a join into masks representing the left and right inputs into the join.static void
splitCols(java.util.List<RexNode> projExprs, ImmutableBitSet groupKey, ImmutableBitSet.Builder baseCols, ImmutableBitSet.Builder projCols)
Forms two bitmaps by splitting the columns in a bitmap according to whether or not the column references the child input or is an expressionstatic RexNode
unionPreds(RexBuilder rexBuilder, RexNode pred1, RexNode pred2)
AND's two predicates together, either of which may be null, removing redundant filters.
-
-
-
Field Detail
-
ARTIFICIAL_SELECTIVITY_FUNC
public static final SqlFunction ARTIFICIAL_SELECTIVITY_FUNC
-
-
Method Detail
-
makeSemiJoinSelectivityRexNode
public static RexNode makeSemiJoinSelectivityRexNode(RelMetadataQuery mq, SemiJoin rel)
Creates a RexNode that stores a selectivity value corresponding to the selectivity of a semijoin. This can be added to a filter to simulate the effect of the semijoin during costing, but should never appear in a real plan since it has no physical implementation.- Parameters:
rel
- the semijoin of interest- Returns:
- constructed rexnode
-
getSelectivityValue
public static double getSelectivityValue(RexNode artificialSelectivityFuncNode)
Returns the selectivity value stored in a call.- Parameters:
artificialSelectivityFuncNode
- Call containing the selectivity value- Returns:
- selectivity value
-
computeSemiJoinSelectivity
public static double computeSemiJoinSelectivity(RelMetadataQuery mq, SemiJoin rel)
Computes the selectivity of a semijoin filter if it is applied on a fact table. The computation is based on the selectivity of the dimension table/columns and the number of distinct values in the fact table columns.- Parameters:
rel
- semijoin rel- Returns:
- calculated selectivity
-
computeSemiJoinSelectivity
public static double computeSemiJoinSelectivity(RelMetadataQuery mq, RelNode factRel, RelNode dimRel, SemiJoin rel)
Computes the selectivity of a semijoin filter if it is applied on a fact table. The computation is based on the selectivity of the dimension table/columns and the number of distinct values in the fact table columns.- Parameters:
factRel
- fact table participating in the semijoindimRel
- dimension table participating in the semijoinrel
- semijoin rel- Returns:
- calculated selectivity
-
computeSemiJoinSelectivity
public static double computeSemiJoinSelectivity(RelMetadataQuery mq, RelNode factRel, RelNode dimRel, java.util.List<java.lang.Integer> factKeyList, java.util.List<java.lang.Integer> dimKeyList)
Computes the selectivity of a semijoin filter if it is applied on a fact table. The computation is based on the selectivity of the dimension table/columns and the number of distinct values in the fact table columns.- Parameters:
factRel
- fact table participating in the semijoindimRel
- dimension table participating in the semijoinfactKeyList
- LHS keys used in the filterdimKeyList
- RHS keys used in the filter- Returns:
- calculated selectivity
-
areColumnsDefinitelyUnique
public static boolean areColumnsDefinitelyUnique(RelMetadataQuery mq, RelNode rel, ImmutableBitSet colMask)
Returns true if the columns represented in a bit mask are definitely known to form a unique column set.- Parameters:
rel
- the relational expression that the column mask corresponds tocolMask
- bit mask containing columns that will be tested for uniqueness- Returns:
- true if bit mask represents a unique column set; false if not (or if no metadata is available)
-
areColumnsUnique
public static java.lang.Boolean areColumnsUnique(RelMetadataQuery mq, RelNode rel, java.util.List<RexInputRef> columnRefs)
-
areColumnsDefinitelyUnique
public static boolean areColumnsDefinitelyUnique(RelMetadataQuery mq, RelNode rel, java.util.List<RexInputRef> columnRefs)
-
areColumnsDefinitelyUniqueWhenNullsFiltered
public static boolean areColumnsDefinitelyUniqueWhenNullsFiltered(RelMetadataQuery mq, RelNode rel, ImmutableBitSet colMask)
Returns true if the columns represented in a bit mask are definitely known to form a unique column set, when nulls have been filtered from the columns.- Parameters:
rel
- the relational expression that the column mask corresponds tocolMask
- bit mask containing columns that will be tested for uniqueness- Returns:
- true if bit mask represents a unique column set; false if not (or if no metadata is available)
-
areColumnsUniqueWhenNullsFiltered
public static java.lang.Boolean areColumnsUniqueWhenNullsFiltered(RelMetadataQuery mq, RelNode rel, java.util.List<RexInputRef> columnRefs)
-
areColumnsDefinitelyUniqueWhenNullsFiltered
public static boolean areColumnsDefinitelyUniqueWhenNullsFiltered(RelMetadataQuery mq, RelNode rel, java.util.List<RexInputRef> columnRefs)
-
setLeftRightBitmaps
public static void setLeftRightBitmaps(ImmutableBitSet groupKey, ImmutableBitSet.Builder leftMask, ImmutableBitSet.Builder rightMask, int nFieldsOnLeft)
Separates a bit-mask representing a join into masks representing the left and right inputs into the join.- Parameters:
groupKey
- original bit-maskleftMask
- left bit-mask to be setrightMask
- right bit-mask to be setnFieldsOnLeft
- number of fields in the left input
-
numDistinctVals
public static java.lang.Double numDistinctVals(java.lang.Double domainSize, java.lang.Double numSelected)
Returns the number of distinct values provided numSelected are selected where there are domainSize distinct values.Note that in the case where domainSize == numSelected, it's not true that the return value should be domainSize. If you pick 100 random values between 1 and 100, you'll most likely end up with fewer than 100 distinct values, because you'll pick some values more than once.
- Parameters:
domainSize
- number of distinct values in the domainnumSelected
- number selected from the domain- Returns:
- number of distinct values for subset selected
-
capInfinity
public static double capInfinity(java.lang.Double d)
Caps a double value at Double.MAX_VALUE if it's currently infinity- Parameters:
d
- the Double object- Returns:
- the double value if it's not infinity; else Double.MAX_VALUE
-
guessSelectivity
public static double guessSelectivity(RexNode predicate)
Returns default estimates for selectivities, in the absence of stats.- Parameters:
predicate
- predicate for which selectivity will be computed; null means true, so gives selectity of 1.0- Returns:
- estimated selectivity
-
guessSelectivity
public static double guessSelectivity(RexNode predicate, boolean artificialOnly)
Returns default estimates for selectivities, in the absence of stats.- Parameters:
predicate
- predicate for which selectivity will be computed; null means true, so gives selectity of 1.0artificialOnly
- return only the selectivity contribution from artificial nodes- Returns:
- estimated selectivity
-
unionPreds
public static RexNode unionPreds(RexBuilder rexBuilder, RexNode pred1, RexNode pred2)
AND's two predicates together, either of which may be null, removing redundant filters.- Parameters:
rexBuilder
- rexBuilder used to construct AND'd RexNodepred1
- first predicatepred2
- second predicate- Returns:
- AND'd predicate or individual predicates if one is null
-
minusPreds
public static RexNode minusPreds(RexBuilder rexBuilder, RexNode pred1, RexNode pred2)
Takes the difference between two predicates, removing from the first any predicates also in the second- Parameters:
rexBuilder
- rexBuilder used to construct AND'd RexNodepred1
- first predicatepred2
- second predicate- Returns:
- MINUS'd predicate list
-
setAggChildKeys
public static void setAggChildKeys(ImmutableBitSet groupKey, Aggregate aggRel, ImmutableBitSet.Builder childKey)
Takes a bitmap representing a set of input references and extracts the ones that reference the group by columns in an aggregate.- Parameters:
groupKey
- the original bitmapaggRel
- the aggregatechildKey
- sets bits from groupKey corresponding to group by columns
-
splitCols
public static void splitCols(java.util.List<RexNode> projExprs, ImmutableBitSet groupKey, ImmutableBitSet.Builder baseCols, ImmutableBitSet.Builder projCols)
Forms two bitmaps by splitting the columns in a bitmap according to whether or not the column references the child input or is an expression- Parameters:
projExprs
- Project expressionsgroupKey
- Bitmap whose columns will be splitbaseCols
- Bitmap representing columns from the child inputprojCols
- Bitmap representing non-child columns
-
cardOfProjExpr
public static java.lang.Double cardOfProjExpr(RelMetadataQuery mq, Project rel, RexNode expr)
Computes the cardinality of a particular expression from the projection list.- Parameters:
rel
- RelNode corresponding to the projectexpr
- projection expression- Returns:
- cardinality
-
getJoinPopulationSize
public static java.lang.Double getJoinPopulationSize(RelMetadataQuery mq, RelNode joinRel, ImmutableBitSet groupKey)
Computes the population size for a set of keys returned from a join- Parameters:
joinRel
- the join relgroupKey
- keys to compute the population for- Returns:
- computed population size
-
getJoinDistinctRowCount
public static java.lang.Double getJoinDistinctRowCount(RelMetadataQuery mq, RelNode joinRel, JoinRelType joinType, ImmutableBitSet groupKey, RexNode predicate, boolean useMaxNdv)
Computes the number of distinct rows for a set of keys returned from a join. Also known as NDV (number of distinct values).- Parameters:
joinRel
- RelNode representing the joinjoinType
- type of joingroupKey
- keys that the distinct row count will be computed forpredicate
- join predicateuseMaxNdv
- If true use formulamax(left NDV, right NDV)
, otherwise useleft NDV * right NDV
.- Returns:
- number of distinct rows
-
getUnionAllRowCount
public static double getUnionAllRowCount(RelMetadataQuery mq, Union rel)
Returns an estimate of the number of rows returned by aUnion
(before duplicates are eliminated).
-
getMinusRowCount
public static double getMinusRowCount(RelMetadataQuery mq, Minus minus)
Returns an estimate of the number of rows returned by aMinus
.
-
getJoinRowCount
public static java.lang.Double getJoinRowCount(RelMetadataQuery mq, Join join, RexNode condition)
Returns an estimate of the number of rows returned by aJoin
.
-
getSemiJoinRowCount
public static java.lang.Double getSemiJoinRowCount(RelMetadataQuery mq, RelNode left, RelNode right, JoinRelType joinType, RexNode condition)
Returns an estimate of the number of rows returned by aSemiJoin
.
-
estimateFilteredRows
public static double estimateFilteredRows(RelNode child, RexProgram program, RelMetadataQuery mq)
-
estimateFilteredRows
public static double estimateFilteredRows(RelNode child, RexNode condition, RelMetadataQuery mq)
-
linear
public static double linear(int x, int minX, int maxX, double minY, double maxY)
Returns a point on a line.The result is always a value between
minY
andmaxY
, even ifx
is not betweenminX
andmaxX
.Examples:
linear(0, 0, 10, 100, 200
} returns 100 because 0 is minXlinear(5, 0, 10, 100, 200
} returns 150 because 5 is mid-way between minX and maxXlinear(5, 0, 10, 100, 200
} returns 160linear(10, 0, 10, 100, 200
} returns 200 because 10 is maxXlinear(-2, 0, 10, 100, 200
} returns 100 because -2 is less than minX and is therefore treated as minXlinear(12, 0, 10, 100, 200
} returns 100 because 12 is greater than maxX and is therefore treated as maxX
-
checkInputForCollationAndLimit
public static boolean checkInputForCollationAndLimit(RelMetadataQuery mq, RelNode input, RelCollation collation, RexNode offset, RexNode fetch)
Returns whether a relational expression is already sorted and has fewer rows than the sum of offset and limit.If this is the case, it is safe to push down a
Sort
with limit and optional offset.
-
-