use of com.wplatform.ddal.value.ValueArray in project jdbc-shards by wplatform.
the class AggregateDataHistogram method getValue.
@Override
Value getValue(Database database, int dataType, boolean distinct) {
if (distinct) {
count = 0;
groupDistinct(database, dataType);
}
ValueArray[] values = new ValueArray[distinctValues.size()];
int i = 0;
for (Value dv : distinctValues.keys()) {
AggregateDataHistogram d = distinctValues.get(dv);
values[i] = ValueArray.get(new Value[] { dv, ValueLong.get(d.count) });
i++;
}
final CompareMode compareMode = database.getCompareMode();
Arrays.sort(values, new Comparator<ValueArray>() {
@Override
public int compare(ValueArray v1, ValueArray v2) {
Value a1 = v1.getList()[0];
Value a2 = v2.getList()[0];
return a1.compareTo(a2, compareMode);
}
});
Value v = ValueArray.get(values);
return v.convertTo(dataType);
}
use of com.wplatform.ddal.value.ValueArray in project jdbc-shards by wplatform.
the class LocalResult method removeDistinct.
/**
* Remove the row from the result set if it exists.
*
* @param values the row
*/
public void removeDistinct(Value[] values) {
if (!distinct) {
DbException.throwInternalError();
}
if (distinctRows != null) {
ValueArray array = ValueArray.get(values);
distinctRows.remove(array);
rowCount = distinctRows.size();
} else {
rowCount = external.removeRow(values);
}
}
use of com.wplatform.ddal.value.ValueArray in project jdbc-shards by wplatform.
the class LocalResult method containsDistinct.
/**
* Check if this result set contains the given row.
*
* @param values the row
* @return true if the row exists
*/
public boolean containsDistinct(Value[] values) {
if (external != null) {
return external.contains(values);
}
if (distinctRows == null) {
distinctRows = ValueHashMap.newInstance();
for (Value[] row : rows) {
if (row.length > visibleColumnCount) {
Value[] r2 = new Value[visibleColumnCount];
System.arraycopy(row, 0, r2, 0, visibleColumnCount);
row = r2;
}
ValueArray array = ValueArray.get(row);
distinctRows.put(array, row);
}
}
ValueArray array = ValueArray.get(values);
return distinctRows.get(array) != null;
}
use of com.wplatform.ddal.value.ValueArray in project jdbc-shards by wplatform.
the class LocalResult method addRow.
/**
* Add a row to this object.
*
* @param values the row to add
*/
@Override
public void addRow(Value[] values) {
if (distinct) {
if (distinctRows != null) {
ValueArray array = ValueArray.get(values);
distinctRows.put(array, values);
rowCount = distinctRows.size();
if (rowCount > maxMemoryRows) {
throw DbException.getUnsupportedException("too big result row " + maxMemoryRows);
//external = new ResultTempTable(session, expressions, true, sort);
//rowCount = external.addRows(distinctRows.values());
//distinctRows = null;
}
} else {
rowCount = external.addRow(values);
}
return;
}
rows.add(values);
rowCount++;
if (rows.size() > maxMemoryRows) {
if (external == null) {
//external = new ResultTempTable(session, expressions, false, sort);
}
addRowsToDisk();
}
}
use of com.wplatform.ddal.value.ValueArray in project jdbc-shards by wplatform.
the class SelectExecutor method queryGroup.
private void queryGroup(int columnCount, LocalResult result) {
ValueHashMap<HashMap<Expression, Object>> groups = ValueHashMap.newInstance();
int rowNumber = 0;
prepared.setCurrentRowNumber(0);
prepared.setCurrentGroup(null);
ValueArray defaultGroup = ValueArray.get(new Value[0]);
int sampleSize = getSampleSizeValue(session);
while (topTableFilter.next()) {
prepared.setCurrentRowNumber(rowNumber + 1);
if (condition == null || Boolean.TRUE.equals(condition.getBooleanValue(session))) {
Value key;
rowNumber++;
if (groupIndex == null) {
key = defaultGroup;
} else {
Value[] keyValues = new Value[groupIndex.length];
// update group
for (int i = 0; i < groupIndex.length; i++) {
int idx = groupIndex[i];
Expression expr = expressions.get(idx);
keyValues[i] = expr.getValue(session);
}
key = ValueArray.get(keyValues);
}
HashMap<Expression, Object> values = groups.get(key);
if (values == null) {
values = new HashMap<Expression, Object>();
groups.put(key, values);
}
prepared.setCurrentGroup(values);
prepared.increaseCurrentGroupRowId();
int len = columnCount;
for (int i = 0; i < len; i++) {
if (groupByExpression == null || !groupByExpression[i]) {
Expression expr = expressions.get(i);
expr.updateAggregate(session);
}
}
if (sampleSize > 0 && rowNumber >= sampleSize) {
break;
}
}
}
if (groupIndex == null && groups.size() == 0) {
groups.put(defaultGroup, new HashMap<Expression, Object>());
}
ArrayList<Value> keys = groups.keys();
for (Value v : keys) {
ValueArray key = (ValueArray) v;
prepared.setCurrentGroup(groups.get(key));
Value[] keyValues = key.getList();
Value[] row = new Value[columnCount];
for (int j = 0; groupIndex != null && j < groupIndex.length; j++) {
row[groupIndex[j]] = keyValues[j];
}
for (int j = 0; j < columnCount; j++) {
if (groupByExpression != null && groupByExpression[j]) {
continue;
}
Expression expr = expressions.get(j);
row[j] = expr.getValue(session);
}
if (isHavingNullOrFalse(row)) {
continue;
}
row = keepOnlyDistinct(row, columnCount);
result.addRow(row);
}
}
Aggregations