Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bolt/expression/CastExpr-tpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ class Converter {
constexpr TypeKind originKind = ToKind::storage;
using LimitType = typename util::
Converter<originKind, void, util::TruncateCastPolicy>::LimitType;
if (from > LimitType::maxLimit()) {
if (LimitType::isPositiveOverflow(from)) {
to = LimitType::max();
return ConvertStatus::INTEGER_OVERFLOW;
}
Expand Down
12 changes: 12 additions & 0 deletions bolt/functions/sparksql/tests/SparkCastExprTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,18 @@ TEST_F(SparkCastExprTest, overflow) {
testCast<double, int8_t>("tinyint", {127.8}, {127});
testCast<double, int8_t>("tinyint", {129.9}, {-127});
testCast<double, int16_t>("smallint", {1234567.89}, {-10617});
testCast<float, int32_t>(
"integer",
{0x1.fffffep30f, 0x1p31f, -0x1p31f},
{2'147'483'520,
std::numeric_limits<int32_t>::max(),
std::numeric_limits<int32_t>::min()});
testCast<double, int64_t>(
"bigint",
{0x1.fffffffffffffp62, 0x1p63, -0x1p63},
{9'223'372'036'854'774'784LL,
std::numeric_limits<int64_t>::max(),
std::numeric_limits<int64_t>::min()});
testCast<double, int64_t>(
"bigint", {std::numeric_limits<double>::max()}, {9223372036854775807});
testCast<double, int64_t>(
Expand Down
15 changes: 13 additions & 2 deletions bolt/type/Conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,17 @@ struct Converter<
struct LimitType {
static constexpr bool kByteOrSmallInt =
std::is_same_v<T, int8_t> || std::is_same_v<T, int16_t>;
using FirstStageType = std::conditional_t<kByteOrSmallInt, int32_t, T>;

template <typename FP>
static bool isPositiveOverflow(const FP& value) {
// Integer max may round up to the first out-of-range value when it is
// converted to FP. The negative minimum is a power of two, so its
// negation gives an exact exclusive upper bound.
return value >=
-static_cast<FP>(std::numeric_limits<FirstStageType>::min());
}

static int64_t minLimit() {
if (kByteOrSmallInt) {
return std::numeric_limits<int32_t>::min();
Expand Down Expand Up @@ -363,7 +374,7 @@ struct Converter<
}
if constexpr (std::is_same_v<T, int128_t>) {
return std::numeric_limits<int128_t>::max();
} else if (v > LimitType::maxLimit()) {
} else if (LimitType::isPositiveOverflow(v)) {
return LimitType::max();
}
if constexpr (std::is_same_v<T, int128_t>) {
Expand Down Expand Up @@ -392,7 +403,7 @@ struct Converter<
}
if constexpr (std::is_same_v<T, int128_t>) {
return std::numeric_limits<int128_t>::max();
} else if (v > LimitType::maxLimit()) {
} else if (LimitType::isPositiveOverflow(v)) {
return LimitType::max();
}
if constexpr (std::is_same_v<T, int128_t>) {
Expand Down
44 changes: 41 additions & 3 deletions bolt/type/tests/ConversionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,12 @@ TEST_F(ConversionsTest, toIntegeralTypes) {
12345,
},
/*truncate*/ true);
testConversion<double, int64_t>(
{0x1.fffffffffffffp62, 0x1p63, -0x1p63},
{9'223'372'036'854'774'784LL,
std::numeric_limits<int64_t>::max(),
std::numeric_limits<int64_t>::min()},
/*truncate*/ true);
testConversion<double, int32_t>(
{
1.888,
Expand All @@ -385,6 +391,16 @@ TEST_F(ConversionsTest, toIntegeralTypes) {
-100,
},
/*truncate*/ true);
testConversion<double, int32_t>(
{0x1.fffffffffffffp30, 0x1p31, -0x1p31},
{std::numeric_limits<int32_t>::max(),
std::numeric_limits<int32_t>::max(),
std::numeric_limits<int32_t>::min()},
/*truncate*/ true);
testConversion<double, int16_t>(
{0x1.fffffffffffffp30, 0x1p31, -0x1p31},
{-1, -1, 0},
/*truncate*/ true);
testConversion<double, int8_t>(
{
12345.67,
Expand All @@ -397,6 +413,10 @@ TEST_F(ConversionsTest, toIntegeralTypes) {
127,
},
/*truncate*/ true);
testConversion<double, int8_t>(
{0x1.fffffffffffffp30, 0x1p31, -0x1p31},
{-1, -1, 0},
/*truncate*/ true);
testConversion<double, int8_t>(
{
1,
Expand Down Expand Up @@ -440,9 +460,27 @@ TEST_F(ConversionsTest, toIntegeralTypes) {
// When TRUNCATE = true.
testConversion<float, int64_t>(
{kInf, kNan}, {9223372036854775807, 0}, /*truncate*/ true);
testConversion<float, int32_t>({kNan}, {0}, /*truncate*/ true);
testConversion<float, int16_t>({kNan}, {0}, /*truncate*/ true);
testConversion<float, int8_t>({kNan}, {0}, /*truncate*/ true);
testConversion<float, int64_t>(
{0x1.fffffep62f, 0x1p63f, -0x1p63f},
{9'223'371'487'098'961'920LL,
std::numeric_limits<int64_t>::max(),
std::numeric_limits<int64_t>::min()},
/*truncate*/ true);
testConversion<float, int32_t>(
{kNan, 0x1.fffffep30f, 0x1p31f, -0x1p31f},
{0,
2'147'483'520,
std::numeric_limits<int32_t>::max(),
std::numeric_limits<int32_t>::min()},
/*truncate*/ true);
testConversion<float, int16_t>(
{kNan, 0x1.fffffep30f, 0x1p31f, -0x1p31f},
{0, -128, -1, 0},
/*truncate*/ true);
testConversion<float, int8_t>(
{kNan, 0x1.fffffep30f, 0x1p31f, -0x1p31f},
{0, -128, -1, 0},
/*truncate*/ true);
}

// From string.
Expand Down