Class RelMetadataQuery
- java.lang.Object
-
- org.apache.calcite.rel.metadata.RelMetadataQuery
-
public class RelMetadataQuery extends java.lang.Object
RelMetadataQuery provides a strongly-typed facade on top ofRelMetadataProvider
for the set of relational expression metadata queries defined as standard within Calcite. The Javadoc on these methods serves as their primary specification.To add a new standard query
Xyz
to this interface, follow these steps:- Add a static method
getXyz
specification to this class. - Add unit tests to
org.apache.calcite.test.RelMetadataTest
. - Write a new provider class
RelMdXyz
in this package. Follow the pattern from an existing class such asRelMdColumnOrigins
, overloading on all of the logical relational expressions to which the query applies. - Add a
SOURCE
static member, similar toRelMdColumnOrigins.SOURCE
. - Register the
SOURCE
object inDefaultRelMetadataProvider
. - Get unit tests working.
Because relational expression metadata is extensible, extension projects can define similar facades in order to specify access to custom metadata. Please do not add queries here (nor on
RelNode
) which lack meaning outside of your extension.Besides adding new metadata queries, extension projects may need to add custom providers for the standard queries in order to handle additional relational expressions (either logical or physical). In either case, the process is the same: write a reflective provider and chain it on to an instance of
DefaultRelMetadataProvider
, pre-pending it to the default providers. Then supply that instance to the planner via the appropriate plugin mechanism. - Add a static method
-
-
Field Summary
-
Constructor Summary
Constructors Modifier Constructor Description private
RelMetadataQuery(boolean dummy)
Creates and initializes the instance that will serve as a prototype for all other instances.protected
RelMetadataQuery(JaninoRelMetadataProvider metadataProvider, RelMetadataQuery prototype)
-
Method Summary
-
-
-
Field Detail
-
map
public final java.util.Map<java.util.List,java.lang.Object> map
Set of active metadata queries, and cache of previous results.
-
metadataProvider
public final JaninoRelMetadataProvider metadataProvider
-
EMPTY
protected static final RelMetadataQuery EMPTY
-
collationHandler
private BuiltInMetadata.Collation.Handler collationHandler
-
columnOriginHandler
private BuiltInMetadata.ColumnOrigin.Handler columnOriginHandler
-
expressionLineageHandler
private BuiltInMetadata.ExpressionLineage.Handler expressionLineageHandler
-
tableReferencesHandler
private BuiltInMetadata.TableReferences.Handler tableReferencesHandler
-
columnUniquenessHandler
private BuiltInMetadata.ColumnUniqueness.Handler columnUniquenessHandler
-
cumulativeCostHandler
private BuiltInMetadata.CumulativeCost.Handler cumulativeCostHandler
-
distinctRowCountHandler
private BuiltInMetadata.DistinctRowCount.Handler distinctRowCountHandler
-
distributionHandler
private BuiltInMetadata.Distribution.Handler distributionHandler
-
explainVisibilityHandler
private BuiltInMetadata.ExplainVisibility.Handler explainVisibilityHandler
-
maxRowCountHandler
private BuiltInMetadata.MaxRowCount.Handler maxRowCountHandler
-
minRowCountHandler
private BuiltInMetadata.MinRowCount.Handler minRowCountHandler
-
memoryHandler
private BuiltInMetadata.Memory.Handler memoryHandler
-
nonCumulativeCostHandler
private BuiltInMetadata.NonCumulativeCost.Handler nonCumulativeCostHandler
-
parallelismHandler
private BuiltInMetadata.Parallelism.Handler parallelismHandler
-
percentageOriginalRowsHandler
private BuiltInMetadata.PercentageOriginalRows.Handler percentageOriginalRowsHandler
-
populationSizeHandler
private BuiltInMetadata.PopulationSize.Handler populationSizeHandler
-
predicatesHandler
private BuiltInMetadata.Predicates.Handler predicatesHandler
-
allPredicatesHandler
private BuiltInMetadata.AllPredicates.Handler allPredicatesHandler
-
nodeTypesHandler
private BuiltInMetadata.NodeTypes.Handler nodeTypesHandler
-
rowCountHandler
private BuiltInMetadata.RowCount.Handler rowCountHandler
-
selectivityHandler
private BuiltInMetadata.Selectivity.Handler selectivityHandler
-
sizeHandler
private BuiltInMetadata.Size.Handler sizeHandler
-
uniqueKeysHandler
private BuiltInMetadata.UniqueKeys.Handler uniqueKeysHandler
-
THREAD_PROVIDERS
public static final java.lang.ThreadLocal<JaninoRelMetadataProvider> THREAD_PROVIDERS
-
-
Constructor Detail
-
RelMetadataQuery
protected RelMetadataQuery(JaninoRelMetadataProvider metadataProvider, RelMetadataQuery prototype)
-
RelMetadataQuery
private RelMetadataQuery(boolean dummy)
Creates and initializes the instance that will serve as a prototype for all other instances.
-
-
Method Detail
-
initialHandler
protected static <H> H initialHandler(java.lang.Class<H> handlerClass)
-
instance
public static RelMetadataQuery instance()
Returns an instance of RelMetadataQuery. It ensures that cycles do not occur while computing metadata.
-
revise
protected <M extends Metadata,H extends MetadataHandler<M>> H revise(java.lang.Class<? extends RelNode> class_, MetadataDef<M> def)
Re-generates the handler for a given kind of metadata, adding support forclass_
if it is not already present.
-
getNodeTypes
public com.google.common.collect.Multimap<java.lang.Class<? extends RelNode>,RelNode> getNodeTypes(RelNode rel)
Returns theBuiltInMetadata.NodeTypes.getNodeTypes()
statistic.- Parameters:
rel
- the relational expression
-
getRowCount
public java.lang.Double getRowCount(RelNode rel)
Returns theBuiltInMetadata.RowCount.getRowCount()
statistic.- Parameters:
rel
- the relational expression- Returns:
- estimated row count, or null if no reliable estimate can be determined
-
getMaxRowCount
public java.lang.Double getMaxRowCount(RelNode rel)
Returns theBuiltInMetadata.MaxRowCount.getMaxRowCount()
statistic.- Parameters:
rel
- the relational expression- Returns:
- max row count
-
getMinRowCount
public java.lang.Double getMinRowCount(RelNode rel)
Returns theBuiltInMetadata.MinRowCount.getMinRowCount()
statistic.- Parameters:
rel
- the relational expression- Returns:
- max row count
-
getCumulativeCost
public RelOptCost getCumulativeCost(RelNode rel)
Returns theBuiltInMetadata.CumulativeCost.getCumulativeCost()
statistic.- Parameters:
rel
- the relational expression- Returns:
- estimated cost, or null if no reliable estimate can be determined
-
getNonCumulativeCost
public RelOptCost getNonCumulativeCost(RelNode rel)
Returns theBuiltInMetadata.NonCumulativeCost.getNonCumulativeCost()
statistic.- Parameters:
rel
- the relational expression- Returns:
- estimated cost, or null if no reliable estimate can be determined
-
getPercentageOriginalRows
public java.lang.Double getPercentageOriginalRows(RelNode rel)
Returns theBuiltInMetadata.PercentageOriginalRows.getPercentageOriginalRows()
statistic.- Parameters:
rel
- the relational expression- Returns:
- estimated percentage (between 0.0 and 1.0), or null if no reliable estimate can be determined
-
getColumnOrigins
public java.util.Set<RelColumnOrigin> getColumnOrigins(RelNode rel, int column)
Returns theBuiltInMetadata.ColumnOrigin.getColumnOrigins(int)
statistic.- Parameters:
rel
- the relational expressioncolumn
- 0-based ordinal for output column of interest- Returns:
- set of origin columns, or null if this information cannot be determined (whereas empty set indicates definitely no origin columns at all)
-
getColumnOrigin
public RelColumnOrigin getColumnOrigin(RelNode rel, int column)
Determines the origin of a column, provided the column maps to a single column that isn't derived.- Parameters:
rel
- the RelNode of the columncolumn
- the offset of the column whose origin we are trying to determine- Returns:
- the origin of a column provided it's a simple column; otherwise, returns null
- See Also:
getColumnOrigins(org.apache.calcite.rel.RelNode, int)
-
getExpressionLineage
public java.util.Set<RexNode> getExpressionLineage(RelNode rel, RexNode expression)
Determines the origin of a column.
-
getTableReferences
public java.util.Set<RexTableInputRef.RelTableRef> getTableReferences(RelNode rel)
Determines the tables used by a plan.
-
getTableOrigin
public RelOptTable getTableOrigin(RelNode rel)
Determines the origin of aRelNode
, provided it maps to a single table, optionally with filtering and projection.- Parameters:
rel
- the RelNode- Returns:
- the table, if the RelNode is a simple table; otherwise null
-
getSelectivity
public java.lang.Double getSelectivity(RelNode rel, RexNode predicate)
Returns theBuiltInMetadata.Selectivity.getSelectivity(RexNode)
statistic.- Parameters:
rel
- the relational expressionpredicate
- predicate whose selectivity is to be estimated againstrel
's output- Returns:
- estimated selectivity (between 0.0 and 1.0), or null if no reliable estimate can be determined
-
getUniqueKeys
public java.util.Set<ImmutableBitSet> getUniqueKeys(RelNode rel)
Returns theBuiltInMetadata.UniqueKeys.getUniqueKeys(boolean)
statistic.- Parameters:
rel
- the relational expression- Returns:
- set of keys, or null if this information cannot be determined (whereas empty set indicates definitely no keys at all)
-
getUniqueKeys
public java.util.Set<ImmutableBitSet> getUniqueKeys(RelNode rel, boolean ignoreNulls)
Returns theBuiltInMetadata.UniqueKeys.getUniqueKeys(boolean)
statistic.- Parameters:
rel
- the relational expressionignoreNulls
- if true, ignore null values when determining whether the keys are unique- Returns:
- set of keys, or null if this information cannot be determined (whereas empty set indicates definitely no keys at all)
-
areRowsUnique
public java.lang.Boolean areRowsUnique(RelNode rel)
Returns whether the rows of a given relational expression are distinct. This is derived by applying theBuiltInMetadata.ColumnUniqueness.areColumnsUnique(org.apache.calcite.util.ImmutableBitSet, boolean)
statistic over all columns.- Parameters:
rel
- the relational expression- Returns:
- true or false depending on whether the rows are unique, or null if not enough information is available to make that determination
-
areColumnsUnique
public java.lang.Boolean areColumnsUnique(RelNode rel, ImmutableBitSet columns)
Returns theBuiltInMetadata.ColumnUniqueness.areColumnsUnique(ImmutableBitSet, boolean)
statistic.- Parameters:
rel
- the relational expressioncolumns
- column mask representing the subset of columns for which uniqueness will be determined- Returns:
- true or false depending on whether the columns are unique, or null if not enough information is available to make that determination
-
areColumnsUnique
public java.lang.Boolean areColumnsUnique(RelNode rel, ImmutableBitSet columns, boolean ignoreNulls)
Returns theBuiltInMetadata.ColumnUniqueness.areColumnsUnique(ImmutableBitSet, boolean)
statistic.- Parameters:
rel
- the relational expressioncolumns
- column mask representing the subset of columns for which uniqueness will be determinedignoreNulls
- if true, ignore null values when determining column uniqueness- Returns:
- true or false depending on whether the columns are unique, or null if not enough information is available to make that determination
-
collations
public com.google.common.collect.ImmutableList<RelCollation> collations(RelNode rel)
Returns theBuiltInMetadata.Collation.collations()
statistic.- Parameters:
rel
- the relational expression- Returns:
- List of sorted column combinations, or null if not enough information is available to make that determination
-
distribution
public RelDistribution distribution(RelNode rel)
Returns theBuiltInMetadata.Distribution.distribution()
statistic.- Parameters:
rel
- the relational expression- Returns:
- List of sorted column combinations, or null if not enough information is available to make that determination
-
getPopulationSize
public java.lang.Double getPopulationSize(RelNode rel, ImmutableBitSet groupKey)
Returns theBuiltInMetadata.PopulationSize.getPopulationSize(ImmutableBitSet)
statistic.- Parameters:
rel
- the relational expressiongroupKey
- column mask representing the subset of columns for which the row count will be determined- Returns:
- distinct row count for the given groupKey, or null if no reliable estimate can be determined
-
getAverageRowSize
public java.lang.Double getAverageRowSize(RelNode rel)
Returns theBuiltInMetadata.Size.averageRowSize()
statistic.- Parameters:
rel
- the relational expression- Returns:
- average size of a row, in bytes, or null if not known
-
getAverageColumnSizes
public java.util.List<java.lang.Double> getAverageColumnSizes(RelNode rel)
Returns theBuiltInMetadata.Size.averageColumnSizes()
statistic.- Parameters:
rel
- the relational expression- Returns:
- a list containing, for each column, the average size of a column value, in bytes. Each value or the entire list may be null if the metadata is not available
-
getAverageColumnSizesNotNull
public java.util.List<java.lang.Double> getAverageColumnSizesNotNull(RelNode rel)
AsgetAverageColumnSizes(org.apache.calcite.rel.RelNode)
but never returns a null list, only ever a list of nulls.
-
isPhaseTransition
public java.lang.Boolean isPhaseTransition(RelNode rel)
Returns theBuiltInMetadata.Parallelism.isPhaseTransition()
statistic.- Parameters:
rel
- the relational expression- Returns:
- whether each physical operator implementing this relational expression belongs to a different process than its inputs, or null if not known
-
splitCount
public java.lang.Integer splitCount(RelNode rel)
Returns theBuiltInMetadata.Parallelism.splitCount()
statistic.- Parameters:
rel
- the relational expression- Returns:
- the number of distinct splits of the data, or null if not known
-
memory
public java.lang.Double memory(RelNode rel)
Returns theBuiltInMetadata.Memory.memory()
statistic.- Parameters:
rel
- the relational expression- Returns:
- the expected amount of memory, in bytes, required by a physical operator implementing this relational expression, across all splits, or null if not known
-
cumulativeMemoryWithinPhase
public java.lang.Double cumulativeMemoryWithinPhase(RelNode rel)
Returns theBuiltInMetadata.Memory.cumulativeMemoryWithinPhase()
statistic.- Parameters:
rel
- the relational expression- Returns:
- the cumulative amount of memory, in bytes, required by the physical operator implementing this relational expression, and all other operators within the same phase, across all splits, or null if not known
-
cumulativeMemoryWithinPhaseSplit
public java.lang.Double cumulativeMemoryWithinPhaseSplit(RelNode rel)
Returns theBuiltInMetadata.Memory.cumulativeMemoryWithinPhaseSplit()
statistic.- Parameters:
rel
- the relational expression- Returns:
- the expected cumulative amount of memory, in bytes, required by the physical operator implementing this relational expression, and all operators within the same phase, within each split, or null if not known
-
getDistinctRowCount
public java.lang.Double getDistinctRowCount(RelNode rel, ImmutableBitSet groupKey, RexNode predicate)
Returns theBuiltInMetadata.DistinctRowCount.getDistinctRowCount(ImmutableBitSet, RexNode)
statistic.- Parameters:
rel
- the relational expressiongroupKey
- column mask representing group by columnspredicate
- pre-filtered predicates- Returns:
- distinct row count for groupKey, filtered by predicate, or null if no reliable estimate can be determined
-
getPulledUpPredicates
public RelOptPredicateList getPulledUpPredicates(RelNode rel)
Returns theBuiltInMetadata.Predicates.getPredicates()
statistic.- Parameters:
rel
- the relational expression- Returns:
- Predicates that can be pulled above this RelNode
-
getAllPredicates
public RelOptPredicateList getAllPredicates(RelNode rel)
Returns theBuiltInMetadata.AllPredicates.getAllPredicates()
statistic.- Parameters:
rel
- the relational expression- Returns:
- All predicates within and below this RelNode
-
isVisibleInExplain
public boolean isVisibleInExplain(RelNode rel, SqlExplainLevel explainLevel)
Returns theBuiltInMetadata.ExplainVisibility.isVisibleInExplain(SqlExplainLevel)
statistic.- Parameters:
rel
- the relational expressionexplainLevel
- level of detail- Returns:
- true for visible, false for invisible; if no metadata is available, defaults to true
-
validatePercentage
private static java.lang.Double validatePercentage(java.lang.Double result)
-
getDistribution
public RelDistribution getDistribution(RelNode rel)
Returns theBuiltInMetadata.Distribution.distribution()
statistic.- Parameters:
rel
- the relational expression- Returns:
- description of how the rows in the relational expression are physically distributed
-
isPercentage
private static boolean isPercentage(java.lang.Double result, boolean fail)
-
isNonNegative
private static boolean isNonNegative(java.lang.Double result, boolean fail)
-
validateResult
private static java.lang.Double validateResult(java.lang.Double result)
-
-