Search in sources :

Example 46 with PlanNode

use of com.actiontech.dble.plan.node.PlanNode in project dble by actiontech.

the class ViewMeta method init.

public ErrorPacket init(boolean isReplace) {
    ViewMetaParser viewParser = new ViewMetaParser(createSql);
    try {
        viewParser.parseCreateView(this);
        // check if the select part has
        this.checkDuplicate(viewParser, isReplace);
        SQLSelectStatement selectStatement = (SQLSelectStatement) RouteStrategyFactory.getRouteStrategy().parserSQL(selectSql);
        MySQLPlanNodeVisitor msv = new MySQLPlanNodeVisitor(this.schema, 63, tmManager, true);
        msv.visit(selectStatement.getSelect().getQuery());
        PlanNode selNode = msv.getTableNode();
        selNode.setUpFields();
        // set the view column name into
        this.setFieldsAlias(selNode);
        viewQuery = new QueryNode(selNode);
    } catch (Exception e) {
        // the select part sql is wrong & report the error
        ErrorPacket error = new ErrorPacket();
        error.setMessage(e.getMessage() == null ? "unknow error".getBytes(StandardCharsets.UTF_8) : e.getMessage().getBytes(StandardCharsets.UTF_8));
        error.setErrNo(CREATE_VIEW_ERROR);
        return error;
    }
    return null;
}
Also used : PlanNode(com.actiontech.dble.plan.node.PlanNode) ErrorPacket(com.actiontech.dble.net.mysql.ErrorPacket) QueryNode(com.actiontech.dble.plan.node.QueryNode) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) MySQLPlanNodeVisitor(com.actiontech.dble.plan.visitor.MySQLPlanNodeVisitor)

Example 47 with PlanNode

use of com.actiontech.dble.plan.node.PlanNode in project dble by actiontech.

the class ViewMeta method initAndSet.

public ErrorPacket initAndSet(boolean isReplace) {
    // check the create sql is legal
    // parse sql into three parts
    ViewMetaParser viewParser = new ViewMetaParser(createSql);
    viewParser.parseCreateView(this);
    try {
        if ("".equals(viewName)) {
            throw new Exception("sql not supported ");
        }
        tmManager.addMetaLock(schema, viewName);
        // check if the select part has
        this.checkDuplicate(viewParser, isReplace);
        SQLSelectStatement selectStatement = (SQLSelectStatement) RouteStrategyFactory.getRouteStrategy().parserSQL(selectSql);
        MySQLPlanNodeVisitor msv = new MySQLPlanNodeVisitor(this.schema, 63, tmManager, true);
        msv.visit(selectStatement.getSelect().getQuery());
        PlanNode selNode = msv.getTableNode();
        selNode.setUpFields();
        // set the view column name into
        this.setFieldsAlias(selNode);
        viewQuery = new QueryNode(selNode);
        tmManager.getCatalogs().get(schema).getViewMetas().put(viewName, this);
    } catch (Exception e) {
        // the select part sql is wrong & report the error
        ErrorPacket error = new ErrorPacket();
        error.setMessage(e.getMessage() == null ? "unknown error".getBytes(StandardCharsets.UTF_8) : e.getMessage().getBytes(StandardCharsets.UTF_8));
        error.setErrNo(CREATE_VIEW_ERROR);
        return error;
    } finally {
        tmManager.removeMetaLock(schema, viewName);
    }
    return null;
}
Also used : PlanNode(com.actiontech.dble.plan.node.PlanNode) ErrorPacket(com.actiontech.dble.net.mysql.ErrorPacket) QueryNode(com.actiontech.dble.plan.node.QueryNode) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) MySQLPlanNodeVisitor(com.actiontech.dble.plan.visitor.MySQLPlanNodeVisitor)

Example 48 with PlanNode

use of com.actiontech.dble.plan.node.PlanNode in project dble by actiontech.

the class JoinNodeHandlerBuilder method buildPre.

