# 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 float, b double, c decimal(20, 10), d int, e int, f varchar(20))
mysql> alter table test.t set tiflash replica 1
mysql> insert into test.t values (12.1235, 12.1235, 12.1235, 1234, 5, 'en_US'),(12.1235, 12.1235, 12.1235, 1234, 4, 'enUS'),(12.1235, 12.1235, 12.1235, 1234, 3, null)

func> wait_table test t

mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select format(a, e, f) as float_val from test.t
float_val
12.12350
12.1235
12.123

mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select format(b, e, f) as double_val from test.t
double_val
12.12350
12.1235
12.124

mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select format(c, e, f) as decimal_val from test.t
decimal_val
12.12350
12.1235
12.124

mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select format(d, e, f) as int_val from test.t
int_val
1,234.00000
1,234.0000
1,234.000

mysql> drop table if exists test.t

mysql> create table test.t(id int, value decimal(65,4))
mysql> alter table test.t set tiflash replica 1
mysql> insert into test.t values(1,9999999999999999999999999999999999999999999999999999999999999.9999)

func> wait_table test t

mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select format(value,-3) as result from test.t
result
10,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000

mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select format(value,0) as result from test.t
result
10,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000

mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select format(value,3) as result from test.t
result
10,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000.000

mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select format(value,10) as result from test.t
result
9,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999.9999000000


mysql> drop table if exists test.t

mysql> create table test.t(id int, value decimal(7,4))
mysql> alter table test.t set tiflash replica 1
mysql> insert into test.t values(1,999.9999)

func> wait_table test t

mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select format(value,-2) as result from test.t
result
1,000

mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select format(value,0) as result from test.t
result
1,000

mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select format(value,2) as result from test.t
result
1,000.00

mysql> set tidb_enforce_mpp=1; set tidb_isolation_read_engines='tiflash'; select format(value,10) as result from test.t
result
999.9999000000

mysql> drop table if exists test.t
