Search in sources :

Example 16 with Pair

use of com.alibaba.cobar.parser.util.Pair in project cobar by alibaba.

the class MySQLDDLParser method alterTable.

/**
     * token of table name has been consumed
     * 
     * @throws SQLSyntaxErrorException
     */
private DDLAlterTableStatement alterTable(DDLAlterTableStatement stmt) throws SQLSyntaxErrorException {
    TableOptions options = new TableOptions();
    stmt.setTableOptions(options);
    Identifier id = null;
    Identifier id2 = null;
    Identifier id3 = null;
    ColumnDefinition colDef = null;
    IndexDefinition indexDef = null;
    Expression expr = null;
    for (int i = 0; lexer.token() != EOF; ++i) {
        if (i > 0) {
            match(PUNC_COMMA);
        }
        if (tableOptions(options)) {
            continue;
        }
        main_switch: switch(lexer.token()) {
            case KW_CONVERT:
                // | CONVERT TO CHARACTER SET charset_name [COLLATE
                // collation_name]
                lexer.nextToken();
                match(KW_TO);
                match(KW_CHARACTER);
                match(KW_SET);
                id = identifier();
                id2 = null;
                if (lexer.token() == KW_COLLATE) {
                    lexer.nextToken();
                    id2 = identifier();
                }
                stmt.setConvertCharset(new Pair<Identifier, Identifier>(id, id2));
                break main_switch;
            case KW_RENAME:
                // | RENAME [TO] new_tbl_name
                if (lexer.nextToken() == KW_TO) {
                    lexer.nextToken();
                }
                id = identifier();
                stmt.setRenameTo(id);
                break main_switch;
            case KW_DROP:
                drop_switch: switch(lexer.nextToken()) {
                    case KW_INDEX:
                    case KW_KEY:
                        // | DROP {INDEX|KEY} index_name
                        lexer.nextToken();
                        id = identifier();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.DropIndex(id));
                        break drop_switch;
                    case KW_PRIMARY:
                        // | DROP PRIMARY KEY
                        lexer.nextToken();
                        match(KW_KEY);
                        stmt.addAlterSpecification(new DDLAlterTableStatement.DropPrimaryKey());
                        break drop_switch;
                    case IDENTIFIER:
                        // | DROP [COLUMN] col_name
                        id = identifier();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.DropColumn(id));
                        break drop_switch;
                    case KW_COLUMN:
                        // | DROP [COLUMN] col_name
                        lexer.nextToken();
                        id = identifier();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.DropColumn(id));
                        break drop_switch;
                    default:
                        throw new SQLSyntaxErrorException("ALTER TABLE error for DROP");
                }
                break main_switch;
            case KW_CHANGE:
                // [FIRST|AFTER col_name]
                if (lexer.nextToken() == KW_COLUMN) {
                    lexer.nextToken();
                }
                id = identifier();
                id2 = identifier();
                colDef = columnDefinition();
                if (lexer.token() == IDENTIFIER) {
                    if ("FIRST".equals(lexer.stringValueUppercase())) {
                        lexer.nextToken();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.ChangeColumn(id, id2, colDef, null));
                    } else if ("AFTER".equals(lexer.stringValueUppercase())) {
                        lexer.nextToken();
                        id3 = identifier();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.ChangeColumn(id, id2, colDef, id3));
                    } else {
                        stmt.addAlterSpecification(new DDLAlterTableStatement.ChangeColumn(id, id2, colDef));
                    }
                } else {
                    stmt.addAlterSpecification(new DDLAlterTableStatement.ChangeColumn(id, id2, colDef));
                }
                break main_switch;
            case KW_ALTER:
                // DEFAULT}
                if (lexer.nextToken() == KW_COLUMN) {
                    lexer.nextToken();
                }
                id = identifier();
                switch(lexer.token()) {
                    case KW_SET:
                        lexer.nextToken();
                        match(KW_DEFAULT);
                        expr = exprParser.expression();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.AlterColumnDefaultVal(id, expr));
                        break;
                    case KW_DROP:
                        lexer.nextToken();
                        match(KW_DEFAULT);
                        stmt.addAlterSpecification(new DDLAlterTableStatement.AlterColumnDefaultVal(id));
                        break;
                    default:
                        throw new SQLSyntaxErrorException("ALTER TABLE error for ALTER");
                }
                break main_switch;
            case KW_ADD:
                add_switch: switch(lexer.nextToken()) {
                    case IDENTIFIER:
                        // | ADD [COLUMN] col_name column_definition [FIRST | AFTER
                        // col_name ]
                        id = identifier();
                        colDef = columnDefinition();
                        if (lexer.token() == IDENTIFIER) {
                            if ("FIRST".equals(lexer.stringValueUppercase())) {
                                lexer.nextToken();
                                stmt.addAlterSpecification(new DDLAlterTableStatement.AddColumn(id, colDef, null));
                            } else if ("AFTER".equals(lexer.stringValueUppercase())) {
                                lexer.nextToken();
                                id2 = identifier();
                                stmt.addAlterSpecification(new DDLAlterTableStatement.AddColumn(id, colDef, id2));
                            } else {
                                stmt.addAlterSpecification(new DDLAlterTableStatement.AddColumn(id, colDef));
                            }
                        } else {
                            stmt.addAlterSpecification(new DDLAlterTableStatement.AddColumn(id, colDef));
                        }
                        break add_switch;
                    case PUNC_LEFT_PAREN:
                        // | ADD [COLUMN] (col_name column_definition,...)
                        lexer.nextToken();
                        for (int j = 0; lexer.token() != PUNC_RIGHT_PAREN; ++j) {
                            DDLAlterTableStatement.AddColumns addColumns = new DDLAlterTableStatement.AddColumns();
                            stmt.addAlterSpecification(addColumns);
                            if (j > 0) {
                                match(PUNC_COMMA);
                            }
                            id = identifier();
                            colDef = columnDefinition();
                            addColumns.addColumn(id, colDef);
                        }
                        match(PUNC_RIGHT_PAREN);
                        break add_switch;
                    case KW_COLUMN:
                        if (lexer.nextToken() == PUNC_LEFT_PAREN) {
                            // | ADD [COLUMN] (col_name column_definition,...)
                            lexer.nextToken();
                            for (int j = 0; lexer.token() != PUNC_RIGHT_PAREN; ++j) {
                                DDLAlterTableStatement.AddColumns addColumns = new DDLAlterTableStatement.AddColumns();
                                stmt.addAlterSpecification(addColumns);
                                if (j > 0) {
                                    match(PUNC_COMMA);
                                }
                                id = identifier();
                                colDef = columnDefinition();
                                addColumns.addColumn(id, colDef);
                            }
                            match(PUNC_RIGHT_PAREN);
                        } else {
                            // | ADD [COLUMN] col_name column_definition [FIRST |
                            // AFTER col_name ]
                            id = identifier();
                            colDef = columnDefinition();
                            if (lexer.token() == IDENTIFIER) {
                                if ("FIRST".equals(lexer.stringValueUppercase())) {
                                    lexer.nextToken();
                                    stmt.addAlterSpecification(new DDLAlterTableStatement.AddColumn(id, colDef, null));
                                } else if ("AFTER".equals(lexer.stringValueUppercase())) {
                                    lexer.nextToken();
                                    id2 = identifier();
                                    stmt.addAlterSpecification(new DDLAlterTableStatement.AddColumn(id, colDef, id2));
                                } else {
                                    stmt.addAlterSpecification(new DDLAlterTableStatement.AddColumn(id, colDef));
                                }
                            } else {
                                stmt.addAlterSpecification(new DDLAlterTableStatement.AddColumn(id, colDef));
                            }
                        }
                        break add_switch;
                    case KW_INDEX:
                    case KW_KEY:
                        // | ADD {INDEX|KEY} [index_name] [index_type]
                        // (index_col_name,...) [index_option] ...
                        id = null;
                        if (lexer.nextToken() == IDENTIFIER) {
                            id = identifier();
                        }
                        indexDef = indexDefinition();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.AddIndex(id, indexDef));
                        break add_switch;
                    case KW_PRIMARY:
                        // | ADD PRIMARY KEY [index_type] (index_col_name,...)
                        // [index_option] ...
                        lexer.nextToken();
                        match(KW_KEY);
                        indexDef = indexDefinition();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.AddPrimaryKey(indexDef));
                        break add_switch;
                    case KW_UNIQUE:
                        // (index_col_name,...) [index_option] ...
                        switch(lexer.nextToken()) {
                            case KW_INDEX:
                            case KW_KEY:
                                lexer.nextToken();
                        }
                        id = null;
                        if (lexer.token() == IDENTIFIER) {
                            id = identifier();
                        }
                        indexDef = indexDefinition();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.AddUniqueKey(id, indexDef));
                        break add_switch;
                    case KW_FULLTEXT:
                        // (index_col_name,...) [index_option] ...
                        switch(lexer.nextToken()) {
                            case KW_INDEX:
                            case KW_KEY:
                                lexer.nextToken();
                        }
                        id = null;
                        if (lexer.token() == IDENTIFIER) {
                            id = identifier();
                        }
                        indexDef = indexDefinition();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.AddFullTextIndex(id, indexDef));
                        break add_switch;
                    case KW_SPATIAL:
                        // (index_col_name,...) [index_option] ...
                        switch(lexer.nextToken()) {
                            case KW_INDEX:
                            case KW_KEY:
                                lexer.nextToken();
                        }
                        id = null;
                        if (lexer.token() == IDENTIFIER) {
                            id = identifier();
                        }
                        indexDef = indexDefinition();
                        stmt.addAlterSpecification(new DDLAlterTableStatement.AddSpatialIndex(id, indexDef));
                        break add_switch;
                    default:
                        throw new SQLSyntaxErrorException("ALTER TABLE error for ADD");
                }
                break main_switch;
            case IDENTIFIER:
                SpecialIdentifier si = specialIdentifiers.get(lexer.stringValueUppercase());
                if (si != null) {
                    switch(si) {
                        case IMPORT:
                            // | IMPORT TABLESPACE
                            lexer.nextToken();
                            matchIdentifier("TABLESPACE");
                            stmt.setImportTableSpace(true);
                            break main_switch;
                        case DISCARD:
                            // | DISCARD TABLESPACE
                            lexer.nextToken();
                            matchIdentifier("TABLESPACE");
                            stmt.setDiscardTableSpace(true);
                            break main_switch;
                        case ENABLE:
                            // | ENABLE KEYS
                            lexer.nextToken();
                            match(KW_KEYS);
                            stmt.setEnableKeys(true);
                            break main_switch;
                        case DISABLE:
                            // | DISABLE KEYS
                            lexer.nextToken();
                            match(KW_KEYS);
                            stmt.setDisableKeys(true);
                            break main_switch;
                        case MODIFY:
                            // AFTER col_name]
                            if (lexer.nextToken() == KW_COLUMN) {
                                lexer.nextToken();
                            }
                            id = identifier();
                            colDef = columnDefinition();
                            if (lexer.token() == IDENTIFIER) {
                                if ("FIRST".equals(lexer.stringValueUppercase())) {
                                    lexer.nextToken();
                                    stmt.addAlterSpecification(new DDLAlterTableStatement.ModifyColumn(id, colDef, null));
                                } else if ("AFTER".equals(lexer.stringValueUppercase())) {
                                    lexer.nextToken();
                                    id2 = identifier();
                                    stmt.addAlterSpecification(new DDLAlterTableStatement.ModifyColumn(id, colDef, id2));
                                } else {
                                    stmt.addAlterSpecification(new DDLAlterTableStatement.ModifyColumn(id, colDef));
                                }
                            } else {
                                stmt.addAlterSpecification(new DDLAlterTableStatement.ModifyColumn(id, colDef));
                            }
                            break main_switch;
                    }
                }
            default:
                throw new SQLSyntaxErrorException("unknown ALTER specification");
        }
    }
    return stmt;
}
Also used : SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) DDLAlterTableStatement(com.alibaba.cobar.parser.ast.stmt.ddl.DDLAlterTableStatement) TableOptions(com.alibaba.cobar.parser.ast.fragment.ddl.TableOptions) Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) IndexDefinition(com.alibaba.cobar.parser.ast.fragment.ddl.index.IndexDefinition) Pair(com.alibaba.cobar.parser.util.Pair) ColumnDefinition(com.alibaba.cobar.parser.ast.fragment.ddl.ColumnDefinition) Expression(com.alibaba.cobar.parser.ast.expression.Expression)

