@@ -0,0 +1,2489 @@
+/*%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;%0A%0Aimport org.eigenbase.relopt.*;%0A%0A/**%0A * Definition of the ordering trait.%0A *%0A * %3Cp%3EOrdering is a physical property (i.e. a trait) because it can be changed%0A * without loss of information. The converter to do this is the %7B@link SortRel%7D%0A * operator.%3C/p%3E%0A *%0A * %3Cp%3EUnlike other current traits, a %7B@link RelNode%7D can have more than one%0A * value of this trait simultaneously. For example,%0A * %3Ccode%3ETableAccessRel(table=TIME_BY_DAY)%3C/code%3E might be sorted by%0A * %3Ccode%3E%7Bthe_year, the_month, the_date%7D%3C/code%3E and also by%0A * %3Ccode%3E%7Btime_id%7D%3C/code%3E. We have to allow a RelNode to belong to more than%0A * one RelSubset (these RelSubsets are always in the same set).%3C/p%3E%0A */%0Apublic class RelCollationTraitDef extends RelTraitDef%3CRelCollation%3E%0A%7B%0A    public static final RelCollationTraitDef INSTANCE =%0A        new RelCollationTraitDef();%0A%0A    private RelCollationTraitDef() %7B%0A    %7D%0A%0A    public Class%3CRelCollation%3E getTraitClass() %7B%0A        return RelCollation.class;%0A    %7D%0A%0A    public String getSimpleName() %7B%0A        return %22sort%22;%0A    %7D%0A%0A    @Override%0A    public boolean multiple() %7B%0A        return true;%0A    %7D%0A%0A    public RelCollation getDefault() %7B%0A        return RelCollationImpl.EMPTY;%0A    %7D%0A%0A    public RelNode convert(%0A        RelOptPlanner planner,%0A        RelNode rel,%0A        RelCollation toCollation,%0A        boolean allowInfiniteCostConverters)%0A    %7B%0A        return new SortRel(%0A            rel.getCluster(),%0A            rel.getTraitSet().plus(toCollation),%0A            rel,%0A            toCollation);%0A    %7D%0A%0A    public boolean canConvert(%0A        RelOptPlanner planner, RelCollation fromTrait, RelCollation toTrait)%0A    %7B%0A        return true;%0A    %7D%0A%7D%0A%0A// End RelCollationTraitDef.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.)