Search in sources :

Example 1 with ItemCondOr

use of com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondOr in project dble by actiontech.

the class FilterUtils method createLogicalFilterNoName.

public static ItemCond createLogicalFilterNoName(boolean and, List<Item> subFilters) {
    ItemCond cond = null;
    if (and)
        cond = new ItemCondAnd(subFilters);
    else
        cond = new ItemCondOr(subFilters);
    PlanUtil.refreshReferTables(cond);
    return cond;
}
Also used : ItemCondAnd(com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondAnd) ItemCond(com.actiontech.dble.plan.common.item.function.operator.logic.ItemCond) ItemCondOr(com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondOr)

Example 2 with ItemCondOr

use of com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondOr 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 ItemCondOr

use of com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondOr in project dble by actiontech.

the class FilterUtils method splitFilter.

/**
 * split the filter until filter is 'boolean filer' or 'orfilter'
 *
 * @param filter
 * @return
 */
public static List<Item> splitFilter(Item filter) {
    if (filter == null)
        throw new RuntimeException("check filter before split");
    List<Item> filterList = new ArrayList<>();
    if (filter.type() == Item.ItemType.COND_ITEM) {
        ItemCond cond = (ItemCond) filter;
        if (cond.functype() == Functype.COND_AND_FUNC) {
            for (int index = 0; index < cond.getArgCount(); index++) {
                Item subFilter = cond.arguments().get(index);
                if (subFilter == null)
                    continue;
                List<Item> subSplits = splitFilter(subFilter);
                filterList.addAll(subSplits);
            }
        } else if (cond.functype() == Functype.COND_OR_FUNC) {
            // step1 divide the args into base  part
            Set<List<Item>> saveSet = new HashSet<List<Item>>();
            for (int index = 0; index < cond.getArgCount(); index++) {
                Item subFilter = cond.arguments().get(index);
                if (subFilter == null)
                    continue;
                List<Item> subSplits = splitFilter(subFilter);
                saveSet.add(subSplits);
            }
            Map<String, List<Item>> itemMap = gourpByRefertTable(filter, saveSet);
            for (Map.Entry<String, List<Item>> entry : itemMap.entrySet()) {
                ItemCondOr x = new ItemCondOr(entry.getValue());
                x.getReferTables().addAll(entry.getValue().get(0).getReferTables());
                filterList.add(x);
            }
            filterList.add(cond);
        } else {
            filterList.add(cond);
        }
    } else {
        filterList.add(filter);
    }
    return filterList;
}
Also used : Item(com.actiontech.dble.plan.common.item.Item) ItemCond(com.actiontech.dble.plan.common.item.function.operator.logic.ItemCond) ItemCondOr(com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondOr)

Example 4 with ItemCondOr

use of com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondOr in project dble by actiontech.

the class MySQLItemVisitor method endVisit.