Example 17 with Pair

use of com.alibaba.cobar.parser.util.Pair in project cobar by alibaba.

the class MySQLDDLParser method ddlStmt.

/**
     * nothing has been pre-consumed
     */
public DDLStatement ddlStmt() throws SQLSyntaxErrorException {
    Identifier idTemp1;
    Identifier idTemp2;
    SpecialIdentifier siTemp;
    switch(lexer.token()) {
        case KW_ALTER:
            boolean ignore = false;
            if (lexer.nextToken() == KW_IGNORE) {
                ignore = true;
                lexer.nextToken();
            }
            switch(lexer.token()) {
                case KW_TABLE:
                    lexer.nextToken();
                    idTemp1 = identifier();
                    DDLAlterTableStatement alterTableStatement = new DDLAlterTableStatement(ignore, idTemp1);
                    return alterTable(alterTableStatement);
                default:
                    throw err("Only ALTER TABLE is supported");
            }
        case KW_CREATE:
            switch(lexer.nextToken()) {
                case KW_UNIQUE:
                case KW_FULLTEXT:
                case KW_SPATIAL:
                    lexer.nextToken();
                case KW_INDEX:
                    lexer.nextToken();
                    idTemp1 = identifier();
                    for (; lexer.token() != KW_ON; lexer.nextToken()) ;
                    lexer.nextToken();
                    idTemp2 = identifier();
                    return new DDLCreateIndexStatement(idTemp1, idTemp2);
                case KW_TABLE:
                    lexer.nextToken();
                    return createTable(false);
                case IDENTIFIER:
                    siTemp = specialIdentifiers.get(lexer.stringValueUppercase());
                    if (siTemp != null) {
                        switch(siTemp) {
                            case TEMPORARY:
                                lexer.nextToken();
                                match(KW_TABLE);
                                return createTable(true);
                            case POLICY:
                                lexer.nextToken();
                                Identifier policyName = identifier();
                                match(PUNC_LEFT_PAREN);
                                ExtDDLCreatePolicy policy = new ExtDDLCreatePolicy(policyName);
                                for (int j = 0; lexer.token() != PUNC_RIGHT_PAREN; ++j) {
                                    if (j > 0) {
                                        match(PUNC_COMMA);
                                    }
                                    Integer id = lexer.integerValue().intValue();
                                    match(LITERAL_NUM_PURE_DIGIT);
                                    Expression val = exprParser.expression();
                                    policy.addProportion(id, val);
                                }
                                match(PUNC_RIGHT_PAREN);
                                return policy;
                        }
                    }
                default:
                    throw err("unsupported DDL for CREATE");
            }
        case KW_DROP:
            switch(lexer.nextToken()) {
                case KW_INDEX:
                    lexer.nextToken();
                    idTemp1 = identifier();
                    match(KW_ON);
                    idTemp2 = identifier();
                    return new DDLDropIndexStatement(idTemp1, idTemp2);
                case KW_TABLE:
                    lexer.nextToken();
                    return dropTable(false);
                case IDENTIFIER:
                    siTemp = specialIdentifiers.get(lexer.stringValueUppercase());
                    if (siTemp != null) {
                        switch(siTemp) {
                            case TEMPORARY:
                                lexer.nextToken();
                                match(KW_TABLE);
                                return dropTable(true);
                            case POLICY:
                                lexer.nextToken();
                                Identifier policyName = identifier();
                                return new ExtDDLDropPolicy(policyName);
                        }
                    }
                default:
                    throw err("unsupported DDL for DROP");
            }
        case KW_RENAME:
            lexer.nextToken();
            match(KW_TABLE);
            idTemp1 = identifier();
            match(KW_TO);
            idTemp2 = identifier();
            List<Pair<Identifier, Identifier>> list;
            if (lexer.token() != PUNC_COMMA) {
                list = new ArrayList<Pair<Identifier, Identifier>>(1);
                list.add(new Pair<Identifier, Identifier>(idTemp1, idTemp2));
                return new DDLRenameTableStatement(list);
            }
            list = new LinkedList<Pair<Identifier, Identifier>>();
            list.add(new Pair<Identifier, Identifier>(idTemp1, idTemp2));
            for (; lexer.token() == PUNC_COMMA; ) {
                lexer.nextToken();
                idTemp1 = identifier();
                match(KW_TO);
                idTemp2 = identifier();
                list.add(new Pair<Identifier, Identifier>(idTemp1, idTemp2));
            }
            return new DDLRenameTableStatement(list);
        case IDENTIFIER:
            SpecialIdentifier si = specialIdentifiers.get(lexer.stringValueUppercase());
            if (si != null) {
                switch(si) {
                    case TRUNCATE:
                        return truncate();
                }
            }
        default:
            throw err("unsupported DDL");
    }
}
Also used : DDLRenameTableStatement(com.alibaba.cobar.parser.ast.stmt.ddl.DDLRenameTableStatement) ExtDDLCreatePolicy(com.alibaba.cobar.parser.ast.stmt.extension.ExtDDLCreatePolicy) DDLAlterTableStatement(com.alibaba.cobar.parser.ast.stmt.ddl.DDLAlterTableStatement) DDLCreateIndexStatement(com.alibaba.cobar.parser.ast.stmt.ddl.DDLCreateIndexStatement) DDLDropIndexStatement(com.alibaba.cobar.parser.ast.stmt.ddl.DDLDropIndexStatement) Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) Expression(com.alibaba.cobar.parser.ast.expression.Expression) ExtDDLDropPolicy(com.alibaba.cobar.parser.ast.stmt.extension.ExtDDLDropPolicy) Pair(com.alibaba.cobar.parser.util.Pair)

