Permalink
Browse files

Extend the logic to map the name of a function used in SQL to the Ope…

…ratorId and implementation of the funcion.
  • Loading branch information...
malin1993ml authored and hzxa21 committed Nov 27, 2017
1 parent cb443fe commit 601fb33cd1c5f062ef231a068e1851feb1cd9876
Showing with 42 additions and 12 deletions.
  1. +4 −1 src/binder/bind_node_visitor.cpp
  2. +2 −2 src/catalog/catalog.cpp
  3. +21 −6 src/function/functions.cpp
  4. +15 −3 src/include/function/functions.h
@@ -203,7 +203,10 @@ void BindNodeVisitor::Visit(expression::FunctionExpression *expr) {
// we have to do the string comparison to find out the corresponding
// DatePartType when scanning every tuple.
auto func_name = expr->GetFuncName();
if (func_name == "date_trunc" || func_name == "extract") {
auto func_operator_id =
function::BuiltInFunctions::GetFuncBySQLName(func_name).op_id;
if (func_operator_id == OperatorId::DateTrunc ||
func_operator_id == OperatorId::Extract) {
// Check the type of the first argument. Should be VARCHAR
auto date_part = expr->GetChild(0);
if (date_part->GetValueType() != type::TypeId::VARCHAR) {
View
@@ -817,7 +817,7 @@ void Catalog::AddBuiltinFunction(
txn)) {
throw CatalogException("Failed to add function " + func_name);
}
function::BuiltInFunctions::AddFunction(func_name, func);
function::BuiltInFunctions::AddFunction(name, func_name, func);
}
const FunctionData Catalog::GetFunction(
@@ -849,7 +849,7 @@ const FunctionData Catalog::GetFunction(
result.argument_types_ = argument_types;
result.func_name_ = proc_catalog_obj->GetSrc();
result.return_type_ = proc_catalog_obj->GetRetType();
result.func_ = function::BuiltInFunctions::GetFuncByName(result.func_name_);
result.func_ = function::BuiltInFunctions::GetFuncBySourceName(result.func_name_);
if (result.func_.impl == nullptr) {
txn_manager.AbortTransaction(txn);
View
@@ -15,16 +15,31 @@
namespace peloton {
namespace function {
std::unordered_map<std::string, BuiltInFuncType> BuiltInFunctions::kFuncMap;
std::unordered_map<std::string, BuiltInFuncType>
BuiltInFunctions::kSourceNameFuncMap;
std::unordered_map<std::string, BuiltInFuncType>
BuiltInFunctions::kSQLNameFuncMap;
void BuiltInFunctions::AddFunction(const std::string &func_name,
void BuiltInFunctions::AddFunction(const std::string &sql_func_name,
const std::string &source_func_name,
BuiltInFuncType func) {
kFuncMap.emplace(func_name, func);
kSourceNameFuncMap.emplace(source_func_name, func);
kSQLNameFuncMap.emplace(sql_func_name, func);
}
BuiltInFuncType BuiltInFunctions::GetFuncByName(const std::string &func_name) {
auto func = kFuncMap.find(func_name);
if (func == kFuncMap.end()) {
BuiltInFuncType BuiltInFunctions::GetFuncBySourceName(
const std::string &func_name) {
auto func = kSourceNameFuncMap.find(func_name);
if (func == kSourceNameFuncMap.end()) {
return {OperatorId::Invalid, nullptr};
}
return func->second;
}
BuiltInFuncType BuiltInFunctions::GetFuncBySQLName(
const std::string &func_name) {
auto func = kSQLNameFuncMap.find(func_name);
if (func == kSQLNameFuncMap.end()) {
return {OperatorId::Invalid, nullptr};
}
return func->second;
@@ -32,12 +32,24 @@ struct BuiltInFuncType {
class BuiltInFunctions {
private:
static std::unordered_map<std::string, BuiltInFuncType> kFuncMap;
// Map the function name in C++ source (should be unique) to the actual
// function implementation
static std::unordered_map<std::string, BuiltInFuncType> kSourceNameFuncMap;
// Map the function name used in SQL to the actual function implementation.
// Different SQL names can map to the same function implementation(alias).
static std::unordered_map<std::string, BuiltInFuncType> kSQLNameFuncMap;
public:
static void AddFunction(const std::string &func_name, BuiltInFuncType func);
static void AddFunction(const std::string &sql_func_name,
const std::string &source_func_name,
BuiltInFuncType func);
// Get the function from the name in C++ source code
static BuiltInFuncType GetFuncBySourceName(const std::string &func_name);
static BuiltInFuncType GetFuncByName(const std::string &func_name);
// Get the function from the name used in SQL
static BuiltInFuncType GetFuncBySQLName(const std::string &func_name);
};
} // namespace function

0 comments on commit 601fb33

Please sign in to comment.