Search in sources :

Example 1 with SortOrder

use of com.wplatform.ddal.result.SortOrder in project jdbc-shards by wplatform.

the class Query method prepareOrder.

/**
     * Create a {@link SortOrder} object given the list of {@link SelectOrderBy}
     * objects. The expression list is extended if necessary.
     *
     * @param orderList       a list of {@link SelectOrderBy} elements
     * @param expressionCount the number of columns in the query
     * @return the {@link SortOrder} object
     */
public SortOrder prepareOrder(ArrayList<SelectOrderBy> orderList, int expressionCount) {
    int size = orderList.size();
    int[] index = new int[size];
    int[] sortType = new int[size];
    for (int i = 0; i < size; i++) {
        SelectOrderBy o = orderList.get(i);
        int idx;
        boolean reverse = false;
        Expression expr = o.columnIndexExpr;
        Value v = expr.getValue(null);
        if (v == ValueNull.INSTANCE) {
            // parameter not yet set - order by first column
            idx = 0;
        } else {
            idx = v.getInt();
            if (idx < 0) {
                reverse = true;
                idx = -idx;
            }
            idx -= 1;
            if (idx < 0 || idx >= expressionCount) {
                throw DbException.get(ErrorCode.ORDER_BY_NOT_IN_RESULT, "" + (idx + 1));
            }
        }
        index[i] = idx;
        boolean desc = o.descending;
        if (reverse) {
            desc = !desc;
        }
        int type = desc ? SortOrder.DESCENDING : SortOrder.ASCENDING;
        if (o.nullsFirst) {
            type += SortOrder.NULLS_FIRST;
        } else if (o.nullsLast) {
            type += SortOrder.NULLS_LAST;
        }
        sortType[i] = type;
    }
    return new SortOrder(session.getDatabase(), index, sortType, orderList);
}
Also used : Value(com.wplatform.ddal.value.Value) SortOrder(com.wplatform.ddal.result.SortOrder)

Example 2 with SortOrder

use of com.wplatform.ddal.result.SortOrder 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 SortOrder

use of com.wplatform.ddal.result.SortOrder in project jdbc-shards by wplatform.

the class Aggregate method getValue.

@Override
public Value getValue(Session session) {
    HashMap<Expression, Object> group = select.getCurrentGroup();
    if (group == null) {
        throw DbException.get(ErrorCode.INVALID_USE_OF_AGGREGATE_FUNCTION_1, getSQL());
    }
    AggregateData data = (AggregateData) group.get(this);
    if (data == null) {
        data = AggregateData.create(type);
    }
    Value v = data.getValue(session.getDatabase(), dataType, distinct);
    if (type == GROUP_CONCAT) {
        ArrayList<Value> list = ((AggregateDataGroupConcat) data).getList();
        if (list == null || list.size() == 0) {
            return ValueNull.INSTANCE;
        }
        if (groupConcatOrderList != null) {
            final SortOrder sortOrder = groupConcatSort;
            Collections.sort(list, new Comparator<Value>() {

                @Override
                public int compare(Value v1, Value v2) {
                    Value[] a1 = ((ValueArray) v1).getList();
                    Value[] a2 = ((ValueArray) v2).getList();
                    return sortOrder.compare(a1, a2);
                }
            });
        }
        StatementBuilder buff = new StatementBuilder();
        String sep = groupConcatSeparator == null ? "," : groupConcatSeparator.getValue(session).getString();
        for (Value val : list) {
            String s;
            if (val.getType() == Value.ARRAY) {
                s = ((ValueArray) val).getList()[0].getString();
            } else {
                s = val.getString();
            }
            if (s == null) {
                continue;
            }
            if (sep != null) {
                buff.appendExceptFirst(sep);
            }
            buff.append(s);
        }
        v = ValueString.get(buff.toString());
    }
    return v;
}
Also used : StatementBuilder(com.wplatform.ddal.util.StatementBuilder) SortOrder(com.wplatform.ddal.result.SortOrder)

Example 4 with SortOrder

use of com.wplatform.ddal.result.SortOrder in project jdbc-shards by wplatform.

the class TableFilter method getBestPlanItem.

/**
     * Get the best plan item (index, cost) to use use for the current join
     * order.
     *
     * @param s     the session
     * @param level 1 for the first table in a join, 2 for the second, and so on
     * @return the best plan item
     */
public PlanItem getBestPlanItem(Session s, int level) {
    PlanItem item;
    if (indexConditions.size() == 0) {
        item = new PlanItem();
        item.setIndex(table.getScanIndex(s));
        item.cost = item.getIndex().getCost(s, null, null, null);
    } else {
        int len = table.getColumns().length;
        int[] masks = new int[len];
        for (IndexCondition condition : indexConditions) {
            if (condition.isEvaluatable()) {
                if (condition.isAlwaysFalse()) {
                    masks = null;
                    break;
                }
                int id = condition.getColumn().getColumnId();
                if (id >= 0) {
                    masks[id] |= condition.getMask(indexConditions);
                }
            }
        }
        SortOrder sortOrder = null;
        if (select != null) {
            sortOrder = select.getSortOrder();
        }
        item = table.getBestPlanItem(s, masks, this, sortOrder);
        // The more index conditions, the earlier the table.
        // This is to ensure joins without indexes run quickly:
        // x (x.a=10); y (x.b=y.b) - see issue 113
        item.cost -= item.cost * indexConditions.size() / 100 / level;
    }
    if (nestedJoin != null) {
        setEvaluatable(nestedJoin);
        item.setNestedJoinPlan(nestedJoin.getBestPlanItem(s, level));
        // TODO optimizer: calculate cost of a join: should use separate
        // expected row number and lookup cost
        item.cost += item.cost * item.getNestedJoinPlan().cost;
    }
    if (join != null) {
        setEvaluatable(join);
        item.setJoinPlan(join.getBestPlanItem(s, level));
        // TODO optimizer: calculate cost of a join: should use separate
        // expected row number and lookup cost
        item.cost += item.cost * item.getJoinPlan().cost;
    }
    return item;
}
Also used : SortOrder(com.wplatform.ddal.result.SortOrder) IndexCondition(com.wplatform.ddal.dbobject.index.IndexCondition)

Aggregations

SortOrder (com.wplatform.ddal.result.SortOrder)4 SelectOrderBy (com.wplatform.ddal.command.dml.SelectOrderBy)1 IndexCondition (com.wplatform.ddal.dbobject.index.IndexCondition)1 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)1 Value (com.wplatform.ddal.value.Value)1