Skip to content
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

Unable to parse ORACLE LISTAGG-Function in combination with OVER-Clause #1652

Closed
andghe opened this issue Oct 25, 2022 · 4 comments · Fixed by #1666
Closed

Unable to parse ORACLE LISTAGG-Function in combination with OVER-Clause #1652

andghe opened this issue Oct 25, 2022 · 4 comments · Fixed by #1666
Assignees
Labels

Comments

@andghe
Copy link

andghe commented Oct 25, 2022

Hi

The parser (version 4.5) reports

Exception in thread "main" net.sf.jsqlparser.JSQLParserException: net.sf.jsqlparser.parser.ParseException: Encountered unexpected token: "BY" "BY"
    at line 10, column 88.

Was expecting one of:

    ")"
    ","
    "BINARY"
    "BIT"
    "CHAR"
    "CHARACTER"
    "DOUBLE"
    "INTERVAL"
    "JSON"
    "SET"
    "SIGNED"
    "UNSIGNED"
    "XML"
    <DT_ZONE>
    <K_DATETIMELITERAL>
    <K_DATE_LITERAL>
    <S_IDENTIFIER>
    <S_QUOTED_IDENTIFIER>

while parsing the following statement (reduced to the minimum with sample data included):

-- not parseable
WITH CTE_DUMMY_DATA(COL_TO_AGG, PART_COL) AS (
    SELECT 'Foo', 1 FROM DUAL
    UNION
    SELECT 'Bar', 2 FROM DUAL
    UNION
    SELECT 'Baz', 1 FROM DUAL
)
SELECT
    LISTAGG (d.COL_TO_AGG, ' / ') WITHIN GROUP (ORDER BY d.COL_TO_AGG) OVER (PARTITION BY d.PART_COL) AS MY_LISTAGG
FROM cte_dummy_data d;

This is fine ORACLE-Sql (see https://docs.oracle.com/cd/E11882_01/server.112/e41084/functions089.htm#SQLRF30030) and returns something like

image

I was able to reduce the Problem to the OVER-Clause, as the following (semantically different) snippet is parseable:

-- parseable
WITH CTE_DUMMY_DATA(COL_TO_AGG, PART_COL) AS (
    SELECT 'Foo', 1 FROM DUAL
    UNION
    SELECT 'Bar', 2 FROM DUAL
    UNION
    SELECT 'Baz', 1 FROM DUAL
)
SELECT
    LISTAGG (d.COL_TO_AGG, ' / ') WITHIN GROUP (ORDER BY d.COL_TO_AGG) AS MY_LISTAGG
FROM cte_dummy_data d;

Fix/Workaround would be appreciated.

@wumpz
Copy link
Member

wumpz commented Oct 28, 2022

within group and over are not supported at the same time. Why not use the order by for window functions? I am not quite sure if I understand the use of within group here. This should work:

WITH CTE_DUMMY_DATA(COL_TO_AGG, PART_COL) AS (
    SELECT 'Foo', 1 FROM DUAL
    UNION
    SELECT 'Bar', 2 FROM DUAL
    UNION
    SELECT 'Baz', 1 FROM DUAL
)
SELECT
    LISTAGG (d.COL_TO_AGG, ' / ') OVER (PARTITION BY d.PART_COL ORDER BY d.COL_TO_AGG) AS MY_LISTAGG
FROM cte_dummy_data d;

@andghe
Copy link
Author

andghe commented Oct 29, 2022

Hi @wumpz

Thank you for your reply. Unfortunately your suggestion is not feasible. According to Oracle's documentation (https://docs.oracle.com/cd/E11882_01/server.112/e41084/functions089.htm#SQLRF30030) the WITHIN GROUP clause is not optional when using LISTAGG. The execution results in an ORA-02000 when the WITHING GROUP clause is omitted.

@manticore-projects
Copy link
Contributor

Greetings,

thank your for reporting.
I do use LISTAGG on Oracle myself and thus will look into this issue soonest.

@wumpz
Copy link
Member

wumpz commented Nov 2, 2022

@andghe unfortunately you are right: http://sqlfiddle.com/#!4/c6069c/4/0. Sorry I missed that.

manticore-projects added a commit to manticore-projects/JSqlParser that referenced this issue Nov 14, 2022
fixes issue JSQLParser#1652
fixes 3 more Special Oracle Tests
wumpz pushed a commit that referenced this issue Nov 20, 2022
* fix: add missing public Getter

Add public Getter for `updateSets`
Fixes #1630

* feat: LISTAGG() with OVER() clause

fixes issue #1652
fixes 3 more Special Oracle Tests

* fix: White-list CURRENT_DATE and CURRENT_TIMESTAMP tokens

allows CURRENT_DATE(3) and CURRENT_TIMESTAMP(3) as regular functions
fixes #1507
fixes #1607

* feat: Deparser for Expression Lists

Visit each Expression of a List instead ExpressionList.toString()
fixes #1608

* fix: Lookahead needed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants