@@ -0,0 +1,1588 @@
+/*%0A// Licensed to Julian Hyde under one or more contributor license%0A// agreements. See the NOTICE file distributed with this work for%0A// additional information regarding copyright ownership.%0A//%0A// Julian Hyde licenses this file to you under the Apache License,%0A// Version 2.0 (the %22License%22); you may not use this file except in%0A// compliance with the License. You may obtain a copy of the License at:%0A//%0A// http://www.apache.org/licenses/LICENSE-2.0%0A//%0A// Unless required by applicable law or agreed to in writing, software%0A// distributed under the License is distributed on an %22AS IS%22 BASIS,%0A// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A// See the License for the specific language governing permissions and%0A// limitations under the License.%0A*/%0Apackage org.eigenbase.rel.rules;%0A%0Aimport org.eigenbase.rel.*;%0Aimport org.eigenbase.relopt.*;%0A%0A/**%0A * Planner rule that removes a %7B@link SortRel%7D if its input is already sorted.%0A * Requires %7B@link RelCollationTraitDef%7D.%0A */%0Apublic class RemoveSortRule extends RelOptRule %7B%0A    public static final RemoveSortRule INSTANCE = new RemoveSortRule();%0A%0A    private RemoveSortRule() %7B%0A        super(%0A            any(SortRel.class),%0A            %22RemoveSortRule%22);%0A    %7D%0A%0A    @Override%0A    public void onMatch(RelOptRuleCall call) %7B%0A        final SortRel sort = (SortRel) call.getRels()%5B0%5D;%0A        final RelCollation collation = sort.getCollation();%0A        final RelTraitSet traits = sort.getTraitSet().replace(collation);%0A        call.transformTo(convert(sort.getChild(), traits));%0A    %7D%0A%7D%0A%0A// End RemoveSortRule.java%0A

Make sortedness a trait, and add a rule, RemoveSortRule, that implements ORDER BY by finding a rel that is already sorted. SortRel is now defined by a RelCollation, not a List<RelFieldCollation>, although they are fairly similar in content. (Most of the functionality is disabled in this commit.)