Skip to content

Allow specific keywords to be parsed as identifiers (fix issue with RENAME COLUMN) #2203

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
Mar 24, 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
33 changes: 31 additions & 2 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,35 @@ TOKEN:
}
}

/**
* Parses identifiers including standard SQL identifiers, quoted identifiers, and specific keywords.
*
* This is used in cases where certain SQL keywords (like NAME, NEXT, VALUE, etc.) can appear
* as identifiers (e.g., table names, column names) depending on the SQL dialect or context.
*
* Supported tokens:
* - <S_IDENTIFIER>: Standard unquoted SQL identifier
* - <S_QUOTED_IDENTIFIER>: Quoted identifier (e.g., `identifier` or "identifier")
* - <K_NAME>, <K_NEXT>, <K_VALUE>, <K_PUBLIC>, <K_STRING>: Specific keywords treated as identifiers
*
* @return Token representing the identifier or keyword used as identifier
*/
Token KeywordOrIdentifier():
{
Token tk;
}
{
(
tk = <S_IDENTIFIER>
| tk = <S_QUOTED_IDENTIFIER>
| tk = <K_NAME>
| tk = <K_NEXT>
| tk = <K_VALUE>
| tk = <K_PUBLIC>
| tk = <K_STRING>
)
{ return tk; }
}

Statement Statement() #Statement:
{
Expand Down Expand Up @@ -7997,9 +8026,9 @@ AlterExpression AlterExpression():
)
|
LOOKAHEAD(2) <K_RENAME> { alterExp.setOperation(AlterOperation.RENAME); } [ <K_COLUMN> { alterExp.hasColumn(true);} ]
( tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER> ) { alterExp.setColOldName(tk.image); }
( tk=KeywordOrIdentifier() ) { alterExp.setColOldName(tk.image); }
<K_TO>
(tk2=<S_IDENTIFIER> | tk2=<S_QUOTED_IDENTIFIER>) { alterExp.setColumnName(tk2.image); }
( tk2=KeywordOrIdentifier() ) { alterExp.setColumnName(tk2.image); }
|
LOOKAHEAD(2)(
<K_RENAME> <K_TO> {alterExp.setOperation(AlterOperation.RENAME_TABLE);}
Expand Down
13 changes: 13 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 @@ -621,6 +621,19 @@ public void testAlterTableRenameColumn() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(sql);
}

@Test
public void testAlterTableRenameColumn2() throws JSQLParserException {
// Additional test case: Renaming column from 'name' to 'full_name'
String sql = "ALTER TABLE test_table RENAME COLUMN name TO full_name";
assertSqlCanBeParsedAndDeparsed(sql);

Alter alter = (Alter) CCJSqlParserUtil.parse(sql);
AlterExpression expression = alter.getAlterExpressions().get(0);
assertEquals(expression.getOperation(), AlterOperation.RENAME);
assertEquals(expression.getColOldName(), "name");
assertEquals(expression.getColumnName(), "full_name");
}

@Test
public void testAlterTableForeignKeyIssue981() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
Expand Down
Loading