Search in sources :

Example 1 with SelectOrderBy

use of com.wplatform.ddal.command.dml.SelectOrderBy in project jdbc-shards by wplatform.

the class Aggregate method getSQLGroupConcat.

private String getSQLGroupConcat() {
    StatementBuilder buff = new StatementBuilder("GROUP_CONCAT(");
    if (distinct) {
        buff.append("DISTINCT ");
    }
    buff.append(on.getSQL());
    if (groupConcatOrderList != null) {
        buff.append(" ORDER BY ");
        for (SelectOrderBy o : groupConcatOrderList) {
            buff.appendExceptFirst(", ");
            buff.append(o.expression.getSQL());
            if (o.descending) {
                buff.append(" DESC");
            }
        }
    }
    if (groupConcatSeparator != null) {
        buff.append(" SEPARATOR ").append(groupConcatSeparator.getSQL());
    }
    return buff.append(')').toString();
}
Also used : SelectOrderBy(com.wplatform.ddal.command.dml.SelectOrderBy) StatementBuilder(com.wplatform.ddal.util.StatementBuilder)

Example 2 with SelectOrderBy

use of com.wplatform.ddal.command.dml.SelectOrderBy in project jdbc-shards by wplatform.

the class Aggregate method initOrder.

private SortOrder initOrder(Session session) {
    int size = groupConcatOrderList.size();
    int[] index = new int[size];
    int[] sortType = new int[size];
    for (int i = 0; i < size; i++) {
        SelectOrderBy o = groupConcatOrderList.get(i);
        index[i] = i + 1;
        int order = o.descending ? SortOrder.DESCENDING : SortOrder.ASCENDING;
        sortType[i] = order;
    }
    return new SortOrder(session.getDatabase(), index, sortType, null);
}
Also used : SelectOrderBy(com.wplatform.ddal.command.dml.SelectOrderBy) SortOrder(com.wplatform.ddal.result.SortOrder)

Example 3 with SelectOrderBy

use of com.wplatform.ddal.command.dml.SelectOrderBy in project jdbc-shards by wplatform.

the class Aggregate method updateAggregate.

@Override
public void updateAggregate(Session session) {
    // TODO aggregates: check nested MIN(MAX(ID)) and so on
    // if (on != null) {
    // on.updateAggregate();
    // }
    HashMap<Expression, Object> group = select.getCurrentGroup();
    if (group == null) {
        // this is a different level (the enclosing query)
        return;
    }
    int groupRowId = select.getCurrentGroupRowId();
    if (lastGroupRowId == groupRowId) {
        // already visited
        return;
    }
    lastGroupRowId = groupRowId;
    AggregateData data = (AggregateData) group.get(this);
    if (data == null) {
        data = AggregateData.create(type);
        group.put(this, data);
    }
    Value v = on == null ? null : on.getValue(session);
    if (type == GROUP_CONCAT) {
        if (v != ValueNull.INSTANCE) {
            v = v.convertTo(Value.STRING);
            if (groupConcatOrderList != null) {
                int size = groupConcatOrderList.size();
                Value[] array = new Value[1 + size];
                array[0] = v;
                for (int i = 0; i < size; i++) {
                    SelectOrderBy o = groupConcatOrderList.get(i);
                    array[i + 1] = o.expression.getValue(session);
                }
                v = ValueArray.get(array);
            }
        }
    }
    data.add(session.getDatabase(), dataType, distinct, v);
}
Also used : SelectOrderBy(com.wplatform.ddal.command.dml.SelectOrderBy)

Example 4 with SelectOrderBy

use of com.wplatform.ddal.command.dml.SelectOrderBy in project jdbc-shards by wplatform.

the class Aggregate method optimize.

