class AvgExpression extends AbstractAggregationFunction {

    @Override
    public ExpressionResult apply(ExpressionContext expressionContext, ExpressionResult input) {
        ExpressionContext scopedContext = expressionContext.scoped(this);
        ExpressionResult result = expression.apply(scopedContext, input);
        AtomicReference<AvgValue> initialSum = scopedContext.computeIfAbsent(alias, () -> new AtomicReference<>(new AvgValue()));
        if (result == null || !result.isNumeric() || result.getValue() == null) {
            return new NumericExpressionResult(initialSum.get().getAverage());
        }

        AvgValue newAvg = initialSum.accumulateAndGet(new AvgValue(1, result.getNumericValue()), AvgValue::add);

        return new NumericExpressionResult(newAvg.getAverage());
    }

    public  AvgExpression(String alias, Expression expression);

}