Search in sources :

Example 1 with ItemScalarSubQuery

use of com.actiontech.dble.plan.common.item.subquery.ItemScalarSubQuery in project dble by actiontech.

the class HandlerTool method createItem.

/**
 * create Item, the Item value referenced by field and changed by field changes
 *
 * @param sel
 * @param fields
 * @param type
 * @return
 */
public static Item createItem(Item sel, List<Field> fields, int startIndex, boolean allPushDown, HandlerType type) {
    Item ret;
    if (sel.basicConstItem())
        return sel;
    if (sel instanceof ItemScalarSubQuery) {
        ItemScalarSubQuery scalarSubQuery = (ItemScalarSubQuery) sel;
        if (scalarSubQuery.getPlanNode().type() == PlanNode.PlanNodeType.NONAME) {
            Item result = scalarSubQuery.getSelect();
            result.setItemName(scalarSubQuery.getItemName());
            result.setAlias(scalarSubQuery.getItemName());
            return result;
        } else {
            return scalarSubQuery.getValue();
        }
    }
    Item.ItemType i = sel.type();
    if (i == Item.ItemType.FUNC_ITEM || i == Item.ItemType.COND_ITEM) {
        ItemFunc func = (ItemFunc) sel;
        if (func.getPushDownName() == null || func.getPushDownName().length() == 0) {
            ret = createFunctionItem(func, fields, startIndex, allPushDown, type);
        } else {
            ret = createFieldItem(func, fields, startIndex);
        }
    } else if (i == Item.ItemType.SUM_FUNC_ITEM) {
        ItemSum sumFunc = (ItemSum) sel;
        if (type != HandlerType.GROUPBY) {
            ret = createFieldItem(sumFunc, fields, startIndex);
        } else if (sumFunc.getPushDownName() == null || sumFunc.getPushDownName().length() == 0) {
            ret = createSumItem(sumFunc, fields, startIndex, allPushDown, type);
        } else {
            ret = createPushDownGroupBy(sumFunc, fields, startIndex);
        }
    } else {
        ret = createFieldItem(sel, fields, startIndex);
    }
    ret.fixFields();
    return ret;
}
Also used : Item(com.actiontech.dble.plan.common.item.Item) ItemSum(com.actiontech.dble.plan.common.item.function.sumfunc.ItemSum) ItemFunc(com.actiontech.dble.plan.common.item.function.ItemFunc) ItemScalarSubQuery(com.actiontech.dble.plan.common.item.subquery.ItemScalarSubQuery)

Example 2 with ItemScalarSubQuery

use of com.actiontech.dble.plan.common.item.subquery.ItemScalarSubQuery in project dble by actiontech.

the class PlanUtil method rebuildSubQueryItem.

public static Item rebuildSubQueryItem(Item item) {
    if (!item.isWithSubQuery()) {
        return item;
    }
    BoolPtr reBuild = new BoolPtr(false);
    if (PlanUtil.isCmpFunc(item)) {
        Item res1 = rebuildBoolSubQuery(item, 0, reBuild);
        if (res1 != null) {
            return res1;
        }
        Item res2 = rebuildBoolSubQuery(item, 1, reBuild);
        if (res2 != null) {
            return res2;
        }
    } else if (item instanceof ItemInSubQuery) {
        ItemInSubQuery inSubItem = (ItemInSubQuery) item;
        if (inSubItem.getValue().size() == 0) {
            return genBoolItem(inSubItem.isNeg());
        } else {
            List<Item> args = new ArrayList<>(inSubItem.getValue().size() + 1);
            args.add(inSubItem.getLeftOperand());
            args.addAll(inSubItem.getValue());
            return new ItemFuncIn(args, inSubItem.isNeg());
        }
    } else if (item instanceof ItemExistsSubQuery) {
        ItemExistsSubQuery existsSubQuery = (ItemExistsSubQuery) item;
        Item result = existsSubQuery.getValue();
        if (result == null) {
            return genBoolItem(existsSubQuery.isNot());
        } else {
            return genBoolItem(!existsSubQuery.isNot());
        }
    } else if (item instanceof ItemCondAnd || item instanceof ItemCondOr) {
        for (int index = 0; index < item.getArgCount(); index++) {
            Item rebuildItem = rebuildSubQueryItem(item.arguments().get(index));
            item.arguments().set(index, rebuildItem);
            item.setItemName(null);
        }
    } else if (item instanceof ItemScalarSubQuery) {
        Item result = ((ItemScalarSubQuery) item).getValue();
        if (result == null || result.getResultItem() == null) {
            return new ItemFuncEqual(new ItemInt(1), new ItemInt(0));
        }
        return result.getResultItem();
    }
    if (!reBuild.get() && item instanceof ItemFunc) {
        return rebuildSubQueryFuncItem(item);
    }
    return item;
}
Also used : ItemCondAnd(com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondAnd) ItemExistsSubQuery(com.actiontech.dble.plan.common.item.subquery.ItemExistsSubQuery) ItemCondOr(com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondOr) ItemInSubQuery(com.actiontech.dble.plan.common.item.subquery.ItemInSubQuery) ItemFunc(com.actiontech.dble.plan.common.item.function.ItemFunc) BoolPtr(com.actiontech.dble.plan.common.ptr.BoolPtr) ItemScalarSubQuery(com.actiontech.dble.plan.common.item.subquery.ItemScalarSubQuery)

Example 3 with ItemScalarSubQuery

