Abdullahsab3 commented on issue #12190:
URL: https://github.com/apache/datafusion/issues/12190#issuecomment-2320263226
Just found something that could be interesting. I think it's a bug in the
AST and the precedence of operators.
I tried explicitly defining the precedence with parentheses for the same
queries above (trying to mimick how the precedence between the operators is
done):
Precedence from right to left (which is not correct)
```
select interval '2 day' - (interval '1 day' + (interval '1 day' + (interval
'5 day' - ( interval '3 day'))));
select interval '2 day' - (interval '1 day' + (interval '1 day'));
select interval '5 day' + (interval '1 day' - (interval '2 day' - (interval
'4 day')));
```
This corresponds with the wrong results that I get from Datafusion:
```
DataFusion CLI v41.0.0
> select interval '2 day' - (interval '1 day' + (interval '1 day' +
(interval '5 day' - ( interval '3 day'))));
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| IntervalMonthDayNano("IntervalMonthDayNano { months: 0, days: 2,
nanoseconds: 0 }") - IntervalMonthDayNano("IntervalMonthDayNano { months: 0,
days: 1, nanoseconds: 0 }") + IntervalMonthDayNano("IntervalMonthDayNano {
months: 0, days: 1, nanoseconds: 0 }") +
IntervalMonthDayNano("IntervalMonthDayNano { months: 0, days: 5, nanoseconds: 0
}") - IntervalMonthDayNano("IntervalMonthDayNano { months: 0, days: 3,
nanoseconds: 0 }") |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 0 years 0 mons -2 days 0 hours 0 mins 0.000000000 secs
|
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row(s) fetched.
Elapsed 0.003 seconds.
> select interval '2 day' - (interval '1 day' + (interval '1 day'));
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| IntervalMonthDayNano("IntervalMonthDayNano { months: 0, days: 2,
nanoseconds: 0 }") - IntervalMonthDayNano("IntervalMonthDayNano { months: 0,
days: 1, nanoseconds: 0 }") + IntervalMonthDayNano("IntervalMonthDayNano {
months: 0, days: 1, nanoseconds: 0 }") |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 0 years 0 mons 0 days 0 hours 0 mins 0.000000000 secs
|
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row(s) fetched.
Elapsed 0.001 seconds.
> select interval '5 day' + (interval '1 day' - (interval '2 day' -
(interval '4 day')));
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| IntervalMonthDayNano("IntervalMonthDayNano { months: 0, days: 5,
nanoseconds: 0 }") + IntervalMonthDayNano("IntervalMonthDayNano { months: 0,
days: 1, nanoseconds: 0 }") - IntervalMonthDayNano("IntervalMonthDayNano {
months: 0, days: 2, nanoseconds: 0 }") -
IntervalMonthDayNano("IntervalMonthDayNano { months: 0, days: 4, nanoseconds: 0
}") |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 0 years 0 mons 8 days 0 hours 0 mins 0.000000000 secs
|
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row(s) fetched.
Elapsed 0.001 seconds.
```
Precdence from left to right (which is what I think should be happening):
```
select (((interval '2 day' - interval '1 day') + interval '1 day') +
interval '5 day') - interval '3 day';
select (interval '2 day' - interval '1 day') + interval '1 day';
select ((interval '5 day' + interval '1 day') - interval '2 day') - interval
'4 day';
```
The results of these queries match the expected results:
```
> select (((interval '2 day' - interval '1 day') + interval '1 day') +
interval '5 day') - interval '3 day';
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| IntervalMonthDayNano("IntervalMonthDayNano { months: 0, days: 2,
nanoseconds: 0 }") - IntervalMonthDayNano("IntervalMonthDayNano { months: 0,
days: 1, nanoseconds: 0 }") + IntervalMonthDayNano("IntervalMonthDayNano {
months: 0, days: 1, nanoseconds: 0 }") +
IntervalMonthDayNano("IntervalMonthDayNano { months: 0, days: 5, nanoseconds: 0
}") - IntervalMonthDayNano("IntervalMonthDayNano { months: 0, days: 3,
nanoseconds: 0 }") |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 0 years 0 mons 4 days 0 hours 0 mins 0.000000000 secs
|
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row(s) fetched.
Elapsed 0.003 seconds.
> select (interval '2 day' - interval '1 day') + interval '1 day';
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| IntervalMonthDayNano("IntervalMonthDayNano { months: 0, days: 2,
nanoseconds: 0 }") - IntervalMonthDayNano("IntervalMonthDayNano { months: 0,
days: 1, nanoseconds: 0 }") + IntervalMonthDayNano("IntervalMonthDayNano {
months: 0, days: 1, nanoseconds: 0 }") |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 0 years 0 mons 2 days 0 hours 0 mins 0.000000000 secs
|
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row(s) fetched.
Elapsed 0.001 seconds.
> select ((interval '5 day' + interval '1 day') - interval '2 day') -
interval '4 day';
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| IntervalMonthDayNano("IntervalMonthDayNano { months: 0, days: 5,
nanoseconds: 0 }") + IntervalMonthDayNano("IntervalMonthDayNano { months: 0,
days: 1, nanoseconds: 0 }") - IntervalMonthDayNano("IntervalMonthDayNano {
months: 0, days: 2, nanoseconds: 0 }") -
IntervalMonthDayNano("IntervalMonthDayNano { months: 0, days: 4, nanoseconds: 0
}") |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 0 years 0 mons 0 days 0 hours 0 mins 0.000000000 secs
|
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row(s) fetched.
Elapsed 0.001 seconds.
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]