@Override
public List<DMLResponseHandler> buildPre() {
    List<DMLResponseHandler> pres = new ArrayList<>();
    PlanNode left = node.getLeftNode();
    PlanNode right = node.getRightNode();
    if (node.getStrategy() == JoinNode.Strategy.NESTLOOP) {
        final boolean isLeftSmall = left.getNestLoopFilters() == null;
        final PlanNode tnSmall = isLeftSmall ? left : right;
        final PlanNode tnBig = isLeftSmall ? right : left;
        // prepare the column for sending
        List<Item> keySources = isLeftSmall ? node.getLeftKeys() : node.getRightKeys();
        List<Item> keyToPasses = isLeftSmall ? node.getRightKeys() : node.getLeftKeys();
        // just find one key as filter later, try to choose a simple column(FIELD_ITEM) from toPasses
        int columnIndex = 0;
        for (int index = 0; index < keyToPasses.size(); index++) {
            Item keyToPass = keyToPasses.get(index);
            if (keyToPass.type().equals(ItemType.FIELD_ITEM)) {
                columnIndex = index;
                break;
            }
        }
        final Item keySource = keySources.get(columnIndex);
        final Item keyToPass = keyToPasses.get(columnIndex);
        DMLResponseHandler endHandler = buildJoinChild(tnSmall, isLeftSmall);
        final TempTableHandler tempHandler = new TempTableHandler(getSequenceId(), session, keySource);
        endHandler.setNextHandler(tempHandler);
        tempHandler.setLeft(isLeftSmall);
        pres.add(tempHandler);
        CallBackHandler tempDone = new CallBackHandler() {

            @Override
            public void call() throws Exception {
                Set<String> valueSet = tempHandler.getValueSet();
                buildNestFilters(tnBig, keyToPass, valueSet, tempHandler.getMaxPartSize());
                DMLResponseHandler bigLh = buildJoinChild(tnBig, !isLeftSmall);
                synchronized (tempHandler) {
                    bigLh.setNextHandler(tempHandler.getNextHandler());
                }
                tempHandler.setCreatedHandler(bigLh);
                HandlerBuilder.startHandler(bigLh);
            }
        };
        if (isExplain) {
            buildNestFiltersForExplain(tnBig, keyToPass);
            DMLResponseHandler bigLh = buildJoinChild(tnBig, !isLeftSmall);
            tempHandler.setCreatedHandler(bigLh);
        }
        tempHandler.setTempDoneCallBack(tempDone);
    } else if (node.getStrategy() == JoinNode.Strategy.SORTMERGE) {
        DMLResponseHandler lh = buildJoinChild(left, true);
        pres.add(lh);
        DMLResponseHandler rh = buildJoinChild(right, false);
        pres.add(rh);
    } else {
        throw new MySQLOutPutException(ErrorCode.ER_QUERYHANDLER, "", "strategy [" + node.getStrategy() + "] not implement yet!");
    }
    return pres;
}
Also used : ArrayList(java.util.ArrayList) TempTableHandler(com.actiontech.dble.backend.mysql.nio.handler.query.impl.TempTableHandler) ItemString(com.actiontech.dble.plan.common.item.ItemString) Item(com.actiontech.dble.plan.common.item.Item) PlanNode(com.actiontech.dble.plan.node.PlanNode) CallBackHandler(com.actiontech.dble.backend.mysql.nio.handler.util.CallBackHandler) DMLResponseHandler(com.actiontech.dble.backend.mysql.nio.handler.query.DMLResponseHandler) MySQLOutPutException(com.actiontech.dble.plan.common.exception.MySQLOutPutException)

Example 49 with PlanNode

use of com.actiontech.dble.plan.node.PlanNode in project dble by actiontech.

the class MergeNodeHandlerBuilder method buildPre.

@Override
protected List<DMLResponseHandler> buildPre() {
    List<DMLResponseHandler> pres = new ArrayList<>();
    for (PlanNode child : node.getChildren()) {
        BaseHandlerBuilder builder = hBuilder.getBuilder(session, child, isExplain);
        if (builder.getSubQueryBuilderList().size() > 0) {
            this.getSubQueryBuilderList().addAll(builder.getSubQueryBuilderList());
        }
        DMLResponseHandler ch = builder.getEndHandler();
        pres.add(ch);
    }
    return pres;
}
Also used : PlanNode(com.actiontech.dble.plan.node.PlanNode) DMLResponseHandler(com.actiontech.dble.backend.mysql.nio.handler.query.DMLResponseHandler) ArrayList(java.util.ArrayList)

