Search in sources :

Example 21 with MySQLOutPutException

use of com.actiontech.dble.plan.common.exception.MySQLOutPutException in project dble by actiontech.

the class MySQLItemVisitor method endVisit.

@Override
public void endVisit(SQLMethodInvokeExpr x) {
    List<Item> args = visitExprList(x.getParameters());
    String funcName = x.getMethodName().toUpperCase();
    Map<String, Object> attributes = x.getAttributes();
    switch(funcName) {
        case "TRIM":
            if (attributes == null) {
                item = new ItemFuncTrim(args.get(0), TrimTypeEnum.DEFAULT);
            } else {
                TrimTypeEnum trimType = TrimTypeEnum.DEFAULT;
                String type = (String) attributes.get(ItemFuncKeyWord.TRIM_TYPE);
                if (type != null) {
                    trimType = TrimTypeEnum.valueOf(type);
                }
                if (attributes.get(ItemFuncKeyWord.FROM) == null) {
                    item = new ItemFuncTrim(args.get(0), trimType);
                } else {
                    SQLCharExpr from = (SQLCharExpr) attributes.get(ItemFuncKeyWord.FROM);
                    item = new ItemFuncTrim(args.get(0), getItem(from), trimType);
                }
            }
            break;
        case "CONVERT":
            if (args.size() >= 2) {
                throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "not supported  CONVERT(expr, type) ,please use CAST(expr AS type)");
            }
            if (attributes == null || attributes.get(ItemFuncKeyWord.USING) == null) {
                throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "CONVERT(... USING ...) is standard SQL syntax");
            }
            item = new ItemFuncConvCharset(args.get(0), (String) attributes.get(ItemFuncKeyWord.USING));
            break;
        case "CHAR":
            if (attributes == null || attributes.get(ItemFuncKeyWord.USING) == null) {
                attributes = x.getParameters().get(0).getAttributes();
            }
            if (attributes == null || attributes.get(ItemFuncKeyWord.USING) == null) {
                item = new ItemFuncChar(args, this.charsetIndex);
            } else {
                item = new ItemFuncChar(args, (String) attributes.get(ItemFuncKeyWord.USING));
            }
            break;
        case "ORD":
            item = new ItemFuncOrd(args, this.charsetIndex);
            break;
        case "ADDDATE":
            if (x.getParameters().get(1) instanceof SQLIntegerExpr) {
                item = new ItemDateAddInterval(args.get(0), args.get(1), MySqlIntervalUnit.DAY, false);
                break;
            }
        // fallthrough
        case "DATE_ADD":
            MySqlIntervalExpr intervalExpr = (MySqlIntervalExpr) (x.getParameters().get(1));
            item = new ItemDateAddInterval(args.get(0), getItem(intervalExpr.getValue()), getIntervalUnit(x.getParameters().get(1)), false);
            break;
        case "SUBDATE":
            if (x.getParameters().get(1) instanceof SQLIntegerExpr) {
                item = new ItemDateAddInterval(args.get(0), args.get(1), MySqlIntervalUnit.DAY, true);
                break;
            }
        // fallthrough
        case "DATE_SUB":
            MySqlIntervalExpr valueExpr = (MySqlIntervalExpr) (x.getParameters().get(1));
            item = new ItemDateAddInterval(args.get(0), getItem(valueExpr.getValue()), getIntervalUnit(x.getParameters().get(1)), true);
            break;
        case "TIMESTAMPADD":
            SQLIdentifierExpr addUnit = (SQLIdentifierExpr) x.getParameters().get(0);
            item = new ItemDateAddInterval(args.get(2), args.get(1), MySqlIntervalUnit.valueOf(addUnit.getSimpleName()), false);
            break;
        case "TIMESTAMPDIFF":
            SQLIdentifierExpr diffUnit = (SQLIdentifierExpr) x.getParameters().get(0);
            item = new ItemFuncTimestampDiff(args.get(1), args.get(2), MySqlIntervalUnit.valueOf(diffUnit.getSimpleName()));
            break;
        case "VAR_SAMP":
            item = new ItemSumVariance(args, 1, false, null);
            break;
        case "VAR_POP":
        case "VARIANCE":
            item = new ItemSumVariance(args, 0, false, null);
            break;
        case "STD":
        case "STDDEV":
        case "STDDEV_POP":
            item = new ItemSumStd(args, 0, false, null);
            break;
        case "STDDEV_SAMP":
            item = new ItemSumStd(args, 1, false, null);
            break;
        case "BIT_AND":
            item = new ItemSumAnd(args, false, null);
            break;
        case "BIT_OR":
            item = new ItemSumOr(args, false, null);
            break;
        case "BIT_XOR":
            item = new ItemSumXor(args, false, null);
            break;
        case "IF":
            item = new ItemFuncIf(args);
            break;
        case "GET_FORMAT":
            SQLExpr expr = x.getParameters().get(0);
            if (expr instanceof SQLIdentifierExpr) {
                Item arg0 = new ItemString(((SQLIdentifierExpr) expr).getName());
                args.set(0, arg0);
            } else {
                throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '" + expr.toString() + "'");
            }
            item = ItemCreate.getInstance().createNativeFunc(funcName, args);
            break;
        default:
            if (ItemCreate.getInstance().isNativeFunc(funcName)) {
                item = ItemCreate.getInstance().createNativeFunc(funcName, args);
            } else {
                // unKnownFunction
                item = new ItemFuncUnknown(funcName, args);
            }
            initName(x);
    }
    item.setWithSubQuery(getArgsSubQueryStatus(args));
    item.setCorrelatedSubQuery(getArgsCorrelatedSubQueryStatus(args));
}
Also used : ItemFuncUnknown(com.actiontech.dble.plan.common.item.function.unknown.ItemFuncUnknown) MySqlIntervalExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalExpr) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) ItemFuncTimestampDiff(com.actiontech.dble.plan.common.item.function.timefunc.ItemFuncTimestampDiff) TrimTypeEnum(com.actiontech.dble.plan.common.item.function.strfunc.ItemFuncTrim.TrimTypeEnum) ItemFuncTrim(com.actiontech.dble.plan.common.item.function.strfunc.ItemFuncTrim) ItemDateAddInterval(com.actiontech.dble.plan.common.item.function.timefunc.ItemDateAddInterval) ItemFuncChar(com.actiontech.dble.plan.common.item.function.strfunc.ItemFuncChar) ItemFuncConvCharset(com.actiontech.dble.plan.common.item.function.castfunc.ItemFuncConvCharset) ItemFuncIf(com.actiontech.dble.plan.common.item.function.operator.controlfunc.ItemFuncIf) MySQLOutPutException(com.actiontech.dble.plan.common.exception.MySQLOutPutException) ItemFuncOrd(com.actiontech.dble.plan.common.item.function.strfunc.ItemFuncOrd)

