|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#pragma once |
| 19 | + |
| 20 | +#include <re2/re2.h> |
| 21 | + |
| 22 | +#include <memory> |
| 23 | +#include <string> |
| 24 | + |
| 25 | +#include "arrow/status.h" |
| 26 | +#include "gandiva/execution_context.h" |
| 27 | +#include "gandiva/function_holder.h" |
| 28 | +#include "gandiva/node.h" |
| 29 | +#include "gandiva/visibility.h" |
| 30 | + |
| 31 | +namespace gandiva { |
| 32 | + |
| 33 | +/// Function Holder for 'replace' |
| 34 | +class GANDIVA_EXPORT ReplaceHolder : public FunctionHolder { |
| 35 | + public: |
| 36 | + ~ReplaceHolder() override = default; |
| 37 | + |
| 38 | + static Status Make(const FunctionNode& node, std::shared_ptr<ReplaceHolder>* holder); |
| 39 | + |
| 40 | + static Status Make(const std::string& sql_pattern, |
| 41 | + std::shared_ptr<ReplaceHolder>* holder); |
| 42 | + |
| 43 | + /// Return a new string with the pattern that matched the regex replaced for |
| 44 | + /// the replace_input parameter. |
| 45 | + const char* operator()(ExecutionContext* ctx, const char* user_input, |
| 46 | + int32_t user_input_len, const char* replace_input, |
| 47 | + int32_t replace_input_len, int32_t* out_length) { |
| 48 | + std::string user_input_as_str(user_input, user_input_len); |
| 49 | + std::string replace_input_as_str(replace_input, replace_input_len); |
| 50 | + |
| 51 | + int32_t total_replaces = |
| 52 | + RE2::GlobalReplace(&user_input_as_str, regex_, replace_input_as_str); |
| 53 | + |
| 54 | + if (total_replaces < 0) { |
| 55 | + return_error(ctx, user_input_as_str, replace_input_as_str); |
| 56 | + *out_length = 0; |
| 57 | + return ""; |
| 58 | + } |
| 59 | + |
| 60 | + if (total_replaces == 0) { |
| 61 | + *out_length = user_input_len; |
| 62 | + return user_input; |
| 63 | + } |
| 64 | + |
| 65 | + *out_length = static_cast<int32_t>(user_input_as_str.size()); |
| 66 | + |
| 67 | + // This condition treats the case where the whole string is replaced by an empty |
| 68 | + // string |
| 69 | + if (*out_length == 0) { |
| 70 | + return ""; |
| 71 | + } |
| 72 | + |
| 73 | + char* result_buffer = reinterpret_cast<char*>(ctx->arena()->Allocate(*out_length)); |
| 74 | + |
| 75 | + if (result_buffer == NULLPTR) { |
| 76 | + ctx->set_error_msg("Could not allocate memory for result"); |
| 77 | + *out_length = 0; |
| 78 | + return ""; |
| 79 | + } |
| 80 | + |
| 81 | + memcpy(result_buffer, user_input_as_str.data(), *out_length); |
| 82 | + |
| 83 | + return result_buffer; |
| 84 | + } |
| 85 | + |
| 86 | + private: |
| 87 | + explicit ReplaceHolder(const std::string& pattern) |
| 88 | + : pattern_(pattern), regex_(pattern) {} |
| 89 | + |
| 90 | + void return_error(ExecutionContext* context, std::string& data, |
| 91 | + std::string& replace_string); |
| 92 | + |
| 93 | + std::string pattern_; // posix pattern string, to help debugging |
| 94 | + RE2 regex_; // compiled regex for the pattern |
| 95 | +}; |
| 96 | + |
| 97 | +} // namespace gandiva |
0 commit comments