Permalink
Browse files

Floor function addition

  • Loading branch information...
saatviks authored and apavlo committed Nov 10, 2017
1 parent b117a8a commit 2a385baf738d45372d5009f95df6b1b126e6b24a
View
@@ -968,6 +968,12 @@ void Catalog::InitializeFunctions() {
"Sqrt", function::BuiltInFuncType{OperatorId::Sqrt,
function::DecimalFunctions::Sqrt},
txn);
AddBuiltinFunction(
"floor", {type::TypeId::DECIMAL}, type::TypeId::DECIMAL, internal_lang,
"Floor", function::BuiltInFuncType{OperatorId::Floor,
function::DecimalFunctions::_Floor},
txn);
/**
* date functions
@@ -10,15 +10,15 @@
//
//===----------------------------------------------------------------------===//
#include "codegen/proxy/string_functions_proxy.h"
#include "codegen/proxy/decimal_functions_proxy.h"
#include "codegen/proxy/type_builder.h"
#include "function/string_functions.h"
#include "function/decimal_functions.h"
namespace peloton {
namespace codegen {
// DEFINE_METHOD(peloton::function, DecimalFunctions, Floor);
DEFINE_METHOD(peloton::function, DecimalFunctions, Floor);
} // namespace codegen
} // namespace peloton
@@ -15,6 +15,7 @@
#include "codegen/lang/if.h"
#include "codegen/value.h"
#include "codegen/proxy/values_runtime_proxy.h"
#include "codegen/proxy/decimal_functions_proxy.h"
#include "codegen/type/boolean_type.h"
#include "codegen/type/integer_type.h"
#include "common/exception.h"
@@ -159,6 +160,23 @@ struct Negate : public TypeSystem::UnaryOperator {
}
};
struct Floor : public TypeSystem::UnaryOperator {
bool SupportsType(const Type &type) const override {
return type.GetSqlType() == Decimal::Instance();
}
Type ResultType(UNUSED_ATTRIBUTE const Type &val_type) const override {
return Type{Decimal::Instance()};
}
Value DoWork(CodeGen &codegen, const Value &val) const override {
llvm::Value *raw_ret = codegen.Call(DecimalFunctionsProxy::Floor,
{val.GetValue()});
return Value{Integer::Instance(), raw_ret};
}
};
// Addition
struct Add : public TypeSystem::BinaryOperator {
bool SupportsTypes(const Type &left_type,
@@ -367,8 +385,9 @@ static std::vector<TypeSystem::ComparisonInfo> kComparisonTable = {
// Unary operators
static Negate kNegOp;
static Floor kFloorOp;
static std::vector<TypeSystem::UnaryOpInfo> kUnaryOperatorTable = {
{OperatorId::Negation, kNegOp}};
{OperatorId::Negation, kNegOp}, {OperatorId::Floor, kFloorOp}};
// Binary operations
static Add kAddOp;
@@ -25,5 +25,19 @@ type::Value DecimalFunctions::Sqrt(const std::vector<type::Value> &args) {
return args[0].Sqrt();
}
// Get square root of the 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>());
return type::ValueFactory::GetDecimalValue(res);
}
double DecimalFunctions::Floor(const double val) {
return floor(val);
}
} // namespace function
} // namespace peloton
@@ -19,7 +19,7 @@ namespace codegen {
PROXY(DecimalFunctions) {
// Proxy everything in function::DecimalFunctions
// DECLARE_METHOD(Floor);
DECLARE_METHOD(Floor);
};
} // namespace codegen
@@ -22,6 +22,8 @@ namespace function {
class DecimalFunctions {
public:
static type::Value Sqrt(const std::vector<type::Value>& args);
static type::Value _Floor(const std::vector<type::Value>& args);
static double Floor(const double val);
};
} // namespace function
View
@@ -1043,6 +1043,7 @@ enum class OperatorId : uint32_t {
BTrim,
Sqrt,
Extract,
Floor,
// Add more operators here, before the last "Invalid" entry
@@ -47,5 +47,22 @@ TEST_F(DecimalFunctionsTests, SqrtTest) {
EXPECT_TRUE(result.IsNull());
}
TEST_F(DecimalFunctionsTests, FloorTest) {
std::vector<double> inputs = {9.5, 3.3, -4.4, 0.0};
std::vector<type::Value> args;
for(double in: inputs) {
args = {type::ValueFactory::GetDecimalValue(in)};
auto result = function::DecimalFunctions::_Floor(args);
EXPECT_FALSE(result.IsNull());
EXPECT_EQ(floor(in), result.GetAs<double>());
}
// NULL CHECK
args = {type::ValueFactory::GetNullValueByType(type::TypeId::DECIMAL)};
auto result = function::DecimalFunctions::_Floor(args);
EXPECT_TRUE(result.IsNull());
}
} // namespace test
} // namespace peloton
@@ -0,0 +1,81 @@
//===----------------------------------------------------------------------===//
//
// Peloton
//
// distinct_sql_test.cpp
//
// Identification: test/sql/distinct_sql_test.cpp
//
// Copyright (c) 2015-17, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#include <memory>
#include "sql/testing_sql_util.h"
#include "catalog/catalog.h"
#include "common/harness.h"
#include "concurrency/transaction_manager_factory.h"
namespace peloton {
namespace test {
class DecimalFunctionsSQLTest : public PelotonTest {};
TEST_F(DecimalFunctionsSQLTest, FloorTest) {
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
auto txn = txn_manager.BeginTransaction();
catalog::Catalog::GetInstance()->CreateDatabase(DEFAULT_DB_NAME, txn);
catalog::Catalog::GetInstance()->Bootstrap();
txn_manager.CommitTransaction(txn);
// Create a t
txn = txn_manager.BeginTransaction();
TestingSQLUtil::ExecuteSQLQuery(
"CREATE TABLE foo(id integer, income decimal);");
// Adding in 2500 random decimal inputs between [-500, 500]
int i;
std::vector<double> inputs;
int lo = -500;
int hi = 500;
int numEntries = 500;
// Setting a seed
std::srand(std::time(0));
for (i = 0; i < numEntries; i++) {
double num = 0.45 + (std::rand() % (hi - lo));
inputs.push_back(num);
std::ostringstream os;
os << "insert into foo values(" << i << ", " << num << ");";
TestingSQLUtil::ExecuteSQLQuery(os.str());
}
EXPECT_EQ(i, numEntries);
// Fetch values from the table
std::vector<StatementResult> result;
std::vector<FieldInfo> tuple_descriptor;
std::string error_message;
int rows_affected;
std::ostringstream os;
os << "select id, floor(income) from foo;";
TestingSQLUtil::ExecuteSQLQuery(os.str(), result, tuple_descriptor,
rows_affected, error_message);
for (i = 0; i < numEntries; i++) {
std::string result_id(
TestingSQLUtil::GetResultValueAsString(result, (2 * i)));
std::string result_income(
TestingSQLUtil::GetResultValueAsString(result, (2 * i) + 1));
int id = std::stoi(result_id);
double income = std::stod(result_income);
EXPECT_EQ(id, i);
EXPECT_DOUBLE_EQ(income, floor(inputs[i]));
}
// free the database just created
txn = txn_manager.BeginTransaction();
catalog::Catalog::GetInstance()->DropDatabaseWithName(DEFAULT_DB_NAME, txn);
txn_manager.CommitTransaction(txn);
}
} // namespace test
} // namespace peloton

0 comments on commit 2a385ba

Please sign in to comment.