hanke580 opened a new issue, #71183:
URL: https://github.com/apache/doris/issues/71183

   ### Search before asking
   
   - [x] I had searched in the 
[issues](https://github.com/apache/doris/issues?q=is%3Aissue) and found no 
similar issues.
   
   
   ### Version
   
   
   - master `b4140d0b0538` (2026-11-30), built from source: `select 
@@version_comment` = `doris version doris-0.0.0-b4140d0b05`
   - doris-4.1.5-release-917b20be838 (`apache/doris:fe-4.1.5` and 
`apache/doris:be-4.1.5`, one FE and one BE)
   
   
   ### What's Wrong?
   
   
   The minute functions take a BIGINT count and turn it into seconds with 
`interval.minute * 60` in 64-bit arithmetic,
   with no overflow check; the range check runs on the wrapped number. Because 
60 * 2^62 is 0 modulo 2^64, a count of
   2^62 + k moves the date by k minutes, and other counts past about 1.5 * 
10^17 land on arbitrary nearby dates. No
   error or warning; counts that do not wrap far enough raise the documented 
error.
   
   ```
   MINUTES_ADD('2020-02-02', 1000000000000)                  -> ERROR 
[E-218]Operation minute_add of 2020-02-02 00:00:00, 1000000000000 out of range
   MINUTES_ADD('2020-02-02', 153722867280912930)             -> ERROR ... out 
of range        (60n just below 2^63)
   MINUTES_ADD('2020-02-02', 4611686018427387909)            -> 2020-02-02 
00:05:00           (2^62 + 5, acts as 5)
   MINUTES_ADD('2020-02-02', 9223372036854775806)            -> 2020-02-01 
23:58:00           (2^63 - 2, acts as -2)
   MINUTES_ADD('2020-02-02', 307445734561825861)             -> 2020-02-02 
00:00:44           (60n = 2^64 + 44 seconds)
   MINUTES_SUB('2020-02-02 02:02:02', 9223372036854775806)   -> 2020-02-02 
02:04:02
   TIMESTAMPADD(MINUTE, 4611686018427387909, '2019-01-02')   -> 2019-01-02 
00:05:00
   DATE_ADD('2020-02-02', INTERVAL 4611686018427387909 MINUTE) -> 2020-02-02 
00:05:00
   DATE_SUB('2020-02-02', INTERVAL 4611686018427387909 MINUTE) -> 2020-02-01 
23:55:00
   BIGINT column n = 4611686018427387909: MINUTES_ADD(d, n)  -> 2020-02-02 
00:05:00
   ```
   
   `EXPLAIN` keeps `minutes_add(...)` (it is not folded), so this is the BE.
   `DateV2Value::date_add_interval()` in `be/src/core/value/vdatetime_value.cpp`
   ([master, lines 
2472-2476](https://github.com/apache/doris/blob/b4140d0b0538f5bf2acedcf8aa7a4d664bbb4927/be/src/core/value/vdatetime_value.cpp#L2472-L2476)):
   
   ```cpp
           int64_t seconds = (this->day() - 1) * 86400L + this->hour() * 3600L 
+ this->minute() * 60 +
                             this->second() +
                             sign * (interval.day * 86400 + interval.hour * 
3600 +
                                     interval.minute * 60 + interval.second) +
                             extra_second;
   ```
   
   `TimeInterval`'s fields are `uint64_t` (`vdatetime_value.h`, lines 77-86), 
so `interval.minute * 60` wraps modulo
   2^64 before the result is checked. The other two `date_add_interval` 
overloads have the same expression
   (lines 1447-1449 in `VecDateTimeValue`, lines 2547-2549). The HOUR, DAY and 
WEEK
   functions take an INT count, so their products cannot wrap (they return NULL 
past 2^31), and SECONDS_ADD does not
   multiply.
   
   
   ### What You Expected?
   
   
   The documented error, as for 10^12: "If the calculation result exceeds the 
valid range of the DATETIME type
   (0000-01-01 00:00:00 to 9999-12-31 23:59:59.999999), an exception is thrown."
   
(https://doris.apache.org/docs/4.x/sql-manual/sql-functions/scalar-functions/date-time-functions/minutes-add/,
 and
   the minutes-sub and timestampadd pages.)
   
   ### How to Reproduce?
   
   One FE and one BE with default settings.
   
   ```sql
   SELECT MINUTES_ADD('2020-02-02', 1000000000000);
   SELECT MINUTES_ADD('2020-02-02', 4611686018427387909);
   SELECT MINUTES_ADD('2020-02-02', 9223372036854775806);
   SELECT MINUTES_ADD('2020-02-02', 307445734561825861);
   SELECT MINUTES_SUB('2020-02-02 02:02:02', 9223372036854775806);
   SELECT TIMESTAMPADD(MINUTE, 4611686018427387909, '2019-01-02');
   SELECT DATE_ADD('2020-02-02', INTERVAL 4611686018427387909 MINUTE);
   SELECT DATE_SUB('2020-02-02', INTERVAL 4611686018427387909 MINUTE);
   
   CREATE DATABASE minutes_repro;
   USE minutes_repro;
   CREATE TABLE mi (id INT, n BIGINT, d DATETIME) DUPLICATE KEY(id) DISTRIBUTED 
BY HASH(id) BUCKETS 1
     PROPERTIES ("replication_num" = "1");
   INSERT INTO mi VALUES (1, 4611686018427387909, '2020-02-02 00:00:00');
   SELECT n, MINUTES_ADD(d, n) FROM mi;
   DROP DATABASE minutes_repro;
   ```
   
   ### Anything Else?
   
   
   Checking the product before it is used, for example with 
`__builtin_mul_overflow(interval.minute, 60, &s)` (and the
   same for the sum), or rejecting `interval.minute > INT64_MAX / 60` up front, 
would give the out-of-range error. #68410
   fixed the same kind of overflow for `quarters * 3`.
   
   ### Are you willing to submit PR?
   
   - [ ] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://www.apache.org/foundation/policies/conduct)
   


-- 
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]

Reply via email to