# Copyright 2023 PingCAP, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

mysql> drop table if exists test.t
mysql> drop table if exists test.t1
mysql> create table if not exists test.t(col_date date, col_datetime datetime, col_string char(24))
mysql> create table if not exists test.t1(col_int bigint(11))
mysql> insert into test.t values('2006-10-11', '2007-10-11 10:10:10', '2006-10-11 10:10:10'), ('2006-10-11', '2006-10-11 10:10:10', '2007-10-11 10:10:10')
mysql> insert into test.t1 values(0), (1), (-1), (111111),(11111111111111), (20201212), (20201212111111),  (-20201212), (-20201212111111), (99999999999999)
mysql> alter table test.t set tiflash replica 1
mysql> alter table test.t1 set tiflash replica 1

func> wait_table test t
func> wait_table test t1

mysql> set @@tidb_isolation_read_engines='tiflash'; select * from test.t where col_date = date(col_string);
+------------+---------------------+---------------------+
| col_date   | col_datetime        | col_string          |
+------------+---------------------+---------------------+
| 2006-10-11 | 2007-10-11 10:10:10 | 2006-10-11 10:10:10 |
+------------+---------------------+---------------------+

mysql> set @@tidb_isolation_read_engines='tiflash'; select * from test.t where col_date < date(col_string);
+------------+---------------------+---------------------+
| col_date   | col_datetime        | col_string          |
+------------+---------------------+---------------------+
| 2006-10-11 | 2006-10-11 10:10:10 | 2007-10-11 10:10:10 |
+------------+---------------------+---------------------+

mysql> set @@tidb_isolation_read_engines='tiflash'; select * from test.t where col_date = date(col_datetime);
+------------+---------------------+---------------------+
| col_date   | col_datetime        | col_string          |
+------------+---------------------+---------------------+
| 2006-10-11 | 2006-10-11 10:10:10 | 2007-10-11 10:10:10 |
+------------+---------------------+---------------------+

mysql> set @@tidb_isolation_read_engines='tiflash'; select * from test.t where col_date < date(col_datetime);
+------------+---------------------+---------------------+
| col_date   | col_datetime        | col_string          |
+------------+---------------------+---------------------+
| 2006-10-11 | 2007-10-11 10:10:10 | 2006-10-11 10:10:10 |
+------------+---------------------+---------------------+

mysql> set @@tidb_isolation_read_engines='tiflash'; set @@session.tidb_enforce_mpp=1; select date(col_int), col_int from test.t1;
+---------------+-----------------+
| date(col_int) | col_int         |
+---------------+-----------------+
| NULL          |               0 |
| NULL          |               1 |
| NULL          |              -1 |
| 2011-11-11    |          111111 |
| 1111-11-11    |  11111111111111 |
| 2020-12-12    |        20201212 |
| 2020-12-12    |  20201212111111 |
| NULL          |       -20201212 |
| NULL          | -20201212111111 |
| NULL          |  99999999999999 |
+---------------+-----------------+
