Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cast datetime to decimal wrong result bug (#4152) #4155

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions dbms/src/Functions/FunctionsTiDBConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -799,11 +799,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 |
+------------------------------------+