Example 22 with MySQLOutPutException

use of com.actiontech.dble.plan.common.exception.MySQLOutPutException 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 23 with MySQLOutPutException

use of com.actiontech.dble.plan.common.exception.MySQLOutPutException in project dble by actiontech.

the class MySQLPlanNodeVisitor method handleWhereCondition.

private void handleWhereCondition(SQLExpr whereExpr) {
    MySQLItemVisitor mev = new MySQLItemVisitor(this.currentDb, this.charsetIndex, this.metaManager);
    whereExpr.accept(mev);
    if (this.tableNode != null) {
        Item whereFilter = mev.getItem();
        tableNode.query(whereFilter);
        if (whereFilter.isWithSubQuery()) {
            tableNode.setSubQuery(true);
            tableNode.setCorrelatedSubQuery(whereFilter.isCorrelatedSubQuery());
        }
    } else {
        throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "from expression is null,check the sql!");
    }
}
Also used : Item(com.actiontech.dble.plan.common.item.Item) MySQLOutPutException(com.actiontech.dble.plan.common.exception.MySQLOutPutException)

Example 24 with MySQLOutPutException

use of com.actiontech.dble.plan.common.exception.MySQLOutPutException in project dble by actiontech.

the class MySQLPlanNodeVisitor method handleGroupBy.

private void handleGroupBy(SQLSelectGroupByClause groupBy) {
    for (SQLExpr p : groupBy.getItems()) {
        if (p instanceof MySqlOrderingExpr) {
            MySqlOrderingExpr groupitem = (MySqlOrderingExpr) p;
            SQLExpr q = groupitem.getExpr();
            MySQLItemVisitor v = new MySQLItemVisitor(this.currentDb, this.charsetIndex, this.metaManager);
            q.accept(v);
            this.tableNode = tableNode.groupBy(v.getItem(), groupitem.getType());
        } else {
            MySQLItemVisitor v = new MySQLItemVisitor(this.currentDb, this.charsetIndex, this.metaManager);
            p.accept(v);
            this.tableNode = tableNode.groupBy(v.getItem(), SQLOrderingSpecification.ASC);
        }
    }
    if (groupBy.isWithRollUp()) {
        throw new MySQLOutPutException(ErrorCode.ER_OPTIMIZER, "", "with rollup is not supported yet!");
    }
    if (groupBy.getHaving() != null) {
        handleHavingCondition(groupBy.getHaving());
    }
}
Also used : MySqlOrderingExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOrderingExpr) MySQLOutPutException(com.actiontech.dble.plan.common.exception.MySQLOutPutException)

