1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.iapi.sql.compile.C_NodeTypes; 25 26 import org.apache.derby.iapi.services.sanity.SanityManager; 27 28 import org.apache.derby.iapi.error.StandardException; 29 30 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 31 32 import org.apache.derby.iapi.reference.SQLState; 33 import org.apache.derby.iapi.reference.Limits; 34 35 import org.apache.derby.impl.sql.compile.ActivationClassBuilder; 36 37 import org.apache.derby.iapi.util.ReuseFactory; 38 39 import java.util.Vector ; 40 41 47 48 public class GroupByList extends OrderedColumnList 49 { 50 int numGroupingColsAdded = 0; 51 52 57 public void addGroupByColumn(GroupByColumn column) 58 { 59 addElement(column); 60 } 61 62 67 public GroupByColumn getGroupByColumn(int position) 68 { 69 if (SanityManager.DEBUG) 70 { 71 SanityManager.ASSERT(position >=0 && position < size(), 72 "position (" + position + 73 ") expected to be between 0 and " + size()); 74 } 75 return (GroupByColumn) elementAt(position); 76 } 77 78 83 public void printSubNodes(int depth) 84 { 85 if (SanityManager.DEBUG) 86 { 87 for (int index = 0; index < size(); index++) 88 { 89 ( (GroupByColumn) elementAt(index) ).treePrint(depth); 90 } 91 } 92 } 93 94 100 public int getNumNeedToAddGroupingCols() 101 { 102 return numGroupingColsAdded; 103 } 104 105 118 public void bindGroupByColumns(SelectNode select, 119 Vector aggregateVector) 120 throws StandardException 121 { 122 FromList fromList = select.getFromList(); 123 ResultColumnList selectRCL = select.getResultColumns(); 124 SubqueryList dummySubqueryList = 125 (SubqueryList) getNodeFactory().getNode( 126 C_NodeTypes.SUBQUERY_LIST, 127 getContextManager()); 128 int numColsAddedHere = 0; 129 int size = size(); 130 131 132 if (size > Limits.DB2_MAX_ELEMENTS_IN_GROUP_BY) 133 { 134 throw StandardException.newException(SQLState.LANG_TOO_MANY_ELEMENTS); 135 } 136 137 138 for (int index = 0; index < size; index++) 139 { 140 GroupByColumn groupByCol = (GroupByColumn) elementAt(index); 141 groupByCol.bindExpression(fromList, 142 dummySubqueryList, aggregateVector); 143 } 144 145 146 int rclSize = selectRCL.size(); 147 for (int index = 0; index < size; index++) 148 { 149 boolean matchFound = false; 150 GroupByColumn groupingCol = (GroupByColumn) elementAt(index); 151 152 155 for (int inner = 0; inner < rclSize; inner++) 156 { 157 ResultColumn selectListRC = (ResultColumn) selectRCL.elementAt(inner); 158 if (!(selectListRC.getExpression() instanceof ColumnReference)) { 159 continue; 160 } 161 162 ColumnReference selectListCR = (ColumnReference) selectListRC.getExpression(); 163 164 if (selectListCR.isEquivalent(groupingCol.getColumnExpression())) { 165 166 groupingCol.setColumnPosition(inner + 1); 167 168 169 selectListRC.markAsGroupingColumn(); 170 matchFound = true; 171 break; 172 } 173 } 174 177 if (! matchFound && 178 groupingCol.getColumnExpression() instanceof ColumnReference) 179 { 180 ResultColumn newRC; 183 184 185 newRC = (ResultColumn) getNodeFactory().getNode( 186 C_NodeTypes.RESULT_COLUMN, 187 groupingCol.getColumnName(), 188 groupingCol.getColumnExpression().getClone(), 189 getContextManager()); 190 newRC.setVirtualColumnId(selectRCL.size() + 1); 191 newRC.markGenerated(); 192 newRC.markAsGroupingColumn(); 193 194 195 selectRCL.addElement(newRC); 196 197 200 groupingCol.setColumnPosition(selectRCL.size()); 201 202 211 numColsAddedHere++; 212 } 213 } 214 215 216 if (SanityManager.DEBUG) 217 { 218 SanityManager.ASSERT(dummySubqueryList.size() == 0, 219 "dummySubqueryList.size() is expected to be 0"); 220 } 221 222 numGroupingColsAdded+= numColsAddedHere; 223 } 224 225 226 227 237 public GroupByColumn findGroupingColumn(ValueNode node) 238 throws StandardException 239 { 240 int sz = size(); 241 for (int i = 0; i < sz; i++) 242 { 243 GroupByColumn gbc = (GroupByColumn)elementAt(i); 244 if (gbc.getColumnExpression().isEquivalent(node)) 245 { 246 return gbc; 247 } 248 } 249 return null; 250 } 251 252 258 public void remapColumnReferencesToExpressions() throws StandardException 259 { 260 GroupByColumn gbc; 261 int size = size(); 262 263 268 for (int index = 0; index < size; index++) 269 { 270 ValueNode retVN; 271 gbc = (GroupByColumn) elementAt(index); 272 273 retVN = gbc.getColumnExpression().remapColumnReferencesToExpressions(); 274 275 if (SanityManager.DEBUG) 276 { 277 SanityManager.ASSERT(retVN instanceof ColumnReference, 278 "retVN expected to be instanceof ColumnReference, not " + 279 retVN.getClass().getName()); 280 } 281 282 gbc.setColumnExpression(retVN); 283 } 284 } 285 286 289 public String toString() 290 { 291 if (SanityManager.DEBUG) 292 { 293 StringBuffer buf = new StringBuffer (); 294 295 for (int index = 0; index < size(); index++) 296 { 297 GroupByColumn groupingCol = (GroupByColumn) elementAt(index); 298 299 buf.append(groupingCol.toString()); 300 } 301 return buf.toString(); 302 } 303 else 304 { 305 return ""; 306 } 307 } 308 309 public void preprocess( 310 int numTables, FromList fromList, SubqueryList whereSubquerys, 311 PredicateList wherePredicates) throws StandardException 312 { 313 for (int index = 0; index < size(); index++) 314 { 315 GroupByColumn groupingCol = (GroupByColumn) elementAt(index); 316 groupingCol.setColumnExpression( 317 groupingCol.getColumnExpression().preprocess( 318 numTables, fromList, whereSubquerys, wherePredicates)); 319 } 320 } 321 } 322 | Popular Tags |