Example 50 with PlanNode

use of com.actiontech.dble.plan.node.PlanNode in project dble by actiontech.

the class PushDownVisitor method visit.

protected void visit(JoinNode join) {
    if (!isTopQuery) {
        sqlBuilder.append(" ( ");
    }
    if (join.isSubQuery() || isTopQuery) {
        buildSelect(join);
        sqlBuilder.append(" from ");
    }
    PlanNode left = join.getLeftNode();
    MysqlVisitor leftVisitor = new GlobalVisitor(left, false);
    leftVisitor.visit();
    replaceableSqlBuilder.append(leftVisitor.getSql());
    sqlBuilder = replaceableSqlBuilder.getCurrentElement().getSb();
    if (join.getLeftOuter() && join.getRightOuter()) {
        throw new RuntimeException("not supported for full outer join");
    } else if (join.getLeftOuter() && !join.getRightOuter()) {
        sqlBuilder.append(" left");
    } else if (join.getRightOuter() && !join.getLeftOuter()) {
        sqlBuilder.append(" right");
    }
    sqlBuilder.append(" join ");
    PlanNode right = join.getRightNode();
    MysqlVisitor rightVisitor = new GlobalVisitor(right, false);
    rightVisitor.visit();
    replaceableSqlBuilder.append(rightVisitor.getSql());
    sqlBuilder = replaceableSqlBuilder.getCurrentElement().getSb();
    StringBuilder joinOnFilterStr = getJoinOn(join, leftVisitor, rightVisitor);
    sqlBuilder.append(joinOnFilterStr.toString());
    if (join.isSubQuery() || isTopQuery) {
        buildWhere(join, leftVisitor.getWhereFilter(), rightVisitor.getWhereFilter());
        buildGroupBy(join);
        // having may contains aggregate function, so it need to calc by middle-ware
        buildOrderBy(join);
        buildLimit(join, sqlBuilder);
    }
    if (!isTopQuery) {
        sqlBuilder.append(" ) ");
        if (join.getAlias() != null)
            sqlBuilder.append(" ").append(join.getAlias()).append(" ");
    }
}
Also used : PlanNode(com.actiontech.dble.plan.node.PlanNode)

Aggregations

PlanNode (com.actiontech.dble.plan.node.PlanNode)50 JoinNode (com.actiontech.dble.plan.node.JoinNode)16 Item (com.actiontech.dble.plan.common.item.Item)14 ArrayList (java.util.ArrayList)10 QueryNode (com.actiontech.dble.plan.node.QueryNode)8 ItemFuncEqual (com.actiontech.dble.plan.common.item.function.operator.cmpfunc.ItemFuncEqual)6 MySQLOutPutException (com.actiontech.dble.plan.common.exception.MySQLOutPutException)5 MySQLPlanNodeVisitor (com.actiontech.dble.plan.visitor.MySQLPlanNodeVisitor)5 ItemSubQuery (com.actiontech.dble.plan.common.item.subquery.ItemSubQuery)4 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)4 DMLResponseHandler (com.actiontech.dble.backend.mysql.nio.handler.query.DMLResponseHandler)3 NamedField (com.actiontech.dble.plan.NamedField)3 ErrorPacket (com.actiontech.dble.net.mysql.ErrorPacket)2 Order (com.actiontech.dble.plan.Order)2 TableNode (com.actiontech.dble.plan.node.TableNode)2 HashSet (java.util.HashSet)2 BaseHandlerBuilder (com.actiontech.dble.backend.mysql.nio.handler.builder.BaseHandlerBuilder)1 HandlerBuilder (com.actiontech.dble.backend.mysql.nio.handler.builder.HandlerBuilder)1 TempTableHandler (com.actiontech.dble.backend.mysql.nio.handler.query.impl.TempTableHandler)1 CallBackHandler (com.actiontech.dble.backend.mysql.nio.handler.util.CallBackHandler)1