Skip to content

Commit

Permalink
support oracle alter table truncate partition (#1954)
Browse files Browse the repository at this point in the history
* feat: oracle alter table truncate partition

* feat: oracle alter table truncate partition

* feat: code format

* feat: code format

---------

Co-authored-by: mjh <majh118@chinaunicom.cn>
  • Loading branch information
SheldonKubor and mjh authored Feb 4, 2024
1 parent d9c4449 commit cc7aa01
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public class AlterExpression implements Serializable {

private boolean useBrackets = false;

private String truncatePartitionName = null;

public Index getOldIndex() {
return oldIndex;
}
Expand Down Expand Up @@ -402,6 +404,19 @@ public void setUk(boolean uk) {
this.uk = uk;
}

public String getTruncatePartitionName() {
return truncatePartitionName;
}

public void setTruncatePartitionName(String truncatePartitionName) {
this.truncatePartitionName = truncatePartitionName;
}

public AlterExpression withTruncatePartitionName(String truncatePartitionName) {
this.truncatePartitionName = truncatePartitionName;
return this;
}

@Override
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity",
"PMD.ExcessiveMethodLength", "PMD.SwitchStmtsShouldHaveDefault"})
Expand Down Expand Up @@ -441,6 +456,9 @@ public String toString() {
&& !pkColumns.isEmpty()) {
// Oracle Multi Column Drop
b.append("DROP (").append(PlainSelect.getStringList(pkColumns)).append(')');
} else if (operation == AlterOperation.TRUNCATE_PARTITION
&& truncatePartitionName != null) {
b.append("TRUNCATE PARTITION ").append(truncatePartitionName);
} else {
if (operation == AlterOperation.COMMENT_WITH_EQUAL_SIGN) {
b.append("COMMENT =").append(" ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
package net.sf.jsqlparser.statement.alter;

public enum AlterOperation {
ADD, ALTER, DROP, DROP_PRIMARY_KEY, DROP_UNIQUE, DROP_FOREIGN_KEY, MODIFY, CHANGE, ALGORITHM, RENAME, RENAME_TABLE, RENAME_INDEX, RENAME_KEY, RENAME_CONSTRAINT, COMMENT, COMMENT_WITH_EQUAL_SIGN, UNSPECIFIC;
ADD, ALTER, DROP, DROP_PRIMARY_KEY, DROP_UNIQUE, DROP_FOREIGN_KEY, MODIFY, CHANGE, ALGORITHM, RENAME, RENAME_TABLE, RENAME_INDEX, RENAME_KEY, RENAME_CONSTRAINT, COMMENT, COMMENT_WITH_EQUAL_SIGN, UNSPECIFIC, TRUNCATE_PARTITION;

public static AlterOperation from(String operation) {
return Enum.valueOf(AlterOperation.class, operation.toUpperCase());
Expand Down
3 changes: 3 additions & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -6255,6 +6255,7 @@ AlterExpression AlterExpression():
AlterExpression.ColumnDropNotNull alterExpressionColumnDropNotNull = null;
AlterExpression.ColumnDropDefault alterExpressionColumnDropDefault = null;
ReferentialAction.Action action = null;
String truncatePartitionName = null;

// for captureRest()
List<String> tokens = new LinkedList<String>();
Expand Down Expand Up @@ -6546,6 +6547,8 @@ AlterExpression AlterExpression():
}
)
|
LOOKAHEAD(2) <K_TRUNCATE> <K_PARTITION> { alterExp.setOperation(AlterOperation.TRUNCATE_PARTITION); } truncatePartitionName = RelObjectName() { alterExp.setTruncatePartitionName(truncatePartitionName); }
|
tokens = captureRest() {
alterExp.setOperation(AlterOperation.UNSPECIFIC);
StringBuilder optionalSpecifier = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -981,4 +981,11 @@ public void testAlterColumnSetCommitTimestamp1() throws JSQLParserException {
assertEquals("UPDATE_DATE_TIME_GMT SET OPTIONS (allow_commit_timestamp=true)",
type.toString());
}

@Test
public void testIssue1890() throws JSQLParserException {
String stmt =
"ALTER TABLE xdmiddle.ft_mid_sop_sms_send_list_daily TRUNCATE PARTITION sum_date";
assertSqlCanBeParsedAndDeparsed(stmt);
}
}

0 comments on commit cc7aa01

Please sign in to comment.