sqlparse icon indicating copy to clipboard operation
sqlparse copied to clipboard

Incorrect parsing of string aggregation with group ordering ("LISTAGG(...) WITHIN GROUP(ORDER BY ... )")

Open turekv opened this issue 2 years ago • 0 comments

Expressions containing string aggregation with group ordering are parsed incorrectly. Example:

statement = parse("SELECT LISTAGG(attr,', ') WITHIN GROUP(ORDER BY attr) as column FROM table")
print(statement[0].tokens)

results in:

[<DML 'SELECT' at 0x225D838FB80>,
 <Whitespace ' ' at 0x225D83945E0>,
 <Identifier 'LISTAG...' at 0x225D83BAF90>,    # "LISTAGG(attr,', ') WITHIN"
 <Whitespace ' ' at 0x225D8385460>,
 <Identifier 'GROUP(...' at 0x225D83BAC80>,    # "GROUP(ORDER BY attr) as column"
 <Whitespace ' ' at 0x225D83A6DC0>,
 <Keyword 'FROM' at 0x225D83A6E20>,
 <Whitespace ' ' at 0x225D83A6E80>,
 <Keyword 'table' at 0x225D83A6EE0>]

I.e., according to sqlparse a table with two columns is to be returned by this SQL query:

  • LISTAGG(attr,', '), alias: WITHIN:

    [<Function 'LISTAG...' at 0x1FA46528200>,
     <Whitespace ' ' at 0x1FA464F9BE0>,
     <Identifier 'WITHIN' at 0x1FA46528970>]
    
  • GROUP(ORDER BY attr), alias: column:

    [<Function 'GROUP(...' at 0x1FA46528890>,
     <Whitespace ' ' at 0x1FA46517BE0>,
     <Keyword 'as' at 0x1FA46517C40>,
     <Whitespace ' ' at 0x1FA46517CA0>,
     <Keyword 'column' at 0x1FA46517D00>]
    

However, this is not the case. The query returns a single column (LISTAGG(attr,', ') WITHIN GROUP(ORDER BY attr), alias: column).

turekv avatar Jan 19 '23 06:01 turekv