Skip to content

Commit

Permalink
s3select: bug fix modulo (#144)
Browse files Browse the repository at this point in the history
fix for modulo involving floating point numbers

Albin Antony <[email protected]>
  • Loading branch information
albin-antony authored Jan 31, 2024
1 parent aef094b commit f7ed12f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
19 changes: 17 additions & 2 deletions include/s3select_oper.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,20 @@ struct binop_modulo
{
throw base_s3select_exception("Mod zero is not allowed");
} else {
return a % b;
return a % b;
}
}
};

struct binop_float_modulo
{
double operator()(double a, double b)
{
if (b == 0)
{
throw base_s3select_exception("Mod zero is not allowed");
} else {
return fmod(a, b);
}
}
};
Expand Down Expand Up @@ -1098,8 +1111,10 @@ class value

value & operator%(const value &v)
{
if(v.type == value_En_t::DECIMAL) {
if(v.type == value_En_t::DECIMAL && this->type == value_En_t::DECIMAL) {
return compute<binop_modulo>(*this,v);
} else if(v.type == value_En_t::FLOAT || this->type == value_En_t::FLOAT) {
return compute<binop_float_modulo>(*this,v);
} else {
throw base_s3select_exception("wrong use of modulo operation!");
}
Expand Down
6 changes: 6 additions & 0 deletions test/s3select_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2070,6 +2070,12 @@ TEST(TestS3selectFunctions, mod)
test_single_column_single_row( "select 5%2 from stdin;","1\n");
}

TEST(TestS3selectFunctions, modfloat)
{
test_single_column_single_row( "select 5.2%2 from stdin;","1.2000000000000002\n");
test_single_column_single_row( "select 5.2%2.5 from stdin;","0.20000000000000018\n");
}

TEST(TestS3selectFunctions, modzero)
{
test_single_column_single_row( "select 0%2 from stdin;","0\n");
Expand Down

0 comments on commit f7ed12f

Please sign in to comment.