001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.compile.GroupByColumn
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.error.StandardException;
025:
026: import org.apache.derby.iapi.sql.dictionary.DataDictionary;
027:
028: import org.apache.derby.iapi.types.TypeId;
029:
030: import org.apache.derby.iapi.reference.SQLState;
031:
032: import org.apache.derby.iapi.services.sanity.SanityManager;
033:
034: import java.util.Vector;
035:
036: /**
037: * A GroupByColumn is a column in the GROUP BY clause.
038: *
039: * @author jerry
040: */
041: public class GroupByColumn extends OrderedColumn {
042: private ValueNode columnExpression;
043:
044: /**
045: * Initializer.
046: *
047: * @param colRef The ColumnReference for the grouping column
048: */
049: public void init(Object colRef) {
050: this .columnExpression = (ValueNode) colRef;
051: }
052:
053: /**
054: * Convert this object to a String. See comments in QueryTreeNode.java
055: * for how this should be done for tree printing.
056: *
057: * @return This object as a String
058: */
059: public String toString() {
060: if (SanityManager.DEBUG) {
061: return "Column Expression: " + columnExpression
062: + super .toString();
063: } else {
064: return "";
065: }
066: }
067:
068: /**
069: * Prints the sub-nodes of this object. See QueryTreeNode.java for
070: * how tree printing is supposed to work.
071: *
072: * @param depth The depth of this node in the tree
073: */
074:
075: public void printSubNodes(int depth) {
076: if (SanityManager.DEBUG) {
077: super .printSubNodes(depth);
078:
079: if (columnExpression != null) {
080: printLabel(depth, "colRef: ");
081: columnExpression.treePrint(depth + 1);
082: }
083: }
084: }
085:
086: /**
087: * Get the name of this column
088: *
089: * @return The name of this column
090: */
091: public String getColumnName() {
092: return columnExpression.getColumnName();
093: }
094:
095: /**
096: * Bind this grouping column.
097: *
098: * @param fromList The FROM list to use for binding
099: * @param subqueryList The SubqueryList we are building as we hit
100: * SubqueryNodes.
101: * @param aggregateVector The aggregate vector we build as we hit
102: * AggregateNodes.
103: *
104: * @exception StandardException Thrown on error
105: */
106:
107: public void bindExpression(FromList fromList,
108: SubqueryList subqueryList, Vector aggregateVector)
109: throws StandardException {
110: /* Bind the ColumnReference to the FromList */
111: columnExpression = (ValueNode) columnExpression.bindExpression(
112: fromList, subqueryList, aggregateVector);
113:
114: // Verify that we can group on the column
115: if (columnExpression.isParameterNode()) {
116: throw StandardException.newException(
117: SQLState.LANG_INVALID_COL_REF_GROUPED_SELECT_LIST,
118: columnExpression);
119: }
120: /*
121: * Do not check to see if we can map user types
122: * to built-in types. The ability to do so does
123: * not mean that ordering will work. In fact,
124: * as of version 2.0, ordering does not work on
125: * user types.
126: */
127: TypeId ctid = columnExpression.getTypeId();
128: if (!ctid.orderable(getClassFactory())) {
129: throw StandardException
130: .newException(
131: SQLState.LANG_COLUMN_NOT_ORDERABLE_DURING_EXECUTION,
132: ctid.getSQLTypeName());
133: }
134: }
135:
136: public ValueNode getColumnExpression() {
137: return columnExpression;
138: }
139:
140: public void setColumnExpression(ValueNode cexpr) {
141: this.columnExpression = cexpr;
142:
143: }
144: }
|