Skip to content

Commit

Permalink
增加mysql ALTER COLUMN SET VISIBLE的解析支持 alibaba#5803
Browse files Browse the repository at this point in the history
增加mysql ALTER COLUMN  SET VISIBLE的解析支持 alibaba#5803
  • Loading branch information
lizongbo committed Mar 26, 2024
1 parent bc380a5 commit b891cba
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public class MySqlAlterTableAlterColumn extends MySqlObjectImpl implements SQLAlterTableItem {
private SQLName column;

private String visibleType;
private boolean dropDefault;
private SQLExpr defaultExpr;

Expand All @@ -44,6 +44,14 @@ public void setDropDefault(boolean dropDefault) {
this.dropDefault = dropDefault;
}

public String getVisibleType() {
return visibleType;
}

public void setVisibleType(String visibleType) {
this.visibleType = visibleType;
}

public SQLExpr getDefaultExpr() {
return defaultExpr;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6679,8 +6679,13 @@ else if (lexer.identifierEquals(FnvHash.Constants.PARTITIONS)) {
alterColumn.setColumn(this.exprParser.name());
if (lexer.token() == Token.SET) {
lexer.nextToken();
accept(Token.DEFAULT);
alterColumn.setDefaultExpr(this.exprParser.expr());
if (lexer.identifierEquals("VISIBLE") || lexer.identifierEquals("INVISIBLE")) {
alterColumn.setVisibleType(lexer.stringVal());
lexer.nextToken();
} else {
accept(Token.DEFAULT);
alterColumn.setDefaultExpr(this.exprParser.expr());
}
} else {
accept(Token.DROP);
accept(Token.DEFAULT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4517,6 +4517,9 @@ public boolean visit(MySqlAlterTableAlterColumn x) {
x.getDefaultExpr().accept(this);
} else if (x.isDropDefault()) {
print0(ucase ? " DROP DEFAULT" : " drop default");
} else if (x.getVisibleType() != null) {
print0(ucase ? " SET " : " set ");
print0(ucase ? x.getVisibleType().toUpperCase() : x.getVisibleType().toLowerCase());
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.alibaba.druid.bvt.sql.mysql.issues;

import java.util.List;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.parser.SQLParserUtils;
import com.alibaba.druid.sql.parser.SQLStatementParser;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* @author lizongbo
* @see <a href="https://github.com/alibaba/druid/issues/5803>Issue来源</a>
* @see <a href="https://dev.mysql.com/doc/refman/8.0/en/invisible-indexes.html">Invisible Indexes</a>
* @see <a href="https://dev.mysql.com/doc/refman/8.0/en/alter-table.html">ALTER TABLE Statement</a>
*/
public class Issue5803 {

@Test
public void test_parse_alter_table() {
for (DbType dbType : new DbType[]{
DbType.mysql,
DbType.mariadb,

}) {

for (String sql : new String[]{
"ALTER TABLE t1 ALTER INDEX i_idx INVISIBLE;",
"ALTER TABLE t1 ALTER INDEX i_idx VISIBLE;",
"ALTER TABLE t2 ALTER INDEX j_idx INVISIBLE;",
"ALTER TABLE t2 ALTER INDEX j_idx INVISIBLE;",
"alter table tt ALTER COLUMN c1 SET VISIBLE;",
"alter table tt ALTER COLUMN c1 SET INVISIBLE;",
}) {
System.out.println(dbType + "原始的sql===" + sql);
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
List<SQLStatement> statementList = parser.parseStatementList();
com.alibaba.druid.sql.ast.statement.SQLJoinTableSource ggg;
String sqlGen = statementList.toString();
System.out.println(dbType + "生成的sql===" + sqlGen);
StringBuilder sb = new StringBuilder();
for (SQLStatement statement : statementList) {
sb.append(statement.toString()).append(";");
}
sb.deleteCharAt(sb.length() - 1);
parser = SQLParserUtils.createSQLStatementParser(sb.toString(), dbType);
List<SQLStatement> statementListNew = parser.parseStatementList();
String sqlGenNew = statementList.toString();
System.out.println(dbType + "重新解析再生成的sql===" + sqlGenNew);
assertEquals(statementList.toString(), statementListNew.toString());
}
}
}
}

0 comments on commit b891cba

Please sign in to comment.