Example 18 with Pair

use of com.alibaba.cobar.parser.util.Pair in project cobar by alibaba.

the class PartitionKeyVisitor method visit.

@Override
public void visit(DMLInsertStatement node) {
    insertReplace(node);
    List<Pair<Identifier, Expression>> dup = node.getDuplicateUpdate();
    if (dup != null) {
        ASTNode[] duplist = new ASTNode[dup.size() * 2];
        int i = 0;
        for (Pair<Identifier, Expression> p : dup) {
            Identifier key = null;
            Expression value = null;
            if (p != null) {
                key = p.getKey();
                value = p.getValue();
            }
            duplist[i++] = key;
            duplist[i++] = value;
        }
        visitChild(2, false, false, duplist);
    }
}
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) ASTNode(com.alibaba.cobar.parser.ast.ASTNode) IndexHint(com.alibaba.cobar.parser.ast.fragment.tableref.IndexHint) Pair(com.alibaba.cobar.parser.util.Pair)

Example 19 with Pair

use of com.alibaba.cobar.parser.util.Pair in project cobar by alibaba.

the class PartitionKeyVisitor method visit.

@Override
public void visit(DMLUpdateStatement node) {
    TableReference tr = node.getTableRefs();
    visitChild(1, false, false, tr);
    List<Pair<Identifier, Expression>> assignmentList = node.getValues();
    if (assignmentList != null && !assignmentList.isEmpty()) {
        List<ASTNode> list = new ArrayList<ASTNode>(assignmentList.size() * 2);
        for (Pair<Identifier, Expression> p : assignmentList) {
            if (p == null)
                continue;
            list.add(p.getKey());
            list.add(p.getValue());
        }
        visitChild(2, false, false, list);
    }
    Expression where = node.getWhere();
    visitChild(2, verdictColumn, false, where);
    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) 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) ArrayList(java.util.ArrayList) ASTNode(com.alibaba.cobar.parser.ast.ASTNode) Pair(com.alibaba.cobar.parser.util.Pair)

