Skip to content

fix Issues: #1524 support hive alter sql : ALTER TABLE name ADD COLUMNS (col type,col2 type.....) #1605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,21 @@ public class AlterExpression {

private boolean hasColumn = false;


private boolean useBrackets=false;

public boolean hasColumn() {
return hasColumn;
}

public boolean useBrackets() {
return useBrackets;
}

public void useBrackets(boolean useBrackets) {
this.useBrackets = useBrackets;
}

public void hasColumn(boolean hasColumn) {
this.hasColumn = hasColumn;
}
Expand Down Expand Up @@ -382,7 +393,7 @@ public void setUk(boolean uk) {
public String toString() {

StringBuilder b = new StringBuilder();

if (operation== AlterOperation.UNSPECIFIC) {
b.append(optionalSpecifier);
} else if (operation== AlterOperation.RENAME_TABLE) {
Expand Down Expand Up @@ -432,7 +443,13 @@ public String toString() {
b.append("COLUMN ");
}
}
if (useBrackets && colDataTypeList.size() == 1){
b.append(" ( ");
}
b.append(PlainSelect.getStringList(colDataTypeList));
if (useBrackets && colDataTypeList.size() == 1 ){
b.append(" ) ");
}
if (colDataTypeList.size() > 1) {
b.append(")");
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -5864,6 +5864,20 @@ AlterExpression AlterExpression():
alterExp.addColDropNotNull( alterExpressionColumnDropNotNull);
}
|
LOOKAHEAD(4) "("
( alterExpressionColumnDataType = AlterExpressionColumnDataType() {
if (alterExp.getOperation()== AlterOperation.ADD ){
alterExp.addColDataType(alterExpressionColumnDataType);
} else if(alterExp.getOperation()== AlterOperation.ALTER){
error_skipto(K_ALTER);
}else if(alterExp.getOperation()== AlterOperation.MODIFY){
error_skipto(K_MODIFY);
}
}
(LOOKAHEAD(2) "," alterExpressionColumnDataType = AlterExpressionColumnDataType() { alterExp.addColDataType(alterExpressionColumnDataType); }) *
)
")" {alterExp.useBrackets(true);}
|
alterExpressionColumnDropDefault = AlterExpressionColumnDropDefault() {
alterExp.addColDropDefault( alterExpressionColumnDropDefault);
}
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ public void testAlterTableAddColumn_ColumnKeyWordImplicit() throws JSQLParserExc
assertEquals("varchar (255)", colDataTypes.get(0).getColDataType().toString());
}


@Test
public void testAlterTableBackBrackets()throws JSQLParserException{
String sql="ALTER TABLE tablename add column (field string comment 'aaaaa')";
Statement statement = CCJSqlParserUtil.parse(sql);
Alter alter=(Alter) statement;
System.out.println(alter.toString());

String sql2="ALTER TABLE tablename add column (field string comment 'aaaaa', field2 string comment 'bbbbb');";
Statement statement2 = CCJSqlParserUtil.parse(sql2);
Alter alter2=(Alter) statement2;
System.out.println(alter2.toString());
}


@Test
public void testAlterTablePrimaryKey() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("ALTER TABLE animals ADD PRIMARY KEY (id)");
Expand Down