Skip to content

Commit

Permalink
feat: add func setrange()
Browse files Browse the repository at this point in the history
  • Loading branch information
wy1433 committed Apr 24, 2024
1 parent a4f8a3a commit 7565895
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/expr/internal_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ ExprValue field(const std::vector<ExprValue>& input);
ExprValue quote(const std::vector<ExprValue>& input);
ExprValue func_char(const std::vector<ExprValue>& input);
ExprValue soundex(const std::vector<ExprValue>& input);

ExprValue setrange(const std::vector<ExprValue>& input);

// datetime functions
ExprValue unix_timestamp(const std::vector<ExprValue>& input);
Expand Down
2 changes: 1 addition & 1 deletion src/expr/fn_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void FunctionManager::register_operators() {
register_object_ret("quote", quote, pb::STRING);
register_object_ret("char", func_char, pb::STRING);
register_object_ret("soundex", soundex, pb::STRING);

register_object_ret("setrange", setrange, pb::STRING);

// date funcs
register_object_ret("unix_timestamp", unix_timestamp, pb::INT64);
Expand Down
26 changes: 26 additions & 0 deletions src/expr/internal_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,32 @@ ExprValue substring_index(const std::vector<ExprValue>& input) {
return tmp;
}

ExprValue setrange(const std::vector<ExprValue>& input) {
if (input.size() != 3) {
return ExprValue::Null();
}
ExprValue tmp(pb::STRING);
std::string str = input[0].get_string();
int64_t offset = input[1].get_numberic<int64_t>();
std::string value = input[2].get_string();
if (offset < 0 || offset > UINT16_MAX) {
return ExprValue::Null();
}
if (offset > str.length()) {
tmp.str_val = str;
tmp.str_val += std::string(offset - str.length(), '\x00');
tmp.str_val += value;
return tmp;
}
tmp.str_val.append(str.begin(), str.begin() + offset);
tmp.str_val += value;
if (offset + value.length() >= str.length()) {
return tmp;
}
tmp.str_val.append(str.begin() + offset + value.length(), str.end());
return tmp;
}

ExprValue unix_timestamp(const std::vector<ExprValue>& input) {
ExprValue tmp(pb::UINT32);
if (input.size() == 0) {
Expand Down

0 comments on commit 7565895

Please sign in to comment.