Example 20 with Pair

use of com.alibaba.cobar.parser.util.Pair in project cobar by alibaba.

the class PartitionKeyVisitor method insertReplace.

private void insertReplace(DMLInsertReplaceStatement node) {
    Identifier table = node.getTable();
    List<Identifier> collist = node.getColumnNameList();
    QueryExpression query = node.getSelect();
    List<RowExpression> rows = node.getRowList();
    tableAsTableFactor(table);
    String tableName = table.getIdTextUpUnescape();
    visitChild(2, false, false, collist);
    if (query != null) {
        query.accept(this);
        return;
    }
    for (RowExpression row : rows) {
        visitChild(2, false, false, row);
    }
    Map<String, List<Object>> colVals = ensureColumnValueByTable(tableName);
    Map<String, Map<Object, Set<Pair<Expression, ASTNode>>>> colValsIndex = ensureColumnValueIndexByTable(tableName);
    if (collist != null) {
        for (int i = 0; i < collist.size(); ++i) {
            String colName = collist.get(i).getIdTextUpUnescape();
            if (isRuledColumn(tableName, colName)) {
                List<Object> valueList = ensureColumnValueList(colVals, colName);
                Map<Object, Set<Pair<Expression, ASTNode>>> valMap = ensureColumnValueIndexObjMap(colValsIndex, colName);
                for (RowExpression row : rows) {
                    Expression expr = row.getRowExprList().get(i);
                    Object value = expr == null ? null : expr.evaluation(evaluationParameter);
                    if (value != Expression.UNEVALUATABLE) {
                        valueList.add(value);
                        addIntoColumnValueIndex(valMap, value, row, node);
                    }
                }
            }
        }
    }
}
Also used : SmallSet(com.alibaba.cobar.util.SmallSet) Set(java.util.Set) HashSet(java.util.HashSet) ShowCharaterSet(com.alibaba.cobar.parser.ast.stmt.dal.ShowCharaterSet) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString) IndexHint(com.alibaba.cobar.parser.ast.fragment.tableref.IndexHint) 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) ASTNode(com.alibaba.cobar.parser.ast.ASTNode) InExpressionList(com.alibaba.cobar.parser.ast.expression.misc.InExpressionList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) Map(java.util.Map) HashMap(java.util.HashMap) Pair(com.alibaba.cobar.parser.util.Pair)

Aggregations

Pair (com.alibaba.cobar.parser.util.Pair)22 Expression (com.alibaba.cobar.parser.ast.expression.Expression)20 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)13 Identifier (com.alibaba.cobar.parser.ast.expression.primary.Identifier)12 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)11 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)10 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)8 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)8 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)8 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)8 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)8 LogicalOrExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression)8 UserExpression (com.alibaba.cobar.parser.ast.expression.misc.UserExpression)8 CaseWhenOperatorExpression (com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression)8 MatchExpression (com.alibaba.cobar.parser.ast.expression.primary.MatchExpression)8 FunctionExpression (com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression)8 LikeExpression (com.alibaba.cobar.parser.ast.expression.string.LikeExpression)8 CollateExpression (com.alibaba.cobar.parser.ast.expression.type.CollateExpression)8 ASTNode (com.alibaba.cobar.parser.ast.ASTNode)6 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)6