org.eigenbase.rel.metadata
Class RelMdUtil

java.lang.Object
  extended by org.eigenbase.rel.metadata.RelMdUtil

public class RelMdUtil
extends Object

RelMdUtil provides utility methods used by the metadata provider methods.

Version:
$Id: //open/dev/farrago/src/org/eigenbase/rel/metadata/RelMdUtil.java#28 $
Author:
Zelaine Fong

Nested Class Summary
private static class RelMdUtil.CardOfProjExpr
           
 
Field Summary
static SqlFunction artificialSelectivityFunc
           
 
Constructor Summary
RelMdUtil()
           
 
Method Summary
static boolean areColumnsDefinitelyUnique(RelNode rel, BitSet colMask)
          Returns true if the columns represented in a bit mask are definitely known to form a unique column set.
static boolean areColumnsDefinitelyUnique(RelNode rel, List<RexInputRef> columnRefs)
           
static boolean areColumnsDefinitelyUniqueWhenNullsFiltered(RelNode rel, BitSet 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 boolean areColumnsDefinitelyUniqueWhenNullsFiltered(RelNode rel, List<RexInputRef> columnRefs)
           
static Boolean areColumnsUnique(RelNode rel, List<RexInputRef> columnRefs)
           
static Boolean areColumnsUniqueWhenNullsFiltered(RelNode rel, List<RexInputRef> columnRefs)
           
static double capInfinity(Double d)
          Caps a double value at Double.MAX_VALUE if it's currently infinity
static Double cardOfProjExpr(ProjectRelBase rel, RexNode expr)
          Computes the cardinality of a particular expression from the projection list
static double computeSemiJoinSelectivity(RelNode factRel, RelNode dimRel, List<Integer> factKeyList, List<Integer> dimKeyList)
          Computes the selectivity of a semijoin filter if it is applied on a fact table.
static double computeSemiJoinSelectivity(RelNode factRel, RelNode dimRel, SemiJoinRel rel)
          Computes the selectivity of a semijoin filter if it is applied on a fact table.
static double computeSemiJoinSelectivity(SemiJoinRel rel)
          Computes the selectivity of a semijoin filter if it is applied on a fact table.
static RexNode findEquiJoinCols(RelNode leftChild, RelNode rightChild, RexNode predicate, BitSet leftJoinCols, BitSet rightJoinCols)
          Locates the columns corresponding to equijoins within a joinrel.
static Double getJoinDistinctRowCount(RelNode joinRel, JoinRelType joinType, BitSet groupKey, RexNode predicate)
          Computes the number of distinct rows for a set of keys returned from a join
static Double getJoinPopulationSize(RelNode joinRel, BitSet groupKey)
          Computes the population size for a set of keys returned from a join
static double getSelectivityValue(RexNode artificialSelecFuncNode)
          Returns the selectivity value stored in the rexnode
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 RexNode makeSemiJoinSelectivityRexNode(SemiJoinRel 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 second
static Double numDistinctVals(Double domainSize, Double numSelected)
          Returns the number of distinct values provided numSelected are selected where there are domainSize distinct values.
static void setAggChildKeys(BitSet groupKey, AggregateRelBase aggRel, BitSet childKey)
          Takes a bitmap representing a set of input references and extracts the ones that reference the group by columns in an aggregate
static BitSet setBitKeys(List<Integer> keys)
          Sets a bitmap corresponding to a list of keys.
static void setLeftRightBitmaps(BitSet groupKey, BitSet leftMask, BitSet rightMask, int nFieldsOnLeft)
          Separates a bitmask representing a join into masks representing the left and right inputs into the join
static void splitCols(RexNode[] projExprs, BitSet groupKey, BitSet baseCols, BitSet 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
static RexNode unionPreds(RexBuilder rexBuilder, RexNode pred1, RexNode pred2)
          AND's two predicates together, either of which may be null, removing redundant filters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

artificialSelectivityFunc

public static final SqlFunction artificialSelectivityFunc
Constructor Detail

RelMdUtil

public RelMdUtil()
Method Detail

makeSemiJoinSelectivityRexNode

public static RexNode makeSemiJoinSelectivityRexNode(SemiJoinRel 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 artificialSelecFuncNode)
Returns the selectivity value stored in the rexnode

Parameters:
artificialSelecFuncNode - rexnode containing the selectivity value
Returns:
selectivity value

computeSemiJoinSelectivity

public static double computeSemiJoinSelectivity(SemiJoinRel 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(RelNode factRel,
                                                RelNode dimRel,
                                                SemiJoinRel 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 semijoin
dimRel - dimension table participating in the semijoin
rel - semijoin rel
Returns:
calculated selectivity

computeSemiJoinSelectivity

public static double computeSemiJoinSelectivity(RelNode factRel,
                                                RelNode dimRel,
                                                List<Integer> factKeyList,
                                                List<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 semijoin
dimRel - dimension table participating in the semijoin
factKeyList - LHS keys used in the filter
dimKeyList - RHS keys used in the filter
Returns:
calculated selectivity

areColumnsDefinitelyUnique

public static boolean areColumnsDefinitelyUnique(RelNode rel,
                                                 BitSet colMask)
Returns true if the columns represented in a bit mask are definitely known to form a unique column set.

Parameters:
rel - the relnode that the column mask correponds to
colMask - 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 Boolean areColumnsUnique(RelNode rel,
                                       List<RexInputRef> columnRefs)

areColumnsDefinitelyUnique

public static boolean areColumnsDefinitelyUnique(RelNode rel,
                                                 List<RexInputRef> columnRefs)

areColumnsDefinitelyUniqueWhenNullsFiltered

public static boolean areColumnsDefinitelyUniqueWhenNullsFiltered(RelNode rel,
                                                                  BitSet 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 relnode that the column mask correponds to
colMask - 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 Boolean areColumnsUniqueWhenNullsFiltered(RelNode rel,
                                                        List<RexInputRef> columnRefs)

areColumnsDefinitelyUniqueWhenNullsFiltered

public static boolean areColumnsDefinitelyUniqueWhenNullsFiltered(RelNode rel,
                                                                  List<RexInputRef> columnRefs)

setBitKeys

public static BitSet setBitKeys(List<Integer> keys)
Sets a bitmap corresponding to a list of keys.

Parameters:
keys - list of keys
Returns:
the bitmap

setLeftRightBitmaps

public static void setLeftRightBitmaps(BitSet groupKey,
                                       BitSet leftMask,
                                       BitSet rightMask,
                                       int nFieldsOnLeft)
Separates a bitmask representing a join into masks representing the left and right inputs into the join

Parameters:
groupKey - original bitmask
leftMask - left bitmask to be set
rightMask - right bitmask to be set
nFieldsOnLeft - number of fields in the left input

numDistinctVals

public static Double numDistinctVals(Double domainSize,
                                     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 domain
numSelected - number selected from the domain
Returns:
number of distinct values for subset selected

capInfinity

public static double capInfinity(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.0
artificialOnly - return only the selectivity contribution from artificial nodes
Returns:
estimated selectivity

findEquiJoinCols

public static RexNode findEquiJoinCols(RelNode leftChild,
                                       RelNode rightChild,
                                       RexNode predicate,
                                       BitSet leftJoinCols,
                                       BitSet rightJoinCols)
Locates the columns corresponding to equijoins within a joinrel.

Parameters:
leftChild - left input into the join
rightChild - right input into the join
predicate - join predicate
leftJoinCols - bitmap that will be set with the columns on the LHS of the join that participate in equijoins
rightJoinCols - bitmap that will be set with the columns on the RHS of the join that participate in equijoins
Returns:
remaining join filters that are not equijoins; may return a RexLiteral true, but never null

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 RexNode
pred1 - first predicate
pred2 - 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 RexNode
pred1 - first predicate
pred2 - second predicate
Returns:
MINUS'd predicate list

setAggChildKeys

public static void setAggChildKeys(BitSet groupKey,
                                   AggregateRelBase aggRel,
                                   BitSet 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 bitmap
aggRel - the aggregate
childKey - sets bits from groupKey corresponding to group by columns

splitCols

public static void splitCols(RexNode[] projExprs,
                             BitSet groupKey,
                             BitSet baseCols,
                             BitSet 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 -
groupKey - bitmap whose columns will be split
baseCols - bitmap representing columns from the child input
projCols - bitmap representing non-child columns

cardOfProjExpr

public static Double cardOfProjExpr(ProjectRelBase rel,
                                    RexNode expr)
Computes the cardinality of a particular expression from the projection list

Parameters:
rel - RelNode corresponding to the project
expr - projection expression
Returns:
cardinality

getJoinPopulationSize

public static Double getJoinPopulationSize(RelNode joinRel,
                                           BitSet groupKey)
Computes the population size for a set of keys returned from a join

Parameters:
joinRel - the join rel
groupKey - keys to compute the population for
Returns:
computed population size

getJoinDistinctRowCount

public static Double getJoinDistinctRowCount(RelNode joinRel,
                                             JoinRelType joinType,
                                             BitSet groupKey,
                                             RexNode predicate)
Computes the number of distinct rows for a set of keys returned from a join

Parameters:
joinRel - RelNode representing the join
joinType - type of join
groupKey - keys that the distinct row count will be computed for
predicate - join predicate
Returns:
number of distinct rows