public AggregationResult toAggregationResult(GroupingExpression exp) {
        int level = exp.getLevel();
        // Is aggregating on list of groups?
        if (level > 1) {
            /*
            * The below aggregator operates on lists of groups in the query language world.
            * Internally, it operates on hits (by evaluating the group-by expression for each hit).
            * The group-by expression is passed to the aggregator by RequestBuilder.
            */
            if (exp instanceof CountAggregator) {
                return new ExpressionCountAggregationResult();
            }
            throw new IllegalInputException(
                    "Can not aggregate on " + GroupingOperation.getLevelDesc(level) + ".");
        }
        if (exp instanceof AvgAggregator avgAggregator) {
            return new AverageAggregationResult()
                    .setExpression(toExpressionNode(avgAggregator.getExpression()));
        }
        if (exp instanceof CountAggregator) {
            return new CountAggregationResult()
                    .setExpression(new ConstantNode(new IntegerResultNode(0)));
        }
        if (exp instanceof MaxAggregator aggregator) {
            return new MaxAggregationResult()
                    .setExpression(toExpressionNode(aggregator.getExpression()));
        }
        if (exp instanceof MinAggregator aggregator) {
            return new MinAggregationResult()
                    .setExpression(toExpressionNode(aggregator.getExpression()));
        }
        if (exp instanceof SumAggregator aggregator) {
            return new SumAggregationResult()
                    .setExpression(toExpressionNode(aggregator.getExpression()));
        }
        if (exp instanceof SummaryValue summaryValue) {
            String summaryName = summaryValue.getSummaryName();
            return new HitsAggregationResult()
                    .setSummaryClass(summaryName != null ? summaryName : defaultSummaryName)
                    .setExpression(new ConstantNode(new IntegerResultNode(0)));
        }
        if (exp instanceof StandardDeviationAggregator aggregator) {
            return new StandardDeviationAggregationResult()
                    .setExpression(toExpressionNode(aggregator.getExpression()));
        }
        if (exp instanceof XorAggregator aggregator) {
            return new XorAggregationResult()
                    .setExpression(toExpressionNode(aggregator.getExpression()));
        }
        throw new IllegalInputException("Can not convert '" + exp + "' to an aggregator.");
    }
Converts the given ast type grouping expression to a corresponding back-end type aggregation result.

@param exp The expression to convert.
@return The corresponding back-end result.
@throws UnsupportedOperationException Thrown if the given expression could not be converted.
container-search/src/main/java/com/yahoo/search/grouping/vespa/ExpressionConverter.java