@Override
        public LogicalPlan apply(LogicalPlan oldPlan) {
            return oldPlan.resolveOperators(plan -> {
                if (plan instanceof Project) {
                    Project project = (Project) plan;
                    List<NamedExpression> cleanedProjectList = mapToList(project.projectList, e -> (NamedExpression) trimNonTopLevelAliases(e));
                    if (!compareList(cleanedProjectList, project.projectList, Expression::fastEquals)) {
                        return new Project(cleanedProjectList, project.child());
                    } else {
                        return plan;
                    }
                } else if (plan instanceof Aggregate) {
                    Aggregate aggregate = (Aggregate) plan;
                    List<Expression> cleanedGroupings = mapToList(aggregate.groupingExpressions, Analyzer::trimAliases);
                    List<NamedExpression> cleanedAggs = mapToList(aggregate.aggregateExpressions, e -> (NamedExpression) trimNonTopLevelAliases(e));
                    if (!compareList(cleanedGroupings, aggregate.groupingExpressions, Expression::fastEquals)
                            || !compareList(cleanedAggs, aggregate.aggregateExpressions, Expression::fastEquals)) {
                        return new Aggregate(
                                cleanedGroupings,
                                cleanedAggs,
                                aggregate.child());
                    } else {
                        return plan;
                    }
                }
                return plan.transformExpressionsDown(e -> {
                    if (e instanceof Alias) {
                        return ((Alias) e).child;
                    }
                    return e;
                });
            });
        }
Cleans up unnecessary Aliases inside the plan. Basically we only need Alias as a top level
expression in Project(project list) or Aggregate(aggregate expressions) or
Window(window expressions).
indexr-query-opt/src/main/java/io/indexr/query/Analyzer.java