Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

UNION with SELECT ... ORDER BY fails to parse #70

Closed
monotaro-yasushi-masuda opened this issue Mar 5, 2019 · 2 comments
Closed

UNION with SELECT ... ORDER BY fails to parse #70

monotaro-yasushi-masuda opened this issue Mar 5, 2019 · 2 comments

Comments

@monotaro-yasushi-masuda
Copy link

monotaro-yasushi-masuda commented Mar 5, 2019

Hello,

First of all, thank you for nice parser, it helps much of my usecases.
Here I report some glitch with parsing UNION-ed SELECTs containing ORDER BY.
Confirmed in moz-sql-parser=2.42.19034, pyparsing=2.2.0 on Python3.6.5.

Thanks

>>> mozql.parse('select a from b order by a asc')
{'select': {'value': 'a'}, 'from': 'b', 'orderby': {'value': 'a', 'sort': 'asc'}}
>>> mozql.parse('select a from b order by a desc')
{'select': {'value': 'a'}, 'from': 'b', 'orderby': {'value': 'a', 'sort': 'desc'}}
>>> mozql.parse('select a from b union select 2')
{'union': [{'select': {'value': 'a'}, 'from': 'b'}, {'select': {'value': 2}}]}
>>> mozql.parse('select a from b oder by a union select 2')
Traceback (most recent call last):
  File "/home/developer/.virtualenvs/3/lib/python3.6/site-packages/moz_sql_parser/__init__.py", line 35, in parse
    parse_result = SQLParser.parseString(sql, parseAll=True)
  File "/home/developer/.virtualenvs/3/lib/python3.6/site-packages/pyparsing.py", line 1632, in parseString
    raise exc
  File "/home/developer/.virtualenvs/3/lib/python3.6/site-packages/pyparsing.py", line 1626, in parseString
    se._parse( instring, loc )
  File "/home/developer/.virtualenvs/3/lib/python3.6/site-packages/pyparsing.py", line 1529, in _parseCache
    value = self._parseNoCache(instring, loc, doActions, callPreParse)
  File "/home/developer/.virtualenvs/3/lib/python3.6/site-packages/pyparsing.py", line 1379, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/home/developer/.virtualenvs/3/lib/python3.6/site-packages/pyparsing.py", line 3395, in parseImpl
    loc, exprtokens = e._parse( instring, loc, doActions )
  File "/home/developer/.virtualenvs/3/lib/python3.6/site-packages/pyparsing.py", line 1529, in _parseCache
    value = self._parseNoCache(instring, loc, doActions, callPreParse)
  File "/home/developer/.virtualenvs/3/lib/python3.6/site-packages/pyparsing.py", line 1383, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "/home/developer/.virtualenvs/3/lib/python3.6/site-packages/pyparsing.py", line 3183, in parseImpl
    raise ParseException(instring, loc, self.errmsg, self)
pyparsing.ParseException: Expected end of text (at char 21), (line:1, col:22)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/developer/.virtualenvs/3/lib/python3.6/site-packages/moz_sql_parser/__init__.py", line 44, in parse
    raise ParseException(sql, e.loc, "Expecting one of (" + (", ".join(expecting)) + ")")
pyparsing.ParseException: Expecting one of (join, offset, union all, right outer join, full outer join, left outer join, right join, order by, left join, limit, full join, having, group by, inner join, where) (at char 21), (line:1, col:22)
@klahnakoski
Copy link
Contributor

Thank you for submitting this! The oder is spelt wrong, but even after fixing that it is still a problem.

@klahnakoski
Copy link
Contributor

After reading the code, I remember: The order by clause must follow any union operations. This goes to improving error reporting. I added a test with the hope it can be made better in the future (509977a)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants