Skip to content
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

feat: support keyword "only" for postgresql #1952

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 @@ -65,6 +65,8 @@ public class PlainSelect extends Select {
*/
private boolean isUsingFinal = false;

private boolean isUsingOnly = false;

@Deprecated
public boolean isUseBrackets() {
return false;
Expand Down Expand Up @@ -213,6 +215,19 @@ public PlainSelect withUsingFinal(boolean usingFinal) {
return this;
}

public boolean isUsingOnly() {
return isUsingOnly;
}

public void setUsingOnly(boolean usingOnly) {
isUsingOnly = usingOnly;
}

public PlainSelect withUsingOnly(boolean usingOnly) {
this.setUsingOnly(usingOnly);
return this;
}

@Override
public void accept(SelectVisitor selectVisitor) {
selectVisitor.visit(this);
Expand Down Expand Up @@ -439,7 +454,11 @@ public StringBuilder appendSelectBodyTo(StringBuilder builder) {
}

if (fromItem != null) {
builder.append(" FROM ").append(fromItem);
builder.append(" FROM ");
if (isUsingOnly) {
builder.append("ONLY ");
}
builder.append(fromItem);
if (lateralViews != null) {
for (LateralView lateralView : lateralViews) {
builder.append(" ").append(lateralView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ public void visit(PlainSelect plainSelect) {

if (plainSelect.getFromItem() != null) {
buffer.append(" FROM ");
if (plainSelect.isUsingOnly()) {
buffer.append("ONLY ");
}
plainSelect.getFromItem().accept(this);

if (plainSelect.getFromItem() instanceof Table) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -2277,6 +2277,10 @@ PlainSelect PlainSelect() #PlainSelect:
[ lateralViews=LateralViews() ]
[ LOOKAHEAD(2) joins=JoinsList() ]
]
[ LOOKAHEAD(3) <K_FROM> <K_ONLY> { plainSelect.setUsingOnly(true); } fromItem=FromItem()
[ lateralViews=LateralViews() ]
[ LOOKAHEAD(2) joins=JoinsList() ]
]

// Clickhouse FINAL as shown at https://clickhouse.com/docs/en/operations/settings/settings#final
[ LOOKAHEAD(2) <K_FINAL> { plainSelect.setUsingFinal(true); } ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5806,4 +5806,11 @@ public void testIssue1907() throws JSQLParserException {
"SELECT * FROM (SELECT year, person, SUM(amount) FROM rentals GROUP BY year, person) t1 ORDER BY year DESC WITH ROLLUP";
assertSqlCanBeParsedAndDeparsed(stmt2);
}

@Test
public void testIssue1908() throws JSQLParserException {
// postgresql14
String stmt = "SELECT * FROM ONLY sys_business_rule";
assertSqlCanBeParsedAndDeparsed(stmt);
}
}
Loading