This is an automated email from the ASF dual-hosted git repository.
panxiaolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new 08740b47cd [FIX](decimalv3) fix decimalv3 value with leading zeros
(#24416)
08740b47cd is described below
commit 08740b47cd460800d3ec940e475b8e6ec452628a
Author: amory <[email protected]>
AuthorDate: Fri Sep 15 13:35:20 2023 +0800
[FIX](decimalv3) fix decimalv3 value with leading zeros (#24416)
now we make error if we deal with leading zeros in decimal value ,
type_precision >= precision will make value overflow and DCHECK will fail , so
if here has leading zero we should only make type_precision > precision to make
value right
---
be/src/util/string_parser.hpp | 6 ++++-
regression-test/data/query_p0/cast/test_cast.out | 19 ++++++++++++++++
.../suites/query_p0/cast/test_cast.groovy | 26 ++++++++++++----------
3 files changed, 38 insertions(+), 13 deletions(-)
diff --git a/be/src/util/string_parser.hpp b/be/src/util/string_parser.hpp
index 0ad46c54d0..3aa35ab47d 100644
--- a/be/src/util/string_parser.hpp
+++ b/be/src/util/string_parser.hpp
@@ -599,9 +599,11 @@ T StringParser::string_to_decimal(const char* s, int len,
int type_precision, in
}
// Ignore leading zeros.
+ bool leading_zero = false;
bool found_value = false;
while (len > 0 && UNLIKELY(*s == '0')) {
found_value = true;
+ leading_zero = true;
++s;
--len;
}
@@ -677,7 +679,9 @@ T StringParser::string_to_decimal(const char* s, int len,
int type_precision, in
for (int i = 0; i < len; ++i) {
const char& c = s[i];
// keep a rounding precision to round the decimal value
- if (LIKELY('0' <= c && c <= '9') && LIKELY(type_precision >=
precision)) {
+ if (LIKELY('0' <= c && c <= '9') &&
+ ((!leading_zero && LIKELY(type_precision >= precision)) ||
+ (leading_zero && type_precision > precision))) {
found_value = true;
// Ignore digits once the type's precision limit is reached.
This avoids
// overflowing the underlying storage while handling a string
like
diff --git a/regression-test/data/query_p0/cast/test_cast.out
b/regression-test/data/query_p0/cast/test_cast.out
new file mode 100644
index 0000000000..b4f08ded8a
--- /dev/null
+++ b/regression-test/data/query_p0/cast/test_cast.out
@@ -0,0 +1,19 @@
+-- This file is automatically generated. You should know what you did if you
want to edit this
+-- !sql_decimalv3 --
+0
+
+-- !sql_decimalv3 --
+1
+
+-- !sql_decimalv3 --
+10
+
+-- !sql_decimalv3 --
+-9.9
+
+-- !sql_decimalv3 --
+9.9
+
+-- !sql_decimalv3 --
+0.0
+
diff --git a/regression-test/suites/query_p0/cast/test_cast.groovy
b/regression-test/suites/query_p0/cast/test_cast.groovy
index 1063845775..43963efef5 100644
--- a/regression-test/suites/query_p0/cast/test_cast.groovy
+++ b/regression-test/suites/query_p0/cast/test_cast.groovy
@@ -47,21 +47,23 @@ suite('test_cast') {
result([[-9.9]])
}
+ // leading-zeros
+ qt_sql_decimalv3 """select CAST('0.29401599228723063' AS DECIMALV3)"""
- test {
- sql " select cast('100000' as DECIMALV3(2, 1)) "
- result([[9.9]])
- }
+ // not-leading-zeros
+ qt_sql_decimalv3 """select CAST('1.29401599228723063' AS DECIMALV3) """
- test {
- sql "select cast('0.2147483648e3' as DECIMALV3(2, 1))"
- result([[9.9]])
- }
+ // not-leading-zeros
+ qt_sql_decimalv3 """ select CAST('10.29401599228723063' AS DECIMALV3) """
- test {
- sql "select cast('0.2147483648e-3' as DECIMALV3(2, 1))"
- result([[0.0]])
- }
+ // overflow with min value
+ qt_sql_decimalv3 """ select cast('-100000' as DECIMALV3(2, 1)) """
+
+ // overflow with max value
+ qt_sql_decimalv3 """ select cast('0.2147483648e3' as DECIMALV3(2, 1))"""
+
+ // overflow with min value
+ qt_sql_decimalv3 """ select cast('0.2147483648e-3' as DECIMALV3(2, 1))"""
def tbl = "test_cast"
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]