@Override
public Expression optimize(Session session) {
    if (on != null) {
        on = on.optimize(session);
        dataType = on.getType();
        scale = on.getScale();
        precision = on.getPrecision();
        displaySize = on.getDisplaySize();
    }
    if (groupConcatOrderList != null) {
        for (SelectOrderBy o : groupConcatOrderList) {
            o.expression = o.expression.optimize(session);
        }
        groupConcatSort = initOrder(session);
    }
    if (groupConcatSeparator != null) {
        groupConcatSeparator = groupConcatSeparator.optimize(session);
    }
    switch(type) {
        case GROUP_CONCAT:
            dataType = Value.STRING;
            scale = 0;
            precision = displaySize = Integer.MAX_VALUE;
            break;
        case COUNT_ALL:
        case COUNT:
            dataType = Value.LONG;
            scale = 0;
            precision = ValueLong.PRECISION;
            displaySize = ValueLong.DISPLAY_SIZE;
            break;
        case SELECTIVITY:
            dataType = Value.INT;
            scale = 0;
            precision = ValueInt.PRECISION;
            displaySize = ValueInt.DISPLAY_SIZE;
            break;
        case HISTOGRAM:
            dataType = Value.ARRAY;
            scale = 0;
            precision = displaySize = Integer.MAX_VALUE;
            break;
        case SUM:
            if (dataType == Value.BOOLEAN) {
                // example: sum(id > 3) (count the rows)
                dataType = Value.LONG;
            } else if (!DataType.supportsAdd(dataType)) {
                throw DbException.get(ErrorCode.SUM_OR_AVG_ON_WRONG_DATATYPE_1, getSQL());
            } else {
                dataType = DataType.getAddProofType(dataType);
            }
            break;
        case AVG:
            if (!DataType.supportsAdd(dataType)) {
                throw DbException.get(ErrorCode.SUM_OR_AVG_ON_WRONG_DATATYPE_1, getSQL());
            }
            break;
        case MIN:
        case MAX:
            break;
        case STDDEV_POP:
        case STDDEV_SAMP:
        case VAR_POP:
        case VAR_SAMP:
            dataType = Value.DOUBLE;
            precision = ValueDouble.PRECISION;
            displaySize = ValueDouble.DISPLAY_SIZE;
            scale = 0;
            break;
        case BOOL_AND:
        case BOOL_OR:
            dataType = Value.BOOLEAN;
            precision = ValueBoolean.PRECISION;
            displaySize = ValueBoolean.DISPLAY_SIZE;
            scale = 0;
            break;
        default:
            DbException.throwInternalError("type=" + type);
    }
    return this;
}
Also used : SelectOrderBy(com.wplatform.ddal.command.dml.SelectOrderBy)

Example 5 with SelectOrderBy

use of com.wplatform.ddal.command.dml.SelectOrderBy in project jdbc-shards by wplatform.

the class SortOrder method getColumn.

/**
     * Get the column for the given table filter, if the sort column is for this
     * filter.
     *
     * @param index  the column index (0, 1,..)
     * @param filter the table filter
     * @return the column, or null
     */
public Column getColumn(int index, TableFilter filter) {
    if (orderList == null) {
        return null;
    }
    SelectOrderBy order = orderList.get(index);
    Expression expr = order.expression;
    if (expr == null) {
        return null;
    }
    expr = expr.getNonAliasExpression();
    if (expr.isConstant()) {
        return null;
    }
    if (!(expr instanceof ExpressionColumn)) {
        return null;
    }
    ExpressionColumn exprCol = (ExpressionColumn) expr;
    if (exprCol.getTableFilter() != filter) {
        return null;
    }
    return exprCol.getColumn();
}
Also used : SelectOrderBy(com.wplatform.ddal.command.dml.SelectOrderBy) Expression(com.wplatform.ddal.command.expression.Expression) ExpressionColumn(com.wplatform.ddal.command.expression.ExpressionColumn)

Aggregations

SelectOrderBy (com.wplatform.ddal.command.dml.SelectOrderBy)5 Expression (com.wplatform.ddal.command.expression.Expression)1 ExpressionColumn (com.wplatform.ddal.command.expression.ExpressionColumn)1 SortOrder (com.wplatform.ddal.result.SortOrder)1 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)1