Skip to content

feat: enhance ALTER TABLE support with UNIQUE KEY and INVISIBLE index #2234

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 3 commits into from
Apr 30, 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@
*/
package net.sf.jsqlparser.statement.alter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
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 All @@ -20,9 +30,6 @@
import net.sf.jsqlparser.statement.create.table.PartitionDefinition;
import net.sf.jsqlparser.statement.select.PlainSelect;

import java.io.Serializable;
import java.util.*;

@SuppressWarnings({"PMD.CyclomaticComplexity"})
public class AlterExpression implements Serializable {

Expand Down Expand Up @@ -94,6 +101,7 @@ public class AlterExpression implements Serializable {
private String constraintSymbol;
private boolean enforced;
private String constraintType;
private boolean invisible;

public Index getOldIndex() {
return oldIndex;
Expand Down Expand Up @@ -628,6 +636,14 @@ public void setConstraintType(String constraintType) {
this.constraintType = constraintType;
}

public boolean isInvisible() {
return invisible;
}

public void setInvisible(boolean invisible) {
this.invisible = invisible;
}

@Override
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity",
"PMD.ExcessiveMethodLength", "PMD.SwitchStmtsShouldHaveDefault"})
Expand All @@ -637,17 +653,27 @@ public String toString() {

if (operation == AlterOperation.UNSPECIFIC) {
b.append(optionalSpecifier);
} else if (operation == AlterOperation.ALTER && constraintType != null) {
b.append("ALTER");
b.append(" ").append(constraintType);

if (constraintSymbol != null) {
b.append(" ").append(constraintSymbol);
} else if (operation == AlterOperation.ALTER && constraintType != null
&& constraintSymbol != null) {
// This is for ALTER INDEX ... INVISIBLE
b.append("ALTER ").append(constraintType).append(" ").append(constraintSymbol);

if (invisible) {
b.append(" INVISIBLE");
} else if (!isEnforced()) {
b.append(" NOT ENFORCED");
} else if (enforced) {
b.append(" ENFORCED");
}
if (!isEnforced()) {
b.append(" NOT ");
} else if (operation == AlterOperation.ADD && constraintType != null
&& constraintSymbol != null) {
b.append("ADD CONSTRAINT ").append(constraintType).append(" ").append(constraintSymbol)
.append(" ");

if (index != null && index.getColumnsNames() != null) {
b.append(" ")
.append(PlainSelect.getStringList(index.getColumnsNames(), true, true));
}
b.append(" ENFORCED");
} else if (operation == AlterOperation.ALTER
&& columnDropDefaultList != null && !columnDropDefaultList.isEmpty()) {
b.append("ALTER ");
Expand Down
168 changes: 93 additions & 75 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -8034,89 +8034,107 @@ AlterExpression AlterExpression():
)
|
(
<K_CONSTRAINT> sk3=RelObjectName()
<K_CONSTRAINT>
(
( tk=<K_FOREIGN> tk2=<K_KEY>
columnNames=ColumnsNamesList()
{
fkIndex = new ForeignKeyIndex()
.withName(sk3)
.withType(tk.image + " " + tk2.image)
.withColumnsNames(columnNames);
columnNames = null;
}
<K_REFERENCES> fkTable=Table() [ LOOKAHEAD(2) columnNames=ColumnsNamesList() ]
{
fkIndex.withTable(fkTable).withReferencedColumnNames(columnNames);
alterExp.setIndex(fkIndex);
}

[LOOKAHEAD(2) (<K_ON> (tk=<K_DELETE> | tk=<K_UPDATE>) action = Action()
{ fkIndex.setReferentialAction(ReferentialAction.Type.from(tk.image), action); }
)]
[LOOKAHEAD(2) (<K_ON> (tk=<K_DELETE> | tk=<K_UPDATE>) action = Action()
{ fkIndex.setReferentialAction(ReferentialAction.Type.from(tk.image), action); }
)]
constraints=AlterExpressionConstraintState() { alterExp.setConstraints(constraints); }
)
|
( tk=<K_PRIMARY> tk2=<K_KEY>
columnNames=ColumnsNamesList()
{
index = new NamedConstraint()
.withName(sk3)
.withType(tk.image + " " + tk2.image)
.withColumnsNames(columnNames);
alterExp.setIndex(index);
}
constraints=AlterExpressionConstraintState() { alterExp.setConstraints(constraints); }
[ <K_USING> sk4=RelObjectName() { alterExp.addParameters("USING", sk4); }]
[ LOOKAHEAD(2) index = IndexWithComment(index) { alterExp.setIndex(index); } ]
)
|
LOOKAHEAD(2) (
{ boolean enforced = true; }
[ tk = <K_NOT> { enforced = false; } ]
<K_ENFORCED> {
alterExp.setEnforced(enforced);
alterExp.setConstraintType("CONSTRAINT");
alterExp.setConstraintSymbol(sk3);
}
)
|
LOOKAHEAD(2)
(
<K_CHECK> {Expression exp = null;} (LOOKAHEAD(2) "(" exp = Expression() ")")* {
CheckConstraint checkCs = new CheckConstraint().withName(sk3).withExpression(exp);
alterExp.setIndex(checkCs);
<K_UNIQUE> (<K_KEY> { alterExp.setConstraintType("UNIQUE KEY"); }
| <K_INDEX> { alterExp.setConstraintType("UNIQUE INDEX"); }
| { alterExp.setConstraintType("UNIQUE"); } )
sk3=RelObjectName() {
alterExp.setConstraintSymbol(sk3);
index = new Index();
}
columnNames=ColumnsNamesList() {
index.setColumnsNames(columnNames);
alterExp.setIndex(index);
}
)
|
sk3=RelObjectName()
(
tk=<K_UNIQUE> (tk2=<K_KEY> { alterExp.setUk(true); } | tk2=<K_INDEX>)?
columnNames=ColumnsNamesList()
{
index = new NamedConstraint()
( tk=<K_FOREIGN> tk2=<K_KEY>
columnNames=ColumnsNamesList()
{
fkIndex = new ForeignKeyIndex()
.withName(sk3)
.withType(tk.image + (tk2!=null?" " + tk2.image:""))
.withType(tk.image + " " + tk2.image)
.withColumnsNames(columnNames);
alterExp.setIndex(index);
}
constraints=AlterExpressionConstraintState() { alterExp.setConstraints(constraints); }
[ <K_USING> sk4=RelObjectName() { alterExp.addParameters("USING", sk4); }]
[ LOOKAHEAD(2) index = IndexWithComment(index) { alterExp.setIndex(index); } ]
)
|
(
tk=<K_KEY>
columnNames=ColumnsNamesList()
{
index = new NamedConstraint()
.withName(sk3)
.withType(tk.image)
.withColumnsNames(columnNames);
alterExp.setIndex(index);
}
constraints=AlterExpressionConstraintState() { alterExp.setConstraints(constraints); }
columnNames = null;
}
<K_REFERENCES> fkTable=Table() [ LOOKAHEAD(2) columnNames=ColumnsNamesList() ]
{
fkIndex.withTable(fkTable).withReferencedColumnNames(columnNames);
alterExp.setIndex(fkIndex);
}

[LOOKAHEAD(2) (<K_ON> (tk=<K_DELETE> | tk=<K_UPDATE>) action = Action()
{ fkIndex.setReferentialAction(ReferentialAction.Type.from(tk.image), action); }
)]
[LOOKAHEAD(2) (<K_ON> (tk=<K_DELETE> | tk=<K_UPDATE>) action = Action()
{ fkIndex.setReferentialAction(ReferentialAction.Type.from(tk.image), action); }
)]
constraints=AlterExpressionConstraintState() { alterExp.setConstraints(constraints); }
)
|
( tk=<K_PRIMARY> tk2=<K_KEY>
columnNames=ColumnsNamesList()
{
index = new NamedConstraint()
.withName(sk3)
.withType(tk.image + " " + tk2.image)
.withColumnsNames(columnNames);
alterExp.setIndex(index);
}
constraints=AlterExpressionConstraintState() { alterExp.setConstraints(constraints); }
[ <K_USING> sk4=RelObjectName() { alterExp.addParameters("USING", sk4); }]
[ LOOKAHEAD(2) index = IndexWithComment(index) { alterExp.setIndex(index); } ]
)
|
LOOKAHEAD(2) (
{ boolean enforced = true; }
[ tk = <K_NOT> { enforced = false; } ]
<K_ENFORCED> {
alterExp.setEnforced(enforced);
alterExp.setConstraintType("CONSTRAINT");
alterExp.setConstraintSymbol(sk3);
}
)
|
(
<K_CHECK> {Expression exp = null;} (LOOKAHEAD(2) "(" exp = Expression() ")")* {
CheckConstraint checkCs = new CheckConstraint().withName(sk3).withExpression(exp);
alterExp.setIndex(checkCs);
}
)
|
(
tk=<K_UNIQUE> (tk2=<K_KEY> { alterExp.setUk(true); } | tk2=<K_INDEX>)?
columnNames=ColumnsNamesList()
{
index = new NamedConstraint()
.withName(sk3)
.withType(tk.image + (tk2!=null?" " + tk2.image:""))
.withColumnsNames(columnNames);
alterExp.setIndex(index);
}
constraints=AlterExpressionConstraintState() { alterExp.setConstraints(constraints); }
[ <K_USING> sk4=RelObjectName() { alterExp.addParameters("USING", sk4); }]
[ LOOKAHEAD(2) index = IndexWithComment(index) { alterExp.setIndex(index); } ]
)
|
(
tk=<K_KEY>
columnNames=ColumnsNamesList()
{
index = new NamedConstraint()
.withName(sk3)
.withType(tk.image)
.withColumnsNames(columnNames);
alterExp.setIndex(index);
}
constraints=AlterExpressionConstraintState() { alterExp.setConstraints(constraints); }
)
)
)
)
Expand Down
102 changes: 94 additions & 8 deletions src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@
*/
package net.sf.jsqlparser.statement.alter;

import static net.sf.jsqlparser.test.TestUtils.*;
import static org.junit.jupiter.api.Assertions.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo;
Expand All @@ -28,12 +37,16 @@
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.alter.AlterExpression.ColumnDataType;
import net.sf.jsqlparser.statement.create.index.CreateIndex;
import net.sf.jsqlparser.statement.create.table.*;
import net.sf.jsqlparser.statement.create.table.CheckConstraint;
import net.sf.jsqlparser.statement.create.table.ForeignKeyIndex;
import net.sf.jsqlparser.statement.create.table.Index;
import net.sf.jsqlparser.statement.create.table.Index.ColumnParams;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import net.sf.jsqlparser.statement.create.table.NamedConstraint;
import net.sf.jsqlparser.statement.create.table.PartitionDefinition;
import static net.sf.jsqlparser.test.TestUtils.assertDeparse;
import static net.sf.jsqlparser.test.TestUtils.assertEqualsObjectTree;
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
import static net.sf.jsqlparser.test.TestUtils.assertStatementCanBeDeparsedAs;

public class AlterTest {

Expand Down Expand Up @@ -2138,4 +2151,77 @@ public void testAlterTableAlterCheckNotEnforced() throws JSQLParserException {

assertSqlCanBeParsedAndDeparsed(sql);
}

@Test
public void testAlterTableAddConstraintUniqueKey() throws JSQLParserException {
String sql = "ALTER TABLE sbtest1 ADD CONSTRAINT UNIQUE KEY ux_c3 (c3)";
Statement stmt = CCJSqlParserUtil.parse(sql);
assertInstanceOf(Alter.class, stmt);

Alter alter = (Alter) stmt;
assertEquals("sbtest1", alter.getTable().getFullyQualifiedName());

List<AlterExpression> alterExpressions = alter.getAlterExpressions();
assertNotNull(alterExpressions);
assertEquals(1, alterExpressions.size());

AlterExpression alterExp = alterExpressions.get(0);
assertEquals(AlterOperation.ADD, alterExp.getOperation());
assertEquals("UNIQUE KEY", alterExp.getConstraintType());
assertEquals("ux_c3", alterExp.getConstraintSymbol());

assertSqlCanBeParsedAndDeparsed(sql);
}

@Test
public void testAlterTableAlterIndexInvisible() throws JSQLParserException {
String sql = "ALTER TABLE sbtest1 ALTER INDEX c4 INVISIBLE";
Statement stmt = CCJSqlParserUtil.parse(sql);
assertInstanceOf(Alter.class, stmt);

Alter alter = (Alter) stmt;
assertEquals("sbtest1", alter.getTable().getFullyQualifiedName());

List<AlterExpression> alterExpressions = alter.getAlterExpressions();
assertNotNull(alterExpressions);
assertEquals(1, alterExpressions.size());

AlterExpression alterExp = alterExpressions.get(0);
assertEquals(AlterOperation.ALTER, alterExp.getOperation());
assertEquals("c4", alterExp.getIndex().getName());
assertEquals("INVISIBLE", alterExp.getIndex().getIndexSpec().get(0));

assertSqlCanBeParsedAndDeparsed(sql);
}

@Test
public void testAlterTableAddIndexInvisible() throws JSQLParserException {
String sql = "ALTER TABLE t1 ADD INDEX k_idx (k) INVISIBLE";
Statement stmt = CCJSqlParserUtil.parse(sql);
assertInstanceOf(Alter.class, stmt);

Alter alter = (Alter) stmt;
assertEquals("t1", alter.getTable().getFullyQualifiedName());

List<AlterExpression> alterExpressions = alter.getAlterExpressions();
assertNotNull(alterExpressions);
assertEquals(1, alterExpressions.size());

AlterExpression alterExp = alterExpressions.get(0);
assertEquals(AlterOperation.ADD, alterExp.getOperation());
assertNotNull(alterExp.getIndex());
assertEquals("k_idx", alterExp.getIndex().getName());
assertEquals("INDEX", alterExp.getIndex().getIndexKeyword());

List<String> columnNames = alterExp.getIndex().getColumnsNames();
assertNotNull(columnNames);
assertEquals(1, columnNames.size());
assertEquals("k", columnNames.get(0));

List<String> indexSpec = alterExp.getIndex().getIndexSpec();
assertNotNull(indexSpec);
assertTrue(indexSpec.contains("INVISIBLE"));

assertSqlCanBeParsedAndDeparsed(sql);
}
}
Loading