Skip to content

Commit b78fdeb

Browse files
committed
fix: read keywords that MySQL or PostgreSQL does not reserve as names after DDL keywords
Signed-off-by: minleejae <mmj9808@gmail.com>
1 parent f01801a commit b78fdeb

2 files changed

Lines changed: 131 additions & 0 deletions

File tree

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,6 +2072,11 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
20722072
return false;
20732073
}
20742074

2075+
// A keyword the dialect does not reserve, after a word that DDL follows with a name
2076+
if (DialectKeywordNames.isNameAfter(getAsString(Feature.dialect), prev, getToken(1).kind)) {
2077+
return true;
2078+
}
2079+
20752080
// ── 2. After delimiters and operators: expression position ─────
20762081
// With K_FROM/K_SELECT removed from RelObjectName's
20772082
// token list, accepting all remaining keywords after these
@@ -4727,6 +4732,56 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
47274732
return false;
47284733
}
47294734
}
4735+
4736+
/**
4737+
* Whether the keyword is a name right after a word that DDL always follows with a name,
4738+
* although elsewhere it starts a clause or an operator. MySQL does not reserve OFFSET,
4739+
* ONLY, RETURNING, ANY, SOME and VALUE, as in ADD COLUMN c INT AFTER offset or
4740+
* KEY any (a), and PostgreSQL does not reserve SET and takes BETWEEN, IF, GROUPING and
4741+
* VALUES as names there, as in ADD set INT or REFERENCES if (b).
4742+
*/
4743+
static boolean isNameAfter(String dialect, Token previous, int kind) {
4744+
if (Dialect.MYSQL.name().equals(dialect)) {
4745+
switch (kind) {
4746+
case K_OFFSET: case K_ONLY: case K_RETURNING:
4747+
case K_ANY: case K_SOME: case K_VALUE:
4748+
return isMySqlNameIntroducer(previous);
4749+
default:
4750+
return false;
4751+
}
4752+
}
4753+
if (Dialect.POSTGRESQL.name().equals(dialect)) {
4754+
if (kind == K_SET || kind == K_BETWEEN) {
4755+
return isPostgreSqlNameIntroducer(previous);
4756+
}
4757+
return (kind == K_IF || kind == K_GROUPING || kind == K_VALUES)
4758+
&& previous.kind == K_REFERENCES;
4759+
}
4760+
return false;
4761+
}
4762+
4763+
private static boolean isMySqlNameIntroducer(Token previous) {
4764+
switch (previous.kind) {
4765+
case K_ADD: case K_ALTER: case K_DROP: case K_MODIFY: case K_TO: case K_INDEX:
4766+
case K_KEY: case K_REFERENCES: case K_DATABASE: case K_SCHEMA: case K_VIEW:
4767+
case K_TRIGGER: case K_PARTITION:
4768+
return true;
4769+
default:
4770+
return "AFTER".equalsIgnoreCase(previous.image);
4771+
}
4772+
}
4773+
4774+
private static boolean isPostgreSqlNameIntroducer(Token previous) {
4775+
switch (previous.kind) {
4776+
case K_ADD: case K_ALTER: case K_DROP: case K_TO: case K_INDEX: case K_PARTITION:
4777+
case K_SCHEMA: case K_SEQUENCE: case K_VIEW: case K_TRIGGER: case K_REFERENCES:
4778+
case K_AUTHORIZATION:
4779+
return true;
4780+
default:
4781+
return "INHERIT".equalsIgnoreCase(previous.image)
4782+
|| "ATTRIBUTE".equalsIgnoreCase(previous.image);
4783+
}
4784+
}
47304785
}
47314786

47324787
/** MySQL's EXPLAIN, PREPARE, EXECUTE, DROP PREPARE and DO: MySQL, MariaDB or no dialect. */

