Skip to content

Commit

Permalink
[Bug](conv) fix conv function parser string failure return wrong resu…
Browse files Browse the repository at this point in the history
…lt (apache#40530)

## Proposed changes

Issue Number: close apache#39618

<!--Describe your changes.-->
  • Loading branch information
zhangstar333 committed Sep 13, 2024
1 parent 3e65fab commit 0ed5540
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
9 changes: 8 additions & 1 deletion be/src/vec/functions/function_conv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@
-- !select4 --
18446744073709551615

-- !select5 --
1 1.464868

Original file line number Diff line number Diff line change
Expand Up @@ -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; """
}

0 comments on commit 0ed5540

Please sign in to comment.