From feaa9cb659bd7ac270be646e77dbd5bdab53e46c Mon Sep 17 00:00:00 2001 From: Gagan Goel Date: Thu, 28 Nov 2019 02:11:22 +0000 Subject: [PATCH] Implement SPACE() and QUOTE() functions. --- utils/funcexp/CMakeLists.txt | 2 + utils/funcexp/func_quote.cpp | 88 ++++++++++++++++++++++++++++++++++++ utils/funcexp/func_space.cpp | 66 +++++++++++++++++++++++++++ utils/funcexp/funcexp.cpp | 2 + utils/funcexp/functor_str.h | 34 ++++++++++++++ 5 files changed, 192 insertions(+) create mode 100644 utils/funcexp/func_quote.cpp create mode 100644 utils/funcexp/func_space.cpp diff --git a/utils/funcexp/CMakeLists.txt b/utils/funcexp/CMakeLists.txt index acee44e8d..fb9aea1f3 100644 --- a/utils/funcexp/CMakeLists.txt +++ b/utils/funcexp/CMakeLists.txt @@ -71,6 +71,7 @@ set(funcexp_LIB_SRCS func_pow.cpp func_period_add.cpp func_period_diff.cpp + func_quote.cpp func_quarter.cpp func_rand.cpp func_regexp.cpp @@ -85,6 +86,7 @@ set(funcexp_LIB_SRCS func_sec_to_time.cpp func_sha.cpp func_sign.cpp + func_space.cpp func_str_to_date.cpp func_strcmp.cpp func_substr.cpp diff --git a/utils/funcexp/func_quote.cpp b/utils/funcexp/func_quote.cpp new file mode 100644 index 000000000..2bf82870f --- /dev/null +++ b/utils/funcexp/func_quote.cpp @@ -0,0 +1,88 @@ +/* Copyright (C) 2019 MariaDB Corporation. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; version 2 of + the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + MA 02110-1301, USA. */ + +#include +using namespace std; + +#include "functor_str.h" +using namespace execplan; + +#include "rowgroup.h" +using namespace rowgroup; + +namespace funcexp +{ + +CalpontSystemCatalog::ColType Func_quote::operationType(FunctionParm& fp, CalpontSystemCatalog::ColType& resultType) +{ + return resultType; +} + +std::string Func_quote::getStrVal(rowgroup::Row& row, + FunctionParm& fp, + bool& isNull, + execplan::CalpontSystemCatalog::ColType& op_ct) +{ + string str; + + stringValue(fp[0], row, isNull, str); + + if (isNull) + { + isNull = false; + return "NULL"; + } + + if (str.empty()) + return "NULL"; + + string result; + result.reserve((str.size() * 1.3) + 2); + + result.push_back('\''); + + for (uint64_t i = 0; i < str.size(); i++) + { + switch(str[i]) + { + case 0: + result.push_back('\\'); + result.push_back('0'); + break; + case '\032': + result.push_back('\\'); + result.push_back('Z'); + break; + case '\'': + case '\\': + result.push_back('\\'); + result.push_back(str[i]); + break; + default: + result.push_back(str[i]); + break; + } + } + + result.push_back('\''); + + return result; +} + +} // namespace funcexp +// vim:ts=4 sw=4: + diff --git a/utils/funcexp/func_space.cpp b/utils/funcexp/func_space.cpp new file mode 100644 index 000000000..7b70a78da --- /dev/null +++ b/utils/funcexp/func_space.cpp @@ -0,0 +1,66 @@ +/* Copyright (C) 2019 MariaDB Corporation. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; version 2 of + the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + MA 02110-1301, USA. */ + +#include +using namespace std; + +#include "functor_str.h" +using namespace execplan; + +#include "rowgroup.h" +using namespace rowgroup; + +namespace funcexp +{ + +CalpontSystemCatalog::ColType Func_space::operationType(FunctionParm& fp, CalpontSystemCatalog::ColType& resultType) +{ + return resultType; +} + +std::string Func_space::getStrVal(rowgroup::Row& row, + FunctionParm& fp, + bool& isNull, + execplan::CalpontSystemCatalog::ColType& op_ct) +{ + + CalpontSystemCatalog::ColDataType ct = fp[0]->data()->resultType().colDataType; + + // Int representation of temporal types can be a very large value, + // so exit early if this is the case + if (ct == CalpontSystemCatalog::DATE || + ct == CalpontSystemCatalog::DATETIME || + ct == CalpontSystemCatalog::TIMESTAMP || + ct == CalpontSystemCatalog::TIME) + { + isNull = true; + return ""; + } + + int64_t count = fp[0]->data()->getIntVal(row, isNull); + + if (isNull || count < 1) + return ""; + + string result(count, ' '); + + return result; +} + +} // namespace funcexp +// vim:ts=4 sw=4: + diff --git a/utils/funcexp/funcexp.cpp b/utils/funcexp/funcexp.cpp index 2444197d7..f96f85b45 100644 --- a/utils/funcexp/funcexp.cpp +++ b/utils/funcexp/funcexp.cpp @@ -175,6 +175,7 @@ FuncExp::FuncExp() fFuncMap["position"] = new Func_instr(); //dlh fFuncMap["pow"] = new Func_pow(); fFuncMap["power"] = new Func_pow(); + fFuncMap["quote"] = new Func_quote(); fFuncMap["quarter"] = new Func_quarter(); fFuncMap["radians"] = new Func_radians(); //dlh fFuncMap["rand"] = new Func_rand(); @@ -192,6 +193,7 @@ FuncExp::FuncExp() fFuncMap["sha1"] = new Func_sha(); fFuncMap["sign"] = new Func_sign(); fFuncMap["sin"] = new Func_sin(); + fFuncMap["space"] = new Func_space(); fFuncMap["sqrt"] = new Func_sqrt(); fFuncMap["str_to_date"] = new Func_str_to_date(); fFuncMap["strcmp"] = new Func_strcmp(); diff --git a/utils/funcexp/functor_str.h b/utils/funcexp/functor_str.h index 24337aab9..2144c44ae 100644 --- a/utils/funcexp/functor_str.h +++ b/utils/funcexp/functor_str.h @@ -848,6 +848,40 @@ public: execplan::CalpontSystemCatalog::ColType& op_ct); }; + +/** @brief Func_space class + */ +class Func_space : public Func_Str +{ +public: + Func_space() : Func_Str("space") {} + virtual ~Func_space() {} + + execplan::CalpontSystemCatalog::ColType operationType(FunctionParm& fp, execplan::CalpontSystemCatalog::ColType& resultType); + + std::string getStrVal(rowgroup::Row& row, + FunctionParm& fp, + bool& isNull, + execplan::CalpontSystemCatalog::ColType& op_ct); +}; + + +/** @brief Func_quote class + */ +class Func_quote : public Func_Str +{ +public: + Func_quote() : Func_Str("quote") {} + virtual ~Func_quote() {} + + execplan::CalpontSystemCatalog::ColType operationType(FunctionParm& fp, execplan::CalpontSystemCatalog::ColType& resultType); + + std::string getStrVal(rowgroup::Row& row, + FunctionParm& fp, + bool& isNull, + execplan::CalpontSystemCatalog::ColType& op_ct); +}; + } #endif