Permalink
Browse files

Addressing comments + Supporting all possible types

  • Loading branch information...
saatviks authored and apavlo committed Nov 10, 2017
1 parent 2a385ba commit b05559a67486959e5539e1fa5ef58223ef03557f
View
@@ -969,11 +969,40 @@ void Catalog::InitializeFunctions() {
function::DecimalFunctions::Sqrt},
txn);
AddBuiltinFunction(
"floor", {type::TypeId::DECIMAL}, type::TypeId::DECIMAL, internal_lang,
"floor", {type::TypeId::DECIMAL},
type::TypeId::DECIMAL, internal_lang,
"Floor", function::BuiltInFuncType{OperatorId::Floor,
function::DecimalFunctions::_Floor},
txn);
/**
* integer functions
*/
AddBuiltinFunction(
"floor", {type::TypeId::INTEGER},
type::TypeId::DECIMAL, internal_lang,
"Floor", function::BuiltInFuncType{OperatorId::Floor,
function::DecimalFunctions::_Floor},
txn);
AddBuiltinFunction(
"floor", {type::TypeId::BIGINT},
type::TypeId::DECIMAL, internal_lang,
"Floor", function::BuiltInFuncType{OperatorId::Floor,
function::DecimalFunctions::_Floor},
txn);
AddBuiltinFunction(
"floor", {type::TypeId::TINYINT},
type::TypeId::DECIMAL, internal_lang,
"Floor", function::BuiltInFuncType{OperatorId::Floor,
function::DecimalFunctions::_Floor},
txn);
AddBuiltinFunction(
"floor", {type::TypeId::SMALLINT},
type::TypeId::DECIMAL, internal_lang,
"Floor", function::BuiltInFuncType{OperatorId::Floor,
function::DecimalFunctions::_Floor},
txn);
/**
* date functions
@@ -25,13 +25,32 @@ type::Value DecimalFunctions::Sqrt(const std::vector<type::Value> &args) {
return args[0].Sqrt();
}
// Get square root of the value
// Get floor value
type::Value DecimalFunctions::_Floor(const std::vector<type::Value> &args) {
PL_ASSERT(args.size() == 1);
if (args[0].IsNull()) {
return type::ValueFactory::GetNullValueByType(type::TypeId::DECIMAL);
}
double res = Floor(args[0].GetAs<double>());
double res;
switch(args[0].GetElementType()) {
case type::TypeId::DECIMAL:
res = Floor(args[0].GetAs<double>());
break;
case type::TypeId::INTEGER:
res = args[0].GetAs<int32_t>();
break;
case type::TypeId::BIGINT:
res = args[0].GetAs<int64_t>();
break;
case type::TypeId::SMALLINT:
res = args[0].GetAs<int16_t>();
break;
case type::TypeId::TINYINT:
res = args[0].GetAs<int8_t>();
break;
default:
return type::ValueFactory::GetNullValueByType(type::TypeId::DECIMAL);
}
return type::ValueFactory::GetDecimalValue(res);
}
@@ -48,6 +48,7 @@ TEST_F(DecimalFunctionsTests, SqrtTest) {
}
TEST_F(DecimalFunctionsTests, FloorTest) {
// Testing Floor with DecimalTypes
std::vector<double> inputs = {9.5, 3.3, -4.4, 0.0};
std::vector<type::Value> args;
for(double in: inputs) {
@@ -57,10 +58,34 @@ TEST_F(DecimalFunctionsTests, FloorTest) {
EXPECT_EQ(floor(in), result.GetAs<double>());
}
// Testing Floor with Integer Types(Should be a no-op)
int64_t numInt64 = 1;
args = {type::ValueFactory::GetIntegerValue(numInt64)};
auto result = function::DecimalFunctions::_Floor(args);
EXPECT_FALSE(result.IsNull());
EXPECT_EQ(numInt64, result.GetAs<double>());
int32_t numInt32 = 1;
args = {type::ValueFactory::GetIntegerValue(numInt32)};
result = function::DecimalFunctions::_Floor(args);
EXPECT_FALSE(result.IsNull());
EXPECT_EQ(numInt32, result.GetAs<double>());
int16_t numInt16 = 1;
args = {type::ValueFactory::GetIntegerValue(numInt32)};
result = function::DecimalFunctions::_Floor(args);
EXPECT_FALSE(result.IsNull());
EXPECT_EQ(numInt16, result.GetAs<double>());
int16_t numInt8 = 1;
args = {type::ValueFactory::GetIntegerValue(numInt8)};
result = function::DecimalFunctions::_Floor(args);
EXPECT_FALSE(result.IsNull());
EXPECT_EQ(numInt8, result.GetAs<double>());
// NULL CHECK
args = {type::ValueFactory::GetNullValueByType(type::TypeId::DECIMAL)};
auto result = function::DecimalFunctions::_Floor(args);
result = function::DecimalFunctions::_Floor(args);
EXPECT_TRUE(result.IsNull());
}
@@ -2,11 +2,11 @@
//
// Peloton
//
// distinct_sql_test.cpp
// decimal_functions_sql_test.cpp
//
// Identification: test/sql/distinct_sql_test.cpp
// Identification: test/sql/decimal_functions_sql_test.cpp
//
// Copyright (c) 2015-17, Carnegie Mellon University Database Group
// Copyright (c) 2015-2017, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
@@ -55,10 +55,9 @@ class DecimalFunctionsSQLTest : public PelotonTest {};
std::vector<FieldInfo> tuple_descriptor;
std::string error_message;
int rows_affected;
std::ostringstream os;
os << "select id, floor(income) from foo;";
std::string testQuery = "select id, floor(income) from foo;";
TestingSQLUtil::ExecuteSQLQuery(os.str(), result, tuple_descriptor,
TestingSQLUtil::ExecuteSQLQuery(testQuery.c_str(), result, tuple_descriptor,
rows_affected, error_message);
for (i = 0; i < numEntries; i++) {
std::string result_id(

0 comments on commit b05559a

Please sign in to comment.