```
show functions | rename synopsis AS foo | stats bar = count(*) by foo | drop foo
```

```
{
    "error": {
        "root_cause": [
            {
                "type": "null_pointer_exception",
                "reason": "Cannot invoke \"org.elasticsearch.xpack.ql.expression.Attribute.id()\" because \"sourceAttribute\" is null"
            }
        ],
        "type": "null_pointer_exception",
        "reason": "Cannot invoke \"org.elasticsearch.xpack.ql.expression.Attribute.id()\" because \"sourceAttribute\" is null"
    },
    "status": 500
}
```

```
[2023-11-14T09:49:51,479][WARN ][r.suppressed             ] [runTask-0] path: /_query, params: {format=txt}, status: 500 java.lang.NullPointerException: Cannot invoke "org.elasticsearch.xpack.ql.expression.Attribute.id()" because "sourceAttribute" is null
        at org.elasticsearch.xpack.esql.planner.EsPhysicalOperationProviders.ordinalGroupingOperatorFactory(EsPhysicalOperationProviders.java:172)
        at org.elasticsearch.xpack.esql.planner.AbstractPhysicalOperationProviders.groupingPhysicalOperation(AbstractPhysicalOperationProviders.java:148)
        at org.elasticsearch.xpack.esql.planner.LocalExecutionPlanner.planAggregation(LocalExecutionPlanner.java:234)
        at org.elasticsearch.xpack.esql.planner.LocalExecutionPlanner.plan(LocalExecutionPlanner.java:182)
        at org.elasticsearch.xpack.esql.planner.LocalExecutionPlanner.planAggregation(LocalExecutionPlanner.java:233)
        at org.elasticsearch.xpack.esql.planner.LocalExecutionPlanner.plan(LocalExecutionPlanner.java:182)
        at org.elasticsearch.xpack.esql.planner.LocalExecutionPlanner.planProject(LocalExecutionPlanner.java:571)
        at org.elasticsearch.xpack.esql.planner.LocalExecutionPlanner.plan(LocalExecutionPlanner.java:196)
        at org.elasticsearch.xpack.esql.planner.LocalExecutionPlanner.planLimit(LocalExecutionPlanner.java:610)
        at org.elasticsearch.xpack.esql.planner.LocalExecutionPlanner.plan(LocalExecutionPlanner.java:200)
        at org.elasticsearch.xpack.esql.planner.LocalExecutionPlanner.planOutput(LocalExecutionPlanner.java:310)
        at org.elasticsearch.xpack.esql.planner.LocalExecutionPlanner.plan(LocalExecutionPlanner.java:224)
        at org.elasticsearch.xpack.esql.planner.LocalExecutionPlanner.plan(LocalExecutionPlanner.java:168)
        at org.elasticsearch.xpack.esql.plugin.ComputeService.runCompute(ComputeService.java:260)
```
x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/optimizer/LogicalPlanOptimizerTests.java
LogicalPlan plan = optimizedPlan("""
            from test
            | limit 1
            | keep first_name, last_name
            | mv_expand first_name
            | limit 10""");
        var limit = as(plan, Limit.class);
        assertThat(limit.limit().fold(), equalTo(10));
        var mvExpand = as(limit.child(), MvExpand.class);
        var project = as(mvExpand.child(), EsqlProject.class);
        limit = as(project.child(), Limit.class);
        assertThat(limit.limit().fold(), equalTo(1));
        as(limit.child(), EsRelation.class);
    }
    /**
     * Expected
     * EsqlProject[[emp_no{f}#141, first_name{f}#142, languages{f}#143, lll{r}#132, salary{f}#147]]
     *  \_TopN[[Order[salary{f}#147,DESC,FIRST], Order[first_name{f}#142,ASC,LAST]],5[INTEGER]]
     *    \_Limit[5[INTEGER]]
     *      \_MvExpand[salary{f}#147]
     *        \_Eval[[languages{f}#143 + 5[INTEGER] AS lll]]
     *          \_Filter[languages{f}#143 > 1[INTEGER]]
     *            \_Limit[10[INTEGER]]
     *              \_MvExpand[first_name{f}#142]
     *                \_TopN[[Order[emp_no{f}#141,DESC,FIRST]],10[INTEGER]]
     *                  \_Filter[emp_no{f}#141 &lt; 10006[INTEGER]]
     *                    \_EsRelation[test][emp_no{f}#141, first_name{f}#142, languages{f}#1..]
     */
    public void testMultipleMvExpandWithSortAndLimit() {
        LogicalPlan plan = optimizedPlan("""