### Description

As we have more rules doing analysis across plans, a number of methods are useful to add to the `QueryPlan` class itself, mainly around expressions:

```
- expressions() - return the expressions declared by this plan
- references() - return the references from the expressions above
```
x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/LogicalPlanOptimizer.java
}
            }
            
            for (NamedExpression agg : aggs) {
                Expression e = agg instanceof Alias a ? a.child() : agg;
                if (e instanceof SurrogateExpression sf) {
                    changed = true;
                    Expression s = sf.surrogate();
                    
                    if (s instanceof AggregateFunction == false) {
                        
                        var surrogateWithRefs = s.transformUp(AggregateFunction.class, af -> {
                            
                            
                            var attr = aggFuncToAttr.get(af);
                            
                            if (attr == null) {
                                var temporaryName = temporaryName(agg, af);
                                
                                var newAlias = new Alias(agg.source(), temporaryName, null, af, null, true);
                                attr = newAlias.toAttribute();
                                aggFuncToAttr.put(af, attr);
                                newAggs.add(newAlias);
                            }
                            return attr;
                        });
                        
                        
                        
                        var aliased = new Alias(agg.source(), agg.name(), null, surrogateWithRefs, agg.toAttribute().id());