Skip to content

Commit 3963600

Browse files
refactor: TableFunction extends Function, supports LATERAL prefix
- implements `Function`, extends `FromItem` - supports `LATERAL` prefix - fixes #1835 Signed-off-by: Andreas Reichel <[email protected]>
1 parent 41d705b commit 3963600

File tree

5 files changed

+107
-21
lines changed

5 files changed

+107
-21
lines changed

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

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,59 @@
1313
import net.sf.jsqlparser.expression.Function;
1414

1515
@SuppressWarnings({"PMD.UncommentedEmptyMethodBody"})
16-
public class TableFunction extends SelectItem<Function> implements FromItem {
16+
public class TableFunction extends Function implements FromItem {
17+
private String prefix = null;
18+
private Alias alias = null;
19+
private Pivot pivot = null;
20+
private UnPivot unPivot = null;
21+
private Function function;
1722

18-
@Override
19-
public void accept(FromItemVisitor fromItemVisitor) {
20-
fromItemVisitor.visit(this);
23+
public TableFunction(Function function) {
24+
this.function = function;
2125
}
2226

23-
@Override
24-
public Pivot getPivot() {
25-
return null;
27+
public TableFunction(String prefix, Function function) {
28+
this.prefix = prefix;
29+
this.function = function;
2630
}
2731

28-
@Override
29-
public void setPivot(Pivot pivot) {
32+
public Function getFunction() {
33+
return function;
34+
}
3035

36+
@Deprecated
37+
public Function getExpression() {
38+
return getFunction();
39+
}
40+
41+
42+
public TableFunction setFunction(Function function) {
43+
this.function = function;
44+
return this;
45+
}
46+
47+
public String getPrefix() {
48+
return prefix;
49+
}
50+
51+
public TableFunction setPrefix(String prefix) {
52+
this.prefix = prefix;
53+
return this;
3154
}
3255

3356
@Override
34-
public UnPivot getUnPivot() {
35-
return null;
57+
public void accept(FromItemVisitor fromItemVisitor) {
58+
fromItemVisitor.visit(this);
3659
}
3760

3861
@Override
39-
public void setUnPivot(UnPivot unpivot) {
62+
public Alias getAlias() {
63+
return alias;
64+
}
4065

66+
@Override
67+
public void setAlias(Alias alias) {
68+
this.alias = alias;
4169
}
4270

4371
@Override
@@ -46,18 +74,49 @@ public TableFunction withAlias(Alias alias) {
4674
}
4775

4876
@Override
49-
public TableFunction withExpression(Function function) {
50-
return (TableFunction) super.withExpression(function);
77+
public Pivot getPivot() {
78+
return pivot;
79+
}
80+
81+
@Override
82+
public void setPivot(Pivot pivot) {
83+
this.pivot = pivot;
5184
}
5285

5386
@Override
5487
public TableFunction withPivot(Pivot pivot) {
5588
return (TableFunction) FromItem.super.withPivot(pivot);
5689
}
5790

91+
@Override
92+
public UnPivot getUnPivot() {
93+
return unPivot;
94+
}
95+
96+
@Override
97+
public void setUnPivot(UnPivot unPivot) {
98+
this.unPivot = unPivot;
99+
}
100+
58101
@Override
59102
public TableFunction withUnPivot(UnPivot unpivot) {
60103
return (TableFunction) FromItem.super.withUnPivot(unpivot);
61104
}
62105

106+
public StringBuilder appendTo(StringBuilder builder) {
107+
if (prefix != null) {
108+
builder.append(prefix).append(" ");
109+
}
110+
builder.append(function.toString());
111+
112+
if (alias != null) {
113+
builder.append(alias);
114+
}
115+
return builder;
116+
}
117+
118+
@Override
119+
public String toString() {
120+
return appendTo(new StringBuilder()).toString();
121+
}
63122
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5063,16 +5063,18 @@ MySQLGroupConcat MySQLGroupConcat():{
50635063

50645064
TableFunction TableFunction():
50655065
{
5066-
Alias alias = null;
5066+
Token prefix = null;
50675067
Function function;
50685068
TableFunction functionItem;
50695069
}
50705070
{
5071-
function=Function() {
5072-
functionItem = new TableFunction().withExpression(function);
5071+
[ prefix = <K_LATERAL> ]
5072+
function=Function()
5073+
{
5074+
return prefix!=null
5075+
? new TableFunction(prefix.image, function)
5076+
: new TableFunction(function);
50735077
}
5074-
[LOOKAHEAD(2) alias=Alias() { functionItem.setAlias(alias); }]
5075-
{ return functionItem; }
50765078
}
50775079

50785080
List<Index.ColumnParams> ColumnNamesWithParamsList() : {

src/test/java/net/sf/jsqlparser/statement/builder/ReflectionModelTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public class ReflectionModelTest {
194194
// new net.sf.jsqlparser.statement.select.SetOperationList(),
195195
new net.sf.jsqlparser.statement.select.Skip(),
196196
// new net.sf.jsqlparser.statement.select.ParenthesedSelect(),
197-
new net.sf.jsqlparser.statement.select.TableFunction(),
197+
// new net.sf.jsqlparser.statement.select.TableFunction("LATERAL", new Function()),
198198
new net.sf.jsqlparser.statement.select.Top(),
199199
new net.sf.jsqlparser.statement.select.UnPivot(),
200200
new net.sf.jsqlparser.statement.select.UnionOp(),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3349,7 +3349,7 @@ public void testTableFunctionWithNoParams() throws Exception {
33493349

33503350
assertTrue(plainSelect.getFromItem() instanceof TableFunction);
33513351
TableFunction fromItem = (TableFunction) plainSelect.getFromItem();
3352-
Function function = fromItem.getExpression();
3352+
Function function = fromItem.getFunction();
33533353
assertNotNull(function);
33543354
assertEquals("SOME_FUNCTION", function.getName());
33553355
assertNull(function.getParameters());
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package net.sf.jsqlparser.statement.select;
2+
3+
import net.sf.jsqlparser.JSQLParserException;
4+
import net.sf.jsqlparser.test.TestUtils;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class TableFunctionTest {
10+
11+
@Test
12+
void testLateralFlat() throws JSQLParserException {
13+
String sqlStr = "WITH t AS (\n" +
14+
" SELECT \n" +
15+
" 'ABC' AS dim, \n" +
16+
" ARRAY_CONSTRUCT('item1', 'item2', 'item3') AS user_items\n" +
17+
")\n" +
18+
"SELECT DIM, count(value) as COUNT_\n" +
19+
"FROM t a,\n" +
20+
"LATERAL FLATTEN(input => a.user_items) b\n" +
21+
"group by 1";
22+
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
23+
}
24+
25+
}

0 commit comments

Comments
 (0)