Search in sources :

Example 11 with Order

use of com.actiontech.dble.plan.Order in project dble by actiontech.

the class SubQueryPreProcessor method transformInSubQuery.

private static SubQueryFilter transformInSubQuery(SubQueryFilter qtn, ItemInSubQuery filter, BoolPtr childTransform) {
    Item leftColumn = filter.getLeftOperand();
    PlanNode query = filter.getPlanNode();
    query = findComparisonsSubQueryToJoinNode(query, childTransform);
    QueryNode changeQuery = new QueryNode(query);
    String alias = AUTOALIAS + query.getPureName();
    changeQuery.setAlias(alias);
    if (query.getColumnsSelected().size() != 1)
        throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "only support subquery of one column");
    query.setSubQuery(true).setDistinct(true);
    final List<Item> newSelects = qtn.query.getColumnsSelected();
    SubQueryFilter result = new SubQueryFilter();
    Item rightColumn = query.getColumnsSelected().get(0);
    qtn.query.setColumnsSelected(new ArrayList<Item>());
    String rightJoinName = rightColumn.getAlias();
    if (StringUtils.isEmpty(rightJoinName)) {
        if (rightColumn instanceof ItemField) {
            rightJoinName = rightColumn.getItemName();
        } else {
            rightColumn.setAlias(AUTONAME);
            rightJoinName = AUTONAME;
        }
    }
    ItemField rightJoinColumn = new ItemField(null, alias, rightJoinName);
    // rename the left column's table name
    result.query = new JoinNode(qtn.query, changeQuery);
    // leave origin sql to new join node
    result.query.setSql(qtn.query.getSql());
    qtn.query.setSql(null);
    result.query.select(newSelects);
    qtn.query.setSubQuery(false);
    if (!qtn.query.getOrderBys().isEmpty()) {
        List<Order> orderBys = new ArrayList<>();
        orderBys.addAll(qtn.query.getOrderBys());
        result.query.setOrderBys(orderBys);
        qtn.query.getOrderBys().clear();
    }
    if (!qtn.query.getGroupBys().isEmpty()) {
        List<Order> groupBys = new ArrayList<>();
        groupBys.addAll(qtn.query.getGroupBys());
        result.query.setGroupBys(groupBys);
        qtn.query.getGroupBys().clear();
        result.query.having(qtn.query.getHavingFilter());
        qtn.query.having(null);
    }
    if (qtn.query.getLimitFrom() != -1) {
        result.query.setLimitFrom(qtn.query.getLimitFrom());
        qtn.query.setLimitFrom(-1);
    }
    if (qtn.query.getLimitTo() != -1) {
        result.query.setLimitTo(qtn.query.getLimitTo());
        qtn.query.setLimitTo(-1);
    }
    if (filter.isNeg()) {
        ((JoinNode) result.query).setLeftOuterJoin().setNotIn(true);
        ItemFuncEqual joinFilter = FilterUtils.equal(leftColumn, rightJoinColumn);
        ((JoinNode) result.query).addJoinFilter(joinFilter);
        result.filter = null;
    } else {
        Item joinFilter = FilterUtils.equal(leftColumn, rightJoinColumn);
        result.query.query(joinFilter);
        result.filter = joinFilter;
    }
    result.query.setUpFields();
    return result;
}
Also used : Order(com.actiontech.dble.plan.Order) JoinNode(com.actiontech.dble.plan.node.JoinNode) ItemFuncEqual(com.actiontech.dble.plan.common.item.function.operator.cmpfunc.ItemFuncEqual) ArrayList(java.util.ArrayList) ItemField(com.actiontech.dble.plan.common.item.ItemField) Item(com.actiontech.dble.plan.common.item.Item) PlanNode(com.actiontech.dble.plan.node.PlanNode) QueryNode(com.actiontech.dble.plan.node.QueryNode) MySQLOutPutException(com.actiontech.dble.plan.common.exception.MySQLOutPutException)

Example 12 with Order

use of com.actiontech.dble.plan.Order in project dble by actiontech.

the class PlanUtil method orderContains.

/**
 * when order1contains order2,return true, order2's column start from order1's first column
 *
 * @param orders1 orders1
 * @param orders2 orders2
 * @return orderContains
 */
public static boolean orderContains(List<Order> orders1, List<Order> orders2) {
    if (orders1.size() < orders2.size())
        return false;
    else {
        for (int index = 0; index < orders2.size(); index++) {
            Order order2 = orders2.get(index);
            Order order1 = orders1.get(index);
            if (!order2.equals(order1)) {
                return false;
            }
        }
        return true;
    }
}
Also used : Order(com.actiontech.dble.plan.Order)

Example 13 with Order

use of com.actiontech.dble.plan.Order in project dble by actiontech.

the class ToStringUtil method orderListString.

public static String orderListString(List<Order> orderList) {
    if (orderList == null)
        return "null";
    if (orderList.isEmpty())
        return " ";
    boolean isFirst = true;
    StringBuilder sb = new StringBuilder();
    for (Order order : orderList) {
        if (isFirst) {
            isFirst = false;
        } else {
            sb.append(", ");
        }
        sb.append(orderString(order));
    }
    return sb.toString();
}
Also used : Order(com.actiontech.dble.plan.Order)

