Source:
public Result visit(Sort e) { if (e.getInput() instanceof Aggregate) { final Aggregate aggregate = (Aggregate) e.getInput(); if (hasTrickyRollup(e, aggregate)) { final Set<Integer> groupList = new LinkedHashSet<>(); for (RelFieldCollation fc : e.collation.getFieldCollations()) { groupList.add(aggregate.getGroupSet().nth(fc.getFieldIndex())); } groupList.addAll(Aggregate.Group.getRollup(aggregate.getGroupSets())); return offsetFetch(e, visitAggregate(aggregate, ImmutableList.copyOf(groupList))); } } if (e.getInput() instanceof Project) { final Project project = (Project) e.getInput(); final Permutation permutation = project.getPermutation(); if (permutation != null && project.getInput() instanceof Aggregate) { final Aggregate aggregate = (Aggregate) project.getInput(); if (hasTrickyRollup(e, aggregate)) { final RelCollation collation = RelCollations.permute(e.collation, permutation); final Sort sort2 = LogicalSort.create(aggregate, collation, e.offset, e.fetch); final Project project2 = LogicalProject.create(sort2, project.getProjects(), project.getRowType()); return visit(project2); } } } Result x = visitChild(0, e.getInput()); Builder builder = x.builder(e, Clause.ORDER_BY); if (builder.select.getSelectList() == null) { final List<SqlNode> selectList = Expressions.list(); for (RelDataTypeField field : e.getRowType().getFieldList()) { addSelect(selectList, builder.context.field(field.getIndex()), e.getRowType()); } builder.select.setSelectList(new SqlNodeList(selectList, POS)); } List<SqlNode> orderByList = Expressions.list(); for (RelFieldCollation field : e.getCollation().getFieldCollations()) { builder.addOrderItem(orderByList, field); } if (!orderByList.isEmpty()) { builder.setOrderBy(new SqlNodeList(orderByList, POS)); x = builder.result(); } x = offsetFetch(e, x); return x; }

Target:
public Result visit(Sort e) { if (e.getInput() instanceof Aggregate) { final Aggregate aggregate = (Aggregate) e.getInput(); if (hasTrickyRollup(e, aggregate)) { final Set<Integer> groupList = new LinkedHashSet<>(); for (RelFieldCollation fc : e.collation.getFieldCollations()) { groupList.add(aggregate.getGroupSet().nth(fc.getFieldIndex())); } groupList.addAll(Aggregate.Group.getRollup(aggregate.getGroupSets())); return offsetFetch(e, visitAggregate(aggregate, ImmutableList.copyOf(groupList))); } } if (e.getInput() instanceof Project) { final Project project = (Project) e.getInput(); final Permutation permutation = project.getPermutation(); if (permutation != null && project.getInput() instanceof Aggregate) { final Aggregate aggregate = (Aggregate) project.getInput(); if (hasTrickyRollup(e, aggregate)) { final RelCollation collation = RelCollations.permute(e.collation, permutation); final Sort sort2 = LogicalSort.create(aggregate, collation, e.offset, e.fetch); final Project project2 = LogicalProject.create(sort2, project.getProjects(), project.getRowType()); return visit(project2); } } } Result x = visitChild(0, e.getInput()); Builder builder = x.builder(e, Clause.ORDER_BY); if (stack.size() != 1 && builder.select.getSelectList() == null) { final List<SqlNode> selectList = Expressions.list(); for (RelDataTypeField field : e.getRowType().getFieldList()) { addSelect(selectList, builder.context.field(field.getIndex()), e.getRowType()); } builder.select.setSelectList(new SqlNodeList(selectList, POS)); } List<SqlNode> orderByList = Expressions.list(); for (RelFieldCollation field : e.getCollation().getFieldCollations()) { builder.addOrderItem(orderByList, field); } if (!orderByList.isEmpty()) { builder.setOrderBy(new SqlNodeList(orderByList, POS)); x = builder.result(); } x = offsetFetch(e, x); return x; }