Class RelMetadataQuery
RelMetadataProvider
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.
-
Field Summary
Fields inherited from class org.apache.calcite.rel.metadata.RelMetadataQueryBase
map, metadataProvider, THREAD_PROVIDERS
-
Constructor Summary
ModifierConstructorDescriptionprotected
Creates the instance withJaninoRelMetadataProvider
instance fromRelMetadataQueryBase.THREAD_PROVIDERS
andEMPTY
as a prototype.RelMetadataQuery
(MetadataHandlerProvider provider) Create a RelMetadataQuery with a givenMetadataHandlerProvider
. -
Method Summary
Modifier and TypeMethodDescription@Nullable Boolean
areColumnsUnique
(RelNode rel, ImmutableBitSet columns) Returns theBuiltInMetadata.ColumnUniqueness.areColumnsUnique(ImmutableBitSet, boolean)
statistic.@Nullable Boolean
areColumnsUnique
(RelNode rel, ImmutableBitSet columns, boolean ignoreNulls) Returns theBuiltInMetadata.ColumnUniqueness.areColumnsUnique(ImmutableBitSet, boolean)
statistic.@Nullable Boolean
areRowsUnique
(RelNode rel) Returns whether the rows of a given relational expression are distinct.@Nullable Boolean
areRowsUnique
(RelNode rel, boolean ignoreNulls) Returns whether the rows of a given relational expression are distinct, optionally ignoring NULL values.@Nullable com.google.common.collect.ImmutableList<RelCollation>
collations
(RelNode rel) Returns theBuiltInMetadata.Collation.collations()
statistic.@Nullable Double
Returns theBuiltInMetadata.Memory.cumulativeMemoryWithinPhase()
statistic.@Nullable Double
Returns theBuiltInMetadata.Memory.cumulativeMemoryWithinPhaseSplit()
statistic.distribution
(RelNode rel) Returns theBuiltInMetadata.Distribution.distribution()
statistic.expand
(RelNode rel, int column, BuiltInMetadata.Measure.Context context) Returns theBuiltInMetadata.Measure.expand(int, BuiltInMetadata.Measure.Context)
statistic.@Nullable RelOptPredicateList
getAllPredicates
(RelNode rel) Returns theBuiltInMetadata.AllPredicates.getAllPredicates()
statistic.Returns theBuiltInMetadata.Size.averageColumnSizes()
statistic.AsgetAverageColumnSizes(org.apache.calcite.rel.RelNode)
but never returns a null list, only ever a list of nulls.@Nullable Double
getAverageRowSize
(RelNode rel) Returns theBuiltInMetadata.Size.averageRowSize()
statistic.@Nullable RelColumnOrigin
getColumnOrigin
(RelNode rel, int column) Determines the origin of a column.@Nullable Set<RelColumnOrigin>
getColumnOrigins
(RelNode rel, int column) Returns theBuiltInMetadata.ColumnOrigin.getColumnOrigins(int)
statistic.@Nullable RelOptCost
getCumulativeCost
(RelNode rel) Returns theBuiltInMetadata.CumulativeCost.getCumulativeCost()
statistic.@Nullable Double
getDistinctRowCount
(RelNode rel, ImmutableBitSet groupKey, @Nullable RexNode predicate) Returns theBuiltInMetadata.DistinctRowCount.getDistinctRowCount(ImmutableBitSet, RexNode)
statistic.@Nullable RelDistribution
getDistribution
(RelNode rel) Returns theBuiltInMetadata.Distribution.distribution()
statistic.getExpressionLineage
(RelNode rel, RexNode expression) Determines the origin of a column.@Nullable RelOptCost
getLowerBoundCost
(RelNode rel, VolcanoPlanner planner) Returns the lower bound cost of a RelNode.@Nullable Double
getMaxRowCount
(RelNode rel) Returns theBuiltInMetadata.MaxRowCount.getMaxRowCount()
statistic.@Nullable Double
getMinRowCount
(RelNode rel) Returns theBuiltInMetadata.MinRowCount.getMinRowCount()
statistic.getNodeTypes
(RelNode rel) Returns theBuiltInMetadata.NodeTypes.getNodeTypes()
statistic.@Nullable RelOptCost
Returns theBuiltInMetadata.NonCumulativeCost.getNonCumulativeCost()
statistic.@Nullable Double
Returns theBuiltInMetadata.PercentageOriginalRows.getPercentageOriginalRows()
statistic.@Nullable Double
getPopulationSize
(RelNode rel, ImmutableBitSet groupKey) Returns theBuiltInMetadata.PopulationSize.getPopulationSize(ImmutableBitSet)
statistic.Returns theBuiltInMetadata.Predicates.getPredicates()
statistic.getRowCount
(RelNode rel) Returns theBuiltInMetadata.RowCount.getRowCount()
statistic.@Nullable Double
getSelectivity
(RelNode rel, @Nullable RexNode predicate) Returns theBuiltInMetadata.Selectivity.getSelectivity(RexNode)
statistic.@Nullable RelOptTable
getTableOrigin
(RelNode rel) Determines the origin of aRelNode
, provided it maps to a single table, optionally with filtering and projection.@Nullable Set<RexTableInputRef.RelTableRef>
Determines the tables used by a plan.@Nullable Set<ImmutableBitSet>
getUniqueKeys
(RelNode rel) Returns theBuiltInMetadata.UniqueKeys.getUniqueKeys(boolean)
statistic.@Nullable Set<ImmutableBitSet>
getUniqueKeys
(RelNode rel, boolean ignoreNulls) Returns theBuiltInMetadata.UniqueKeys.getUniqueKeys(boolean)
statistic.static RelMetadataQuery
instance()
Returns an instance of RelMetadataQuery.@Nullable Boolean
Returns theBuiltInMetadata.Measure.isMeasure(int)
statistic.@Nullable Boolean
isPhaseTransition
(RelNode rel) Returns theBuiltInMetadata.Parallelism.isPhaseTransition()
statistic.isVisibleInExplain
(RelNode rel, SqlExplainLevel explainLevel) Returns theBuiltInMetadata.ExplainVisibility.isVisibleInExplain(SqlExplainLevel)
statistic.@Nullable Double
Returns theBuiltInMetadata.Memory.memory()
statistic.@Nullable Integer
splitCount
(RelNode rel) Returns theBuiltInMetadata.Parallelism.splitCount()
statistic.Methods inherited from class org.apache.calcite.rel.metadata.RelMetadataQueryBase
clearCache, handler, initialHandler, revise, revise
-
Constructor Details
-
RelMetadataQuery
protected RelMetadataQuery()Creates the instance withJaninoRelMetadataProvider
instance fromRelMetadataQueryBase.THREAD_PROVIDERS
andEMPTY
as a prototype. -
RelMetadataQuery
Create a RelMetadataQuery with a givenMetadataHandlerProvider
.- Parameters:
provider
- The provider to use for construction.
-
-
Method Details
-
instance
Returns an instance of RelMetadataQuery. It ensures that cycles do not occur while computing metadata. -
getNodeTypes
public @Nullable com.google.common.collect.Multimap<Class<? extends RelNode>,RelNode> getNodeTypes(RelNode rel) Returns theBuiltInMetadata.NodeTypes.getNodeTypes()
statistic.- Parameters:
rel
- the relational expression
-
getRowCount
Returns theBuiltInMetadata.RowCount.getRowCount()
statistic.- Parameters:
rel
- the relational expression- Returns:
- estimated row count, or null if no reliable estimate can be determined
-
getMaxRowCount
Returns theBuiltInMetadata.MaxRowCount.getMaxRowCount()
statistic.- Parameters:
rel
- the relational expression- Returns:
- max row count
-
getMinRowCount
Returns theBuiltInMetadata.MinRowCount.getMinRowCount()
statistic.- Parameters:
rel
- the relational expression- Returns:
- max row count
-
getCumulativeCost
Returns theBuiltInMetadata.CumulativeCost.getCumulativeCost()
statistic.- Parameters:
rel
- the relational expression- Returns:
- estimated cost, or null if no reliable estimate can be determined
-
getNonCumulativeCost
Returns theBuiltInMetadata.NonCumulativeCost.getNonCumulativeCost()
statistic.- Parameters:
rel
- the relational expression- Returns:
- estimated cost, or null if no reliable estimate can be determined
-
getPercentageOriginalRows
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
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
Determines the origin of a column.- 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
- See Also:
-
getExpressionLineage
Determines the origin of a column. -
getTableReferences
Determines the tables used by a plan. -
getTableOrigin
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
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
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
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
Returns whether the rows of a given relational expression are distinct, optionally ignoring NULL values.This is derived by applying the
BuiltInMetadata.ColumnUniqueness.areColumnsUnique(org.apache.calcite.util.ImmutableBitSet, boolean)
statistic over all columns. IfBuiltInMetadata.MaxRowCount.getMaxRowCount()
is less than or equal to one, we shortcut the process and declare the rows unique.- Parameters:
rel
- the relational expressionignoreNulls
- if true, ignore null values when determining column uniqueness- Returns:
- whether the rows are unique, or null if not enough information is available to make that determination
-
areRowsUnique
Returns whether the rows of a given relational expression are distinct.Derived by calling
areRowsUnique(RelNode, boolean)
.- Parameters:
rel
- the relational expression- Returns:
- whether the rows are unique, or null if not enough information is available to make that determination
-
areColumnsUnique
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 @Nullable 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
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
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
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
Returns theBuiltInMetadata.Size.averageRowSize()
statistic.- Parameters:
rel
- the relational expression- Returns:
- average size of a row, in bytes, or null if not known
-
getAverageColumnSizes
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
AsgetAverageColumnSizes(org.apache.calcite.rel.RelNode)
but never returns a null list, only ever a list of nulls. -
isPhaseTransition
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
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
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
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
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
-
isMeasure
Returns theBuiltInMetadata.Measure.isMeasure(int)
statistic.- Parameters:
rel
- The relational expressioncolumn
- Output column of the relational expression- Returns:
- whether column is a measure
-
expand
Returns theBuiltInMetadata.Measure.expand(int, BuiltInMetadata.Measure.Context)
statistic.- Parameters:
rel
- The relational expressioncolumn
- Output column of the relational expressioncontext
- Context of the use of the measure- Returns:
- expression for measure in the context
-
getDistinctRowCount
public @Nullable Double getDistinctRowCount(RelNode rel, ImmutableBitSet groupKey, @Nullable 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
Returns theBuiltInMetadata.Predicates.getPredicates()
statistic.- Parameters:
rel
- the relational expression- Returns:
- Predicates that can be pulled above this RelNode
-
getAllPredicates
Returns theBuiltInMetadata.AllPredicates.getAllPredicates()
statistic.- Parameters:
rel
- the relational expression- Returns:
- All predicates within and below this RelNode
-
isVisibleInExplain
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
-
getDistribution
Returns theBuiltInMetadata.Distribution.distribution()
statistic.- Parameters:
rel
- the relational expression- Returns:
- description of how the rows in the relational expression are physically distributed
-
getLowerBoundCost
Returns the lower bound cost of a RelNode.
-