📄 orderbylist.java
字号:
OrderByColumn prev_obc = (OrderByColumn) elementAt(inner); if (colPosition == prev_obc.getColumnPosition()) { removeElementAt(loc); break; } } } } /** generate the sort result set operating over the source expression. @param acb the tool for building the class @param mb the method the generated code is to go into @exception StandardException thrown on failure */ public void generate(ActivationClassBuilder acb, MethodBuilder mb, ResultSetNode child) throws StandardException { /* ** If sorting is not required, don't generate a sort result set - ** just return the child result set. */ if ( ! sortNeeded) { child.generate(acb, mb); return; } /* Get the next ResultSet#, so we can number this ResultSetNode, its * ResultColumnList and ResultSet. * * REMIND: to do this properly (if order bys can live throughout * the tree) there ought to be an OrderByNode that holds its own * ResultColumnList that is a lsit of virtual column nodes pointing * to the source's result columns. But since we know it is outermost, * we just gloss over that and get ourselves a resultSetNumber * directly. */ CompilerContext cc = getCompilerContext(); /* create the orderItem and stuff it in. */ int orderItem = acb.addItem(acb.getColumnOrdering(this)); /* Generate the SortResultSet: * arg1: childExpress - Expression for childResultSet * arg2: distinct - always false, we have a separate node * for distincts * arg3: isInSortedOrder - is the source result set in sorted order * arg4: orderItem - entry in saved objects for the ordering * arg5: Activation * arg6: rowAllocator - method to construct rows for fetching * from the sort * arg7: row size * arg8: resultSetNumber * arg9: estimated row count * arg10: estimated cost * arg11: closeCleanup */ acb.pushGetResultSetFactoryExpression(mb); child.generate(acb, mb); int resultSetNumber = cc.getNextResultSetNumber(); // is a distinct query mb.push(false); // not in sorted order mb.push(false); mb.push(orderItem); acb.pushThisAsActivation(mb); // row allocator child.getResultColumns().generateHolder(acb, mb); mb.push(child.getResultColumns().getTotalColumnSize()); mb.push(resultSetNumber); // Get the cost estimate for the child // RESOLVE - we will eventually include the cost of the sort CostEstimate costEstimate = child.getFinalCostEstimate(); mb.push(costEstimate.rowCount()); mb.push(costEstimate.getEstimatedCost()); /** if this is the statement result set (today, it always is), and there is a current date/time request, then a method will have been generated. Otherwise, a simple null is passed in to the result set method. */ acb.pushResultSetClosedMethodFieldAccess(mb); mb.callMethod(VMOpcode.INVOKEINTERFACE, (String) null, "getSortResultSet", ClassName.NoPutResultSet, 11); } /* RequiredRowOrdering interface */ /** * @see RequiredRowOrdering#sortRequired * * @exception StandardException Thrown on error */ public int sortRequired(RowOrdering rowOrdering) throws StandardException { return sortRequired(rowOrdering, (JBitSet) null); } /** * @see RequiredRowOrdering#sortRequired * * @exception StandardException Thrown on error */ public int sortRequired(RowOrdering rowOrdering, JBitSet tableMap) throws StandardException { /* ** Currently, all indexes are ordered ascending, so a descending ** ORDER BY always requires a sort. */ if (alwaysSort) { return RequiredRowOrdering.SORT_REQUIRED; } /* ** Step through the columns in this list, and ask the ** row ordering whether it is ordered on each column. */ int position = 0; int size = size(); for (int loc = 0; loc < size; loc++) { OrderByColumn obc = getOrderByColumn(loc); // ResultColumn rc = obc.getResultColumn(); /* ** This presumes that the OrderByColumn refers directly to ** the base column, i.e. there is no intervening VirtualColumnNode. */ // ValueNode expr = obc.getNonRedundantExpression(); ValueNode expr = obc.getResultColumn().getExpression(); if ( ! (expr instanceof ColumnReference)) { return RequiredRowOrdering.SORT_REQUIRED; } ColumnReference cr = (ColumnReference) expr; /* ** Check whether the table referred to is in the table map (if any). ** If it isn't, we may have an ordering that does not require ** sorting for the tables in a partial join order. Look for ** columns beyond this column to see whether a referenced table ** is found - if so, sorting is required (for example, in a ** case like ORDER BY S.A, T.B, S.C, sorting is required). */ if (tableMap != null) { if ( ! tableMap.get(cr.getTableNumber())) { /* Table not in partial join order */ for (int remainingPosition = loc + 1; remainingPosition < size(); remainingPosition++) { OrderByColumn remainingobc = getOrderByColumn(loc); ResultColumn remainingrc = remainingobc.getResultColumn(); ValueNode remainingexpr = remainingrc.getExpression(); if (remainingexpr instanceof ColumnReference) { ColumnReference remainingcr = (ColumnReference) remainingexpr; if (tableMap.get(remainingcr.getTableNumber())) { return RequiredRowOrdering.SORT_REQUIRED; } } } return RequiredRowOrdering.NOTHING_REQUIRED; } } if ( ! rowOrdering.alwaysOrdered(cr.getTableNumber())) { /* ** Check whether the ordering is ordered on this column in ** this position. */ if ( ! rowOrdering.orderedOnColumn( obc.isAscending() ? RowOrdering.ASCENDING : RowOrdering.DESCENDING, position, cr.getTableNumber(), cr.getColumnNumber() )) { return RequiredRowOrdering.SORT_REQUIRED; } /* ** The position to ask about is for the columns in tables ** that are *not* always ordered. The always-ordered tables ** are not counted as part of the list of ordered columns */ position++; } } return RequiredRowOrdering.NOTHING_REQUIRED; } /** * @see RequiredRowOrdering#estimateCost * * @exception StandardException Thrown on error */ public void estimateCost(double estimatedInputRows, RowOrdering rowOrdering, CostEstimate resultCost) throws StandardException { /* ** Do a bunch of set-up the first time: get the SortCostController, ** the template row, the ColumnOrdering array, and the estimated ** row size. */ if (scc == null) { scc = getCompilerContext().getSortCostController(); resultRow = resultToSort.getResultColumns().buildEmptyRow().getRowArray(); columnOrdering = getColumnOrdering(); estimatedRowSize = resultToSort.getResultColumns().getTotalColumnSize(); } long inputRows = (long) estimatedInputRows; long exportRows = inputRows; double sortCost; sortCost = scc.getSortCost( (DataValueDescriptor[]) resultRow, columnOrdering, false, inputRows, exportRows, estimatedRowSize ); resultCost.setCost(sortCost, estimatedInputRows, estimatedInputRows); } /** @see RequiredRowOrdering#sortNeeded */ public void sortNeeded() { sortNeeded = true; } /** @see RequiredRowOrdering#sortNotNeeded */ public void sortNotNeeded() { sortNeeded = false; } /** * Remap all ColumnReferences in this tree to be clones of the * underlying expression. * * @return Nothing. * * @exception StandardException Thrown on error */ void remapColumnReferencesToExpressions() throws StandardException { } /** * Get whether or not a sort is needed. * * @return Whether or not a sort is needed. */ public boolean getSortNeeded() { return sortNeeded; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -