viirya commented on code in PR #599:
URL: 
https://github.com/apache/spark-connect-swift/pull/599#discussion_r4299043512


##########
Tests/SparkConnectTests/UDFRegistrationTests.swift:
##########
@@ -0,0 +1,43 @@
+//
+// 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.
+
+#if canImport(FoundationEssentials)
+import FoundationEssentials
+#else
+import Foundation
+#endif
+import Testing
+
+@testable import SparkConnect
+
+/// A test suite for `UDFRegistration`
+@Suite(.serialized)
+struct UDFRegistrationTests {
+
+  @Test
+  func registerJavaWithUnknownClass() async throws {
+    let spark = try await SparkSession.builder.getOrCreate()
+    await #expect(throws: Error.self) {
+      try await spark.udf.registerJava("java_udf", 
"org.apache.spark.NoSuchUDF", .string)
+    }
+    await #expect(throws: Error.self) {

Review Comment:
   With a nonexistent class the server fails at `loadClass`, before the output 
type is ever looked at. So this second call takes the same server path as the 
first one and can't catch a bug in the `nil` return-type handling. I'd either 
drop it or point it at a class that gets past class loading. If there's no such 
class without a jar fixture, dropping it seems fine.



##########
Sources/SparkConnect/UDFRegistration.swift:
##########
@@ -0,0 +1,46 @@
+//
+// 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.
+
+/// Functions for registering user-defined functions. To access this, use 
``SparkSession/udf``.
+public actor UDFRegistration: Sendable {
+  var spark: SparkSession
+
+  init(spark: SparkSession) {
+    self.spark = spark
+  }
+
+  /// Register a Java user-defined function as a SQL function.
+  ///
+  /// The class must implement one of the `UDF1` ... `UDF22` interfaces and be 
available in the

Review Comment:
   nit: The server accepts `UDF0` as well (`case 1 => UDF0` in 
`classic.UDFRegistration.registerJava`), so `UDF0` ... `UDF22` would be 
accurate. It may also be worth saying that the class needs a public no-arg 
constructor and has to implement the interface directly. The lookup uses 
`clazz.getGenericInterfaces`, so an interface inherited from a superclass isn't 
detected, which is an easy trap to fall into.



##########
Sources/SparkConnect/SparkConnectClient.swift:
##########
@@ -953,6 +953,26 @@ public actor SparkConnectClient {
     return plan
   }
 
+  /// Register a Java user-defined function as a SQL function.

Review Comment:
   nit: This repeats the public doc in `UDFRegistration` almost word for word. 
A one-line summary would be enough for an internal method.



##########
Tests/SparkConnectTests/UDFRegistrationTests.swift:
##########
@@ -0,0 +1,43 @@
+//
+// 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.
+
+#if canImport(FoundationEssentials)
+import FoundationEssentials
+#else
+import Foundation
+#endif
+import Testing
+
+@testable import SparkConnect
+
+/// A test suite for `UDFRegistration`
+@Suite(.serialized)
+struct UDFRegistrationTests {
+
+  @Test
+  func registerJavaWithUnknownClass() async throws {
+    let spark = try await SparkSession.builder.getOrCreate()
+    await #expect(throws: Error.self) {

Review Comment:
   `Error.self` matches any failure, so this passes even if the request is 
malformed (e.g. `javaUdf` not set → `INVALID_ONE_OF_FIELD`) or the call never 
reaches the planner. Could we assert something that's only reachable through 
`handleRegisterJavaUDF`? An existing class without a `UDFn` interface works 
well for that:
   
   ```swift
   let error = try await #require(throws: Error.self) {
     try await spark.udf.registerJava("java_udf", "java.lang.String", .string)
   }
   #expect("\(error)".contains("NO_UDF_INTERFACE"))
   ```
   
   `NO_UDF_INTERFACE` has existed since 3.4, so it's stable across the 
4.0/4.1/4.2 CI images. For the unknown-class case, checking that the message 
contains `NoSuchUDF` would also be stronger than `Error.self`.



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