hvanhovell commented on code in PR #42995:
URL: https://github.com/apache/spark/pull/42995#discussion_r1330380393
##########
connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/expressions/UserDefinedFunction.scala:
##########
@@ -183,6 +183,7 @@ object ScalarUserDefinedFunction {
function: AnyRef,
inputEncoders: Seq[AgnosticEncoder[_]],
outputEncoder: AgnosticEncoder[_]): ScalarUserDefinedFunction = {
+ SparkConnectClosureCleaner.clean(function)
Review Comment:
So in theory we could defer serialization with your change. One of the
motivations for doing it immediately that we would capture a partially
constructed parent class; this prevents a self-reference which we can't
deserialize. OTOH it is probably better to throw an exception where we
construct the UDF, instead of throwing one when we submit the query for
execution/analysis.
##########
connector/connect/client/jvm/src/test/scala/org/apache/spark/sql/application/ReplE2ESuite.scala:
##########
@@ -362,4 +362,103 @@ class ReplE2ESuite extends RemoteSparkSession with
BeforeAndAfterEach {
val output = runCommandsInShell(input)
assertContains("noException: Boolean = true", output)
}
+
+ test("closure cleaner") {
+ val input =
+ """
+ |class NonSerializable(val id: Int = -1) { }
+ |
+ |val x = 100; val y = new NonSerializable
+ |val t = 200
+ |def foo(): Int = { x }; def bar(): Int = { y.id }; val z = new
NonSerializable
Review Comment:
NIT: Is this a way to force ammonite to treat this as a single line
(compilation unit)?
If so, then using ```{ ... }``` is bit easier.
##########
core/src/main/scala/org/apache/spark/util/SparkClosureCleaner.scala:
##########
@@ -0,0 +1,49 @@
+/*
+ * 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.
+ */
+
+package org.apache.spark.util
+
+import scala.collection.mutable
+
+import org.apache.spark.{SparkEnv, SparkException}
+
+private[spark] object SparkClosureCleaner {
+ /**
+ * Clean the given closure in place.
+ *
+ * More specifically, this renders the given closure serializable as long as
it does not
+ * explicitly reference unserializable objects.
+ *
+ * @param closure the closure to clean
+ * @param checkSerializable whether to verify that the closure is
serializable after cleaning
+ * @param cleanTransitively whether to clean enclosing closures transitively
+ */
+ def clean(
+ closure: AnyRef,
+ checkSerializable: Boolean = true,
+ cleanTransitively: Boolean = true): Unit = {
+ if (ClosureCleaner.clean(closure, cleanTransitively, mutable.Map.empty)) {
Review Comment:
NIT, for readability it is a nit nicer to put this in a boolean.
--
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]