Skip to content

Commit c05bd1d

Browse files
committed
fix: keep all clauses when deparsing SELECT without FROM (#2483)
PlainSelect.appendTo printed the post-FROM clause chain (oracle hierarchical, PREFERRING, GROUP BY, HAVING, QUALIFY, WINDOW, EMIT CHANGES) only when a FROM item was present; the "without from" branch rendered WHERE and PREWHERE alone. Move the shared clause chain out of the fromItem branch, mirroring SelectDeParser, so a FROM-less SELECT renders every clause stored on the AST. Output for SELECTs with FROM is unchanged (same checks in the same order). Signed-off-by: 付典 <fudianchn@gmail.com>
1 parent ad69ecc commit c05bd1d

2 files changed

Lines changed: 52 additions & 37 deletions

File tree

src/main/java/net/sf/jsqlparser/statement/select/PlainSelect.java

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -607,43 +607,35 @@ public StringBuilder appendSelectBodyTo(StringBuilder builder) {
607607
if (ksqlWindow != null) {
608608
builder.append(" WINDOW ").append(ksqlWindow);
609609
}
610-
if (preWhere != null) {
611-
builder.append(" PREWHERE ").append(preWhere);
612-
}
613-
if (where != null) {
614-
builder.append(" WHERE ").append(where);
615-
}
616-
if (oracleHierarchical != null) {
617-
builder.append(oracleHierarchical);
618-
}
619-
if (preferringClause != null) {
620-
builder.append(" ").append(preferringClause);
621-
}
622-
if (groupBy != null) {
623-
builder.append(" ").append(groupBy);
624-
}
625-
if (having != null) {
626-
builder.append(" HAVING ").append(having);
627-
}
628-
if (qualify != null) {
629-
builder.append(" QUALIFY ").append(qualify);
630-
}
631-
if (windowDefinitions != null) {
632-
builder.append(" WINDOW ");
633-
builder.append(windowDefinitions.stream().map(WindowDefinition::toString)
634-
.collect(joining(", ")));
635-
}
636-
if (emitChanges) {
637-
builder.append(" EMIT CHANGES");
638-
}
639-
} else {
640-
// without from
641-
if (preWhere != null) {
642-
builder.append(" PREWHERE ").append(preWhere);
643-
}
644-
if (where != null) {
645-
builder.append(" WHERE ").append(where);
646-
}
610+
}
611+
if (preWhere != null) {
612+
builder.append(" PREWHERE ").append(preWhere);
613+
}
614+
if (where != null) {
615+
builder.append(" WHERE ").append(where);
616+
}
617+
if (oracleHierarchical != null) {
618+
builder.append(oracleHierarchical);
619+
}
620+
if (preferringClause != null) {
621+
builder.append(" ").append(preferringClause);
622+
}
623+
if (groupBy != null) {
624+
builder.append(" ").append(groupBy);
625+
}
626+
if (having != null) {
627+
builder.append(" HAVING ").append(having);
628+
}
629+
if (qualify != null) {
630+
builder.append(" QUALIFY ").append(qualify);
631+
}
632+
if (windowDefinitions != null) {
633+
builder.append(" WINDOW ");
634+
builder.append(windowDefinitions.stream().map(WindowDefinition::toString)
635+
.collect(joining(", ")));
636+
}
637+
if (emitChanges) {
638+
builder.append(" EMIT CHANGES");
647639
}
648640
if (intoTempTable != null) {
649641
builder.append(" INTO TEMP ").append(intoTempTable);

src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,6 +1907,29 @@ public void testSelectFunction() throws JSQLParserException {
19071907
assertSqlCanBeParsedAndDeparsed(statement);
19081908
}
19091909

1910+
@Test
1911+
public void testSelectWithoutFromRetainsWhereGroupByHaving() throws JSQLParserException {
1912+
assertSqlCanBeParsedAndDeparsed("SELECT 1 WHERE 1 = 1");
1913+
PlainSelect plainSelect =
1914+
(PlainSelect) assertSqlCanBeParsedAndDeparsed("SELECT 1 GROUP BY 1 HAVING 1 = 1");
1915+
assertNotNull(plainSelect.getGroupBy());
1916+
assertNotNull(plainSelect.getHaving());
1917+
}
1918+
1919+
@Test
1920+
public void testSelectWithoutFromRetainsWindowAndQualify() throws JSQLParserException {
1921+
PlainSelect plainSelect =
1922+
(PlainSelect) assertSqlCanBeParsedAndDeparsed("SELECT 1 WINDOW w AS (ORDER BY 1)");
1923+
assertEquals(1, plainSelect.getWindowDefinitions().size());
1924+
assertSqlCanBeParsedAndDeparsed("SELECT 1 QUALIFY 1 = 1");
1925+
}
1926+
1927+
@Test
1928+
public void testSelectWithoutFromRetainsHierarchicalAndPreferring() throws JSQLParserException {
1929+
assertSqlCanBeParsedAndDeparsed("SELECT 1 START WITH 1 = 1 CONNECT BY LEVEL <= 1");
1930+
assertSqlCanBeParsedAndDeparsed("SELECT 1 PREFERRING HIGH 1");
1931+
}
1932+
19101933
@Test
19111934
public void testWeirdSelect() throws JSQLParserException {
19121935
String sql =

0 commit comments

Comments
 (0)