Copilot commented on code in PR #13274:
URL: https://github.com/apache/gluten/pull/13274#discussion_r4273355414


##########
cpp/velox/udf/examples/MyUDF.cc:
##########
@@ -108,6 +109,84 @@ class MyUdfPlusOneRegisterer final : public 
gluten::UdfRegisterer {
 
 } // namespace myudfplusone
 
+namespace myudfsumrow {
+
+template <typename T>
+struct MyUdfSumRowFunction {
+  VELOX_DEFINE_FUNCTION_TYPES(T);
+
+  FOLLY_ALWAYS_INLINE bool call(int64_t& result, const arg_type<Row<int64_t, 
int64_t>>& row) {
+    const auto a = row.template at<0>();
+    const auto b = row.template at<1>();
+    if (!a.has_value() || !b.has_value()) {
+      return false;
+    }
+    result = a.value() + b.value();
+    return true;
+  }

Review Comment:
   The UDF dereferences the struct argument unconditionally. If the *struct 
itself* is null (e.g., `CAST(NULL AS STRUCT<a:bigint,b:bigint>)`), accessing 
`row.at<0>()`/`row.at<1>()` can be invalid. Fix by explicitly handling a null 
struct argument (e.g., check `row.has_value()` / nullability per Velox UDF 
conventions) before reading fields, and return `false` to produce a null result 
when the input row is null.



##########
cpp/velox/udf/examples/MyUDF.cc:
##########
@@ -108,6 +109,84 @@ class MyUdfPlusOneRegisterer final : public 
gluten::UdfRegisterer {
 
 } // namespace myudfplusone
 
+namespace myudfsumrow {
+
+template <typename T>
+struct MyUdfSumRowFunction {
+  VELOX_DEFINE_FUNCTION_TYPES(T);
+
+  FOLLY_ALWAYS_INLINE bool call(int64_t& result, const arg_type<Row<int64_t, 
int64_t>>& row) {
+    const auto a = row.template at<0>();
+    const auto b = row.template at<1>();
+    if (!a.has_value() || !b.has_value()) {
+      return false;
+    }
+    result = a.value() + b.value();
+    return true;
+  }
+};
+
+// name: myudf_sum_row
+// signatures:
+//    struct<a:bigint,b:bigint> -> bigint
+// type: SimpleFunction
+class MyUdfSumRowRegisterer final : public gluten::UdfRegisterer {
+ public:
+  int getNumUdf() override {
+    return 1;
+  }
+
+  void populateUdfEntries(int& index, gluten::UdfEntry* udfEntries) override {
+    udfEntries[index++] = {name_.c_str(), kBigInt, 1, arg_, false, false};
+  }
+
+  void registerSignatures() override {
+    facebook::velox::registerFunction<MyUdfSumRowFunction, int64_t, 
Row<int64_t, int64_t>>({name_});
+  }
+
+ private:
+  const std::string name_ = "myudf_sum_row";
+  const char* arg_[1] = {kStructAB};
+};
+
+} // namespace myudfsumrow
+
+namespace myudfmakerow {
+
+template <typename T>
+struct MyUdfMakeRowFunction {
+  VELOX_DEFINE_FUNCTION_TYPES(T);
+
+  FOLLY_ALWAYS_INLINE void call(out_type<Row<int64_t, int64_t>>& result, const 
int64_t& a) {
+    result = std::make_tuple(a, a + 1);

Review Comment:
   This example UDF takes `a` as non-nullable (`const int64_t&`). In Spark SQL, 
`BIGINT` inputs are generally nullable, so passing null could lead to incorrect 
behavior or crashes depending on how the binding is performed. Consider using 
Velox's nullable argument type (`arg_type<int64_t>`) and emitting a null result 
when `a` is null, or otherwise documenting/enforcing non-nullability in the 
signature/registration.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to