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

Mathematical operations in where #20

Closed
anvog opened this issue Jul 28, 2016 · 5 comments
Closed

Mathematical operations in where #20

anvog opened this issue Jul 28, 2016 · 5 comments

Comments

@anvog
Copy link

anvog commented Jul 28, 2016

SELECT * FROM temptable where value1 = ((value2 * 5) - value3)

The statement above will throw the error.

Syntax error found near Binary Expression

Brackets should be fine.
Any other way to structure the equations?

@nwronski
Copy link
Contributor

nwronski commented Aug 2, 2016

It looks like the grammar isn't happy with the parenthesis on the left side ((value2 * 5)) of the expression. Your example is definitely valid. I will look into a fix for this one.

Looking at it closer, there appears to also be a problem when trying to switch the two sides of the inner expression:

SELECT * FROM temptable where value1 = (value3 * -1 + (value2 * 5))

Currently, the parser is treating this as if it is the expression: value3 * -(1 + (value2 * 5))

@nwronski
Copy link
Contributor

nwronski commented Aug 28, 2016

Based on further investigation, the expression parsing works for simple expressions, but is not obeying order of operations or correctly composing complex expressions. For example:

-- parsed as -(1 * (2+3)) instead of (-1) * (2 + 3)
SELECT * FROM t where -1 * (2 + 3)

-- parsed as 3 + (4 * (5 > 20)) instead of (3 + (4 * 5)) > 20
SELECT * FROM t where 3 + 4 * 5 > 20

I will need to rewrite the expression parsing grammar for the next release to:

  • Obey order of operations: * is higher than +, etc...
  • Compose in the correct order: unary expressions, then binary expressions, then logical and/or (AND, OR), etc...
  • Allow for nesting parenthesis on either side of a binary expression

Expect these changes in release v0.15.0.

nwronski added a commit that referenced this issue Oct 2, 2016
Better support for order of operations and correct order when
composing expressions using AND, OR, etc...

Also fixed issue where expressions such as  `x NOT NULL` were
treated as a unary expressions instead of binary expressions.

WIP - Do not merge
nwronski added a commit that referenced this issue Oct 2, 2016
Got rid of a bunch of unused code, cleaned up the utility
functions, moved the utilities back into the grammar.pegjs file to
avoid the extra magic of injecting the import statement into the
built parser.

Fixed the binary expression logic to properly handle cases such
as:

``` sql
SELECT * FROM t where -1 * (2 + 3);
SELECT * FROM t where 3 + 4 * 5 > 20;
SELECT * FROM t where v1 = ((v2 * 5) - v3);
```

Updated SQL in specs to contain correct ASTs for specs related
to expression grouping.

Should have broken this into several commits but the changes
are too intertwined to easily break apart now.

Refs #20
Refs #3
@nwronski
Copy link
Contributor

nwronski commented Oct 3, 2016

@anvog I just released a beta version of the parser that contains the fix for the issue you were experiencing 0.15.0-beta.

You can install it via npm using (for now):

npm i sqlite-parser@beta

or, you can also try it out online here.

Let me know if you encounter any more problems, thanks!

@nwronski nwronski closed this as completed Oct 3, 2016
@nwronski
Copy link
Contributor

nwronski commented Oct 3, 2016

Whoops, didn't mean to close this one just yet. Will close after 0.15.0 is out of beta.

@nwronski nwronski reopened this Oct 3, 2016
nwronski added a commit that referenced this issue Oct 3, 2016
The format expr BETWEEN expr AND expr was failing to be
parsed after the latest round of changes.
@nwronski
Copy link
Contributor

nwronski commented Oct 9, 2016

Closing this one, but still need beta flag to install at the moment. (Latest: v1.0.0-beta)

npm i sqlite-parser@beta

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