Skip to content

Commit 3900464

Browse files
authored
add support for MATERIALIZED in WITH clause (#2128)
1 parent 8f8439e commit 3900464

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class WithItem<T extends ParenthesedStatement> {
2828
private List<SelectItem<?>> withItemList;
2929
private boolean recursive = false;
3030

31+
private boolean materialized = false;
32+
3133
public WithItem(T statement, Alias alias) {
3234
this.statement = statement;
3335
this.alias = alias;
@@ -79,6 +81,14 @@ public void setRecursive(boolean recursive) {
7981
this.recursive = recursive;
8082
}
8183

84+
public boolean isMaterialized() {
85+
return materialized;
86+
}
87+
88+
public void setMaterialized(boolean materialized) {
89+
this.materialized = materialized;
90+
}
91+
8292
/**
8393
* The {@link SelectItem}s in this WITH (for example the A,B,C in "WITH mywith (A,B,C) AS ...")
8494
*
@@ -108,6 +118,7 @@ public String toString() {
108118
builder.append(")");
109119
}
110120
builder.append(" AS ");
121+
builder.append(materialized ? "MATERIALIZED " : "");
111122
builder.append(statement);
112123
return builder.toString();
113124
}
@@ -121,8 +132,9 @@ public WithItem<?> withWithItemList(List<SelectItem<?>> withItemList) {
121132
return this;
122133
}
123134

124-
public WithItem<?> withRecursive(boolean recursive) {
135+
public WithItem<?> withRecursive(boolean recursive, boolean materialized) {
125136
this.setRecursive(recursive);
137+
this.setMaterialized(materialized);
126138
return this;
127139
}
128140

src/main/java/net/sf/jsqlparser/util/deparser/SelectDeParser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,9 @@ public <S> StringBuilder visit(WithItem<?> withItem, S context) {
682682
.append(PlainSelect.getStringList(withItem.getWithItemList(), true, true));
683683
}
684684
buffer.append(" AS ");
685+
if (withItem.isMaterialized()) {
686+
buffer.append("MATERIALIZED ");
687+
}
685688
StatementDeParser statementDeParser =
686689
new StatementDeParser((ExpressionDeParser) expressionVisitor, this, buffer);
687690
statementDeParser.deParse(withItem.getParenthesedStatement());

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2705,6 +2705,7 @@ List<WithItem<?>> WithList():
27052705
WithItem<?> WithItem() #WithItem:
27062706
{
27072707
boolean recursive = false;
2708+
boolean materialized = false;
27082709
String name;
27092710
List<SelectItem<?>> selectItems = null;
27102711
ParenthesedStatement statement;
@@ -2714,6 +2715,7 @@ WithItem<?> WithItem() #WithItem:
27142715
name=RelObjectName()
27152716
[ "(" selectItems=SelectItemsList() ")" ]
27162717
<K_AS>
2718+
[ LOOKAHEAD(2) <K_MATERIALIZED> { materialized = true; } ]
27172719
(
27182720
LOOKAHEAD(2) statement = ParenthesedSelect()
27192721
|
@@ -2726,7 +2728,7 @@ WithItem<?> WithItem() #WithItem:
27262728
{
27272729
WithItem<?> withItem = new WithItem(statement, new Alias(name, false));
27282730
return withItem
2729-
.withRecursive(recursive)
2731+
.withRecursive(recursive, materialized)
27302732
.withWithItemList(selectItems);
27312733
}
27322734
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3186,6 +3186,12 @@ public void testSelectOracleColl() throws JSQLParserException {
31863186
"SELECT * FROM the_table tt WHERE TT.COL1 = lines(idx).COL1");
31873187
}
31883188

3189+
@Test
3190+
public void testSelectWithMaterializedWith() throws JSQLParserException {
3191+
assertSqlCanBeParsedAndDeparsed(
3192+
"WITH tokens_with_supply AS MATERIALIZED (SELECT * FROM tokens) SELECT * FROM tokens_with_supply");
3193+
}
3194+
31893195
@Test
31903196
public void testSelectInnerWith() throws JSQLParserException {
31913197
String stmt =

0 commit comments

Comments
 (0)