@Override
public void endVisit(SQLBinaryOpExpr x) {
    Item itemLeft = getItem(x.getLeft());
    SQLExpr rightExpr = x.getRight();
    Item itemRight = getItem();
    if (itemRight instanceof ItemInSubQuery && (rightExpr instanceof SQLSomeExpr || rightExpr instanceof SQLAllExpr || rightExpr instanceof SQLAnyExpr)) {
        item = itemRight;
        return;
    }
    switch(x.getOperator()) {
        case Is:
            // is null, or is unknown
            if (itemRight instanceof ItemNull || itemRight instanceof ItemString) {
                item = new ItemFuncIsnull(itemLeft);
            } else if (itemRight instanceof ItemInt) {
                ItemInt itemBool = (ItemInt) itemRight;
                if (itemBool.valInt().longValue() == 1) {
                    // is true
                    item = new ItemFuncIstrue(itemLeft);
                } else {
                    item = new ItemFuncIsfalse(itemLeft);
                }
            } else {
                throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "not support type:" + x.getRight());
            }
            break;
        case IsNot:
            // is not null, or is not unknown
            if (itemRight instanceof ItemNull || itemRight instanceof ItemString) {
                item = new ItemFuncIsnotnull(itemLeft);
            } else if (itemRight instanceof ItemInt) {
                ItemInt itemBool = (ItemInt) itemRight;
                if (itemBool.valInt().longValue() == 1) {
                    // is true
                    item = new ItemFuncIsnottrue(itemLeft);
                } else {
                    item = new ItemFuncIsnotfalse(itemLeft);
                }
            } else {
                throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "not support type:" + x.getRight());
            }
            break;
        case Escape:
            if (itemLeft instanceof ItemFuncLike) {
                // A LIKE B ESCAPE C ,A is "itemLeft"
                SQLBinaryOpExpr like = (SQLBinaryOpExpr) (x.getLeft());
                Item itemLikeLeft = getItem(like.getLeft());
                Item itemLikeRight = getItem(x.getRight());
                boolean isNot = (like.getOperator() == SQLBinaryOperator.NotLike);
                item = new ItemFuncLike(itemLikeLeft, itemLikeRight, itemRight, isNot);
            } else {
                throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "not supported kind expression:" + x.getOperator());
            }
            break;
        case NotLike:
            item = new ItemFuncLike(itemLeft, itemRight, null, true);
            break;
        case Like:
            item = new ItemFuncLike(itemLeft, itemRight, null, false);
            break;
        case Equality:
            item = new ItemFuncEqual(itemLeft, itemRight);
            break;
        case Add:
            item = new ItemFuncPlus(itemLeft, itemRight);
            break;
        case Divide:
            item = new ItemFuncDiv(itemLeft, itemRight);
            break;
        case DIV:
            item = new ItemFuncIntDiv(itemLeft, itemRight);
            break;
        case Mod:
        case Modulus:
            item = new ItemFuncMod(itemLeft, itemRight);
            break;
        case Multiply:
            item = new ItemFuncMul(itemLeft, itemRight);
            break;
        case Subtract:
            item = new ItemFuncMinus(itemLeft, itemRight);
            break;
        case PG_And:
        case BooleanAnd:
            List<Item> argsAnd = new ArrayList<>();
            argsAnd.add(itemLeft);
            argsAnd.add(itemRight);
            item = new ItemCondAnd(argsAnd);
            break;
        case Concat:
        case BooleanOr:
            List<Item> argsOr = new ArrayList<>();
            argsOr.add(itemLeft);
            argsOr.add(itemRight);
            item = new ItemCondOr(argsOr);
            break;
        case BooleanXor:
            item = new ItemFuncXor(itemLeft, itemRight);
            break;
        case BitwiseAnd:
            item = new ItemFuncBitAnd(itemLeft, itemRight);
            break;
        case BitwiseOr:
            item = new ItemFuncBitOr(itemLeft, itemRight);
            break;
        case BitwiseXor:
            item = new ItemFuncBitXor(itemLeft, itemRight);
            break;
        case LeftShift:
            item = new ItemFuncLeftShift(itemLeft, itemRight);
            break;
        case RightShift:
            item = new ItemFuncRightShift(itemLeft, itemRight);
            break;
        case GreaterThan:
            item = new ItemFuncGt(itemLeft, itemRight);
            break;
        case GreaterThanOrEqual:
            item = new ItemFuncGe(itemLeft, itemRight);
            break;
        case NotEqual:
        case LessThanOrGreater:
            item = new ItemFuncNe(itemLeft, itemRight);
            break;
        case LessThan:
            item = new ItemFuncLt(itemLeft, itemRight);
            break;
        case LessThanOrEqual:
            item = new ItemFuncLe(itemLeft, itemRight);
            break;
        case LessThanOrEqualOrGreaterThan:
            item = new ItemFuncStrictEqual(itemLeft, itemRight);
            break;
        case RegExp:
            item = new ItemFuncRegex(itemLeft, itemRight);
            break;
        case NotRegExp:
            item = new ItemFuncRegex(itemLeft, itemRight);
            item = new ItemFuncNot(item);
            break;
        case Assignment:
            throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "not support assignment");
        default:
            throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "not supported kind expression:" + x.getOperator());
    }
    item.setWithSubQuery(itemLeft.isWithSubQuery() || itemRight.isWithSubQuery());
    item.setCorrelatedSubQuery(itemLeft.isCorrelatedSubQuery() || itemRight.isCorrelatedSubQuery());
    initName(x);
}
Also used : ArrayList(java.util.ArrayList) ItemFuncNot(com.actiontech.dble.plan.common.item.function.operator.logic.ItemFuncNot) ItemCondOr(com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondOr) ItemInSubQuery(com.actiontech.dble.plan.common.item.subquery.ItemInSubQuery) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) ItemCondAnd(com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondAnd) ItemFuncXor(com.actiontech.dble.plan.common.item.function.operator.logic.ItemFuncXor) MySQLOutPutException(com.actiontech.dble.plan.common.exception.MySQLOutPutException)

