Search in sources :

Example 26 with Identifier

use of com.alibaba.cobar.parser.ast.expression.primary.Identifier in project cobar by alibaba.

the class PartitionKeyVisitor method visit.

@Override
public void visit(ComparisionIsExpression node) {
    Expression operand = node.getOperand();
    visitChild(2, false, false, operand);
    if (verdictColumn && (operand instanceof Identifier)) {
        Identifier col = (Identifier) operand;
        String table = tableAlias.get(col.getLevelUnescapeUpName(2));
        if (isRuledColumn(table, col.getIdTextUpUnescape())) {
            switch(node.getMode()) {
                case ComparisionIsExpression.IS_FALSE:
                    addColumnValue(table, col.getIdTextUpUnescape(), LiteralBoolean.FALSE, node, null);
                    break;
                case ComparisionIsExpression.IS_TRUE:
                    addColumnValue(table, col.getIdTextUpUnescape(), LiteralBoolean.TRUE, node, null);
                    break;
                case ComparisionIsExpression.IS_NULL:
                    addColumnValue(table, col.getIdTextUpUnescape(), null, node, null);
                    break;
            }
        }
    }
}
Also used : Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) BetweenAndExpression(com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression) ComparisionEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) FunctionExpression(com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression) MatchExpression(com.alibaba.cobar.parser.ast.expression.primary.MatchExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) ReplacableExpression(com.alibaba.cobar.parser.ast.expression.ReplacableExpression) LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) UnaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression) ComparisionNullSafeEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression) LikeExpression(com.alibaba.cobar.parser.ast.expression.string.LikeExpression) CollateExpression(com.alibaba.cobar.parser.ast.expression.type.CollateExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) UserExpression(com.alibaba.cobar.parser.ast.expression.misc.UserExpression) PolyadicOperatorExpression(com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression) BinaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString)

Example 27 with Identifier

use of com.alibaba.cobar.parser.ast.expression.primary.Identifier in project cobar by alibaba.

the class PartitionKeyVisitor method visit.

@Override
public void visit(DDLDropTableStatement node) {
    visitChild(1, false, false, node.getTableNames());
    List<Identifier> tbs = node.getTableNames();
    if (tbs != null) {
        for (Identifier tb : tbs) {
            addTable(tb.getIdTextUpUnescape());
        }
    }
}
Also used : Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier)

Example 28 with Identifier

use of com.alibaba.cobar.parser.ast.expression.primary.Identifier in project cobar by alibaba.

the class PartitionKeyVisitor method visit.

@Override
public void visit(DMLDeleteStatement node) {
    TableReference tr = node.getTableRefs();
    List<Identifier> tbs = node.getTableNames();
    if (tr == null) {
        Identifier table = tbs.get(0);
        tableAsTableFactor(table);
    } else {
        visitChild(1, verdictColumn, false, tr);
        for (Identifier tb : tbs) {
            if (tb instanceof Wildcard) {
                int trim = tb.trimParent(2, trimSchema);
                schemaTrimmed = schemaTrimmed || trim == Identifier.PARENT_TRIMED;
                customedSchema = customedSchema || trim == Identifier.PARENT_IGNORED;
            } else {
                int trim = tb.trimParent(1, trimSchema);
                schemaTrimmed = schemaTrimmed || trim == Identifier.PARENT_TRIMED;
                customedSchema = customedSchema || trim == Identifier.PARENT_IGNORED;
            }
        }
    }
    Expression where = node.getWhereCondition();
    visitChild(2, verdictColumn, false, where);
    if (tr == null) {
        OrderBy order = node.getOrderBy();
        visitChild(2, false, false, order);
    }
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) TableReference(com.alibaba.cobar.parser.ast.fragment.tableref.TableReference) Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) Wildcard(com.alibaba.cobar.parser.ast.expression.primary.Wildcard) BetweenAndExpression(com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression) ComparisionEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) FunctionExpression(com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression) MatchExpression(com.alibaba.cobar.parser.ast.expression.primary.MatchExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) ReplacableExpression(com.alibaba.cobar.parser.ast.expression.ReplacableExpression) LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) UnaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression) ComparisionNullSafeEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression) LikeExpression(com.alibaba.cobar.parser.ast.expression.string.LikeExpression) CollateExpression(com.alibaba.cobar.parser.ast.expression.type.CollateExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) UserExpression(com.alibaba.cobar.parser.ast.expression.misc.UserExpression) PolyadicOperatorExpression(com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression) BinaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) IndexHint(com.alibaba.cobar.parser.ast.fragment.tableref.IndexHint)

Example 29 with Identifier

use of com.alibaba.cobar.parser.ast.expression.primary.Identifier in project cobar by alibaba.

the class PartitionKeyVisitor method visit.

