Skip to content

[Bug] Doris 4.0.5 CAST string scientific notation to DECIMAL returns incorrect result #63085

@garywangcoding

Description

@garywangcoding

Search before asking

  • I had searched in the issues and found no similar issues.

Version

Apache Doris 4.0.5

This issue does not exist in Doris 2.1 according to my test.

What's Wrong?

In Doris 4.0.5, casting a VARCHAR value in scientific notation to DECIMAL returns an incorrect result.

For example:

`CAST('1.4E+4' AS DECIMAL(38, 15))`

Actual result:

14.000000000000000

Expected result:

14000.000000000000000

The same issue also happens for other scientific notation strings, such as:

2.128E+4
-1.4E+4
1.4e+4

However, casting the same string to DOUBLE works correctly:

`CAST('1.4E+4' AS DOUBLE)`
-- 14000

Also, casting via DOUBLE works correctly:

`CAST(CAST('1.4E+4' AS DOUBLE) AS DECIMAL(38, 15))`
-- 14000.000000000000000

What You Expected?

CAST(VARCHAR scientific notation AS DECIMAL) should correctly parse scientific notation.

Expected:

CAST('1.4E+4' AS DECIMAL(38, 15))
-- 14000.000000000000000

CAST('2.128E+4' AS DECIMAL(38, 15))
-- 21280.000000000000000

CAST('-1.4E+4' AS DECIMAL(38, 15))
-- -14000.000000000000000

How to Reproduce?

CREATE TABLE t (
    s VARCHAR(64)
)
PROPERTIES (
    "replication_num" = "1"
);

INSERT INTO t VALUES
('1.4E+4'),
('2.128E+4'),
('1.4e+4'),
('-1.4E+4'),
('0E-9'),
('1.60'),
('14000');

SELECT
    s AS raw_str,
    CAST(s AS DECIMAL(38, 15)) AS col_cast_decimal,
    CAST(s AS DOUBLE) AS col_cast_double,
    CAST(CAST(s AS DOUBLE) AS DECIMAL(38, 15)) AS via_double
FROM t
ORDER BY s;

Actual result in Doris 4.0.5:

+----------+------------------------+-----------------+------------------------+
| raw_str  | col_cast_decimal       | col_cast_double | via_double             |
+----------+------------------------+-----------------+------------------------+
| -1.4E+4  | -14.000000000000000    | -14000          | -14000.000000000000000 |
| 0E-9     | 0.000000000000000      | 0               | 0.000000000000000      |
| 1.4E+4   | 14.000000000000000     | 14000           | 14000.000000000000000  |
| 1.4e+4   | 14.000000000000000     | 14000           | 14000.000000000000000  |
| 1.60     | 1.600000000000000      | 1.6             | 1.600000000000000      |
| 14000    | 14000.000000000000000  | 14000           | 14000.000000000000000  |
| 2.128E+4 | 2128.000000000000000   | 21280           | 21280.000000000000000  |
+----------+------------------------+-----------------+------------------------+

Anything Else?

This seems to be a regression because the same case works correctly in Doris 2.1.

The issue seems to be specific to the direct cast path:

VARCHAR -> DECIMAL

because these paths work correctly:

VARCHAR -> DOUBLE
VARCHAR -> DOUBLE -> DECIMAL

This is risky for financial or balance-related data, because scientific notation strings may be silently converted to incorrect DECIMAL values instead of throwing an error.

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions