AcKing-Sam opened a new pull request, #32518: URL: https://github.com/apache/doris/pull/32518
## Proposed changes Issue Number: [#31517](https://github.com/apache/doris/issues/31517) **[Bug] cast date to decimal get wrong result** Below case got the wrong result: ```mysql set experimental_enable_nereids_planner = true; select cast(a as decimal) from (select cast("2000-01-01" as date) a) ta; mysql> select cast(a as decimal) from (select cast("2000-01-01" as date) a) ta; +----------------------------+ | cast(a as DECIMALV3(9, 0)) | +----------------------------+ | 1024033 | +----------------------------+ 1 row in set (0.066 sec) ``` After fix: ```mysql mysql> select cast(a as decimal) from (select cast("2000-01-01" as date) a) ta; +----------------------------+ | cast(a as DECIMALV3(9, 0)) | +----------------------------+ | 20000101 | +----------------------------+ 1 row in set (0.02 sec) ``` ## Further comments For this bug, I read the source code in `be/src/vec/functions/function_cast.h`: ```cpp if constexpr (IsDataTypeDecimal<FromDataType> || IsDataTypeDecimal<ToDataType>) { // ... } else if constexpr (IsTimeType<FromDataType>) { // ... } else if constexpr (IsTimeV2Type<FromDataType>) { // ... } else { // ... } ``` From the if-condition branch, it is clear that `cast from date to decimal` will step into the first branch(from or to decimal), and the under-lying cast for date to uint is missed. This is the reason for this bug. Therefore, I try to add cast date to int in the first branch. Then, I read the source code in `be/src/vec/data_types/data_type_decimal.h`. I found the function `convert_to_decimals` is **only for casting between other integral types and decimals**. So I add the cast in `convert_to_decimal` before calling `convert_to_decimals`. However, I think there must be some potential bugs when casting between date/datetime and decimal, which is a future work. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
