Skip to content

Commit

Permalink
Fix convertStringToInt on "+1" input (#8064)
Browse files Browse the repository at this point in the history
Summary:
Fix below exception in convertStringToInt on "+1" input.

```
C++ exception with description "Exception: VeloxUserError
Error Source: USER
Error Code: INVALID_ARGUMENT
Reason: Cannot cast VARCHAR '+1' to TINYINT. Encountered a non-digit character
```

Pull Request resolved: #8064

Reviewed By: xiaoxmeng

Differential Revision: D52204221

Pulled By: mbasmanova

fbshipit-source-id: adf0fed8277c6a074ec3bdaa7f9a977708736943
  • Loading branch information
rui-mo authored and facebook-github-bot committed Dec 15, 2023
1 parent c4fdbb3 commit 01756db
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
2 changes: 2 additions & 0 deletions velox/docs/functions/presto/conversion.rst
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ Valid examples if cast_to_int_by_truncate=true
SELECT cast('12345.67' as bigint); -- 12345
SELECT cast('1.2' as tinyint); -- 1
SELECT cast('-1.8' as tinyint); -- -1
SELECT cast('+1' as tinyint); -- 1
SELECT cast('1.' as tinyint); -- 1
SELECT cast('-1' as tinyint); -- -1
SELECT cast('-1.' as tinyint); -- -1
SELECT cast('0.' as tinyint); -- 0
SELECT cast('.' as tinyint); -- 0
Expand Down
4 changes: 2 additions & 2 deletions velox/expression/tests/CastExprTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1150,8 +1150,6 @@ TEST_F(CastExprTest, primitiveInvalidCornerCases) {
"bigint", {"infinity"}, "Encountered a non-digit character");
testInvalidCast<std::string>(
"bigint", {"nan"}, "Encountered a non-digit character");
testInvalidCast<std::string>(
"tinyint", {"+1"}, "Encountered a non-digit character");
}

// To floating-point.
Expand Down Expand Up @@ -1253,7 +1251,9 @@ TEST_F(CastExprTest, primitiveValidCornerCases) {
testCast<std::string, int8_t>("tinyint", {"1.23444"}, {1});
testCast<std::string, int8_t>("tinyint", {".2355"}, {0});
testCast<std::string, int8_t>("tinyint", {"-1.8"}, {-1});
testCast<std::string, int8_t>("tinyint", {"+1"}, {1});
testCast<std::string, int8_t>("tinyint", {"1."}, {1});
testCast<std::string, int8_t>("tinyint", {"-1"}, {-1});
testCast<std::string, int8_t>("tinyint", {"-1."}, {-1});
testCast<std::string, int8_t>("tinyint", {"0."}, {0});
testCast<std::string, int8_t>("tinyint", {"."}, {0});
Expand Down
7 changes: 4 additions & 3 deletions velox/type/Conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@ struct Converter<
bool negative = false;
// Setting decimalPoint flag
bool decimalPoint = false;
if (v[0] == '-') {
if (v[0] == '-' || v[0] == '+') {
if (len == 1) {
VELOX_USER_FAIL("Cannot cast an '-' string to an integral value.");
VELOX_USER_FAIL(
"Cannot cast an '{}' string to an integral value.", v[0]);
}
negative = true;
negative = v[0] == '-';
index = 1;
}
if (negative) {
Expand Down
6 changes: 2 additions & 4 deletions velox/type/tests/ConversionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ TEST_F(ConversionsTest, toIntegeralTypes) {
"0.",
".",
"-.",
"+1",
},
{
1,
Expand All @@ -496,12 +497,9 @@ TEST_F(ConversionsTest, toIntegeralTypes) {
0,
0,
0,
1,
},
/*truncate*/ true);

// When TRUNCATE = true, invalid cases.
testConversion<std::string, int8_t>(
{"1234567", "+1"}, {}, /*truncate*/ true, false, /*expectError*/ true);
testConversion<std::string, int64_t>(
{
"1a",
Expand Down

0 comments on commit 01756db

Please sign in to comment.