Example 14 with Order

use of com.actiontech.dble.plan.Order in project dble by actiontech.

the class MySQLItemVisitor method endVisit.

@Override
public void endVisit(SQLAggregateExpr x) {
    List<Item> args = visitExprList(x.getArguments());
    String funcName = x.getMethodName().toUpperCase();
    SQLAggregateOption option = x.getOption();
    boolean isDistinct = option != null;
    switch(funcName) {
        case "MAX":
            item = new ItemSumMax(args, false, null);
            break;
        case "MIN":
            item = new ItemSumMin(args, false, null);
            break;
        case "SUM":
            item = new ItemSumSum(args, isDistinct, false, null);
            break;
        case "AVG":
            item = new ItemSumAvg(args, isDistinct, false, null);
            break;
        case "GROUP_CONCAT":
            SQLOrderBy orderExpr = (SQLOrderBy) x.getAttribute(ItemFuncKeyWord.ORDER_BY);
            List<Order> orderList = null;
            if (orderExpr != null) {
                orderList = new ArrayList<>();
                for (SQLSelectOrderByItem orderItem : orderExpr.getItems()) {
                    Order order = new Order(getItem(orderItem.getExpr()), orderItem.getType());
                    orderList.add(order);
                }
            }
            SQLCharExpr charExpr = (SQLCharExpr) x.getAttribute(ItemFuncKeyWord.SEPARATOR);
            String separator = ",";
            if (charExpr != null) {
                separator = charExpr.getText();
            }
            item = new ItemFuncGroupConcat(args, isDistinct, orderList, separator, false, null);
            break;
        case "COUNT":
            item = new ItemSumCount(args, isDistinct, false, null);
            break;
        case "STDDEV":
            item = new ItemSumStd(args, 0, false, null);
            break;
        default:
            throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "not supported " + funcName);
    }
}
Also used : Order(com.actiontech.dble.plan.Order) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) MySQLOutPutException(com.actiontech.dble.plan.common.exception.MySQLOutPutException)

Example 15 with Order

use of com.actiontech.dble.plan.Order in project dble by actiontech.

the class PlanNode method setUpRefers.

// column refered start
public void setUpRefers(boolean isPushDownNode) {
    sumFuncs.clear();
    referContext.setPushDownNode(isPushDownNode);
    // select
    for (Item sel : columnsSelected) {
        setUpItemRefer(sel);
    }
    if (type() == PlanNodeType.JOIN) {
        JoinNode jn = (JoinNode) this;
        if (!isPushDownNode) {
            for (Item bf : jn.getJoinFilter()) setUpItemRefer(bf);
            setUpItemRefer(jn.getOtherJoinOnFilter());
        }
    }
    // where, pushdown node does 't need where
    if (!isPushDownNode) {
        setUpItemRefer(whereFilter);
    }
    // group by
    for (Order groupBy : groups) {
        setUpItemRefer(groupBy.getItem());
    }
    // having
    setUpItemRefer(havingFilter);
    // order by
    for (Order orderBy : orderBys) {
        setUpItemRefer(orderBy.getItem());
    }
    // make list
    for (List<Item> selSet : columnsReferredCache.asMap().values()) {
        columnsReferList.addAll(selSet);
    }
}
Also used : Order(com.actiontech.dble.plan.Order) Item(com.actiontech.dble.plan.common.item.Item)

Aggregations

Order (com.actiontech.dble.plan.Order)27 Item (com.actiontech.dble.plan.common.item.Item)16 RowDataComparator (com.actiontech.dble.backend.mysql.nio.handler.util.RowDataComparator)4 MySQLOutPutException (com.actiontech.dble.plan.common.exception.MySQLOutPutException)4 ArrayList (java.util.ArrayList)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3 DistinctLocalResult (com.actiontech.dble.backend.mysql.store.DistinctLocalResult)2 FieldPacket (com.actiontech.dble.net.mysql.FieldPacket)2 ItemField (com.actiontech.dble.plan.common.item.ItemField)2 ItemFuncEqual (com.actiontech.dble.plan.common.item.function.operator.cmpfunc.ItemFuncEqual)2 ItemSum (com.actiontech.dble.plan.common.item.function.sumfunc.ItemSum)2 PlanNode (com.actiontech.dble.plan.node.PlanNode)2 SQLSelectOrderByItem (com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)2 DirectGroupByHandler (com.actiontech.dble.backend.mysql.nio.handler.query.impl.groupby.DirectGroupByHandler)1 OrderedGroupByHandler (com.actiontech.dble.backend.mysql.nio.handler.query.impl.groupby.OrderedGroupByHandler)1 ResultStore (com.actiontech.dble.plan.common.external.ResultStore)1 Field (com.actiontech.dble.plan.common.field.Field)1 JoinNode (com.actiontech.dble.plan.node.JoinNode)1 QueryNode (com.actiontech.dble.plan.node.QueryNode)1 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)1