-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Incorrect behavior of arithmetic operations between time values #12190
Comments
I don't know the root cause, but I'd just add it seems to to where there's subtraction. E.g.
works, but
doesn't. I wasn't able to find a purely add case that doesn't produce the correct answer (though doesn't necessarily rule it out). |
Just an educated guess, but could it be that the arithmetic operators are not passed correctly from the parsed AST, or that the AST is not constructed correctly? Not entirely sure, but it seems that the first arithmetic operator is always the one that is applied on the entire expression. For example:
Minus is applied on all of them. I think this is the result of Another example:
which seems to be of similar behavior. The minus is applied on all of them, even transforming the latest minus into a plus. I think that's the result of Another weird example with a plus:
The results seem to correspond with |
Just found something that could be interesting. I think it's a bug in the AST and the I tried explicitly defining the precedence/order with parentheses for the same queries above (trying to mimick how the precedence between the operators is preserved):
This corresponds with the wrong results that I get from Datafusion:
Precdence from left to right (which is what I think is what should be happening):
The results of these queries match the expected results:
|
I think the examples have
Otherwise, there's a potentially related example: apache/datafusion-sqlparser-rs#1345 |
Indeed, this is parsing problem. I modified --- datafusion/sql/tests/cases/plan_to_sql.rs
+++ datafusion/sql/tests/cases/plan_to_sql.rs
@@ -54,6 +54,12 @@ fn roundtrip_expr() {
"sum((age * 2))",
r#"sum((age * 2))"#,
),
+ (
+ TableReference::bare("person"),
+ "'2024-01-10 01:23:45'::timestamp - interval '1 day' - interval '2 day'",
+ // FIXME this is incorrect!
+ "(CAST('2024-01-10 01:23:45' AS TIMESTAMP) - (INTERVAL '0 YEARS 0 MONS 1 DAYS 0 HOURS 0 MINS 0.000000000 SECS' - INTERVAL '0 YEARS 0 MONS 2 DAYS 0 HOURS 0 MINS 0.000000000 SECS'))",
+ ),
];
let roundtrip = |table, sql: &str| -> Result<String> {
|
I think apache/datafusion-sqlparser-rs#1398 fixed this issue. |
thank you @samuelcolvin and @alamb |
Nice! I expect the next release of sqlparser to be released sometime this week and thus the change will be available in DataFusion as well: apache/datafusion-sqlparser-rs#1384 |
sqlparser-rs 0.51.0 is now available on crates.io: https://crates.io/crates/sqlparser/0.51.0 🎉 |
I believe this will be fixed by #12222 from @samuelcolvin |
Describe the bug
Consider the following query:
While the expected result should be
2024-08-27 08:21:26.000000
.When I add parentheses to the query, the results are what I expect:
The issue seems to happening when you have multiple (more than 2?) operands in arithmetic. e.g.:
When you have two operands, the results seem to match what you'd expect:
To Reproduce
You can use the same queries mentioned above.
Expected behavior
Mentioned above as well
Additional context
No response
The text was updated successfully, but these errors were encountered: