--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.sql.compile.OrderByNode\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.sql.compile;\r
+\r
+import org.apache.derby.iapi.error.StandardException;\r
+import org.apache.derby.iapi.services.compiler.MethodBuilder;\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+import org.apache.derby.iapi.sql.ResultColumnDescriptor;\r
+\r
+/**\r
+ * An OrderByNode represents a result set for a sort operation\r
+ * for an order by list. It is expected to only be generated at \r
+ * the end of optimization, once we have determined that a sort\r
+ * is required.\r
+ *\r
+ */\r
+public class OrderByNode extends SingleChildResultSetNode\r
+{\r
+\r
+ OrderByList orderByList;\r
+\r
+ /**\r
+ * Initializer for a OrderByNode.\r
+ *\r
+ * @param childResult The child ResultSetNode\r
+ * @param orderByList The order by list.\r
+ * @param tableProperties Properties list associated with the table\r
+ *\r
+ * @exception StandardException Thrown on error\r
+ */\r
+ public void init(\r
+ Object childResult,\r
+ Object orderByList,\r
+ Object tableProperties)\r
+ throws StandardException\r
+ {\r
+ ResultSetNode child = (ResultSetNode) childResult;\r
+\r
+ super.init(childResult, tableProperties);\r
+\r
+ this.orderByList = (OrderByList) orderByList;\r
+\r
+ ResultColumnList prRCList;\r
+\r
+ /*\r
+ We want our own resultColumns, which are virtual columns\r
+ pointing to the child result's columns.\r
+\r
+ We have to have the original object in the distinct node,\r
+ and give the underlying project the copy.\r
+ */\r
+\r
+ /* We get a shallow copy of the ResultColumnList and its \r
+ * ResultColumns. (Copy maintains ResultColumn.expression for now.)\r
+ */\r
+ prRCList = child.getResultColumns().copyListAndObjects();\r
+ resultColumns = child.getResultColumns();\r
+ child.setResultColumns(prRCList);\r
+\r
+ /* Replace ResultColumn.expression with new VirtualColumnNodes\r
+ * in the DistinctNode's RCL. (VirtualColumnNodes include\r
+ * pointers to source ResultSetNode, this, and source ResultColumn.)\r
+ */\r
+ resultColumns.genVirtualColumnNodes(this, prRCList);\r
+ }\r
+\r
+ /**\r
+ * Convert this object to a String. See comments in QueryTreeNode.java\r
+ * for how this should be done for tree printing.\r
+ *\r
+ * @return This object as a String\r
+ */\r
+\r
+ public String toString()\r
+ {\r
+ if (SanityManager.DEBUG)\r
+ {\r
+ return childResult.toString() + "\n" + \r
+ "orderByList: " + \r
+ (orderByList != null ? orderByList.toString() : "null") + "\n" +\r
+ super.toString();\r
+ }\r
+ else\r
+ {\r
+ return "";\r
+ }\r
+ }\r
+\r
+ ResultColumnDescriptor[] makeResultDescriptors()\r
+ {\r
+ return childResult.makeResultDescriptors();\r
+ }\r
+\r
+ /**\r
+ * generate the distinct result set operating over the source\r
+ * resultset.\r
+ *\r
+ * @exception StandardException Thrown on error\r
+ */\r
+ public void generate(ActivationClassBuilder acb,\r
+ MethodBuilder mb)\r
+ throws StandardException\r
+ {\r
+ // Get the cost estimate for the child\r
+ if (costEstimate == null)\r
+ {\r
+ costEstimate = childResult.getFinalCostEstimate();\r
+ }\r
+\r
+ orderByList.generate(acb, mb, childResult);\r
+ }\r
+}\r