Skip to content

Commit

Permalink
add support for MATERIALIZED in WITH clause (#2128)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomershay authored Dec 23, 2024
1 parent 8f8439e commit 3900464
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
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

0 comments on commit 3900464

Please sign in to comment.