mirror of
https://github.com/dragonflydb/dragonfly.git
synced 2024-12-14 11:58:02 +00:00
fix(core): Auto cast doubles to ints in lua scripts (#922)
fix(core): Auto convert double to integers in lua scripts
This commit is contained in:
parent
b7abe269f1
commit
a37ccbfb28
2 changed files with 22 additions and 5 deletions
|
@ -84,13 +84,20 @@ void RedisTranslator::OnString(std::string_view str) {
|
|||
ArrayPost();
|
||||
}
|
||||
|
||||
// Doubles are not supported by Redis, however we can support them.
|
||||
// Here is the use-case:
|
||||
// local foo = redis.call('zscore', 'myzset', 'one')
|
||||
// assert(type(foo) == "number")
|
||||
void RedisTranslator::OnDouble(double d) {
|
||||
static constexpr double kConvertEps = std::numeric_limits<double>::epsilon();
|
||||
|
||||
double fractpart, intpart;
|
||||
fractpart = modf(d, &intpart);
|
||||
|
||||
ArrayPre();
|
||||
lua_pushnumber(lua_, d);
|
||||
|
||||
// Convert to integer when possible to allow converting to string without trailing zeros.
|
||||
if (abs(fractpart) < kConvertEps && intpart < std::numeric_limits<lua_Integer>::max() &&
|
||||
intpart > std::numeric_limits<lua_Integer>::min())
|
||||
lua_pushinteger(lua_, static_cast<lua_Integer>(d));
|
||||
else
|
||||
lua_pushnumber(lua_, d);
|
||||
ArrayPost();
|
||||
}
|
||||
|
||||
|
|
|
@ -381,6 +381,16 @@ TEST_F(MultiTest, Eval) {
|
|||
ASSERT_THAT(resp, ArrLen(3));
|
||||
const auto& arr = resp.GetVec();
|
||||
EXPECT_THAT(arr, ElementsAre("a", "b", "c"));
|
||||
|
||||
Run({"zadd", "z1", "123", "a", "12345678912345", "b", "12.5", "c"});
|
||||
const char* kGetScore = "return redis.call('ZSCORE', KEYS[1], ARGV[1]) .. '-works'";
|
||||
|
||||
resp = Run({"eval", kGetScore, "1", "z1", "a"});
|
||||
EXPECT_EQ(resp, "123-works");
|
||||
resp = Run({"eval", kGetScore, "1", "z1", "b"});
|
||||
EXPECT_EQ(resp, "12345678912345-works");
|
||||
resp = Run({"eval", kGetScore, "1", "z1", "c"});
|
||||
EXPECT_EQ(resp, "12.5-works");
|
||||
}
|
||||
|
||||
TEST_F(MultiTest, Watch) {
|
||||
|
|
Loading…
Reference in a new issue