Skip to content

fix: unary negation operator with operators: Mul, Div and Mod #902

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

Merged
merged 1 commit into from
Jun 22, 2023
Merged

fix: unary negation operator with operators: Mul, Div and Mod #902

merged 1 commit into from
Jun 22, 2023

Conversation

izveigor
Copy link
Contributor

One part of apache/datafusion#6649.
Unary operator does not work correctly with operators: Mul, Div and Mod.
For example (Arrow-Datafusion):

❯ select - 1234 % 12 % 3 * 1545 % 23;
+------------------------------------------------------------------+
| (- Int64(1234) % Int64(12) % Int64(3) * Int64(1545) % Int64(23)) |
+------------------------------------------------------------------+
| -4                                                               |
+------------------------------------------------------------------+
1 row in set. Query took 0.023 seconds.

But should be:

❯ select - 1234 % 12 % 3 * 1545 % 23;
+------------------------------------------------------------------+
| (- Int64(1234) % Int64(12) % Int64(3) * Int64(1545) % Int64(23)) |
+------------------------------------------------------------------+
| -17                                                              |
+------------------------------------------------------------------+
1 row in set. Query took 0.023 seconds.

The main problem is the wrong precedence (See https://github.com/sqlparser-rs/sqlparser-rs/blob/main/src/parser.rs#L797)

@coveralls
Copy link

Pull Request Test Coverage Report for Build 5346759290

  • 15 of 16 (93.75%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.004%) to 86.904%

Changes Missing Coverage Covered Lines Changed/Added Lines %
tests/sqlparser_common.rs 12 13 92.31%
Totals Coverage Status
Change from base Build 5338035255: 0.004%
Covered Lines: 14951
Relevant Lines: 17204

💛 - Coveralls

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @izveigor -- this makes sense to me

#[test]
fn parse_unary_math_with_multiply() {
use self::Expr::*;
let sql = "-a * -b";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran this case without any of the code changes in this PR and it fails like the following (aka it is parased as -(a * -b)

---- parse_unary_math_with_multiply stdout ----
thread 'parse_unary_math_with_multiply' panicked at 'assertion failed: `(left == right)`
  left: `BinaryOp { left: UnaryOp { op: Minus, expr: Identifier(Ident { value: "a", quote_style: None }) }, op: Multiply, right: UnaryOp { op: Minus, expr: Identifier(Ident { value: "b", quote_style: None }) } }`,
 right: `UnaryOp { op: Minus, expr: BinaryOp { left: Identifier(Ident { value: "a", quote_style: None }), op: Multiply, right: UnaryOp { op: Minus, expr: Identifier(Ident { value: "b", quote_style: None }) } } }`', tests/sqlparser_common.rs:1100:5
stack backtrace:

@alamb alamb merged commit 8877cba into apache:main Jun 22, 2023
serprex pushed a commit to serprex/sqlparser-rs that referenced this pull request Nov 6, 2023
MazterQyou pushed a commit to cube-js/sqlparser-rs that referenced this pull request Jan 13, 2024
MazterQyou pushed a commit to cube-js/sqlparser-rs that referenced this pull request Jan 13, 2024
mcheshkov pushed a commit to cube-js/sqlparser-rs that referenced this pull request Sep 2, 2024
…pache#902)

Can drop this after rebase on commit 8877cba " fix: unary negation operator with operators: Mul, Div and Mod (apache#902)", first released in 0.35.0
mcheshkov pushed a commit to cube-js/sqlparser-rs that referenced this pull request Sep 3, 2024
…pache#902)

Can drop this after rebase on commit 8877cba " fix: unary negation operator with operators: Mul, Div and Mod (apache#902)", first released in 0.35.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants