You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/05/02 07:39:44 UTC

[GitHub] [arrow] projjal commented on a change in pull request #10059: ARROW-12410: [C++][Gandiva] Implement regexp_replace function on Gandiva

projjal commented on a change in pull request #10059:
URL: https://github.com/apache/arrow/pull/10059#discussion_r624647491



##########
File path: cpp/src/gandiva/replace_holder.h
##########
@@ -0,0 +1,99 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#pragma once
+
+#include <memory>
+#include <string>
+
+#include <re2/re2.h>
+
+#include "arrow/status.h"
+
+#include "gandiva/execution_context.h"
+#include "gandiva/function_holder.h"
+#include "gandiva/node.h"
+#include "gandiva/visibility.h"
+
+namespace gandiva {
+
+/// Function Holder for 'replace'
+class GANDIVA_EXPORT ReplaceHolder : public FunctionHolder {
+ public:
+  ~ReplaceHolder() override = default;
+
+  static Status Make(const FunctionNode& node, std::shared_ptr<ReplaceHolder>* holder);
+
+  static Status Make(const std::string& sql_pattern,
+                     std::shared_ptr<ReplaceHolder>* holder);
+
+  /// Return true if the data matches the pattern.
+  const char* operator()(ExecutionContext* ptr, std::string& user_input,
+                         std::string& replace_input, int32_t* out_length) {
+    int32_t user_input_length = static_cast<int32_t>(user_input.size());
+    int32_t total_replaces = RE2::GlobalReplace(&user_input, regex_, replace_input);
+    bool was_replaced = false;
+
+    if (total_replaces < 0) {
+      return_error(ptr, user_input, replace_input);
+      *out_length = 0;
+    } else if (total_replaces == 0) {
+      *out_length = user_input_length;
+    } else {
+      was_replaced = true;
+    }
+
+    *out_length = static_cast<int32_t>(user_input.size());
+
+    if (!was_replaced) {
+      return user_input.data();
+    }
+
+    // This condition treats the case where the whole string is replaced by an empty
+    // string
+    if (*out_length == 0) {
+      return "";
+    }
+    auto context = reinterpret_cast<gandiva::ExecutionContext*>(ptr);

Review comment:
       This cast doesn't seem required. Just rename the argument ptr to context

##########
File path: cpp/src/gandiva/replace_holder.h
##########
@@ -0,0 +1,99 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#pragma once
+
+#include <memory>
+#include <string>
+
+#include <re2/re2.h>
+
+#include "arrow/status.h"
+
+#include "gandiva/execution_context.h"
+#include "gandiva/function_holder.h"
+#include "gandiva/node.h"
+#include "gandiva/visibility.h"
+
+namespace gandiva {
+
+/// Function Holder for 'replace'
+class GANDIVA_EXPORT ReplaceHolder : public FunctionHolder {
+ public:
+  ~ReplaceHolder() override = default;
+
+  static Status Make(const FunctionNode& node, std::shared_ptr<ReplaceHolder>* holder);
+
+  static Status Make(const std::string& sql_pattern,
+                     std::shared_ptr<ReplaceHolder>* holder);
+
+  /// Return true if the data matches the pattern.
+  const char* operator()(ExecutionContext* ptr, std::string& user_input,
+                         std::string& replace_input, int32_t* out_length) {
+    int32_t user_input_length = static_cast<int32_t>(user_input.size());
+    int32_t total_replaces = RE2::GlobalReplace(&user_input, regex_, replace_input);
+    bool was_replaced = false;
+
+    if (total_replaces < 0) {
+      return_error(ptr, user_input, replace_input);
+      *out_length = 0;
+    } else if (total_replaces == 0) {
+      *out_length = user_input_length;
+    } else {
+      was_replaced = true;
+    }
+
+    *out_length = static_cast<int32_t>(user_input.size());

Review comment:
       Its probably better to move this statement inside the else statement above

##########
File path: cpp/src/gandiva/replace_holder.h
##########
@@ -0,0 +1,99 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#pragma once
+
+#include <memory>
+#include <string>
+
+#include <re2/re2.h>
+
+#include "arrow/status.h"
+
+#include "gandiva/execution_context.h"
+#include "gandiva/function_holder.h"
+#include "gandiva/node.h"
+#include "gandiva/visibility.h"
+
+namespace gandiva {
+
+/// Function Holder for 'replace'
+class GANDIVA_EXPORT ReplaceHolder : public FunctionHolder {
+ public:
+  ~ReplaceHolder() override = default;
+
+  static Status Make(const FunctionNode& node, std::shared_ptr<ReplaceHolder>* holder);
+
+  static Status Make(const std::string& sql_pattern,
+                     std::shared_ptr<ReplaceHolder>* holder);
+
+  /// Return true if the data matches the pattern.
+  const char* operator()(ExecutionContext* ptr, std::string& user_input,
+                         std::string& replace_input, int32_t* out_length) {
+    int32_t user_input_length = static_cast<int32_t>(user_input.size());
+    int32_t total_replaces = RE2::GlobalReplace(&user_input, regex_, replace_input);
+    bool was_replaced = false;
+
+    if (total_replaces < 0) {
+      return_error(ptr, user_input, replace_input);
+      *out_length = 0;
+    } else if (total_replaces == 0) {
+      *out_length = user_input_length;
+    } else {
+      was_replaced = true;
+    }
+
+    *out_length = static_cast<int32_t>(user_input.size());
+
+    if (!was_replaced) {
+      return user_input.data();
+    }
+
+    // This condition treats the case where the whole string is replaced by an empty
+    // string
+    if (*out_length == 0) {
+      return "";
+    }
+    auto context = reinterpret_cast<gandiva::ExecutionContext*>(ptr);
+
+    char* result_buffer =
+        reinterpret_cast<char*>(context->arena()->Allocate(*out_length));
+
+    if (result_buffer == nullptr) {
+      context = reinterpret_cast<gandiva::ExecutionContext*>(ptr);

Review comment:
       cast not required

##########
File path: cpp/src/gandiva/replace_holder.h
##########
@@ -0,0 +1,99 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#pragma once
+
+#include <memory>
+#include <string>
+
+#include <re2/re2.h>
+
+#include "arrow/status.h"
+
+#include "gandiva/execution_context.h"
+#include "gandiva/function_holder.h"
+#include "gandiva/node.h"
+#include "gandiva/visibility.h"
+
+namespace gandiva {
+
+/// Function Holder for 'replace'
+class GANDIVA_EXPORT ReplaceHolder : public FunctionHolder {
+ public:
+  ~ReplaceHolder() override = default;
+
+  static Status Make(const FunctionNode& node, std::shared_ptr<ReplaceHolder>* holder);
+
+  static Status Make(const std::string& sql_pattern,
+                     std::shared_ptr<ReplaceHolder>* holder);
+
+  /// Return true if the data matches the pattern.
+  const char* operator()(ExecutionContext* ptr, std::string& user_input,
+                         std::string& replace_input, int32_t* out_length) {
+    int32_t user_input_length = static_cast<int32_t>(user_input.size());
+    int32_t total_replaces = RE2::GlobalReplace(&user_input, regex_, replace_input);
+    bool was_replaced = false;
+
+    if (total_replaces < 0) {
+      return_error(ptr, user_input, replace_input);
+      *out_length = 0;

Review comment:
       return "" here

##########
File path: cpp/src/gandiva/replace_holder.h
##########
@@ -0,0 +1,99 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#pragma once
+
+#include <memory>
+#include <string>
+
+#include <re2/re2.h>
+
+#include "arrow/status.h"
+
+#include "gandiva/execution_context.h"
+#include "gandiva/function_holder.h"
+#include "gandiva/node.h"
+#include "gandiva/visibility.h"
+
+namespace gandiva {
+
+/// Function Holder for 'replace'
+class GANDIVA_EXPORT ReplaceHolder : public FunctionHolder {
+ public:
+  ~ReplaceHolder() override = default;
+
+  static Status Make(const FunctionNode& node, std::shared_ptr<ReplaceHolder>* holder);
+
+  static Status Make(const std::string& sql_pattern,
+                     std::shared_ptr<ReplaceHolder>* holder);
+
+  /// Return true if the data matches the pattern.
+  const char* operator()(ExecutionContext* ptr, std::string& user_input,
+                         std::string& replace_input, int32_t* out_length) {
+    int32_t user_input_length = static_cast<int32_t>(user_input.size());
+    int32_t total_replaces = RE2::GlobalReplace(&user_input, regex_, replace_input);
+    bool was_replaced = false;
+
+    if (total_replaces < 0) {
+      return_error(ptr, user_input, replace_input);
+      *out_length = 0;
+    } else if (total_replaces == 0) {
+      *out_length = user_input_length;
+    } else {
+      was_replaced = true;
+    }
+
+    *out_length = static_cast<int32_t>(user_input.size());
+
+    if (!was_replaced) {
+      return user_input.data();

Review comment:
       You can't return user_input.data() since it wil get deallocated when returning from the parent function. Its better just pass the user_input char* and len as parameters to the method, instead of passing std::string and memcpying for no replace

##########
File path: cpp/src/gandiva/replace_holder.h
##########
@@ -0,0 +1,99 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#pragma once
+
+#include <memory>
+#include <string>
+
+#include <re2/re2.h>
+
+#include "arrow/status.h"
+
+#include "gandiva/execution_context.h"
+#include "gandiva/function_holder.h"
+#include "gandiva/node.h"
+#include "gandiva/visibility.h"
+
+namespace gandiva {
+
+/// Function Holder for 'replace'
+class GANDIVA_EXPORT ReplaceHolder : public FunctionHolder {
+ public:
+  ~ReplaceHolder() override = default;
+
+  static Status Make(const FunctionNode& node, std::shared_ptr<ReplaceHolder>* holder);
+
+  static Status Make(const std::string& sql_pattern,
+                     std::shared_ptr<ReplaceHolder>* holder);
+
+  /// Return true if the data matches the pattern.

Review comment:
       Update the comment




-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org