Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix floating to int128 and negative to int128 #2374

Merged
merged 1 commit into from
Nov 9, 2023
Merged
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
5 changes: 3 additions & 2 deletions src/common/types/int128_t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,9 @@ bool TryCastInt128Template(int128_t input, DST& result) {
throw common::OverflowException(
"Cast failed. Cannot cast " + Int128_t::ToString(input) + " to unsigned type.");
}
if (input.low >= uint64_t(function::NumericLimits<DST>::maximum()) -
if (input.low >= function::NumericLimits<uint64_t>::maximum() -
uint64_t(function::NumericLimits<DST>::maximum())) {
result = static_cast<DST>(function::NumericLimits<DST>::maximum() - input.low) - 1;
result = -DST(function::NumericLimits<uint64_t>::maximum() - input.low) - 1;
return true;
}
break;
Expand Down Expand Up @@ -520,6 +520,7 @@ bool castFloatingToInt128(REAL_T value, int128_t& result) {
if (negative) {
value = -value;
}
value = std::nearbyint(value);
result.low = (uint64_t)fmod(value, REAL_T(function::NumericLimits<uint64_t>::maximum()));
result.high = (uint64_t)(value / REAL_T(function::NumericLimits<uint64_t>::maximum()));
if (negative) {
Expand Down
11 changes: 9 additions & 2 deletions test/test_files/tinysnb/function/cast.test
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@
-LOG CastFromFLOAT
-STATEMENT MATCH (p:person) WHERE p.ID=0 RETURN to_int16(p.height), to_int32(p.height), to_int64(p.height), to_int128(p.height), to_double(p.height)
---- 1
2|2|2|1|1.731000
2|2|2|2|1.731000
-STATEMENT MATCH (p:person) WHERE p.ID=0 RETURN cast(p.height, "uint8"), cast(p.height, "int16"), cast(p.height, "int32"), cast(p.height, "int64"), cast(p.height, "int128"), cast(p.height, "double"), cast(p.height, "float")
---- 1
2|2|2|2|1|1.731000|1.731000
2|2|2|2|2|1.731000|1.731000

-LOG CastFromDOUBLE
-STATEMENT MATCH (p:person) WHERE p.ID=0 RETURN to_int16(p.eyeSight), to_int32(p.eyeSight), to_int64(p.eyeSight), to_int128(p.eyeSight), to_float(p.eyeSight)
Expand Down Expand Up @@ -1088,3 +1088,10 @@ False
-STATEMENT RETURN cast("[423, 321, 423]", "INT64[3]"), cast(null, "INT64[5]"), cast("[432.43214]", "FLOAT[1]"), cast("[4, -5]", "double[2]"), cast("[4234, 42312, 432, 1321]", "INT32[4]"), cast("[-32768]", "INT16[1]")
---- 1
[423,321,423]||[432.432129]|[4.000000,-5.000000]|[4234,42312,432,1321]|[-32768]
-STATEMENT Return cast(cast(-4324324, "int128"), "int64")
---- 1
-4324324

-STATEMENT Return to_int64(to_int128(-4324324))
---- 1
-4324324