Search in sources :

Example 16 with Item

use of com.actiontech.dble.plan.common.item.Item 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 17 with Item

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

the class HandlerTool method createSumItem.

/**
 * @param f
 * @param fields
 * @param startIndex
 * @param allPushDown
 * @param type
 * @return
 */
private static ItemSum createSumItem(ItemSum f, List<Field> fields, int startIndex, boolean allPushDown, HandlerType type) {
    ItemSum 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 = (ItemSum) 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) ItemSum(com.actiontech.dble.plan.common.item.function.sumfunc.ItemSum) ItemInt(com.actiontech.dble.plan.common.item.ItemInt) ArrayList(java.util.ArrayList)

Example 18 with Item

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

the class ViewMeta method setFieldsAlias.

private void setFieldsAlias(PlanNode selNode) throws Exception {
    if (viewColumnMeta != null) {
        // check if the column number of view is same as the selectList in selectStatement
        if (viewColumnMeta.size() != selNode.getColumnsSelected().size()) {
            // return error
            throw new Exception("The Column_list Size and Select_statement Size Not Match");
        }
        for (int i = 0; i < viewColumnMeta.size(); i++) {
            selNode.getColumnsSelected().get(i).setAlias(viewColumnMeta.get(i).trim());
        }
    } else {
        List<Item> selectList = selNode.getColumnsSelected();
        Set<String> tempMap = new HashSet<String>();
        for (Item t : selectList) {
            if (t.getAlias() != null) {
                if (tempMap.contains(t.getAlias())) {
                    throw new Exception("Duplicate column name '" + t.getItemName() + "'");
                }
                tempMap.add(t.getAlias());
            } else {
                if (tempMap.contains(t.getItemName())) {
                    throw new Exception("Duplicate column name '" + t.getItemName() + "'");
                }
                tempMap.add(t.getItemName());
            }
        }
    }
}
Also used : Item(com.actiontech.dble.plan.common.item.Item)

Example 19 with Item

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

the class MySQLcom method aggResultType.

public static ItemResult aggResultType(List<Item> items, int startIndex, int size) {
    ItemResult type = ItemResult.STRING_RESULT;
    /* Skip beginning NULL items */
    int index = 0, indexEnd;
    Item item;
    for (index = startIndex, indexEnd = startIndex + size; index < indexEnd; index++) {
        item = items.get(index);
        if (item.type() != ItemType.NULL_ITEM) {
            type = item.resultType();
            index++;
            break;
        }
    }
    /* Combine result types. Note: NULL items don't affect the result */
    for (; index < indexEnd; index++) {
        item = items.get(index);
        if (item.type() != ItemType.NULL_ITEM)
            type = itemStoreType(type, item);
    }
    return type;
}
Also used : Item(com.actiontech.dble.plan.common.item.Item) ItemResult(com.actiontech.dble.plan.common.item.Item.ItemResult)

Example 20 with Item

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

the class ItemFunc method countRealLength.

/**
 * Set max_length/decimals of function if function is floating point and
 * result length/precision depends on argument ones.
 */
public void countRealLength() {
    int length = 0;
    decimals = 0;
    maxLength = 0;
    for (Item arg : args) {
        if (decimals != NOT_FIXED_DEC) {
            decimals = Math.max(decimals, arg.getDecimals());
            length = Math.max(length, arg.getMaxLength() - arg.getDecimals());
        }
        maxLength = Math.max(maxLength, arg.getMaxLength());
    }
    if (decimals != NOT_FIXED_DEC) {
        maxLength = length;
        length += decimals;
        if (// If previous operation gave overflow
        length < maxLength)
            maxLength = Integer.MAX_VALUE;
        else
            maxLength = length;
    }
}
Also used : Item(com.actiontech.dble.plan.common.item.Item)

Aggregations

Item (com.actiontech.dble.plan.common.item.Item)122 ArrayList (java.util.ArrayList)26 Order (com.actiontech.dble.plan.Order)16 PlanNode (com.actiontech.dble.plan.node.PlanNode)14 MySQLOutPutException (com.actiontech.dble.plan.common.exception.MySQLOutPutException)10 ItemField (com.actiontech.dble.plan.common.item.ItemField)10 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)7 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)7 Test (org.junit.Test)7 FieldPacket (com.actiontech.dble.net.mysql.FieldPacket)6 ItemFuncEqual (com.actiontech.dble.plan.common.item.function.operator.cmpfunc.ItemFuncEqual)6 ItemSum (com.actiontech.dble.plan.common.item.function.sumfunc.ItemSum)6 JoinNode (com.actiontech.dble.plan.node.JoinNode)6 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)6 ItemFunc (com.actiontech.dble.plan.common.item.function.ItemFunc)5 ItemInt (com.actiontech.dble.plan.common.item.ItemInt)4 ItemString (com.actiontech.dble.plan.common.item.ItemString)4 BigDecimal (java.math.BigDecimal)4 RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)3 NamedField (com.actiontech.dble.plan.NamedField)3