kou commented on code in PR #38632:
URL: https://github.com/apache/arrow/pull/38632#discussion_r1388534999


##########
cpp/src/gandiva/external_c_functions.cc:
##########
@@ -0,0 +1,94 @@
+// 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
+
+#include "llvm/IR/Type.h"

Review Comment:
   ```suggestion
   #include <llvm/IR/Type.h>
   ```



##########
cpp/src/gandiva/external_c_functions.cc:
##########
@@ -0,0 +1,94 @@
+// 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
+
+#include "llvm/IR/Type.h"
+
+#include "gandiva/engine.h"
+#include "gandiva/exported_funcs.h"
+
+namespace gandiva {
+static arrow::Result<llvm::Type*> AsLLVMType(const DataTypePtr& from_type,
+                                             LLVMTypes* types) {
+  switch (from_type->id()) {
+    case arrow::Type::BOOL:
+      return types->i1_type();
+    case arrow::Type::INT8:
+    case arrow::Type::UINT8:
+      return types->i8_type();
+    case arrow::Type::INT16:
+    case arrow::Type::UINT16:
+      return types->i16_type();
+    case arrow::Type::INT32:
+    case arrow::Type::UINT32:
+      return types->i32_type();
+    case arrow::Type::INT64:
+    case arrow::Type::UINT64:
+      return types->i64_type();
+    case arrow::Type::FLOAT:
+      return types->float_type();
+    case arrow::Type::DOUBLE:
+      return types->double_type();
+    default:
+      return Status::NotImplemented("Unsupported arrow data type: " +
+                                    from_type->ToString());
+  }
+}
+
+// map from a NativeFunction's signature to the corresponding LLVM signature
+static arrow::Result<std::pair<std::vector<llvm::Type*>, llvm::Type*>> 
MapToLLVMSignature(
+    const FunctionSignature& sig, const NativeFunction& func, LLVMTypes* 
types) {
+  std::vector<llvm::Type*> arg_llvm_types;
+  arg_llvm_types.reserve(sig.param_types().size());
+  if (func.NeedsContext()) {
+    arg_llvm_types.emplace_back(types->i64_type());
+  }
+  if (func.NeedsFunctionHolder()) {
+    arg_llvm_types.emplace_back(types->i64_type());
+  }
+  for (auto const& arg : sig.param_types()) {
+    if (arg->id() == arrow::Type::STRING) {
+      arg_llvm_types.emplace_back(types->i8_ptr_type());
+      arg_llvm_types.emplace_back(types->i32_type());
+    } else {
+      ARROW_ASSIGN_OR_RAISE(auto arg_llvm_type, AsLLVMType(arg, types));
+      arg_llvm_types.emplace_back(arg_llvm_type);
+    }
+  }
+  llvm::Type* ret_llvm_type;
+  if (sig.ret_type()->id() == arrow::Type::STRING) {
+    // for string output, the last arg is the output length
+    arg_llvm_types.emplace_back(types->i32_ptr_type());

Review Comment:
   It seems that `push_back()` is suitable than `emplace_back()` because we 
already have `llvm::Type*`.



##########
cpp/src/gandiva/function_holder_maker_registry.cc:
##########
@@ -0,0 +1,72 @@
+// 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.
+
+#include "gandiva/function_holder_maker_registry.h"
+
+#include <functional>
+
+#include "arrow/util/string.h"
+#include "gandiva/function_holder.h"
+#include "gandiva/interval_holder.h"
+#include "gandiva/random_generator_holder.h"
+#include "gandiva/regex_functions_holder.h"
+#include "gandiva/to_date_holder.h"
+
+namespace gandiva {
+
+using arrow::internal::AsciiToLower;
+
+FunctionHolderMakerRegistry::FunctionHolderMakerRegistry()
+    : function_holder_makers_(DefaultHolderMakers()) {}
+
+arrow::Status FunctionHolderMakerRegistry::Register(const std::string& name,
+                                                    FunctionHolderMaker 
holder_maker) {
+  function_holder_makers_.emplace(AsciiToLower(name), std::move(holder_maker));
+  return arrow::Status::OK();
+}
+
+template <typename HolderType>
+static arrow::Result<FunctionHolderPtr> HolderMaker(const FunctionNode& node) {
+  std::shared_ptr<HolderType> derived_instance;
+  ARROW_RETURN_NOT_OK(HolderType::Make(node, &derived_instance));
+  return derived_instance;
+}

Review Comment:
   If we change `HolderType::Make()` to use `Result` instead of `Status` (e.g. 
`Status LikeHolder::Make(...)` -> `Result<std::shared_ptr<LikeHolder>> 
LikeHolder::Make(...)`), we can remove this helper template function?
   If so, we can do it as a follow-up task.



##########
cpp/src/gandiva/function_registry.cc:
##########
@@ -109,11 +109,35 @@ arrow::Status FunctionRegistry::Register(const 
std::vector<NativeFunction>& func
   return Status::OK();
 }
 
+arrow::Status FunctionRegistry::Register(
+    NativeFunction func, void* c_function_ptr,
+    std::optional<FunctionHolderMaker> function_holder_maker) {
+  if (function_holder_maker.has_value()) {
+    // all signatures should have the same base name, use the first 
signature's base name
+    auto const& func_base_name = func.signatures().begin()->base_name();
+    ARROW_RETURN_NOT_OK(holder_maker_registry_.Register(
+        func_base_name, std::move(function_holder_maker.value())));

Review Comment:
   Do we need this `std::move()`?
   I think that a function call result is already rvalue.



##########
cpp/src/gandiva/external_c_functions.cc:
##########
@@ -0,0 +1,94 @@
+// 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
+
+#include "llvm/IR/Type.h"
+
+#include "gandiva/engine.h"
+#include "gandiva/exported_funcs.h"
+
+namespace gandiva {
+static arrow::Result<llvm::Type*> AsLLVMType(const DataTypePtr& from_type,
+                                             LLVMTypes* types) {
+  switch (from_type->id()) {
+    case arrow::Type::BOOL:
+      return types->i1_type();
+    case arrow::Type::INT8:
+    case arrow::Type::UINT8:
+      return types->i8_type();
+    case arrow::Type::INT16:
+    case arrow::Type::UINT16:
+      return types->i16_type();
+    case arrow::Type::INT32:
+    case arrow::Type::UINT32:
+      return types->i32_type();
+    case arrow::Type::INT64:
+    case arrow::Type::UINT64:
+      return types->i64_type();
+    case arrow::Type::FLOAT:
+      return types->float_type();
+    case arrow::Type::DOUBLE:
+      return types->double_type();
+    default:
+      return Status::NotImplemented("Unsupported arrow data type: " +
+                                    from_type->ToString());
+  }
+}
+
+// map from a NativeFunction's signature to the corresponding LLVM signature
+static arrow::Result<std::pair<std::vector<llvm::Type*>, llvm::Type*>> 
MapToLLVMSignature(
+    const FunctionSignature& sig, const NativeFunction& func, LLVMTypes* 
types) {
+  std::vector<llvm::Type*> arg_llvm_types;
+  arg_llvm_types.reserve(sig.param_types().size());

Review Comment:
   It seems that this size may be less than used size. Should we compute 
correct size here? 



##########
cpp/src/gandiva/function_registry.cc:
##########
@@ -109,11 +109,35 @@ arrow::Status FunctionRegistry::Register(const 
std::vector<NativeFunction>& func
   return Status::OK();
 }
 
+arrow::Status FunctionRegistry::Register(
+    NativeFunction func, void* c_function_ptr,
+    std::optional<FunctionHolderMaker> function_holder_maker) {
+  if (function_holder_maker.has_value()) {
+    // all signatures should have the same base name, use the first 
signature's base name
+    auto const& func_base_name = func.signatures().begin()->base_name();
+    ARROW_RETURN_NOT_OK(holder_maker_registry_.Register(
+        func_base_name, std::move(function_holder_maker.value())));
+  }
+  c_functions_.emplace_back(func, c_function_ptr);
+  ARROW_RETURN_NOT_OK(FunctionRegistry::Add(std::move(func)));
+  return Status::OK();

Review Comment:
   ```suggestion
     return FunctionRegistry::Add(std::move(func));
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to