Skip to content

Add support for MySQL ALTER TABLE partition options in parser #2211

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
Apr 2, 2025
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
100 changes: 85 additions & 15 deletions src/main/java/net/sf/jsqlparser/statement/alter/AlterExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package net.sf.jsqlparser.statement.alter;

import java.util.stream.Collectors;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.statement.ReferentialAction;
import net.sf.jsqlparser.statement.ReferentialAction.Action;
Expand Down Expand Up @@ -79,6 +80,11 @@ public class AlterExpression implements Serializable {
private String partitionType;
private Expression partitionExpression;
private List<String> partitionColumns;
private int coalescePartitionNumber;

private String exchangePartitionTableName;
private boolean exchangePartitionWithValidation;
private boolean exchangePartitionWithoutValidation;


public Index getOldIndex() {
Expand Down Expand Up @@ -524,6 +530,38 @@ public List<String> getPartitionColumns() {
return partitionColumns;
}

public void setExchangePartitionTableName(String exchangePartitionTableName) {
this.exchangePartitionTableName = exchangePartitionTableName;
}

public String getExchangePartitionTableName() {
return exchangePartitionTableName;
}

public void setCoalescePartitionNumber(int coalescePartitionNumber) {
this.coalescePartitionNumber = coalescePartitionNumber;
}

public int getCoalescePartitionNumber() {
return coalescePartitionNumber;
}

public void setExchangePartitionWithValidation(boolean exchangePartitionWithValidation) {
this.exchangePartitionWithValidation = exchangePartitionWithValidation;
}

public boolean isExchangePartitionWithValidation() {
return exchangePartitionWithValidation;
}

public void setExchangePartitionWithoutValidation(boolean exchangePartitionWithoutValidation) {
this.exchangePartitionWithoutValidation = exchangePartitionWithoutValidation;
}

public boolean isExchangePartitionWithoutValidation() {
return exchangePartitionWithoutValidation;
}

@Override
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity",
"PMD.ExcessiveMethodLength", "PMD.SwitchStmtsShouldHaveDefault"})
Expand Down Expand Up @@ -628,31 +666,63 @@ public String toString() {
&& !pkColumns.isEmpty()) {
// Oracle Multi Column Drop
b.append("DROP (").append(PlainSelect.getStringList(pkColumns)).append(')');
} else if (operation == AlterOperation.DISCARD_PARTITION && partitions != null) {
b.append("DISCARD PARTITION ").append(PlainSelect.getStringList(partitions));
if (tableOption != null) {
b.append(" ").append(tableOption);
}
} else if (operation == AlterOperation.IMPORT_PARTITION) {
b.append("IMPORT PARTITION ").append(PlainSelect.getStringList(partitions));
if (tableOption != null) {
b.append(" ").append(tableOption);
}
} else if (operation == AlterOperation.TRUNCATE_PARTITION
&& partitions != null) {
b.append("TRUNCATE PARTITION ").append(PlainSelect.getStringList(partitions));
} else if (operation == AlterOperation.COALESCE_PARTITION) {
b.append("COALESCE PARTITION ").append(coalescePartitionNumber);
} else if (operation == AlterOperation.REORGANIZE_PARTITION
&& partitions != null
&& partitionDefinitions != null) {
b.append("REORGANIZE PARTITION ")
.append(PlainSelect.getStringList(partitions))
.append(" INTO (")
.append(partitionDefinitions.stream()
.map(PartitionDefinition::toString)
.collect(Collectors.joining(", ")))
.append(")");
} else if (operation == AlterOperation.EXCHANGE_PARTITION) {
b.append("EXCHANGE PARTITION ");
b.append(partitions.get(0)).append(" WITH TABLE ").append(exchangePartitionTableName);
if (exchangePartitionWithValidation) {
b.append(" WITH VALIDATION ");
} else if (exchangePartitionWithoutValidation) {
b.append(" WITHOUT VALIDATION ");
}
} else if (operation == AlterOperation.ANALYZE_PARTITION && partitions != null) {
b.append("ANALYZE PARTITION ").append(PlainSelect.getStringList(partitions));
} else if (operation == AlterOperation.CHECK_PARTITION && partitions != null) {
b.append("CHECK PARTITION ").append(PlainSelect.getStringList(partitions));
} else if (operation == AlterOperation.OPTIMIZE_PARTITION && partitions != null) {
b.append("OPTIMIZE PARTITION ").append(PlainSelect.getStringList(partitions));
} else if (operation == AlterOperation.REBUILD_PARTITION && partitions != null) {
b.append("REBUILD PARTITION ").append(PlainSelect.getStringList(partitions));
} else if (operation == AlterOperation.REPAIR_PARTITION && partitions != null) {
b.append("REPAIR PARTITION ").append(PlainSelect.getStringList(partitions));
} else if (operation == AlterOperation.REMOVE_PARTITIONING) {
b.append("REMOVE PARTITIONING");
} else if (operation == AlterOperation.PARTITION_BY) {
b.append("PARTITION BY ").append(partitionType).append(" ");
if (partitionExpression != null) {
b.append("(").append(partitionExpression).append(") ");
} else if (partitionColumns != null && !partitionColumns.isEmpty()) {
b.append("COLUMNS(").append(String.join(", ", partitionColumns)).append(") ");
}
b.append("(");
for (int i = 0; i < partitionDefinitions.size(); i++) {
PartitionDefinition partition = partitionDefinitions.get(i);
b.append("PARTITION ").append(partition.getPartitionName())
.append(" ").append(partition.getPartitionOperation())
.append(" (").append(PlainSelect.getStringList(partition.getValues()))
.append(")");
if (partition.getStorageEngine() != null) {
b.append(" ENGINE = ").append(partition.getStorageEngine());
}
if (i < partitionDefinitions.size() - 1) {
b.append(", ");
}
}
b.append(")");

b.append("(").append(partitionDefinitions.stream()
.map(PartitionDefinition::toString)
.collect(Collectors.joining(", ")))
.append(")");
} 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, CONVERT, COLLATE, ALGORITHM, RENAME, RENAME_TABLE, RENAME_INDEX, RENAME_KEY, RENAME_CONSTRAINT, COMMENT, COMMENT_WITH_EQUAL_SIGN, UNSPECIFIC, ADD_PARTITION, DROP_PARTITION, PARTITION_BY, TRUNCATE_PARTITION, SET_TABLE_OPTION, ENGINE, FORCE, LOCK, DISCARD_TABLESPACE, IMPORT_TABLESPACE, DISABLE_KEYS, ENABLE_KEYS;
ADD, ALTER, DROP, DROP_PRIMARY_KEY, DROP_UNIQUE, DROP_FOREIGN_KEY, MODIFY, CHANGE, CONVERT, COLLATE, ALGORITHM, RENAME, RENAME_TABLE, RENAME_INDEX, RENAME_KEY, RENAME_CONSTRAINT, COMMENT, COMMENT_WITH_EQUAL_SIGN, UNSPECIFIC, ADD_PARTITION, DROP_PARTITION, DISCARD_PARTITION, IMPORT_PARTITION, TRUNCATE_PARTITION, COALESCE_PARTITION, REORGANIZE_PARTITION, EXCHANGE_PARTITION, ANALYZE_PARTITION, CHECK_PARTITION, OPTIMIZE_PARTITION, REBUILD_PARTITION, REPAIR_PARTITION, REMOVE_PARTITIONING, PARTITION_BY, SET_TABLE_OPTION, ENGINE, FORCE, LOCK, DISCARD_TABLESPACE, IMPORT_TABLESPACE, DISABLE_KEYS, ENABLE_KEYS;

public static AlterOperation from(String operation) {
return Enum.valueOf(AlterOperation.class, operation.toUpperCase());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.io.Serializable;
import java.util.List;
import net.sf.jsqlparser.statement.select.PlainSelect;

public class PartitionDefinition implements Serializable {
private String partitionName;
Expand Down Expand Up @@ -57,4 +58,17 @@ public String getStorageEngine() {
public void setStorageEngine(String storageEngine) {
this.storageEngine = storageEngine;
}

@Override
public String toString() {
StringBuilder b = new StringBuilder();
b.append("PARTITION ").append(partitionName)
.append(" ").append(partitionOperation)
.append(" (").append(PlainSelect.getStringList(values))
.append(")");
if (storageEngine != null) {
b.append(" ENGINE = ").append(storageEngine);
}
return b.toString();
}
}
Loading
Loading