@Override
public void visit(BetweenAndExpression node) {
    Expression fst = node.getFirst();
    Expression snd = node.getSecond();
    Expression trd = node.getThird();
    visitChild(2, false, false, fst, snd, trd);
    if (verdictColumn && !node.isNot() && fst instanceof Identifier) {
        Identifier col = (Identifier) fst;
        String table = tableAlias.get(col.getLevelUnescapeUpName(2));
        if (isRuledColumn(table, col.getIdTextUpUnescape())) {
            Object e1 = snd.evaluation(evaluationParameter);
            Object e2 = trd.evaluation(evaluationParameter);
            if (e1 != Expression.UNEVALUATABLE && e2 != Expression.UNEVALUATABLE && e1 != null && e2 != null) {
                if (compareEvaluatedValue(e1, e2)) {
                    addColumnValue(table, col.getIdTextUpUnescape(), e1, node, null);
                }
            }
        }
    }
}
Also used : Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) BetweenAndExpression(com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression) ComparisionEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) FunctionExpression(com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression) MatchExpression(com.alibaba.cobar.parser.ast.expression.primary.MatchExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) ReplacableExpression(com.alibaba.cobar.parser.ast.expression.ReplacableExpression) LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) UnaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression) ComparisionNullSafeEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression) LikeExpression(com.alibaba.cobar.parser.ast.expression.string.LikeExpression) CollateExpression(com.alibaba.cobar.parser.ast.expression.type.CollateExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) UserExpression(com.alibaba.cobar.parser.ast.expression.misc.UserExpression) PolyadicOperatorExpression(com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression) BinaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString)

Example 30 with Identifier

use of com.alibaba.cobar.parser.ast.expression.primary.Identifier in project cobar by alibaba.

the class MySQLDMLParserTest method testGroupBy.

public void testGroupBy() throws SQLSyntaxErrorException {
    String sql = "group by c1 asc, c2 desc  , c3 with rollup";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDMLParser parser = getDMLParser(lexer);
    GroupBy groupBy = parser.groupBy();
    String output = output2MySQL(groupBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC), new Pair<Expression, SortOrder>(new Identifier(null, "c2"), SortOrder.DESC), new Pair<Expression, SortOrder>(new Identifier(null, "c3"), SortOrder.ASC)), groupBy.getOrderByList());
    Assert.assertEquals("GROUP BY c1, c2 DESC, c3 WITH ROLLUP", output);
    sql = "group by c1 asc, c2 desc  , c3 ";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    groupBy = parser.groupBy();
    output = output2MySQL(groupBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC), new Pair<Expression, SortOrder>(new Identifier(null, "c2"), SortOrder.DESC), new Pair<Expression, SortOrder>(new Identifier(null, "c3"), SortOrder.ASC)), groupBy.getOrderByList());
    Assert.assertEquals("GROUP BY c1, c2 DESC, c3", output);
    sql = "group by c1   ";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    groupBy = parser.groupBy();
    output = output2MySQL(groupBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC)), groupBy.getOrderByList());
    Assert.assertEquals("GROUP BY c1", output);
    sql = "group by c1 asc  ";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    groupBy = parser.groupBy();
    output = output2MySQL(groupBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC)), groupBy.getOrderByList());
    Assert.assertEquals("GROUP BY c1", output);
    sql = "group by c1 desc  ";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    groupBy = parser.groupBy();
    output = output2MySQL(groupBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.DESC)), groupBy.getOrderByList());
    Assert.assertEquals("GROUP BY c1 DESC", output);
    sql = "group by c1 with rollup  ";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    groupBy = parser.groupBy();
    output = output2MySQL(groupBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC)), groupBy.getOrderByList());
    Assert.assertEquals("GROUP BY c1 WITH ROLLUP", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) GroupBy(com.alibaba.cobar.parser.ast.fragment.GroupBy) Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) Pair(com.alibaba.cobar.parser.util.Pair)

Aggregations

Identifier (com.alibaba.cobar.parser.ast.expression.primary.Identifier)50 Expression (com.alibaba.cobar.parser.ast.expression.Expression)28 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)18 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)18 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)14 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)13 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)13 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)13 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)13 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)13 LogicalOrExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression)13 UserExpression (com.alibaba.cobar.parser.ast.expression.misc.UserExpression)13 CaseWhenOperatorExpression (com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression)13 MatchExpression (com.alibaba.cobar.parser.ast.expression.primary.MatchExpression)13 FunctionExpression (com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression)13 LikeExpression (com.alibaba.cobar.parser.ast.expression.string.LikeExpression)13 CollateExpression (com.alibaba.cobar.parser.ast.expression.type.CollateExpression)13 Pair (com.alibaba.cobar.parser.util.Pair)12 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)11 PolyadicOperatorExpression (com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression)11