Skip to content

Commit

Permalink
Fix cudf::strings::is_fixed_point checking of overflow for decimal32 (#…
Browse files Browse the repository at this point in the history
…9093)

While working on `decimal128` support, @codereport found a bug in the `cudf::strings::is_fixed_point` logic where a large integer (in a strings column) could return true/valid even though it overflows the `Rep` type for `decimal32 type`. The gtest values did not include a value that would have shown this error. This PR adds the test string and fixes the logic properly check the overflow condition.

The current logic was relying on storing intermediate values into `uint64_t` types so any number that would fit in `uint64_t` would not be detected as overflow for `decimal32`. This PR fixes functions to use the input type storage type more to help identify the overflow correctly and to help with specializing for `decimal128`.

Authors:
  - David Wendt (https://github.com/davidwendt)

Approvers:
  - Conor Hoekstra (https://github.com/codereport)
  - Nghia Truong (https://github.com/ttnghia)

URL: #9093
  • Loading branch information
davidwendt authored Aug 25, 2021
1 parent 2a566dd commit d29c441
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
28 changes: 18 additions & 10 deletions cpp/include/cudf/strings/detail/convert/fixed_point.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,34 @@
#include <thrust/optional.h>
#include <thrust/pair.h>

#include <type_traits>

namespace cudf {
namespace strings {
namespace detail {

/**
* @brief Return the integer component of a decimal string.
*
* This is reads everything up to the exponent 'e' notation.
* This reads everything up to the exponent 'e' notation.
* The return includes the integer digits and any exponent offset.
*
* @tparam UnsignedDecimalType The unsigned version of the desired decimal type.
* Use the `std::make_unsigned_t` to create the
* unsigned type from the storage type.
*
* @param[in,out] iter Start of characters to parse
* @param[in] end End of characters to parse
* @return Integer component and exponent offset.
*/
__device__ inline thrust::pair<uint64_t, int32_t> parse_integer(char const*& iter,
char const* iter_end,
const char decimal_pt_char = '.')
template <typename UnsignedDecimalType>
__device__ inline thrust::pair<UnsignedDecimalType, int32_t> parse_integer(
char const*& iter, char const* iter_end, const char decimal_pt_char = '.')
{
// highest value where another decimal digit cannot be appended without an overflow;
// this preserves the most digits when scaling the final result
constexpr uint64_t decimal_max = (std::numeric_limits<uint64_t>::max() - 9L) / 10L;
// this preserves the most digits when scaling the final result for this type
constexpr UnsignedDecimalType decimal_max =
(std::numeric_limits<UnsignedDecimalType>::max() - 9L) / 10L;

uint64_t value = 0; // for checking overflow
int32_t exp_offset = 0;
Expand All @@ -56,7 +63,7 @@ __device__ inline thrust::pair<uint64_t, int32_t> parse_integer(char const*& ite
if (value > decimal_max) {
exp_offset += static_cast<int32_t>(!decimal_found);
} else {
value = (value * 10) + static_cast<uint64_t>(ch - '0');
value = (value * 10) + static_cast<UnsignedDecimalType>(ch - '0');
exp_offset -= static_cast<int32_t>(decimal_found);
}
}
Expand Down Expand Up @@ -130,7 +137,8 @@ __device__ DecimalType parse_decimal(char const* iter, char const* iter_end, int
// if string begins with a sign, continue with next character
if (sign != 0) ++iter;

auto [value, exp_offset] = parse_integer(iter, iter_end);
using UnsignedDecimalType = std::make_unsigned_t<DecimalType>;
auto [value, exp_offset] = parse_integer<UnsignedDecimalType>(iter, iter_end);
if (value == 0) { return DecimalType{0}; }

// check for exponent
Expand All @@ -143,9 +151,9 @@ __device__ DecimalType parse_decimal(char const* iter, char const* iter_end, int

// shift the output value based on the exp_ten and the scale values
if (exp_ten < scale) {
value = value / static_cast<uint64_t>(exp10(static_cast<double>(scale - exp_ten)));
value = value / static_cast<UnsignedDecimalType>(exp10(static_cast<double>(scale - exp_ten)));
} else {
value = value * static_cast<uint64_t>(exp10(static_cast<double>(exp_ten - scale)));
value = value * static_cast<UnsignedDecimalType>(exp10(static_cast<double>(exp_ten - scale)));
}

return static_cast<DecimalType>(value) * (sign == 0 ? 1 : sign);
Expand Down
12 changes: 6 additions & 6 deletions cpp/src/strings/convert/convert_fixed_point.cu
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ struct string_to_decimal_check_fn {

auto const iter_end = d_str.data() + d_str.size_bytes();

auto [value, exp_offset] = parse_integer(iter, iter_end);
using UnsignedDecimalType = std::make_unsigned_t<DecimalType>;
auto [value, exp_offset] = parse_integer<UnsignedDecimalType>(iter, iter_end);

// only exponent notation is expected here
if ((iter < iter_end) && (*iter != 'e' && *iter != 'E')) { return false; }
Expand All @@ -112,11 +113,10 @@ struct string_to_decimal_check_fn {
exp_ten += exp_offset;

// finally, check for overflow based on the exp_ten and scale values
return (exp_ten < scale)
? true
: value <= static_cast<uint64_t>(
std::numeric_limits<DecimalType>::max() /
static_cast<DecimalType>(exp10(static_cast<double>(exp_ten - scale))));
return (exp_ten < scale) or
value <= static_cast<UnsignedDecimalType>(
std::numeric_limits<DecimalType>::max() /
static_cast<DecimalType>(exp10(static_cast<double>(exp_ten - scale))));
}
};

Expand Down
28 changes: 18 additions & 10 deletions cpp/tests/strings/fixed_point_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,31 +189,39 @@ TEST_F(StringsConvertTest, IsFixedPoint)
"9223372036854775807",
"-9223372036854775807",
"9223372036854775808",
"9223372036854775808000",
"100E2147483648",
});
results = cudf::strings::is_fixed_point(cudf::strings_column_view(big_numbers),
results = cudf::strings::is_fixed_point(cudf::strings_column_view(big_numbers),
cudf::data_type{cudf::type_id::DECIMAL32});
auto const expected32 =
cudf::test::fixed_width_column_wrapper<bool>({true, true, false, false, false, false, false});
auto const expected32 = cudf::test::fixed_width_column_wrapper<bool>(
{true, true, false, false, false, false, false, false});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*results, expected32);

results = cudf::strings::is_fixed_point(cudf::strings_column_view(big_numbers),
results = cudf::strings::is_fixed_point(cudf::strings_column_view(big_numbers),
cudf::data_type{cudf::type_id::DECIMAL64});
auto const expected64 =
cudf::test::fixed_width_column_wrapper<bool>({true, true, true, true, true, false, false});
auto const expected64 = cudf::test::fixed_width_column_wrapper<bool>(
{true, true, true, true, true, false, false, false});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*results, expected64);

results = cudf::strings::is_fixed_point(
cudf::strings_column_view(big_numbers),
cudf::data_type{cudf::type_id::DECIMAL32, numeric::scale_type{10}});
auto const expected32_scaled =
cudf::test::fixed_width_column_wrapper<bool>({true, true, true, true, true, true, false});
auto const expected32_scaled = cudf::test::fixed_width_column_wrapper<bool>(
{true, true, true, true, true, true, false, false});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*results, expected32_scaled);

results = cudf::strings::is_fixed_point(
cudf::strings_column_view(big_numbers),
cudf::data_type{cudf::type_id::DECIMAL64, numeric::scale_type{10}});
auto const expected64_scaled_positive =
cudf::test::fixed_width_column_wrapper<bool>({true, true, true, true, true, true, true, false});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*results, expected64_scaled_positive);

results = cudf::strings::is_fixed_point(
cudf::strings_column_view(big_numbers),
cudf::data_type{cudf::type_id::DECIMAL64, numeric::scale_type{-5}});
auto const expected64_scaled =
cudf::test::fixed_width_column_wrapper<bool>({true, true, true, false, false, false, false});
auto const expected64_scaled = cudf::test::fixed_width_column_wrapper<bool>(
{true, true, true, false, false, false, false, false});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*results, expected64_scaled);
}

0 comments on commit d29c441

Please sign in to comment.