apache / apache/doris

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

Open
#63,085 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
15.9k
Forks
3.9k
Avg merge
2d 23h
Merged PRs (30d)
520

Description

### Search before asking

- [x] I had searched in the [issues](https://github.com/apache/doris/issues?q=is%3Aissue) 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

- [x] I agree to follow this project's [Code of Conduct](https://www.apache.org/foundation/policies/conduct)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.