Skip to content

Commit f0ba123

Browse files
authored
fix: add 'K_TYPE' to KeywordOrIdentifier to allow 'type' as a column name (#2448)
TYPE is a non-reserved keyword in MySQL, so statements like 'ALTER TABLE tbl DROP COLUMN type' are valid without quoting. Follows the same approach as #2340 (K_DATA). Fixes #2447
1 parent 81ec799 commit f0ba123

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1756,7 +1756,7 @@ TOKEN:
17561756
* Supported tokens:
17571757
* - <S_IDENTIFIER>: Standard unquoted SQL identifier
17581758
* - <S_QUOTED_IDENTIFIER>: Quoted identifier (e.g., `identifier` or "identifier")
1759-
* - <K_NAME>, <K_NEXT>, <K_VALUE>, <K_PUBLIC>, <K_STRING>, <K_DATA>: Specific keywords treated as identifiers
1759+
* - <K_NAME>, <K_NEXT>, <K_VALUE>, <K_PUBLIC>, <K_STRING>, <K_DATA>, <K_TYPE>: Specific keywords treated as identifiers
17601760
*
17611761
* @return Token representing the identifier or keyword used as identifier
17621762
*/
@@ -1774,6 +1774,7 @@ Token KeywordOrIdentifier():
17741774
| tk = <K_PUBLIC>
17751775
| tk = <K_STRING>
17761776
| tk = <K_DATA>
1777+
| tk = <K_TYPE>
17771778
)
17781779
{ return tk; }
17791780
}

src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ public void testAlterTableDropColumnIssue2339() throws JSQLParserException {
259259
assertSqlCanBeParsedAndDeparsed("ALTER TABLE test DROP COLUMN Data");
260260
}
261261

262+
@Test
263+
public void testAlterTableDropColumnIssue2447() throws JSQLParserException {
264+
assertSqlCanBeParsedAndDeparsed("ALTER TABLE test DROP COLUMN type");
265+
}
266+
262267
@Test
263268
public void testAlterTableDropConstraint() throws JSQLParserException {
264269
final String sql = "ALTER TABLE test DROP CONSTRAINT YYY";
@@ -469,6 +474,11 @@ public void testAlterTableChangeColumnIssue2339() throws JSQLParserException {
469474
assertSqlCanBeParsedAndDeparsed("ALTER TABLE tb_test CHANGE data INT (10)");
470475
}
471476

477+
@Test
478+
public void testAlterTableChangeColumnIssue2447() throws JSQLParserException {
479+
assertSqlCanBeParsedAndDeparsed("ALTER TABLE tb_test CHANGE type INT (10)");
480+
}
481+
472482
@Test
473483
public void testAlterTableAddColumnWithZone() throws JSQLParserException {
474484
assertSqlCanBeParsedAndDeparsed(
@@ -668,6 +678,18 @@ public void testAlterTableRenameColumn2() throws JSQLParserException {
668678
assertEquals(expression.getColumnName(), "full_name");
669679
}
670680

681+
@Test
682+
public void testAlterTableRenameColumnIssue2447() throws JSQLParserException {
683+
String sql = "ALTER TABLE test_table RENAME COLUMN type TO type2";
684+
assertSqlCanBeParsedAndDeparsed(sql);
685+
686+
Alter alter = (Alter) CCJSqlParserUtil.parse(sql);
687+
AlterExpression expression = alter.getAlterExpressions().get(0);
688+
assertEquals(expression.getOperation(), AlterOperation.RENAME);
689+
assertEquals(expression.getColOldName(), "type");
690+
assertEquals(expression.getColumnName(), "type2");
691+
}
692+
671693
@Test
672694
public void testAlterTableForeignKeyIssue981() throws JSQLParserException {
673695
assertSqlCanBeParsedAndDeparsed(

0 commit comments

Comments
 (0)