Class Aggregate
- java.lang.Object
-
- org.apache.calcite.rel.AbstractRelNode
-
- org.apache.calcite.rel.SingleRel
-
- org.apache.calcite.rel.core.Aggregate
-
- All Implemented Interfaces:
java.lang.Cloneable
,RelOptNode
,RelNode
- Direct Known Subclasses:
Bindables.BindableAggregate
,ElasticsearchAggregate
,EnumerableAggregate
,GeodeAggregate
,JdbcRules.JdbcAggregate
,LogicalAggregate
,MongoAggregate
,PigAggregate
public abstract class Aggregate extends SingleRel
Relational operator that eliminates duplicates and computes totals.It corresponds to the
GROUP BY
operator in a SQL query statement, together with the aggregate functions in theSELECT
clause.Rules:
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Aggregate.AggCallBinding
Implementation of theSqlOperatorBinding
interface for anaggregate call
applied to a set of operands in the context of aLogicalAggregate
.static class
Aggregate.Group
What kind of roll-up is it?-
Nested classes/interfaces inherited from interface org.apache.calcite.rel.RelNode
RelNode.Context
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.List<AggregateCall>
aggCalls
protected ImmutableBitSet
groupSet
com.google.common.collect.ImmutableList<ImmutableBitSet>
groupSets
boolean
indicator
Whether there are indicator fields.static com.google.common.base.Predicate<Aggregate>
IS_NOT_GRAND_TOTAL
Deprecated.static com.google.common.base.Predicate<Aggregate>
IS_SIMPLE
Deprecated.static com.google.common.base.Predicate<Aggregate>
NO_INDICATOR
Deprecated.-
Fields inherited from class org.apache.calcite.rel.AbstractRelNode
digest, id, rowType, traitSet
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
Aggregate(RelOptCluster cluster, RelTraitSet traits, RelNode child, boolean indicator, ImmutableBitSet groupSet, java.util.List<ImmutableBitSet> groupSets, java.util.List<AggregateCall> aggCalls)
Creates an Aggregate.protected
Aggregate(RelInput input)
Creates an Aggregate by parsing serialized output.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description private static boolean
allContain(java.util.List<ImmutableBitSet> groupSets, int groupKey)
RelOptCost
computeSelfCost(RelOptPlanner planner, RelMetadataQuery mq)
Returns the cost of this plan (not including children).boolean
containsDistinctCall()
Returns whether any of the aggregates are DISTINCT.RelNode
copy(RelTraitSet traitSet, java.util.List<RelNode> inputs)
Creates a copy of this relational expression, perhaps changing traits and inputs.abstract Aggregate
copy(RelTraitSet traitSet, RelNode input, boolean indicator, ImmutableBitSet groupSet, java.util.List<ImmutableBitSet> groupSets, java.util.List<AggregateCall> aggCalls)
Creates a copy of this aggregate.protected RelDataType
deriveRowType()
static RelDataType
deriveRowType(RelDataTypeFactory typeFactory, RelDataType inputRowType, boolean indicator, ImmutableBitSet groupSet, java.util.List<ImmutableBitSet> groupSets, java.util.List<AggregateCall> aggCalls)
Computes the row type of anAggregate
before it exists.double
estimateRowCount(RelMetadataQuery mq)
Returns an estimate of the number of rows this relational expression will return.RelWriter
explainTerms(RelWriter pw)
Describes the inputs and attributes of this relational expression.java.util.List<AggregateCall>
getAggCallList()
Returns a list of calls to aggregate functions.int
getGroupCount()
Returns the number of grouping fields.ImmutableBitSet
getGroupSet()
Returns a bit set of the grouping fields.com.google.common.collect.ImmutableList<ImmutableBitSet>
getGroupSets()
Returns the list of grouping sets computed by this Aggregate.Aggregate.Group
getGroupType()
Returns the type of roll-up.int
getIndicatorCount()
Returns the number of indicator fields.java.util.List<Pair<AggregateCall,java.lang.String>>
getNamedAggCalls()
Returns a list of calls to aggregate functions together with their output field names.static boolean
isNotGrandTotal(Aggregate aggregate)
private boolean
isPredicate(RelNode input, int index)
static boolean
isSimple(Aggregate aggregate)
boolean
isValid(Litmus litmus, RelNode.Context context)
Returns whether this relational expression is valid.static boolean
noIndicator(Aggregate aggregate)
private boolean
typeMatchesInferred(AggregateCall aggCall, Litmus litmus)
Returns whether the inferred type of anAggregateCall
matches the type it was given when it was created.-
Methods inherited from class org.apache.calcite.rel.SingleRel
childrenAccept, getInput, getInputs, replaceInput
-
Methods inherited from class org.apache.calcite.rel.AbstractRelNode
accept, accept, collectVariablesSet, collectVariablesUsed, computeDigest, computeSelfCost, explain, getChildExps, getCluster, getCollationList, getConvention, getCorrelVariable, getDescription, getDigest, getExpectedInputRowType, getId, getInput, getQuery, getRelTypeName, getRows, getRowType, getTable, getTraitSet, getVariablesSet, getVariablesStopped, isDistinct, isKey, isValid, metadata, onRegister, recomputeDigest, register, sole, toString
-
-
-
-
Field Detail
-
IS_SIMPLE
@Deprecated public static final com.google.common.base.Predicate<Aggregate> IS_SIMPLE
Deprecated.
-
NO_INDICATOR
@Deprecated public static final com.google.common.base.Predicate<Aggregate> NO_INDICATOR
Deprecated.
-
IS_NOT_GRAND_TOTAL
@Deprecated public static final com.google.common.base.Predicate<Aggregate> IS_NOT_GRAND_TOTAL
Deprecated.
-
indicator
public final boolean indicator
Whether there are indicator fields.We strongly discourage the use indicator fields, because they cause the output row type of GROUPING SETS queries to be different from regular GROUP BY queries, and recommend that you set this field to
false
.
-
aggCalls
protected final java.util.List<AggregateCall> aggCalls
-
groupSet
protected final ImmutableBitSet groupSet
-
groupSets
public final com.google.common.collect.ImmutableList<ImmutableBitSet> groupSets
-
-
Constructor Detail
-
Aggregate
protected Aggregate(RelOptCluster cluster, RelTraitSet traits, RelNode child, boolean indicator, ImmutableBitSet groupSet, java.util.List<ImmutableBitSet> groupSets, java.util.List<AggregateCall> aggCalls)
Creates an Aggregate.All members of
groupSets
must be sub-sets ofgroupSet
. For a simpleGROUP BY
,groupSets
is a singleton list containinggroupSet
.If
GROUP BY
is not specified, or equivalently ifGROUP BY ()
is specified,groupSet
will be the empty set, andgroupSets
will have one element, that empty set.If
CUBE
,ROLLUP
orGROUPING SETS
are specified,groupSets
will have additional elements, but they must each be a subset ofgroupSet
, and they must be sorted by inclusion:(0, 1, 2), (1), (0, 2), (0), ()
.- Parameters:
cluster
- Clustertraits
- Traitschild
- Childindicator
- Whether row type should include indicator fields to indicate which grouping set is active; true is deprecatedgroupSet
- Bit set of grouping fieldsgroupSets
- List of all grouping sets; null for justgroupSet
aggCalls
- Collection of calls to aggregate functions
-
Aggregate
protected Aggregate(RelInput input)
Creates an Aggregate by parsing serialized output.
-
-
Method Detail
-
isSimple
public static boolean isSimple(Aggregate aggregate)
- See Also:
Bug.CALCITE_461_FIXED
-
isNotGrandTotal
public static boolean isNotGrandTotal(Aggregate aggregate)
-
noIndicator
public static boolean noIndicator(Aggregate aggregate)
-
isPredicate
private boolean isPredicate(RelNode input, int index)
-
copy
public final RelNode copy(RelTraitSet traitSet, java.util.List<RelNode> inputs)
Description copied from interface:RelNode
Creates a copy of this relational expression, perhaps changing traits and inputs.Sub-classes with other important attributes are encouraged to create variants of this method with more parameters.
- Specified by:
copy
in interfaceRelNode
- Overrides:
copy
in classAbstractRelNode
- Parameters:
traitSet
- Trait setinputs
- Inputs- Returns:
- Copy of this relational expression, substituting traits and inputs
-
copy
public abstract Aggregate copy(RelTraitSet traitSet, RelNode input, boolean indicator, ImmutableBitSet groupSet, java.util.List<ImmutableBitSet> groupSets, java.util.List<AggregateCall> aggCalls)
Creates a copy of this aggregate.- Parameters:
traitSet
- Traitsinput
- Inputindicator
- Whether row type should include indicator fields to indicate which grouping set is active; must be true if aggregate is not simplegroupSet
- Bit set of grouping fieldsgroupSets
- List of all grouping sets; null for justgroupSet
aggCalls
- Collection of calls to aggregate functions- Returns:
- New
Aggregate
if any parameter differs from the value of thisAggregate
, or justthis
if all the parameters are the same - See Also:
copy(org.apache.calcite.plan.RelTraitSet, java.util.List)
-
getAggCallList
public java.util.List<AggregateCall> getAggCallList()
Returns a list of calls to aggregate functions.- Returns:
- list of calls to aggregate functions
-
getNamedAggCalls
public java.util.List<Pair<AggregateCall,java.lang.String>> getNamedAggCalls()
Returns a list of calls to aggregate functions together with their output field names.- Returns:
- list of calls to aggregate functions and their output field names
-
getGroupCount
public int getGroupCount()
Returns the number of grouping fields. These grouping fields are the leading fields in both the input and output records.NOTE: The
getGroupSet()
data structure allows for the grouping fields to not be on the leading edge. New code should, if possible, assume that grouping fields are in arbitrary positions in the input relational expression.- Returns:
- number of grouping fields
-
getIndicatorCount
public int getIndicatorCount()
Returns the number of indicator fields.This is the same as
getGroupCount()
ifindicator
is true, zero ifindicator
is false.The offset of the first aggregate call in the output record is always groupCount + indicatorCount.
- Returns:
- number of indicator fields
-
getGroupSet
public ImmutableBitSet getGroupSet()
Returns a bit set of the grouping fields.- Returns:
- bit set of ordinals of grouping fields
-
getGroupSets
public com.google.common.collect.ImmutableList<ImmutableBitSet> getGroupSets()
Returns the list of grouping sets computed by this Aggregate.- Returns:
- List of all grouping sets; null for just
groupSet
-
explainTerms
public RelWriter explainTerms(RelWriter pw)
Description copied from class:AbstractRelNode
Describes the inputs and attributes of this relational expression. Each node should callsuper.explainTerms
, then call theRelWriterImpl.input(String, RelNode)
andRelWriterImpl.item(String, Object)
methods for each input and attribute.- Overrides:
explainTerms
in classSingleRel
- Parameters:
pw
- Plan writer- Returns:
- Plan writer for fluent-explain pattern
-
estimateRowCount
public double estimateRowCount(RelMetadataQuery mq)
Description copied from interface:RelNode
Returns an estimate of the number of rows this relational expression will return.NOTE jvs 29-Mar-2006: Don't call this method directly. Instead, use
RelMetadataQuery.getRowCount(org.apache.calcite.rel.RelNode)
, which gives plugins a chance to override the rel's default ideas about row count.- Specified by:
estimateRowCount
in interfaceRelNode
- Overrides:
estimateRowCount
in classSingleRel
- Parameters:
mq
- Metadata query- Returns:
- Estimate of the number of rows this relational expression will return
-
computeSelfCost
public RelOptCost computeSelfCost(RelOptPlanner planner, RelMetadataQuery mq)
Description copied from interface:RelNode
Returns the cost of this plan (not including children). The base implementation throws an error; derived classes should override.NOTE jvs 29-Mar-2006: Don't call this method directly. Instead, use
RelMetadataQuery.getNonCumulativeCost(org.apache.calcite.rel.RelNode)
, which gives plugins a chance to override the rel's default ideas about cost.- Specified by:
computeSelfCost
in interfaceRelNode
- Overrides:
computeSelfCost
in classAbstractRelNode
- Parameters:
planner
- Planner for cost calculationmq
- Metadata query- Returns:
- Cost of this plan (not including children)
-
deriveRowType
protected RelDataType deriveRowType()
- Overrides:
deriveRowType
in classSingleRel
-
deriveRowType
public static RelDataType deriveRowType(RelDataTypeFactory typeFactory, RelDataType inputRowType, boolean indicator, ImmutableBitSet groupSet, java.util.List<ImmutableBitSet> groupSets, java.util.List<AggregateCall> aggCalls)
Computes the row type of anAggregate
before it exists.- Parameters:
typeFactory
- Type factoryinputRowType
- Input row typeindicator
- Whether row type should include indicator fields to indicate which grouping set is active; must be true if aggregate is not simplegroupSet
- Bit set of grouping fieldsgroupSets
- List of all grouping sets; null for justgroupSet
aggCalls
- Collection of calls to aggregate functions- Returns:
- Row type of the aggregate
-
allContain
private static boolean allContain(java.util.List<ImmutableBitSet> groupSets, int groupKey)
-
isValid
public boolean isValid(Litmus litmus, RelNode.Context context)
Description copied from interface:RelNode
Returns whether this relational expression is valid.If assertions are enabled, this method is typically called with
litmus
=THROW
, as follows:assert rel.isValid(Litmus.THROW)
This signals that the method can throw an
AssertionError
if it is not valid.- Specified by:
isValid
in interfaceRelNode
- Overrides:
isValid
in classAbstractRelNode
- Parameters:
litmus
- What to do if invalidcontext
- Context for validity checking- Returns:
- Whether relational expression is valid
-
typeMatchesInferred
private boolean typeMatchesInferred(AggregateCall aggCall, Litmus litmus)
Returns whether the inferred type of anAggregateCall
matches the type it was given when it was created.- Parameters:
aggCall
- Aggregate calllitmus
- What to do if an error is detected (types do not match)- Returns:
- Whether the inferred and declared types match
-
containsDistinctCall
public boolean containsDistinctCall()
Returns whether any of the aggregates are DISTINCT.- Returns:
- Whether any of the aggregates are DISTINCT
-
getGroupType
public Aggregate.Group getGroupType()
Returns the type of roll-up.- Returns:
- Type of roll-up
-
-