Example 25 with MySQLOutPutException

use of com.actiontech.dble.plan.common.exception.MySQLOutPutException in project dble by actiontech.

the class MySQLPlanNodeVisitor method visit.

public void visit(SQLTableSource tables) {
    if (tables instanceof SQLExprTableSource) {
        SQLExprTableSource table = (SQLExprTableSource) tables;
        MySQLPlanNodeVisitor mtv = new MySQLPlanNodeVisitor(this.currentDb, this.charsetIndex, this.metaManager, this.isSubQuery);
        mtv.visit(table);
        this.tableNode = mtv.getTableNode();
        this.containSchema = mtv.isContainSchema();
    } else if (tables instanceof SQLJoinTableSource) {
        SQLJoinTableSource joinTables = (SQLJoinTableSource) tables;
        MySQLPlanNodeVisitor mtv = new MySQLPlanNodeVisitor(this.currentDb, this.charsetIndex, this.metaManager, this.isSubQuery);
        mtv.visit(joinTables);
        this.tableNode = mtv.getTableNode();
        this.containSchema = mtv.isContainSchema();
    } else if (tables instanceof SQLUnionQueryTableSource) {
        if (tables.getAlias() == null) {
            throw new MySQLOutPutException(ErrorCode.ER_DERIVED_MUST_HAVE_ALIAS, "", "Every derived table must have its own alias");
        }
        SQLUnionQueryTableSource unionTables = (SQLUnionQueryTableSource) tables;
        MySQLPlanNodeVisitor mtv = new MySQLPlanNodeVisitor(this.currentDb, this.charsetIndex, this.metaManager, this.isSubQuery);
        mtv.visit(unionTables);
        this.tableNode = new QueryNode(mtv.getTableNode());
        this.containSchema = mtv.isContainSchema();
    } else if (tables instanceof SQLSubqueryTableSource) {
        if (tables.getAlias() == null) {
            throw new MySQLOutPutException(ErrorCode.ER_DERIVED_MUST_HAVE_ALIAS, "", "Every derived table must have its own alias");
        }
        SQLSubqueryTableSource subQueryTables = (SQLSubqueryTableSource) tables;
        MySQLPlanNodeVisitor mtv = new MySQLPlanNodeVisitor(this.currentDb, this.charsetIndex, this.metaManager, this.isSubQuery);
        mtv.visit(subQueryTables);
        this.tableNode = new QueryNode(mtv.getTableNode());
        this.containSchema = mtv.isContainSchema();
    }
    if (tables.getAlias() != null) {
        this.tableNode.setAlias(tables.getAlias());
    }
}
Also used : MySQLOutPutException(com.actiontech.dble.plan.common.exception.MySQLOutPutException)

Aggregations

MySQLOutPutException (com.actiontech.dble.plan.common.exception.MySQLOutPutException)35 Item (com.actiontech.dble.plan.common.item.Item)10 NamedField (com.actiontech.dble.plan.NamedField)9 ItemField (com.actiontech.dble.plan.common.item.ItemField)6 SQLSelectOrderByItem (com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)6 PlanNode (com.actiontech.dble.plan.node.PlanNode)5 ArrayList (java.util.ArrayList)5 Order (com.actiontech.dble.plan.Order)4 PushDownVisitor (com.actiontech.dble.backend.mysql.nio.handler.builder.sqlvisitor.PushDownVisitor)3 ItemInSubQuery (com.actiontech.dble.plan.common.item.subquery.ItemInSubQuery)3 RouteResultsetNode (com.actiontech.dble.route.RouteResultsetNode)3 CastType (com.actiontech.dble.plan.common.CastType)2 ItemString (com.actiontech.dble.plan.common.item.ItemString)2 ItemFuncEqual (com.actiontech.dble.plan.common.item.function.operator.cmpfunc.ItemFuncEqual)2 ItemCondAnd (com.actiontech.dble.plan.common.item.function.operator.logic.ItemCondAnd)2 ItemFuncNot (com.actiontech.dble.plan.common.item.function.operator.logic.ItemFuncNot)2 ItemAllAnySubQuery (com.actiontech.dble.plan.common.item.subquery.ItemAllAnySubQuery)2 JoinNode (com.actiontech.dble.plan.node.JoinNode)2 HandlerBuilder (com.actiontech.dble.backend.mysql.nio.handler.builder.HandlerBuilder)1 DMLResponseHandler (com.actiontech.dble.backend.mysql.nio.handler.query.DMLResponseHandler)1