Search in sources :

Example 1 with ValueArray

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);
}
Also used : Value(com.wplatform.ddal.value.Value) CompareMode(com.wplatform.ddal.value.CompareMode) ValueArray(com.wplatform.ddal.value.ValueArray)

Example 2 with ValueArray

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);
    }
}
Also used : ValueArray(com.wplatform.ddal.value.ValueArray)

Example 3 with ValueArray

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;
}
Also used : Value(com.wplatform.ddal.value.Value) ValueArray(com.wplatform.ddal.value.ValueArray)

Example 4 with ValueArray

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();
    }
}
Also used : ValueArray(com.wplatform.ddal.value.ValueArray)

Example 5 with ValueArray

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);
    }
}
Also used : HashMap(java.util.HashMap) ValueHashMap(com.wplatform.ddal.util.ValueHashMap) Expression(com.wplatform.ddal.command.expression.Expression) Value(com.wplatform.ddal.value.Value) ValueArray(com.wplatform.ddal.value.ValueArray)

Aggregations

ValueArray (com.wplatform.ddal.value.ValueArray)5 Value (com.wplatform.ddal.value.Value)3 Expression (com.wplatform.ddal.command.expression.Expression)1 ValueHashMap (com.wplatform.ddal.util.ValueHashMap)1 CompareMode (com.wplatform.ddal.value.CompareMode)1 HashMap (java.util.HashMap)1