Example 5 with ItemCondOr

use of com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondOr in project dble by actiontech.

the class FilterPreProcessor method convertOrToIn.

/**
 * change single Logicalfilter(or) into in
 *
 * @param filter
 */
private static Item convertOrToIn(Item filter) {
    if (filter == null)
        return null;
    if (filter.type().equals(Item.ItemType.COND_ITEM)) {
        if (filter instanceof ItemCondAnd) {
            ItemCondAnd andFilter = (ItemCondAnd) filter;
            for (int index = 0; index < andFilter.getArgCount(); index++) {
                andFilter.arguments().set(index, convertOrToIn(andFilter.arguments().get(index)));
            }
            andFilter.setItemName(null);
            PlanUtil.refreshReferTables(andFilter);
            return andFilter;
        } else {
            // or
            ItemCondOr orFilter = (ItemCondOr) filter;
            HashMap<Item, Set<Item>> inMap = new HashMap<>();
            List<Item> newSubFilterList = new ArrayList<>();
            for (int index = 0; index < orFilter.getArgCount(); index++) {
                Item subFilter = orFilter.arguments().get(index);
                if (subFilter == null)
                    continue;
                if (subFilter instanceof ItemFuncEqual) {
                    Item a = subFilter.arguments().get(0);
                    Item b = subFilter.arguments().get(1);
                    if (!a.canValued() && b.canValued()) {
                        if (!inMap.containsKey(a))
                            inMap.put(a, new HashSet<Item>());
                        inMap.get(a).add(b);
                    } else {
                        // stop convert
                        return filter;
                    }
                } else {
                    Item subNew = convertOrToIn(subFilter);
                    newSubFilterList.add(subNew);
                }
            }
            for (Map.Entry<Item, Set<Item>> entry : inMap.entrySet()) {
                List<Item> args = new ArrayList<>();
                args.add(entry.getKey());
                args.addAll(entry.getValue());
                ItemFuncIn inItem = new ItemFuncIn(args, false);
                PlanUtil.refreshReferTables(inItem);
                newSubFilterList.add(inItem);
            }
            return FilterUtils.or(newSubFilterList);
        }
    }
    return filter;
}
Also used : ItemCondAnd(com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondAnd) ItemCondOr(com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondOr) Item(com.actiontech.dble.plan.common.item.Item)

Aggregations

ItemCondOr (com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondOr)5 ItemCondAnd (com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondAnd)4 Item (com.actiontech.dble.plan.common.item.Item)2 ItemCond (com.actiontech.dble.plan.common.item.function.operator.logic.ItemCond)2 ItemInSubQuery (com.actiontech.dble.plan.common.item.subquery.ItemInSubQuery)2 MySQLOutPutException (com.actiontech.dble.plan.common.exception.MySQLOutPutException)1 ItemFunc (com.actiontech.dble.plan.common.item.function.ItemFunc)1 ItemFuncNot (com.actiontech.dble.plan.common.item.function.operator.logic.ItemFuncNot)1 ItemFuncXor (com.actiontech.dble.plan.common.item.function.operator.logic.ItemFuncXor)1 ItemExistsSubQuery (com.actiontech.dble.plan.common.item.subquery.ItemExistsSubQuery)1 ItemScalarSubQuery (com.actiontech.dble.plan.common.item.subquery.ItemScalarSubQuery)1 BoolPtr (com.actiontech.dble.plan.common.ptr.BoolPtr)1 SQLSelectOrderByItem (com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)1 ArrayList (java.util.ArrayList)1