Skip to content

Commit

Permalink
Fix cast datetime to decimal wrong result bug (#4152) (#4156)
Browse files Browse the repository at this point in the history
close #4151
  • Loading branch information
ti-chi-bot authored Jun 15, 2022
1 parent 5c2bacf commit 5c0c626
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
6 changes: 3 additions & 3 deletions dbms/src/Functions/FunctionsTiDBConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -809,11 +809,11 @@ struct TiDBConvertToDecimal
template <typename U>
static U toTiDBDecimal(MyDateTime & date_time, PrecType prec, ScaleType scale, int fsp, const Context & context)
{
UInt64 value_without_fsp = date_time.year * 10000000000ULL + date_time.month * 100000000ULL + date_time.day * 100000
+ date_time.hour * 1000 + date_time.minute * 100 + date_time.second;
UInt64 value_without_fsp = date_time.year * 10000000000ULL + date_time.month * 100000000ULL + date_time.day * 1000000ULL
+ date_time.hour * 10000ULL + date_time.minute * 100 + date_time.second;
if (fsp > 0)
{
Int128 value = value_without_fsp * 1000000 + date_time.micro_second;
Int128 value = static_cast<Int128>(value_without_fsp) * 1000000 + date_time.micro_second;
Decimal128 decimal(value);
return toTiDBDecimal<Decimal128, U>(decimal, 6, prec, scale, context);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/fullstack-test/expr/cast_as_decimal.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
mysql> drop table if exists test.t1;
mysql> create table test.t1(c1 datetime(5));
mysql> insert into test.t1 values('2022-10-10 10:10:10.12345');
mysql> alter table test.t1 set tiflash replica 1;
func> wait_table test t1
mysql> set @@tidb_isolation_read_engines='tiflash'; set @@tidb_enforce_mpp = 1; select cast(test.t1.c1 as decimal(16, 3)) from test.t1;
+------------------------------------+
| cast(test.t1.c1 as decimal(16, 3)) |
+------------------------------------+
| 9999999999999.999 |
+------------------------------------+
mysql> set @@tidb_isolation_read_engines='tiflash'; set @@tidb_enforce_mpp = 1; select cast(test.t1.c1 as decimal(17, 3)) from test.t1;
+------------------------------------+
| cast(test.t1.c1 as decimal(17, 3)) |
+------------------------------------+
| 20221010101010.123 |
+------------------------------------+
mysql> set @@tidb_isolation_read_engines='tiflash'; set @@tidb_enforce_mpp = 1; select cast(test.t1.c1 as decimal(18, 3)) from test.t1;
+------------------------------------+
| cast(test.t1.c1 as decimal(18, 3)) |
+------------------------------------+
| 20221010101010.123 |
+------------------------------------+

0 comments on commit 5c0c626

Please sign in to comment.