Skip to content

Commit

Permalink
refactor: TableFunction extends Function, supports LATERAL prefix
Browse files Browse the repository at this point in the history
- implements `Function`, extends `FromItem`
- supports `LATERAL` prefix
- fixes #1835

Signed-off-by: Andreas Reichel <[email protected]>
  • Loading branch information
manticore-projects committed Aug 20, 2023
1 parent 41d705b commit 3963600
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 21 deletions.
87 changes: 73 additions & 14 deletions src/main/java/net/sf/jsqlparser/statement/select/TableFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,59 @@
import net.sf.jsqlparser.expression.Function;

@SuppressWarnings({"PMD.UncommentedEmptyMethodBody"})
public class TableFunction extends SelectItem<Function> implements FromItem {
public class TableFunction extends Function implements FromItem {
private String prefix = null;
private Alias alias = null;
private Pivot pivot = null;
private UnPivot unPivot = null;
private Function function;

@Override
public void accept(FromItemVisitor fromItemVisitor) {
fromItemVisitor.visit(this);
public TableFunction(Function function) {
this.function = function;
}

@Override
public Pivot getPivot() {
return null;
public TableFunction(String prefix, Function function) {
this.prefix = prefix;
this.function = function;
}

@Override
public void setPivot(Pivot pivot) {
public Function getFunction() {
return function;
}

@Deprecated
public Function getExpression() {
return getFunction();
}


public TableFunction setFunction(Function function) {
this.function = function;
return this;
}

public String getPrefix() {
return prefix;
}

public TableFunction setPrefix(String prefix) {
this.prefix = prefix;
return this;
}

@Override
public UnPivot getUnPivot() {
return null;
public void accept(FromItemVisitor fromItemVisitor) {
fromItemVisitor.visit(this);
}

@Override
public void setUnPivot(UnPivot unpivot) {
public Alias getAlias() {
return alias;
}

@Override
public void setAlias(Alias alias) {
this.alias = alias;
}

@Override
Expand All @@ -46,18 +74,49 @@ public TableFunction withAlias(Alias alias) {
}

@Override
public TableFunction withExpression(Function function) {
return (TableFunction) super.withExpression(function);
public Pivot getPivot() {
return pivot;
}

@Override
public void setPivot(Pivot pivot) {
this.pivot = pivot;
}

@Override
public TableFunction withPivot(Pivot pivot) {
return (TableFunction) FromItem.super.withPivot(pivot);
}

@Override
public UnPivot getUnPivot() {
return unPivot;
}

@Override
public void setUnPivot(UnPivot unPivot) {
this.unPivot = unPivot;
}

@Override
public TableFunction withUnPivot(UnPivot unpivot) {
return (TableFunction) FromItem.super.withUnPivot(unpivot);
}

public StringBuilder appendTo(StringBuilder builder) {
if (prefix != null) {
builder.append(prefix).append(" ");
}
builder.append(function.toString());

if (alias != null) {
builder.append(alias);
}
return builder;
}

@Override
public String toString() {
return appendTo(new StringBuilder()).toString();
}
}
12 changes: 7 additions & 5 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -5063,16 +5063,18 @@ MySQLGroupConcat MySQLGroupConcat():{

TableFunction TableFunction():
{
Alias alias = null;
Token prefix = null;
Function function;
TableFunction functionItem;
}
{
function=Function() {
functionItem = new TableFunction().withExpression(function);
[ prefix = <K_LATERAL> ]
function=Function()
{
return prefix!=null
? new TableFunction(prefix.image, function)
: new TableFunction(function);
}
[LOOKAHEAD(2) alias=Alias() { functionItem.setAlias(alias); }]
{ return functionItem; }
}

List<Index.ColumnParams> ColumnNamesWithParamsList() : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public class ReflectionModelTest {
// new net.sf.jsqlparser.statement.select.SetOperationList(),
new net.sf.jsqlparser.statement.select.Skip(),
// new net.sf.jsqlparser.statement.select.ParenthesedSelect(),
new net.sf.jsqlparser.statement.select.TableFunction(),
// new net.sf.jsqlparser.statement.select.TableFunction("LATERAL", new Function()),
new net.sf.jsqlparser.statement.select.Top(),
new net.sf.jsqlparser.statement.select.UnPivot(),
new net.sf.jsqlparser.statement.select.UnionOp(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3349,7 +3349,7 @@ public void testTableFunctionWithNoParams() throws Exception {

assertTrue(plainSelect.getFromItem() instanceof TableFunction);
TableFunction fromItem = (TableFunction) plainSelect.getFromItem();
Function function = fromItem.getExpression();
Function function = fromItem.getFunction();
assertNotNull(function);
assertEquals("SOME_FUNCTION", function.getName());
assertNull(function.getParameters());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.sf.jsqlparser.statement.select;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.test.TestUtils;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class TableFunctionTest {

@Test
void testLateralFlat() throws JSQLParserException {
String sqlStr = "WITH t AS (\n" +
" SELECT \n" +
" 'ABC' AS dim, \n" +
" ARRAY_CONSTRUCT('item1', 'item2', 'item3') AS user_items\n" +
")\n" +
"SELECT DIM, count(value) as COUNT_\n" +
"FROM t a,\n" +
"LATERAL FLATTEN(input => a.user_items) b\n" +
"group by 1";
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}

}

0 comments on commit 3963600

Please sign in to comment.