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

add support for MATERIALIZED in WITH clause #2128

Merged
merged 1 commit into from
Dec 23, 2024
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
14 changes: 13 additions & 1 deletion src/main/java/net/sf/jsqlparser/statement/select/WithItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class WithItem<T extends ParenthesedStatement> {
private List<SelectItem<?>> withItemList;
private boolean recursive = false;

private boolean materialized = false;

public WithItem(T statement, Alias alias) {
this.statement = statement;
this.alias = alias;
Expand Down Expand Up @@ -79,6 +81,14 @@ public void setRecursive(boolean recursive) {
this.recursive = recursive;
}

public boolean isMaterialized() {
return materialized;
}

public void setMaterialized(boolean materialized) {
this.materialized = materialized;
}

/**
* The {@link SelectItem}s in this WITH (for example the A,B,C in "WITH mywith (A,B,C) AS ...")
*
Expand Down Expand Up @@ -108,6 +118,7 @@ public String toString() {
builder.append(")");
}
builder.append(" AS ");
builder.append(materialized ? "MATERIALIZED " : "");
builder.append(statement);
return builder.toString();
}
Expand All @@ -121,8 +132,9 @@ public WithItem<?> withWithItemList(List<SelectItem<?>> withItemList) {
return this;
}

public WithItem<?> withRecursive(boolean recursive) {
public WithItem<?> withRecursive(boolean recursive, boolean materialized) {
this.setRecursive(recursive);
this.setMaterialized(materialized);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,9 @@ public <S> StringBuilder visit(WithItem<?> withItem, S context) {
.append(PlainSelect.getStringList(withItem.getWithItemList(), true, true));
}
buffer.append(" AS ");
if (withItem.isMaterialized()) {
buffer.append("MATERIALIZED ");
}
StatementDeParser statementDeParser =
new StatementDeParser((ExpressionDeParser) expressionVisitor, this, buffer);
statementDeParser.deParse(withItem.getParenthesedStatement());
Expand Down
4 changes: 3 additions & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -2705,6 +2705,7 @@ List<WithItem<?>> WithList():
WithItem<?> WithItem() #WithItem:
{
boolean recursive = false;
boolean materialized = false;
String name;
List<SelectItem<?>> selectItems = null;
ParenthesedStatement statement;
Expand All @@ -2714,6 +2715,7 @@ WithItem<?> WithItem() #WithItem:
name=RelObjectName()
[ "(" selectItems=SelectItemsList() ")" ]
<K_AS>
[ LOOKAHEAD(2) <K_MATERIALIZED> { materialized = true; } ]
(
LOOKAHEAD(2) statement = ParenthesedSelect()
|
Expand All @@ -2726,7 +2728,7 @@ WithItem<?> WithItem() #WithItem:
{
WithItem<?> withItem = new WithItem(statement, new Alias(name, false));
return withItem
.withRecursive(recursive)
.withRecursive(recursive, materialized)
.withWithItemList(selectItems);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3186,6 +3186,12 @@ public void testSelectOracleColl() throws JSQLParserException {
"SELECT * FROM the_table tt WHERE TT.COL1 = lines(idx).COL1");
}

@Test
public void testSelectWithMaterializedWith() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
"WITH tokens_with_supply AS MATERIALIZED (SELECT * FROM tokens) SELECT * FROM tokens_with_supply");
}

@Test
public void testSelectInnerWith() throws JSQLParserException {
String stmt =
Expand Down
Loading