Skip to content

Add support for TableFunction [WITH OFFSET|ORDINALITY] #2219

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

Merged
merged 5 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class TableFunction extends Function implements FromItem {
private Pivot pivot = null;
private UnPivot unPivot = null;
private Function function;
private String withClause = null;

public TableFunction(Function function) {
this.function = function;
Expand All @@ -30,6 +31,17 @@ public TableFunction(String prefix, Function function) {
this.function = function;
}

public TableFunction(Function function, String withClause) {
this.function = function;
this.withClause = withClause;
}

public TableFunction(String prefix, Function function, String withClause) {
this.prefix = prefix;
this.function = function;
this.withClause = withClause;
}

public TableFunction(String prefix, String name, Expression... parameters) {
this.prefix = prefix;
this.function = new Function(name, parameters);
Expand Down Expand Up @@ -62,6 +74,19 @@ public TableFunction setPrefix(String prefix) {
return this;
}

public String getWithClause() {
return withClause;
}

public void setWithClause(String withClause) {
this.withClause = withClause;
}

public TableFunction withWithClause(String withClause) {
this.withClause = withClause;
return this;
}

@Override
public <T, S> T accept(FromItemVisitor<T> fromItemVisitor, S context) {
return fromItemVisitor.visit(this, context);
Expand Down Expand Up @@ -128,6 +153,10 @@ public StringBuilder appendTo(StringBuilder builder) {
}
builder.append(function.toString());

if (withClause != null) {
builder.append(" ").append(withClause);
}

if (alias != null) {
builder.append(alias);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,10 @@ public <S> StringBuilder visit(TableFunction tableFunction, S context) {
}
tableFunction.getFunction().accept(this.expressionVisitor, context);

if (tableFunction.getWithClause() != null) {
builder.append(" ").append(tableFunction.getWithClause());
}

if (tableFunction.getAlias() != null) {
builder.append(tableFunction.getAlias());
}
Expand Down
12 changes: 10 additions & 2 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,8 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_WHERE:"WHERE">
| <K_WINDOW:"WINDOW">
| <K_WITH:"WITH">
| <K_WITH_OFFSET:"WITH OFFSET">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need the compound token. Just WITH and ORDINALITY/ The goal must be to define as few tokens as possible.

| <K_WITH_ORDINALITY:"WITH ORDINALITY">
| <K_WITH_TIES:"WITH TIES">
| <K_WITHIN:"WITHIN">
| <K_WITHOUT:"WITHOUT">
Expand Down Expand Up @@ -6636,14 +6638,20 @@ TableFunction TableFunction():
Token prefix = null;
Function function;
TableFunction functionItem;
Token withClause = null;
}
{
[ prefix = <K_LATERAL> ]
function=Function()
[ withClause = <K_WITH_OFFSET> | withClause = <K_WITH_ORDINALITY> ]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ <K_WITH> ( withClause = <K_OFFSET> | withClause = <K_ORDINALITY> ) ]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried changing to this, but there is a new test failing:
KeywordsTest.testRelObjectNameWithoutValue(String)

Keyword ORDINALITY

net.sf.jsqlparser.JSQLParserException: net.sf.jsqlparser.parser.ParseException: Encountered unexpected token: "SELECT" <K_SELECT>
    at line 1, column 1.

Was expecting one of:

    "WITH"


	at [email protected]/net.sf.jsqlparser.parser.CCJSqlParserUtil.parseStatement(CCJSqlParserUtil.java:352)
	at [email protected]/net.sf.jsqlparser.parser.CCJSqlParserUtil.parse(CCJSqlParserUtil.java:125)
	at [email protected]/net.sf.jsqlparser.parser.CCJSqlParserUtil.parse(CCJSqlParserUtil.java:91)
	at [email protected]/net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed(TestUtils.java:98)
	at [email protected]/net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed(TestUtils.java:85)
	at [email protected]/net.sf.jsqlparser.statement.KeywordsTest.testRelObjectNameWithoutValue(KeywordsTest.java:53)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found the issue, I needed to add ORDINALITY to the restricted SQL2016 keywords

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried changing to this, but there is a new test failing:
KeywordsTest.testRelObjectNameWithoutValue(String)

Please see here for an explanation: https://manticore-projects.com/JSQLParser/contribution.html#manage-reserved-keywords

I don't think, we need to reserve those keywords but only whitelist them properly by running gradle updateKeywords.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made those changes, and pushed up. Thank you

{
return prefix!=null
? new TableFunction(prefix.image, function)
: new TableFunction(function);
? withClause!=null
? new TableFunction(prefix.image, function, withClause.image)
: new TableFunction(prefix.image, function)
: withClause!=null
? new TableFunction(function, withClause.image)
: new TableFunction(function);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.test.TestUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

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

Expand Down Expand Up @@ -47,4 +49,14 @@ void testTableFunctionWithNamedParameterWhereNameIsOuterKeyword() throws JSQLPar
" lateral flatten(input => i.DATA_VALUE:Friends, outer => true) AS f1;";
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}

@ParameterizedTest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

@ValueSource(strings = {
"WITH OFFSET",
"WITH ORDINALITY"
})
void testTableFunctionWithSupportedWithClauses(String withClause) throws JSQLParserException {
String sqlStr = "SELECT * FROM UNNEST(ARRAY[1, 2, 3]) " + withClause + " AS t(a, b)";
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}
}
4 changes: 3 additions & 1 deletion src/test/resources/simple_parsing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,6 @@ FROM EMPLOYEE THIS_EMP, DINFO, DINFOMAX
WHERE THIS_EMP.JOB = 'SALESREP'
AND THIS_EMP.WORKDEPT = DINFO.DEPTNO

select * from Person where deptname='it' AND NOT (age=24)
select * from Person where deptname='it' AND NOT (age=24)

select * from unnest(array[4,5,6]) with ordinality;
Loading