use of com.actiontech.dble.plan.common.item.subquery.ItemScalarSubQuery in project dble by actiontech.

the class PlanUtil method rebuildSubQueryFuncItem.

private static Item rebuildSubQueryFuncItem(Item item) {
    ItemFunc func = (ItemFunc) item;
    Item itemTmp = item.cloneItem();
    for (int index = 0; index < func.getArgCount(); index++) {
        Item arg = item.arguments().get(index);
        if (arg instanceof ItemScalarSubQuery) {
            Item result = ((ItemScalarSubQuery) arg).getValue();
            if (result == null || result.getResultItem() == null) {
                itemTmp.arguments().set(index, new ItemNull());
            } else {
                itemTmp.arguments().set(index, result.getResultItem());
            }
        } else if (arg instanceof ItemInSubQuery) {
            ItemInSubQuery inSubItem = (ItemInSubQuery) arg;
            if (inSubItem.getValue().size() == 0) {
                itemTmp.arguments().set(index, genBoolItem(inSubItem.isNeg()));
            } else {
                List<Item> newArgs = new ArrayList<>(inSubItem.getValue().size() + 1);
                newArgs.add(inSubItem.getLeftOperand());
                newArgs.addAll(inSubItem.getValue());
                itemTmp.arguments().set(index, new ItemFuncIn(newArgs, inSubItem.isNeg()));
            }
            itemTmp.setItemName(null);
            return itemTmp;
        } else if (arg instanceof ItemFunc) {
            itemTmp.arguments().set(index, rebuildSubQueryItem(arg));
        }
    }
    itemTmp.setItemName(null);
    return itemTmp;
}
Also used : ItemFunc(com.actiontech.dble.plan.common.item.function.ItemFunc) ItemInSubQuery(com.actiontech.dble.plan.common.item.subquery.ItemInSubQuery) ItemScalarSubQuery(com.actiontech.dble.plan.common.item.subquery.ItemScalarSubQuery)

Example 4 with ItemScalarSubQuery

use of com.actiontech.dble.plan.common.item.subquery.ItemScalarSubQuery in project dble by actiontech.

the class MySQLItemVisitor method endVisit.

@Override
public void endVisit(SQLSelectStatement node) {
    SQLSelectQuery sqlSelect = node.getSelect().getQuery();
    item = new ItemScalarSubQuery(currentDb, sqlSelect, metaManager);
}
Also used : SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) ItemScalarSubQuery(com.actiontech.dble.plan.common.item.subquery.ItemScalarSubQuery)

Example 5 with ItemScalarSubQuery

use of com.actiontech.dble.plan.common.item.subquery.ItemScalarSubQuery in project dble by actiontech.

the class SubQueryPreProcessor method findComparisonsSubQueryToJoinNode.

/**
 * http://dev.mysql.com/doc/refman/5.0/en/comparisons-using-subqueries.html
 */
private static PlanNode findComparisonsSubQueryToJoinNode(PlanNode qtn, BoolPtr childTransform) {
    for (int i = 0; i < qtn.getChildren().size(); i++) {
        PlanNode child = qtn.getChildren().get(i);
        qtn.getChildren().set(i, findComparisonsSubQueryToJoinNode(child, childTransform));
    }
    for (Item itemSelect : qtn.getColumnsSelected()) {
        if (itemSelect instanceof ItemScalarSubQuery) {
            ItemScalarSubQuery scalarSubQuery = (ItemScalarSubQuery) itemSelect;
            findComparisonsSubQueryToJoinNode(scalarSubQuery.getPlanNode(), childTransform);
        }
    }
    // having contains sub query
    buildSubQuery(qtn, new SubQueryFilter(), qtn.getHavingFilter(), false, childTransform);
    SubQueryFilter find = new SubQueryFilter();
    find.query = qtn;
    find.filter = null;
    Item where = qtn.getWhereFilter();
    SubQueryFilter result = buildSubQuery(qtn, find, where, false, childTransform);
    if (result != find) {
        // that means where filter only contains sub query,just replace it
        result.query.query(result.filter);
        qtn.query(null);
        // change result.filter and rebuild
        result.query.setUpFields();
        childTransform.set(true);
        return result.query;
    } else {
        if (childTransform.get()) {
            qtn.setUpFields();
        }
        return qtn;
    }
}
Also used : Item(com.actiontech.dble.plan.common.item.Item) PlanNode(com.actiontech.dble.plan.node.PlanNode) ItemScalarSubQuery(com.actiontech.dble.plan.common.item.subquery.ItemScalarSubQuery)

Aggregations

ItemScalarSubQuery (com.actiontech.dble.plan.common.item.subquery.ItemScalarSubQuery)6 ItemFunc (com.actiontech.dble.plan.common.item.function.ItemFunc)3 Item (com.actiontech.dble.plan.common.item.Item)2 ItemInSubQuery (com.actiontech.dble.plan.common.item.subquery.ItemInSubQuery)2 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)2 ItemCondAnd (com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondAnd)1 ItemCondOr (com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondOr)1 ItemSum (com.actiontech.dble.plan.common.item.function.sumfunc.ItemSum)1 ItemExistsSubQuery (com.actiontech.dble.plan.common.item.subquery.ItemExistsSubQuery)1 BoolPtr (com.actiontech.dble.plan.common.ptr.BoolPtr)1 PlanNode (com.actiontech.dble.plan.node.PlanNode)1