All Implemented Interfaces:
NamedWriteable, Writeable, PostAnalysisPlanVerificationAware, Resolvable
Direct Known Subclasses:
Absent, AllFirst, AllLast, Avg, Count, CountDistinct, DimensionValues, First, FirstDocId, HistogramMerge, Last, Max, Median, Min, NumericAggregate, Present, Sample, SpatialAggregateFunction, StdDev, TimeSeriesAggregateFunction, Top, Values, Variance, WeightedAvg

public abstract class AggregateFunction extends Function implements PostAnalysisPlanVerificationAware
A type of Function that takes multiple values and extracts a single value out of them. For example, AVG(). - Aggregate functions can have an optional filter and window, which default to Literal.TRUE and NO_WINDOW. - The aggregation function should be composed as: source, field, filter, window, parameters. Extra parameters should go to the parameters after the filter and window.
  • Field Details

    • NO_WINDOW

      public static final Literal NO_WINDOW
    • WINDOW_INTERVAL

      public static final TransportVersion WINDOW_INTERVAL
  • Constructor Details

  • Method Details

    • readWindow

      protected static Expression readWindow(StreamInput in) throws IOException
      Throws:
      IOException
    • writeTo

      public final void writeTo(StreamOutput out) throws IOException
      Specified by:
      writeTo in interface Writeable
      Throws:
      IOException
    • field

      public Expression field()
    • parameters

      public List<? extends Expression> parameters()
    • hasFilter

      public boolean hasFilter()
    • filter

      public Expression filter()
    • resolveType

      protected Expression.TypeResolution resolveType()
      Overrides:
      resolveType in class Expression
    • withFilter

      public abstract AggregateFunction withFilter(Expression filter)
      Attach a filter to the aggregate function.
    • withParameters

      public AggregateFunction withParameters(List<? extends Expression> parameters)
    • window

      public Expression window()
      Return the window associated with the aggregate function.
    • hasWindow

      public boolean hasWindow()
      Whether the aggregate function has a window different than NO_WINDOW.
    • aggregateInputReferences

      public AttributeSet aggregateInputReferences(Supplier<List<Attribute>> inputAttributes)
      Returns the set of input attributes required by this aggregate function, excluding those referenced by the filter.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Function
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Function
    • postAnalysisPlanVerification

      public BiConsumer<LogicalPlan,Failures> postAnalysisPlanVerification()
      Description copied from interface: PostAnalysisPlanVerificationAware
      Allows the implementer to return a consumer that will perform self-validation in the context of the tree structure the implementer is part of. This usually involves checking the type and configuration of the children or that of the parent.

      It is often more useful to perform the checks as extended as it makes sense, over stopping at the first failure. This will allow the author to progress faster to a correct query.

      Example: a GroupingFunction instance, which models a function to group documents to aggregate over, can only be used in the context of the STATS command, modeled by the Aggregate class. This is how this verification is performed:

           
            @Override
            public BiConsumer<LogicalPlan, Failures> postAnalysisPlanVerification() {
                return (p, failures) -> {
                    if (p instanceof Aggregate == false) {
                        p.forEachExpression(
                            GroupingFunction.class,
                            gf -> failures.add(fail(gf, "cannot use grouping function [{}] outside of a STATS command", gf.sourceText()))
                        );
                    }
                };
            }
           
           
      Specified by:
      postAnalysisPlanVerification in interface PostAnalysisPlanVerificationAware
      Returns:
      a consumer that will receive a tree to check and an accumulator of failures found during inspection.