<commit_before>/* This file is part of VoltDB.
 * Copyright (C) 2008-2012 VoltDB Inc.
 *
 * VoltDB 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, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VoltDB 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 VoltDB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "expressions/functionexpression.h"
#include "expressions/expressionutil.h"

namespace voltdb {

/** implement a forced SQL ERROR function (for test and example purposes) for either integer or string types **/
template<> inline NValue NValue::callUnary<FUNC_VOLT_SQL_ERROR>() const {
    const char* sqlstatecode;
    const char* msgtext;
    const ValueType type = getValueType();
    char msg_format_buffer[1024];
    char state_format_buffer[6];
    if (type == VALUE_TYPE_VARCHAR) {
        const int32_t valueLength = getObjectLength();
        const char *valueChars = reinterpret_cast<char*>(getObjectValue());
        snprintf(msg_format_buffer, std::min((int32_t)sizeof(msg_format_buffer), valueLength+1), "%s", valueChars);
        sqlstatecode = SQLException::nonspecific_error_code_for_error_forced_by_user;
        msgtext = msg_format_buffer;
    } else {
        int64_t intValue = castAsBigIntAndGetValue(); // let cast throw if invalid
        if (intValue == 0) {
            return *this;
        }
        snprintf(state_format_buffer, sizeof(state_format_buffer), "%05ld", (long) intValue);
        sqlstatecode = state_format_buffer;
        msgtext = SQLException::specific_error_specified_by_user;
    }
    throw SQLException(sqlstatecode, msgtext);
}

/** implement the 2-argument forced SQL ERROR function (for test and example purposes) */
template<> inline NValue NValue::call<FUNC_VOLT_SQL_ERROR>(const std::vector<NValue>& arguments) {
    assert(arguments.size() == 2);
    const char* sqlstatecode;
    char msg_format_buffer[1024];
    char state_format_buffer[6];

    const NValue& codeArg = arguments[0];
    if (codeArg.isNull()) {
        sqlstatecode = SQLException::nonspecific_error_code_for_error_forced_by_user;
    } else {
        int64_t intValue = codeArg.castAsBigIntAndGetValue(); // let cast throw if invalid
        if (intValue == 0) {
            return codeArg;
        }
        snprintf(state_format_buffer, sizeof(state_format_buffer), "%05ld", (long) intValue);
        sqlstatecode = state_format_buffer;
    }

    const NValue& strValue = arguments[1];
    if (strValue.isNull()) {
        msg_format_buffer[0] = '\0';
    } else {
        if (strValue.getValueType() != VALUE_TYPE_VARCHAR) {
            throwCastSQLException (strValue.getValueType(), VALUE_TYPE_VARCHAR);
        }
        const int32_t valueLength = strValue.getObjectLength();
        char *valueChars = reinterpret_cast<char*>(strValue.getObjectValue());
        snprintf(msg_format_buffer, std::min((int32_t)sizeof(msg_format_buffer), valueLength+1), "%s", valueChars);
    }
    throw SQLException(sqlstatecode, msg_format_buffer);
}

namespace functionexpression {

/*
 * Constant (no parameter) function. (now, random)
 */
template <int F>
class ConstantFunctionExpression : public AbstractExpression {
public:
    ConstantFunctionExpression(const std::string& sqlName, const std::string& uniqueName)
        : AbstractExpression(EXPRESSION_TYPE_FUNCTION) {<commit_msg>Partial fix for ENG-3576 - Fix minor valgrind error in non-critical code. r=ssantoro<commit_after>/* This file is part of VoltDB.
 * Copyright (C) 2008-2012 VoltDB Inc.
 *
 * VoltDB 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, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VoltDB 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 VoltDB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "expressions/functionexpression.h"
#include "expressions/expressionutil.h"

namespace voltdb {

/** implement a forced SQL ERROR function (for test and example purposes) for either integer or string types **/
template<> inline NValue NValue::callUnary<FUNC_VOLT_SQL_ERROR>() const {
    const char* sqlstatecode;
    const char* msgtext;
    const ValueType type = getValueType();
    char msg_format_buffer[1024];
    char state_format_buffer[6];
    if (type == VALUE_TYPE_VARCHAR) {
        const int32_t valueLength = getObjectLength();
        const char *valueChars = reinterpret_cast<char*>(getObjectValue());
        std::string valueStr(valueChars, valueLength);
        snprintf(msg_format_buffer, sizeof(msg_format_buffer), "%s", valueStr.c_str());
        sqlstatecode = SQLException::nonspecific_error_code_for_error_forced_by_user;
        msgtext = msg_format_buffer;
    } else {
        int64_t intValue = castAsBigIntAndGetValue(); // let cast throw if invalid
        if (intValue == 0) {
            return *this;
        }
        snprintf(state_format_buffer, sizeof(state_format_buffer), "%05ld", (long) intValue);
        sqlstatecode = state_format_buffer;
        msgtext = SQLException::specific_error_specified_by_user;
    }
    throw SQLException(sqlstatecode, msgtext);
}

/** implement the 2-argument forced SQL ERROR function (for test and example purposes) */
template<> inline NValue NValue::call<FUNC_VOLT_SQL_ERROR>(const std::vector<NValue>& arguments) {
    assert(arguments.size() == 2);
    const char* sqlstatecode;
    char msg_format_buffer[1024];
    char state_format_buffer[6];

    const NValue& codeArg = arguments[0];
    if (codeArg.isNull()) {
        sqlstatecode = SQLException::nonspecific_error_code_for_error_forced_by_user;
    } else {
        int64_t intValue = codeArg.castAsBigIntAndGetValue(); // let cast throw if invalid
        if (intValue == 0) {
            return codeArg;
        }
        snprintf(state_format_buffer, sizeof(state_format_buffer), "%05ld", (long) intValue);
        sqlstatecode = state_format_buffer;
    }

    const NValue& strValue = arguments[1];
    if (strValue.isNull()) {
        msg_format_buffer[0] = '\0';
    } else {
        if (strValue.getValueType() != VALUE_TYPE_VARCHAR) {
            throwCastSQLException (strValue.getValueType(), VALUE_TYPE_VARCHAR);
        }
        const int32_t valueLength = strValue.getObjectLength();
        char *valueChars = reinterpret_cast<char*>(strValue.getObjectValue());
        std::string valueStr(valueChars, valueLength);
        snprintf(msg_format_buffer, sizeof(msg_format_buffer), "%s", valueStr.c_str());
    }
    throw SQLException(sqlstatecode, msg_format_buffer);
}

namespace functionexpression {

/*
 * Constant (no parameter) function. (now, random)
 */
template <int F>
class ConstantFunctionExpression : public AbstractExpression {
public:
    ConstantFunctionExpression(const std::string& sqlName, const std::string& uniqueName)
        : AbstractExpression(EXPRESSION_TYPE_FUNCTION) {
{'fraction_non_alphanumeric': 0.09777602316094224, 'fraction_numerical': 0.011580471114620345, 'mean_word_length': 3.837683004455761, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '357143', 'n_tokens_mistral': 2249, 'n_tokens_neox': 2174, 'n_words': 625}