augustoasilva commented on a change in pull request #11551: URL: https://github.com/apache/arrow/pull/11551#discussion_r743325768
########## File path: cpp/src/gandiva/gdv_function_stubs.cc ########## @@ -794,6 +795,83 @@ const char* gdv_fn_initcap_utf8(int64_t context, const char* data, int32_t data_ *out_len = out_idx; return out; } + +GANDIVA_EXPORT +const char* gdv_fn_mask_first_n(int64_t context, const char* data, int32_t data_len, + int32_t n_to_mask, int32_t* out_len) { + if (data_len <= 0) { + *out_len = 0; + return ""; + } + + std::string str(data, data_len); Review comment: I did a new implementation without std::string, and more simple and efficiente on the for loop ########## File path: cpp/src/gandiva/gdv_function_stubs.cc ########## @@ -794,6 +795,83 @@ const char* gdv_fn_initcap_utf8(int64_t context, const char* data, int32_t data_ *out_len = out_idx; return out; } + +GANDIVA_EXPORT +const char* gdv_fn_mask_first_n(int64_t context, const char* data, int32_t data_len, + int32_t n_to_mask, int32_t* out_len) { + if (data_len <= 0) { + *out_len = 0; + return ""; Review comment: Yes, it should, so I fixed ########## File path: cpp/src/gandiva/gdv_function_stubs.cc ########## @@ -794,6 +795,83 @@ const char* gdv_fn_initcap_utf8(int64_t context, const char* data, int32_t data_ *out_len = out_idx; return out; } + +GANDIVA_EXPORT +const char* gdv_fn_mask_first_n(int64_t context, const char* data, int32_t data_len, + int32_t n_to_mask, int32_t* out_len) { + if (data_len <= 0) { + *out_len = 0; + return ""; + } + + std::string str(data, data_len); + int32_t counter = 0; + for (char& c : str) { Review comment: I did a new implementation without std::string, and more simple and efficiente on the for loop ########## File path: cpp/src/gandiva/gdv_function_stubs.cc ########## @@ -794,6 +795,83 @@ const char* gdv_fn_initcap_utf8(int64_t context, const char* data, int32_t data_ *out_len = out_idx; return out; } + +GANDIVA_EXPORT +const char* gdv_fn_mask_first_n(int64_t context, const char* data, int32_t data_len, + int32_t n_to_mask, int32_t* out_len) { + if (data_len <= 0) { + *out_len = 0; + return ""; + } + + std::string str(data, data_len); + int32_t counter = 0; + for (char& c : str) { + if (n_to_mask > 0 && counter < n_to_mask && isalnum(c)) { + if (isdigit(c)) { + c = 'n'; + } else { + if (isupper(c)) { + c = 'X'; + } else { + c = 'x'; + } + } + counter++; + } + } + + *out_len = static_cast<int32_t>(str.size()); + + char* out = reinterpret_cast<char*>(gdv_fn_context_arena_malloc(context, *out_len)); + if (out == nullptr) { + gdv_fn_context_set_error_msg(context, "Could not allocate memory for output string"); + *out_len = 0; + return ""; + } + + memcpy(out, str.c_str(), *out_len); + return out; +} + +GANDIVA_EXPORT +const char* gdv_fn_mask_last_n(int64_t context, const char* data, int32_t data_len, + int32_t n_to_mask, int32_t* out_len) { + if (data_len <= 0) { + *out_len = 0; + return ""; + } + + std::string str(data, data_len); + std::reverse(str.begin(), str.end()); Review comment: It was simple to implement, but the new code is way lighter and simpler ########## File path: cpp/src/gandiva/gdv_function_stubs.cc ########## @@ -794,6 +795,83 @@ const char* gdv_fn_initcap_utf8(int64_t context, const char* data, int32_t data_ *out_len = out_idx; return out; } + +GANDIVA_EXPORT +const char* gdv_fn_mask_first_n(int64_t context, const char* data, int32_t data_len, + int32_t n_to_mask, int32_t* out_len) { + if (data_len <= 0) { + *out_len = 0; + return ""; + } + + std::string str(data, data_len); + int32_t counter = 0; + for (char& c : str) { + if (n_to_mask > 0 && counter < n_to_mask && isalnum(c)) { + if (isdigit(c)) { + c = 'n'; + } else { + if (isupper(c)) { + c = 'X'; + } else { + c = 'x'; + } + } + counter++; + } + } + + *out_len = static_cast<int32_t>(str.size()); + + char* out = reinterpret_cast<char*>(gdv_fn_context_arena_malloc(context, *out_len)); + if (out == nullptr) { + gdv_fn_context_set_error_msg(context, "Could not allocate memory for output string"); + *out_len = 0; + return ""; + } + + memcpy(out, str.c_str(), *out_len); + return out; +} + +GANDIVA_EXPORT +const char* gdv_fn_mask_last_n(int64_t context, const char* data, int32_t data_len, Review comment: Did the same improvements on mask_last_n ########## File path: cpp/src/gandiva/gdv_function_stubs_test.cc ########## @@ -766,4 +766,40 @@ TEST(TestGdvFnStubs, TestCastVarbinaryFloat8) { ctx.Reset(); } +TEST(TestGdvFnStubs, TestMaskFirstN) { Review comment: If it passes a negative number, it will threat it as a positive number, so I did the handling of the negative number on the function ########## File path: cpp/src/gandiva/gdv_function_stubs.cc ########## @@ -1599,5 +1677,31 @@ void ExportedStubFunctions::AddMappings(Engine* engine) const { engine->AddGlobalMappingForFunc("gdv_fn_initcap_utf8", types->i8_ptr_type() /*return_type*/, args, reinterpret_cast<void*>(gdv_fn_initcap_utf8)); + + // gdv_fn_mask_first_n + args = { Review comment: Sounds good to me -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org