Search in sources :

Example 1 with ItemFunc

use of com.actiontech.dble.plan.common.item.function.ItemFunc in project dble by actiontech.

the class HandlerTool method createFunctionItem.

protected static ItemFunc createFunctionItem(ItemFunc f, List<Field> fields, int startIndex, boolean allPushDown, HandlerType type) {
    ItemFunc ret = null;
    List<Item> args = new ArrayList<>();
    for (int index = 0; index < f.getArgCount(); index++) {
        Item arg = f.arguments().get(index);
        Item newArg = null;
        if (arg.isWild())
            newArg = new ItemInt(0);
        else
            newArg = createItem(arg, fields, startIndex, allPushDown, type);
        if (newArg == null)
            throw new RuntimeException("Function argument not found:" + arg);
        args.add(newArg);
    }
    ret = (ItemFunc) f.reStruct(args, allPushDown, fields);
    ret.setItemName(f.getPushDownName() == null ? f.getItemName() : f.getPushDownName());
    return ret;
}
Also used : Item(com.actiontech.dble.plan.common.item.Item) ItemFunc(com.actiontech.dble.plan.common.item.function.ItemFunc) ItemInt(com.actiontech.dble.plan.common.item.ItemInt) ArrayList(java.util.ArrayList)

Example 2 with ItemFunc

use of com.actiontech.dble.plan.common.item.function.ItemFunc 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 3 with ItemFunc

use of com.actiontech.dble.plan.common.item.function.ItemFunc in project dble by actiontech.

the class SelectedProcessor method mergePushOrderBy.

private static void mergePushOrderBy(Item orderSel, List<Item> mergeSelects) {
    if (orderSel instanceof ItemField) {
        if (!mergeSelects.contains(orderSel))
            mergeSelects.add(orderSel);
    } else if (orderSel instanceof ItemFunc) {
        ItemFunc func = (ItemFunc) orderSel;
        if (func.isWithSumFunc()) {
            for (int index = 0; index < func.getArgCount(); index++) {
                Item arg = func.arguments().get(index);
                mergePushOrderBy(arg, mergeSelects);
            }
        } else {
            if (!mergeSelects.contains(func)) {
                mergeSelects.add(func);
            }
            // union's order by must be found from selects
            func.setPushDownName(func.getItemName());
        }
    } else if (orderSel instanceof ItemSum) {
        ItemSum func = (ItemSum) orderSel;
        for (int index = 0; index < func.getArgCount(); index++) {
            Item arg = func.arguments().get(index);
            mergePushOrderBy(arg, mergeSelects);
        }
    }
}
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) ItemField(com.actiontech.dble.plan.common.item.ItemField)

Example 4 with ItemFunc

use of com.actiontech.dble.plan.common.item.function.ItemFunc 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 5 with ItemFunc

use of com.actiontech.dble.plan.common.item.function.ItemFunc in project dble by actiontech.

the class PlanUtil method findJoinKeysAndRemoveIt.

/**
 * Join change the a.id=b.id in where filter in join node into join condition, and remove from where
 */
public static void findJoinKeysAndRemoveIt(List<Item> dnfNode, JoinNode join) {
    if (dnfNode.isEmpty()) {
        return;
    }
    List<Item> joinFilters = new LinkedList<>();
    for (Item subItem : dnfNode) {
        // sinple condition
        if (subItem.type().equals(ItemType.FUNC_ITEM) || subItem.type().equals(ItemType.COND_ITEM)) {
            ItemFunc sub = (ItemFunc) subItem;
            if (!(sub.functype().equals(Functype.EQ_FUNC)))
                continue;
            if (join.isInnerJoin() && isJoinKey((ItemFuncEqual) sub, join)) {
                joinFilters.add(sub);
                join.addJoinFilter((ItemFuncEqual) sub);
            }
        }
    }
    dnfNode.removeAll(joinFilters);
}
Also used : ItemFunc(com.actiontech.dble.plan.common.item.function.ItemFunc)

Aggregations

ItemFunc (com.actiontech.dble.plan.common.item.function.ItemFunc)9 Item (com.actiontech.dble.plan.common.item.Item)5 ItemField (com.actiontech.dble.plan.common.item.ItemField)3 ItemScalarSubQuery (com.actiontech.dble.plan.common.item.subquery.ItemScalarSubQuery)3 ItemSum (com.actiontech.dble.plan.common.item.function.sumfunc.ItemSum)2 ItemInSubQuery (com.actiontech.dble.plan.common.item.subquery.ItemInSubQuery)2 ItemInt (com.actiontech.dble.plan.common.item.ItemInt)1 ItemCondAnd (com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondAnd)1 ItemCondOr (com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondOr)1 ItemExistsSubQuery (com.actiontech.dble.plan.common.item.subquery.ItemExistsSubQuery)1 BoolPtr (com.actiontech.dble.plan.common.ptr.BoolPtr)1 ArrayList (java.util.ArrayList)1