001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.compile.OrderByNode
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.sql.compile;
023:
024: import org.apache.derby.iapi.services.context.ContextManager;
025:
026: import org.apache.derby.iapi.sql.compile.Optimizable;
027: import org.apache.derby.iapi.sql.compile.OptimizableList;
028: import org.apache.derby.iapi.sql.compile.OptimizablePredicate;
029: import org.apache.derby.iapi.sql.compile.OptimizablePredicateList;
030: import org.apache.derby.iapi.sql.compile.Optimizer;
031: import org.apache.derby.iapi.sql.compile.CostEstimate;
032: import org.apache.derby.iapi.sql.compile.Visitable;
033: import org.apache.derby.iapi.sql.compile.Visitor;
034:
035: import org.apache.derby.iapi.sql.dictionary.DataDictionary;
036:
037: import org.apache.derby.iapi.sql.execute.ExecutionContext;
038:
039: import org.apache.derby.iapi.sql.Activation;
040: import org.apache.derby.iapi.sql.ResultColumnDescriptor;
041: import org.apache.derby.iapi.sql.ResultSet;
042:
043: import org.apache.derby.iapi.error.StandardException;
044:
045: import org.apache.derby.impl.sql.compile.ActivationClassBuilder;
046:
047: import org.apache.derby.iapi.services.compiler.MethodBuilder;
048:
049: import org.apache.derby.iapi.services.sanity.SanityManager;
050:
051: import org.apache.derby.iapi.util.JBitSet;
052:
053: import java.util.Properties;
054:
055: /**
056: * An OrderByNode represents a result set for a sort operation
057: * for an order by list. It is expected to only be generated at
058: * the end of optimization, once we have determined that a sort
059: * is required.
060: *
061: * @author jerry
062: */
063: public class OrderByNode extends SingleChildResultSetNode {
064:
065: OrderByList orderByList;
066:
067: /**
068: * Initializer for a OrderByNode.
069: *
070: * @param childResult The child ResultSetNode
071: * @param orderByList The order by list.
072: * @param tableProperties Properties list associated with the table
073: *
074: * @exception StandardException Thrown on error
075: */
076: public void init(Object childResult, Object orderByList,
077: Object tableProperties) throws StandardException {
078: ResultSetNode child = (ResultSetNode) childResult;
079:
080: super .init(childResult, tableProperties);
081:
082: this .orderByList = (OrderByList) orderByList;
083:
084: ResultColumnList prRCList;
085:
086: /*
087: We want our own resultColumns, which are virtual columns
088: pointing to the child result's columns.
089:
090: We have to have the original object in the distinct node,
091: and give the underlying project the copy.
092: */
093:
094: /* We get a shallow copy of the ResultColumnList and its
095: * ResultColumns. (Copy maintains ResultColumn.expression for now.)
096: */
097: prRCList = child.getResultColumns().copyListAndObjects();
098: resultColumns = child.getResultColumns();
099: child.setResultColumns(prRCList);
100:
101: /* Replace ResultColumn.expression with new VirtualColumnNodes
102: * in the DistinctNode's RCL. (VirtualColumnNodes include
103: * pointers to source ResultSetNode, this, and source ResultColumn.)
104: */
105: resultColumns.genVirtualColumnNodes(this , prRCList);
106: }
107:
108: /**
109: * Convert this object to a String. See comments in QueryTreeNode.java
110: * for how this should be done for tree printing.
111: *
112: * @return This object as a String
113: */
114:
115: public String toString() {
116: if (SanityManager.DEBUG) {
117: return childResult.toString()
118: + "\n"
119: + "orderByList: "
120: + (orderByList != null ? orderByList.toString()
121: : "null") + "\n" + super .toString();
122: } else {
123: return "";
124: }
125: }
126:
127: ResultColumnDescriptor[] makeResultDescriptors(ExecutionContext ec) {
128: return childResult.makeResultDescriptors(ec);
129: }
130:
131: /**
132: * generate the distinct result set operating over the source
133: * resultset.
134: *
135: * @exception StandardException Thrown on error
136: */
137: public void generate(ActivationClassBuilder acb, MethodBuilder mb)
138: throws StandardException {
139: // Get the cost estimate for the child
140: if (costEstimate == null) {
141: costEstimate = childResult.getFinalCostEstimate();
142: }
143:
144: orderByList.generate(acb, mb, childResult);
145: }
146: }
|