[cherry-pick](branch-21) fix conv function parser string failure return wrong result (#40530) (#41964)

## Proposed changes

Issue Number: close #39618
cherry-pick from master (#40530)
This commit is contained in:
zhangstar333
2024-10-17 14:45:46 +08:00
committed by GitHub
parent 0b41cd2472
commit 67d057a711
3 changed files with 21 additions and 1 deletions

View File

@ -205,13 +205,20 @@ struct ConvStringImpl {
ColumnString* result_column, NullMap& result_null_map,
size_t index) {
StringRef str = data_column->get_data_at(index);
auto new_size = str.size;
// eg: select conv('1.464868',10,2); the result should be return 1.
// But StringParser::string_to_int will PARSE_FAILURE and return 0,
// so should handle the point part of number firstly if need convert '1.464868' to number 1
if (auto pos = str.to_string_view().find_first_of('.'); pos != std::string::npos) {
new_size = pos;
}
StringParser::ParseResult parse_res;
// select conv('ffffffffffffff', 24, 2);
// if 'ffffffffffffff' parse as int64_t will be overflow, will be get max value: std::numeric_limits<int64_t>::max()
// so change it parse as uint64_t, and return value could still use int64_t, in function decimal_to_base could handle it.
// But if the value is still overflow in uint64_t, will get max value of uint64_t
int64_t decimal_num =
StringParser::string_to_int<uint64_t>(str.data, str.size, src_base, &parse_res);
StringParser::string_to_int<uint64_t>(str.data, new_size, src_base, &parse_res);
if (src_base < 0 && decimal_num >= 0) {
result_null_map[index] = true;
result_column->insert_default();

View File

@ -11,3 +11,6 @@
-- !select4 --
18446744073709551615
-- !select5 --
1 1.464868

View File

@ -23,5 +23,15 @@ suite("test_conv") {
qt_select3 "select conv('-ff', 24, 2);"
// if beyond the max value of uint64, use max_uint64 as res
qt_select4 "select conv('fffffffffffffffffffffffffffffffff', 24, 10);"
sql """DROP TABLE IF EXISTS `test_tb`; """
sql """ create table test_tb(int_1 int, float_2 float) PROPERTIES (
"replication_num" = "1"
);
"""
sql """ insert into test_tb values(1, 1.464868); """
qt_select5 """ select conv(float_2,10,2),float_2 from test_tb; """
}