diff --git a/utils/funcexp/func_lpad.cpp b/utils/funcexp/func_lpad.cpp index dd25ee191..b5acfd362 100644 --- a/utils/funcexp/func_lpad.cpp +++ b/utils/funcexp/func_lpad.cpp @@ -20,7 +20,7 @@ * * ****************************************************************************/ - +#include "errorids.h" #include using namespace std; @@ -39,6 +39,9 @@ using namespace joblist; namespace funcexp { +const string Func_lpad::fPad = " "; + + CalpontSystemCatalog::ColType Func_lpad::operationType(FunctionParm& fp, CalpontSystemCatalog::ColType& resultType) { // operation type is not used by this functor @@ -114,17 +117,33 @@ std::string Func_lpad::getStrVal(rowgroup::Row& row, } break; + case execplan::CalpontSystemCatalog::CHAR: + case execplan::CalpontSystemCatalog::VARCHAR: + { + const string& strval = fp[1]->data()->getStrVal(row, isNull); + len = strtol(strval.c_str(), NULL, 10); + break; + } + default: { - len = fp[1]->data()->getIntVal(row, isNull); + std::ostringstream oss; + oss << "lpad parameter 2 must be numeric, not " << execplan::colDataTypeToString(fp[1]->data()->resultType().colDataType); + throw logging::IDBExcept(oss.str(), logging::ERR_DATATYPE_NOT_SUPPORT); + } } if (len < 1) return ""; + // MCOL-2182 As of MariaDB 10.3 the third parameter - pad characters - is optional // The pad characters. - const string& pad = fp[2]->data()->getStrVal(row, isNull); + const string* pad = &fPad; + if (fp.size() > 2) + { + pad = &fp[2]->data()->getStrVal(row, isNull); + } if (isNull) return ""; @@ -172,11 +191,11 @@ std::string Func_lpad::getStrVal(rowgroup::Row& row, // This is the case where there's room to pad. // Convert the pad string to wide - padwclen = pad.length(); // A guess to start. + padwclen = pad->length(); // A guess to start. size_t padbufsize = (padwclen + 1) * sizeof(wchar_t); wchar_t* wcpad = (wchar_t*)alloca(padbufsize); // padwclen+1 is for giving count for the terminating null - size_t padlen = utf8::idb_mbstowcs(wcpad, pad.c_str(), padwclen + 1); + size_t padlen = utf8::idb_mbstowcs(wcpad, pad->c_str(), padwclen + 1); // How many chars do we need? size_t padspace = len - strSize; diff --git a/utils/funcexp/func_rpad.cpp b/utils/funcexp/func_rpad.cpp index 7de0dfb78..7a6b94e0c 100644 --- a/utils/funcexp/func_rpad.cpp +++ b/utils/funcexp/func_rpad.cpp @@ -20,7 +20,7 @@ * * ****************************************************************************/ - +#include "errorids.h" #include using namespace std; @@ -39,6 +39,8 @@ using namespace joblist; namespace funcexp { +const string Func_rpad::fPad = " "; + CalpontSystemCatalog::ColType Func_rpad::operationType(FunctionParm& fp, CalpontSystemCatalog::ColType& resultType) { // operation type is not used by this functor @@ -114,9 +116,19 @@ std::string Func_rpad::getStrVal(rowgroup::Row& row, } break; + case execplan::CalpontSystemCatalog::CHAR: + case execplan::CalpontSystemCatalog::VARCHAR: + { + const string& strval = fp[1]->data()->getStrVal(row, isNull); + len = strtol(strval.c_str(), NULL, 10); + break; + } + default: { - len = fp[1]->data()->getIntVal(row, isNull); + std::ostringstream oss; + oss << "lpad parameter 2 must be numeric, not " << execplan::colDataTypeToString(fp[1]->data()->resultType().colDataType); + throw logging::IDBExcept(oss.str(), logging::ERR_DATATYPE_NOT_SUPPORT); } } @@ -124,7 +136,12 @@ std::string Func_rpad::getStrVal(rowgroup::Row& row, return ""; // The pad characters. - const string& pad = fp[2]->data()->getStrVal(row, isNull); + // MCOL-2182 As of MariaDB 10.3 the third parameter - pad characters - is optional + const string* pad = &fPad; + if (fp.size() > 2) + { + pad = &fp[2]->data()->getStrVal(row, isNull); + } if (isNull) return ""; @@ -172,10 +189,10 @@ std::string Func_rpad::getStrVal(rowgroup::Row& row, // This is the case where there's room to pad. // Convert the pad string to wide - padwclen = pad.length(); // A guess to start. + padwclen = pad->length(); // A guess to start. int padbufsize = (padwclen + 1) * sizeof(wchar_t); wchar_t* wcpad = (wchar_t*)alloca(padbufsize); - size_t padlen = utf8::idb_mbstowcs(wcpad, pad.c_str(), padwclen + 1); + size_t padlen = utf8::idb_mbstowcs(wcpad, pad->c_str(), padwclen + 1); // How many chars do we need? unsigned int padspace = len - strSize; diff --git a/utils/funcexp/functor_str.h b/utils/funcexp/functor_str.h index efd4ae33c..5402cc646 100644 --- a/utils/funcexp/functor_str.h +++ b/utils/funcexp/functor_str.h @@ -325,6 +325,7 @@ public: */ class Func_lpad : public Func_Str { + static const string fPad; public: Func_lpad() : Func_Str("lpad") {} virtual ~Func_lpad() {} @@ -342,6 +343,7 @@ public: */ class Func_rpad : public Func_Str { + static const string fPad; public: Func_rpad() : Func_Str("rpad") {} virtual ~Func_rpad() {}