Search in sources :

Example 86 with Item

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

the class InSubQueryHandler method fieldEofResponse.

@Override
public void fieldEofResponse(byte[] headerNull, List<byte[]> fieldsNull, List<FieldPacket> fieldPackets, byte[] eofNull, boolean isLeft, BackendConnection conn) {
    if (terminate.get()) {
        return;
    }
    lock.lock();
    try {
        // create field for first time
        if (this.fieldPackets.isEmpty()) {
            this.fieldPackets = fieldPackets;
            sourceField = HandlerTool.createField(this.fieldPackets.get(0));
            Item select = itemSubQuery.getSelect();
            select.setPushDownName(select.getAlias());
            Item tmpItem = HandlerTool.createItem(select, Collections.singletonList(this.sourceField), 0, isAllPushDown(), type());
            itemSubQuery.setFiled(tmpItem);
        }
    } finally {
        lock.unlock();
    }
}
Also used : Item(com.actiontech.dble.plan.common.item.Item)

Example 87 with Item

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

the class HandlerTool method createPushDownGroupBy.

// ------------------------------- helper methods ------------------------
/**
 * 1.count(id) : count(id) = sum[count(id) 0...n];
 * 2.sum(id): sum(id) = sum[sum(id) 0...n];
 * 3.avg(id) avg(id) = sum[sum(id) 0...n]/sum[count(id) 0...n];
 *
 * @param sumFunction aggregate function name
 * @param fields
 * @return
 */
protected static Item createPushDownGroupBy(ItemSum sumFunction, List<Field> fields, int startIndex) {
    String funName = sumFunction.funcName().toUpperCase();
    String colName = sumFunction.getItemName();
    String pdName = sumFunction.getPushDownName();
    Item ret = null;
    List<Item> args = new ArrayList<>();
    if (funName.equalsIgnoreCase("AVG")) {
        String colNameSum = colName.replace(funName + "(", "SUM(");
        String colNameCount = colName.replace(funName + "(", "COUNT(");
        Item sumFuncSum = new ItemField(null, null, colNameSum);
        sumFuncSum.setPushDownName(pdName.replace(MysqlVisitor.getMadeAggAlias(funName), MysqlVisitor.getMadeAggAlias("SUM")));
        Item sumFuncCount = new ItemField(null, null, colNameCount);
        sumFuncCount.setPushDownName(pdName.replace(MysqlVisitor.getMadeAggAlias(funName), MysqlVisitor.getMadeAggAlias("COUNT")));
        Item itemSum = createFieldItem(sumFuncSum, fields, startIndex);
        Item itemCount = createFieldItem(sumFuncCount, fields, startIndex);
        args.add(itemSum);
        args.add(itemCount);
    } else if (funName.equalsIgnoreCase("STD") || funName.equalsIgnoreCase("STDDEV_POP") || funName.equalsIgnoreCase("STDDEV_SAMP") || funName.equalsIgnoreCase("STDDEV") || funName.equalsIgnoreCase("VAR_POP") || funName.equalsIgnoreCase("VAR_SAMP") || funName.equalsIgnoreCase("VARIANCE")) {
        // variance: v[0]:count,v[1]:sum,v[2]:variance(locally)
        String colNameCount = colName.replace(funName + "(", "COUNT(");
        String colNameSum = colName.replace(funName + "(", "SUM(");
        String colNameVar = colName.replace(funName + "(", "VARIANCE(");
        Item sumFuncCount = new ItemField(null, null, colNameCount);
        sumFuncCount.setPushDownName(pdName.replace(MysqlVisitor.getMadeAggAlias(funName), MysqlVisitor.getMadeAggAlias("COUNT")));
        Item sumFuncSum = new ItemField(null, null, colNameSum);
        sumFuncSum.setPushDownName(pdName.replace(MysqlVisitor.getMadeAggAlias(funName), MysqlVisitor.getMadeAggAlias("SUM")));
        Item sumFuncVar = new ItemField(null, null, colNameVar);
        sumFuncVar.setPushDownName(pdName.replace(MysqlVisitor.getMadeAggAlias(funName), MysqlVisitor.getMadeAggAlias("VARIANCE")));
        Item itemCount = createFieldItem(sumFuncCount, fields, startIndex);
        Item itemSum = createFieldItem(sumFuncSum, fields, startIndex);
        Item itemVar = createFieldItem(sumFuncVar, fields, startIndex);
        args.add(itemCount);
        args.add(itemSum);
        args.add(itemVar);
    } else {
        Item subItem = createFieldItem(sumFunction, fields, startIndex);
        args.add(subItem);
    }
    ret = sumFunction.reStruct(args, true, fields);
    ret.setItemName(sumFunction.getPushDownName() == null ? sumFunction.getItemName() : sumFunction.getPushDownName());
    return ret;
}
Also used : Item(com.actiontech.dble.plan.common.item.Item) ArrayList(java.util.ArrayList) ItemField(com.actiontech.dble.plan.common.item.ItemField)

Example 88 with Item

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

the class HandlerTool method getItemListBytes.

public static List<byte[]> getItemListBytes(List<Item> items) {
    List<byte[]> ret = new ArrayList<>(items.size());
    for (Item item : items) {
        byte[] b = item.getRowPacketByte();
        ret.add(b);
    }
    return ret;
}
Also used : Item(com.actiontech.dble.plan.common.item.Item) ArrayList(java.util.ArrayList)

Example 89 with Item

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

the class TestMySQLItemVisitor method testJoinCondition.

@Test
public void testJoinCondition() {
    MySqlSelectQueryBlock query = getQuery("select a.col1,b.col2  from table1 a inner join table2 b on a.id =b.id");
    SQLJoinTableSource from = (SQLJoinTableSource) query.getFrom();
    MySQLItemVisitor v = new MySQLItemVisitor(this.currentDb, utf8Charset, null);
    from.getCondition().accept(v);
    Item item = v.getItem();
    Assert.assertEquals(true, "a.id = b.id".equals(item.getItemName()));
}
Also used : Item(com.actiontech.dble.plan.common.item.Item) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) Test(org.junit.Test)

Example 90 with Item

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

the class TestMySQLItemVisitor method testGroupbyHaving.

@Test
public void testGroupbyHaving() {
    MySqlSelectQueryBlock query = getQuery("select col1  from table1 group by col1 having count(*)>1  ");
    SQLSelectGroupByClause groupBy = query.getGroupBy();
    SQLExpr q = groupBy.getHaving();
    MySQLItemVisitor v = new MySQLItemVisitor(this.currentDb, utf8Charset, null);
    q.accept(v);
    Item item = v.getItem();
    Assert.assertEquals(true, "COUNT(*) > 1".equals(item.getItemName()));
}
Also used : Item(com.actiontech.dble.plan.common.item.Item) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) Test(org.junit.Test)

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