`SUBTIME` function returns an error in DATETIME type related to TIMESTAMP type
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Bug Report
### 1. Minimal reproduce step
```sql
CREATE TABLE t1 (a TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6));
INSERT INTO t1 VALUES ('1970-01-01 01:00:01.000000');
INSERT INTO t1 VALUES ('1970-01-01 01:00:01.000001');
CREATE TABLE t2 (
`SUBTIME(a, '00:00:00.000001')` datetime(6) DEFAULT NULL,
`SUBTIME(a, '10:00:00.000001')` datetime(6) DEFAULT NULL
);
INSERT INTO t2 SELECT
SUBTIME(a, '00:00:00.000001'),
SUBTIME(a, '10:00:00.000001')
FROM t1;
SELECT * FROM t2;
```
### 2. What did you expect to see?
```sql
mysql> INSERT INTO t2 SELECT
-> SUBTIME(a, '00:00:00.000001'),
-> SUBTIME(a, '10:00:00.000001')
-> FROM t1;
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM t2;
+-------------------------------+-------------------------------+
| SUBTIME(a, '00:00:00.000001') | SUBTIME(a, '10:00:00.000001') |
+-------------------------------+-------------------------------+
| 1970-01-01 01:00:00.999999 | 1969-12-31 15:00:00.999999 |
| 1970-01-01 01:00:01.000000 | 1969-12-31 15:00:01.000000 |
+-------------------------------+-------------------------------+
```
### 3. What did you see instead
```sql
tidb> INSERT INTO t2 SELECT
-> SUBTIME(a, '00:00:00.000001'),
-> SUBTIME(a, '10:00:00.000001')
-> FROM t1;
ERROR 1292 (22007): Incorrect time value: '{1969 12 31 15 0 0 999999}'
```
### 4. What is your TiDB version?
```*************************** 1. row ***************************
tidb_version(): Release Version: v6.2.0-alpha-230-g47a4f3a01
Edition: Community
Git Commit Hash: 47a4f3a017768dda0b9a826451486c5539bc8427
Git Branch: master
UTC Build Time: 2022-07-08 20:42:13
GoVersion: go1.18.1
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
Store: unistore
```
Contributor guide
Assessment
This issue has not been assessed yet.