Search in sources :

Example 26 with Item

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

the class TestMySQLItemVisitor method testOrderby.

@Test
public void testOrderby() {
    MySqlSelectQueryBlock query = getQuery("select col1,col2  from table1 order by col1 asc, col2 desc ");
    SQLOrderBy orderBy = query.getOrderBy();
    int i = 0;
    for (SQLSelectOrderByItem p : orderBy.getItems()) {
        i++;
        String orderCol = "col" + i;
        SQLExpr expr = p.getExpr();
        MySQLItemVisitor v = new MySQLItemVisitor(this.currentDb, utf8Charset, null);
        expr.accept(v);
        Item item = v.getItem();
        Assert.assertEquals(true, orderCol.equals(item.getItemName()));
    }
}
Also used : Item(com.actiontech.dble.plan.common.item.Item) SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) Test(org.junit.Test)

Example 27 with Item

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

the class ItemFuncCase method findItem.

/**
 * Find and return matching items for CASE or ELSE item if all compares are
 * failed or NULL if ELSE item isn't defined.
 * <p>
 * IMPLEMENTATION In order to do correct comparisons of the CASE expression
 * (the expression between CASE and the first WHEN) with each WHEN
 * expression several comparators are used. One for each result type. CASE
 * expression can be evaluated up to # of different result types are used.
 * To check whether the CASE expression already was evaluated for a
 * particular result type a bit mapped variable value_added_map is used.
 * Result types are mapped to it according to their int values i.e.
 * STRING_RESULT is mapped to bit 0, REAL_RESULT to bit 1, so on.
 *
 * @retval NULL Nothing found and there is no ELSE expression defined
 * @retval item Found item or ELSE item if defined and all comparisons are
 * failed
 */
private Item findItem() {
    if (firstExprNum == -1) {
        for (int i = 0; i < ncases; i += 2) {
            // No expression between CASE and the first WHEN
            if (args.get(i).valBool())
                return args.get(i + 1);
            continue;
        }
    } else {
        /* Compare every WHEN argument with it and return the first match */
        Item leftCmpItem = args.get(firstExprNum);
        if (leftCmpItem.isNull() || leftCmpItem.type() == ItemType.NULL_ITEM) {
            return elseExprNum != -1 ? args.get(elseExprNum) : null;
        }
        for (int i = 0; i < ncases; i += 2) {
            if (args.get(i).type() == ItemType.NULL_ITEM)
                continue;
            Item rightCmpItem = args.get(i);
            ArgComparator comparator = new ArgComparator(leftCmpItem, rightCmpItem);
            comparator.setCmpFunc(null, leftCmpItem, rightCmpItem, false);
            if (comparator.compare() == 0 && !rightCmpItem.isNullValue())
                return args.get(i + 1);
        }
    }
    // No, WHEN clauses all missed, return ELSE expression
    return elseExprNum != -1 ? args.get(elseExprNum) : null;
}
Also used : Item(com.actiontech.dble.plan.common.item.Item) ArgComparator(com.actiontech.dble.plan.common.item.function.operator.cmpfunc.util.ArgComparator)

Example 28 with Item

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

the class ItemFuncCase method valDecimal.

@Override
public BigDecimal valDecimal() {
    Item item = findItem();
    if (item == null) {
        nullValue = true;
        return null;
    }
    BigDecimal res = item.valDecimal();
    nullValue = item.isNullValue();
    return res;
}
Also used : Item(com.actiontech.dble.plan.common.item.Item) BigDecimal(java.math.BigDecimal)

Example 29 with Item

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

the class ItemFuncCase method toExpression.

// @Override
// protected Item cloneStruct() {
// List<Item> newArgList = cloneStructList(args);
// return new Item_func_case(newArgList, ncases, first_expr_num,
// else_expr_num);
// }
@Override
public SQLExpr toExpression() {
    SQLCaseExpr caseExpr = new SQLCaseExpr();
    List<SQLExpr> exprList = toExpressionList(args);
    for (int index = 0; index < ncases; ) {
        SQLExpr exprCond = exprList.get(index++);
        SQLExpr exprValue = exprList.get(index++);
        SQLCaseExpr.Item item = new SQLCaseExpr.Item(exprCond, exprValue);
        caseExpr.addItem(item);
    }
    if (firstExprNum > 0) {
        caseExpr.setValueExpr(exprList.get(firstExprNum));
    }
    if (elseExprNum > 0) {
        caseExpr.setElseExpr(exprList.get(elseExprNum));
    }
    return caseExpr;
}
Also used : Item(com.actiontech.dble.plan.common.item.Item) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLCaseExpr(com.alibaba.druid.sql.ast.expr.SQLCaseExpr)

Example 30 with Item

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

the class ItemFuncIf method valReal.

@Override
public BigDecimal valReal() {
    Item arg = args.get(0).valBool() ? args.get(1) : args.get(2);
    BigDecimal value = arg.valReal();
    nullValue = arg.isNullValue();
    return value;
}
Also used : Item(com.actiontech.dble.plan.common.item.Item) BigDecimal(java.math.BigDecimal)

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