‎src/test/java/net/sf/jsqlparser/statement/KeywordsTest.java‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import net.sf.jsqlparser.statement.select.SetOperationList;
5555
import net.sf.jsqlparser.statement.select.SampleClause;
5656
import net.sf.jsqlparser.statement.select.WithItem;
57+
import net.sf.jsqlparser.statement.truncate.Truncate;
5758
import net.sf.jsqlparser.util.TablesNamesFinder;
5859
import org.junit.jupiter.api.Test;
5960
import org.junit.jupiter.params.ParameterizedTest;
@@ -784,4 +785,79 @@ void testOtherDialectKeywordsKeepTheirClausesWithoutTheDialect() throws JSQLPars
784785
assertEquals("xor", ((PlainSelect) assertRoundTrip("SELECT a xor FROM t",
785786
Dialect.POSTGRESQL)).getSelectItem(0).getAlias().getName());
786787
}
788+
789+
// MySQL does not reserve OFFSET, ONLY and RETURNING, and ANY, SOME and VALUE are its index and
790+
// referenced table names; PostgreSQL does not reserve SET, and takes BETWEEN, IF, GROUPING and
791+
// VALUES as these names too.
792+
public static Stream<Arguments> dialectNamePositions() {
793+
Stream<Arguments> mySqlClauseWords = Stream.of("offset", "only", "returning")
794+
.flatMap(word -> Stream.of("ALTER TABLE t ADD COLUMN c INT AFTER " + word,
795+
"ALTER TABLE t DROP " + word, "ALTER TABLE t ADD " + word + " INT",
796+
"ALTER TABLE t MODIFY " + word + " INT NOT NULL",
797+
"ALTER TABLE t ALTER " + word + " DROP DEFAULT",
798+
"ALTER TABLE t RENAME COLUMN c TO " + word,
799+
"ALTER TABLE t RENAME TO " + word,
800+
"ALTER TABLE t ADD INDEX " + word + " (a)",
801+
"ALTER TABLE t RENAME INDEX " + word + " TO ix",
802+
"CREATE INDEX " + word + " ON t (a)",
803+
"CREATE VIEW " + word + " AS SELECT 1"))
804+
.map(sql -> Arguments.of(sql, Dialect.MYSQL));
805+
Stream<Arguments> mySqlIndexWords = Stream.of("any", "some", "value")
806+
.flatMap(word -> Stream.of("CREATE TABLE t (a INT, KEY " + word + " (a))",
807+
"ALTER TABLE t ADD UNIQUE INDEX " + word + " (a)",
808+
"ALTER TABLE t ADD FOREIGN KEY " + word + " (a) REFERENCES p (b)",
809+
"ALTER TABLE t ADD FOREIGN KEY (a) REFERENCES " + word + " (b)"))
810+
.map(sql -> Arguments.of(sql, Dialect.MYSQL));
811+
Stream<Arguments> postgreSqlWords = Stream.of("set", "between")
812+
.flatMap(word -> Stream.of("ALTER TABLE t ADD " + word + " INT",
813+
"ALTER TABLE t ALTER " + word + " TYPE BIGINT",
814+
"ALTER TABLE t DROP " + word,
815+
"ALTER TABLE t RENAME COLUMN c TO " + word,
816+
"ALTER INDEX " + word + " RENAME TO ix",
817+
"CREATE INDEX " + word + " ON t (a)",
818+
"DROP INDEX " + word, "ALTER TABLE t SET SCHEMA " + word,
819+
"CREATE SEQUENCE " + word, "CREATE VIEW " + word + " AS SELECT 1",
820+
"ALTER TABLE t ATTACH PARTITION " + word + " FOR VALUES IN (1)",
821+
"ALTER TABLE t OWNER TO " + word))
822+
.map(sql -> Arguments.of(sql, Dialect.POSTGRESQL));
823+
Stream<Arguments> postgreSqlReferences = Stream.of("set", "between", "if", "grouping",
824+
"values")
825+
.map(word -> Arguments.of(
826+
"ALTER TABLE t ADD CONSTRAINT fk FOREIGN KEY (a) REFERENCES " + word
827+
+ " (b)",
828+
Dialect.POSTGRESQL));
829+
return Stream.of(mySqlClauseWords, mySqlIndexWords, postgreSqlWords, postgreSqlReferences)
830+
.flatMap(stream -> stream);
831+
}
832+
833+
@ParameterizedTest(name = "{1}: {0}")
834+
@MethodSource("dialectNamePositions")
835+
void testDialectNonReservedKeywordAsName(String sql, Dialect dialect)
836+
throws JSQLParserException {
837+
assertRoundTrip(sql, dialect);
838+
}
839+
840+
@Test
841+
void testDialectNonReservedKeywordsKeepTheirClauses() throws JSQLParserException {
842+
assertRoundTrip("SELECT a FROM t LIMIT 10 OFFSET 5", Dialect.MYSQL);
843+
assertRoundTrip("START TRANSACTION READ ONLY", Dialect.MYSQL);
844+
assertRoundTrip("SELECT JSON_VALUE(j, '$.a' RETURNING DECIMAL (10, 2)) FROM t",
845+
Dialect.MYSQL);
846+
assertRoundTrip("SELECT * FROM t WHERE a = ANY (SELECT b FROM u)", Dialect.MYSQL);
847+
assertRoundTrip("INSERT INTO t VALUES (1)", Dialect.MYSQL);
848+
849+
assertRoundTrip("UPDATE t SET a = 1", Dialect.POSTGRESQL);
850+
assertRoundTrip("SELECT * FROM t WHERE a BETWEEN 1 AND 2", Dialect.POSTGRESQL);
851+
assertRoundTrip("CREATE INDEX ON t (a)", Dialect.POSTGRESQL);
852+
assertRoundTrip("CREATE INDEX IF NOT EXISTS i ON t (a)", Dialect.POSTGRESQL);
853+
assertRoundTrip("DROP INDEX IF EXISTS i", Dialect.POSTGRESQL);
854+
assertRoundTrip("ALTER TYPE ty ADD VALUE 'x'", Dialect.POSTGRESQL);
855+
assertRoundTrip("ALTER TABLE t ALTER COLUMN c SET DEFAULT 1", Dialect.POSTGRESQL);
856+
857+
// PostgreSQL's ONLY prefix
858+
assertTrue(((Alter) assertRoundTrip("ALTER TABLE ONLY t ADD COLUMN c INT",
859+
Dialect.POSTGRESQL)).isUseOnly());
860+
assertTrue(((Truncate) assertRoundTrip("TRUNCATE ONLY t", Dialect.POSTGRESQL))
861+
.isOnly());
862+
}
787863
}

0 commit comments

Comments
 (0)