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

Incorrect order of operations #91

Closed
ianmcook opened this issue Aug 12, 2019 · 4 comments
Closed

Incorrect order of operations #91

ianmcook opened this issue Aug 12, 2019 · 4 comments
Assignees

Comments

@ianmcook
Copy link

ianmcook commented Aug 12, 2019

Rules for order of operations specify that addition and subtraction have the same priority and should be applied from left to right. Similarly, multiplication and division have the same priority and should be applied from left to right. But the implementation in moz_sql_parser treats addition as higher priority than subtraction, and it treats multiplication as higher priority than division. For example, the output from moz_sql_parser is as follows:

>>> from moz_sql_parser import parse
>>> import json
>>> json.dumps(parse("select 5-4+2"))
'{"select": {"value": {"sub": [5, {"add": [4, 2]}]}}}'
>>> json.dumps(parse("select 5/4*2"))
'{"select": {"value": {"div": [5, {"mul": [4, 2]}]}}}'

To follow the rules for order of operations, the output should be as follows:

>>> json.dumps(parse("select 5-4+2"))
'{"select": {"value": {"add": [{"sub": [5, 4]}, 2]}}}'
>>> json.dumps(parse("select 5/4*2"))
'{"select": {"value": {"mul": [{"div": [5, 4]}, 2]}}}'
@klahnakoski klahnakoski self-assigned this Jan 23, 2020
@klahnakoski
Copy link
Contributor

This will make the parser logic more complicated. It may be best introduced into pyparsing.

@klahnakoski
Copy link
Contributor

pyparsing/pyparsing#174

I am keeping this issue open because it is not that difficult to "just do it"

@klahnakoski
Copy link
Contributor

The fix is here:

e383a57?w=1

@klahnakoski
Copy link
Contributor

the PR will be merged shortly

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