GroupedResult.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.hsqldb |
![]() |
![]() |
HSQL Database Engine |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/* Copyright (c) 2001-2007, The HSQL Development Group
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the HSQL Development Group nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hsqldb;
import org.hsqldb.lib.ArrayUtil;
import org.hsqldb.lib.OrderedHashSet;
import org.hsqldb.navigator.DataRowSetNavigator;
import org.hsqldb.navigator.RowSetNavigator;
import org.hsqldb.result.Result;
/**
* This class is used for grouping select results, especially for select
* statements that include group by clause and nested aggregate functions.
* It is used by the <b>Select</b> class regardless the existence of group by
* clause.
* <p>
* When a group by clause is defined, a <b>ResultGroup</b> is used to hold
* all column values and <b>AggregatingValue</b>s for each group. When a group
* by clause is not defined, one <b>ResultGroup</b> is used to hold all the
* results.<p>
*
* All <b>ResultGroup</b>s are placed in a <b>HashSet</b>. Adding a new row
* will first retrieve the corresponding group from the table, based on the
* values in the group by columns. If a group is found, then the row
* associated with the group will be returned. Otherwise a new group is
* created with the new row, and the new row is returned.
* <p>
* The <b>Select</b> can then update the values and <b>AggregatingValue</b>s
* in the returned row, rather than the original row. This approach enables
* nested aggregate functions, such as "count(id)+2, 20-count(id),
* max(id)-min(id)" support.
*
* @author Tony Lai
* @version 1.7.2
* @since 1.7.2
* @see Expression
* @see Select
*/
// fredt@users - patch 1.7.2 - mods to use new HashSet class and to separate addRow and getRow operations
class GroupedResult {
/** @todo fredt - initialise results on first use */
private Result result;
int groupBegin;
int groupEnd;
private final boolean isGrouped;
private final boolean isAggregated;
private OrderedHashSet groups;
private ResultGroup currGroup;
private int size;
Expression[] exprColumns;
Session session;
GroupedResult(Session session, Select select) throws HsqlException {
RowSetNavigator navigator = new DataRowSetNavigator(session, select);
result = Result.newResult(navigator);
result.metaData = select.resultMetaData;
groupBegin = select.visibleColumnCount;
groupEnd = groupBegin + select.groupByColumnCount;
isGrouped = groupBegin != groupEnd;
isAggregated = select.isAggregated;
exprColumns = select.exprColumns;
if (isGrouped || isAggregated) {
groups = new OrderedHashSet();
}
this.session = session;
}
Object[] getRow(Object[] row) {
if (isGrouped) {
ResultGroup newGroup = new ResultGroup(row);
ResultGroup group = (ResultGroup) groups.get(newGroup);
if (group != null) {
ArrayUtil.copyArray(group.data, row, row.length);
}
} else if (isAggregated) {
if (currGroup != null) {
ArrayUtil.copyArray(currGroup.data, row, row.length);
}
}
return row;
}
void addRow(Object[] row) {
if (isGrouped) {
ResultGroup newGroup = new ResultGroup(row);
currGroup = (ResultGroup) groups.get(newGroup);
if (currGroup == null) {
currGroup = newGroup;
groups.add(currGroup);
if (!isAggregated) {
result.getNavigator().add(row);
}
size++;
} else {
System.arraycopy(row, 0, currGroup.data, 0, row.length);
}
} else if (isAggregated) {
if (currGroup == null) {
currGroup = new ResultGroup(row);
groups.add(currGroup);
size++;
} else {
System.arraycopy(row, 0, currGroup.data, 0, row.length);
}
} else {
result.getNavigator().add(row);
size++;
}
}
int size() {
return size;
}
RowSetNavigator navigator() {
return result.initialiseNavigator();
}
Result getResult() throws HsqlException {
if (isAggregated) {
for (int i = 0; i < groups.size(); i++) {
ResultGroup group = (ResultGroup) groups.get(i);
for (int j = 0; j < exprColumns.length; j++) {
if (exprColumns[j].isAggregate()) {
group.data[j] =
exprColumns[j].getAggregatedValue(session,
group.data[j]);
}
}
result.getNavigator().add(group.data);
}
}
return result;
}
class ResultGroup {
Object[] data;
int hashCode;
private ResultGroup(Object[] row) {
this.data = row;
hashCode = 0;
for (int i = groupBegin; i < groupEnd; i++) {
if (row[i] != null) {
hashCode += row[i].hashCode();
}
}
}
public int hashCode() {
return hashCode;
}
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof ResultGroup)) {
return false;
}
ResultGroup group = (ResultGroup) obj;
for (int i = groupBegin; i < groupEnd; i++) {
if (!equals(data[i], group.data[i])) {
return false;
}
}
return true;
}
private boolean equals(Object o1, Object o2) {
return (o1 == null) ? o2 == null
: o1.equals(o2);
}
}
}
The table below shows all metrics for GroupedResult.java.



