# 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> create table test.t(a char(10), b int, c tinyint unsigned)
mysql> insert into test.t values('', -3, 2), ('abc', -3, 2)
mysql> alter table test.t set tiflash replica 1

func> wait_table test t

mysql> set session tidb_isolation_read_engines='tiflash'; set tidb_allow_mpp=0; set tidb_allow_tiflash_cop = ON; select a from test.t where substring(a, -3, 4) = 'abc'
a
abc

mysql> set session tidb_isolation_read_engines='tiflash'; set tidb_allow_mpp=0; set tidb_allow_tiflash_cop = ON; select a from test.t where substring(a, -3, 2) = 'ab'
a
abc

mysql> set session tidb_isolation_read_engines='tiflash'; set tidb_allow_mpp=0; set tidb_allow_tiflash_cop = ON; select a from test.t where substring(a, b, c) = 'ab'
a
abc

mysql> set session tidb_isolation_read_engines='tiflash'; set tidb_allow_mpp=0; set tidb_allow_tiflash_cop = ON; select a from test.t where substring(a, -4, 3) = 'abc'
# Empty

mysql> set session tidb_isolation_read_engines='tiflash'; set tidb_allow_mpp=0; set tidb_allow_tiflash_cop = ON; select count(*) from test.t where substring(a, 0, 3) = '' order by a
count(*)
2

mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select substring(t2.a, 1, 3) as a from ( select 1 a from test.t ) t2
a
1
1

mysql> drop table if exists test.t

mysql> drop table if exists test.t1
mysql> create table test.t1 (c1 int, c2 int)
mysql> insert into test.t1 (c1, c2) values (-82 ,  -4), (  1 ,   4)
mysql> alter table test.t1 set tiflash replica 1
func> wait_table test t1

mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select c2, substring('abc', c2) as b FROM test.t1 order by 1, 2
+------+------+
| c2   | b    |
+------+------+
|   -4 |      |
|    4 |      |
+------+------+

mysql> set session tidb_isolation_read_engines='tiflash'; set tidb_allow_mpp=0; set tidb_allow_tiflash_cop = ON; select c2, substring('abc', c2) as b FROM test.t1 order by 1, 2
+------+------+
| c2   | b    |
+------+------+
|   -4 |      |
|    4 |      |
+------+------+

mysql> drop table if exists test.t1
