-
Notifications
You must be signed in to change notification settings - Fork 28.6k
[SPARK-48845][SQL] GenericUDF catch exceptions from children #47268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
eb2b0a2
50e07fc
5fc7497
66f7e08
f8877e5
b7ed133
0465984
6b1b661
d3ef302
0203bcc
6c47f98
c154823
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,7 +136,13 @@ private[hive] case class HiveGenericUDF( | |
|
||
override def eval(input: InternalRow): Any = { | ||
children.zipWithIndex.foreach { | ||
case (child, idx) => evaluator.setArg(idx, child.eval(input)) | ||
case (child, idx) => | ||
try { | ||
evaluator.setArg(idx, child.eval(input)) | ||
} catch { | ||
case t: Throwable => | ||
evaluator.setException(idx, t) | ||
} | ||
} | ||
evaluator.evaluate() | ||
} | ||
|
@@ -157,10 +163,15 @@ private[hive] case class HiveGenericUDF( | |
val setValues = evals.zipWithIndex.map { | ||
case (eval, i) => | ||
s""" | ||
|if (${eval.isNull}) { | ||
| $refEvaluator.setArg($i, null); | ||
|} else { | ||
| $refEvaluator.setArg($i, ${eval.value}); | ||
|try { | ||
| ${eval.code} | ||
| if (${eval.isNull}) { | ||
| $refEvaluator.setArg($i, null); | ||
| } else { | ||
| $refEvaluator.setArg($i, ${eval.value}); | ||
| } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, we should catch the exceptions from child's executions.
The Exception can catch most of throwable problems. It's ok to be change to Throwable. |
||
|} catch (Throwable t) { | ||
| $refEvaluator.setException($i, t); | ||
|} | ||
|""".stripMargin | ||
} | ||
|
@@ -169,7 +180,6 @@ private[hive] case class HiveGenericUDF( | |
val resultTerm = ctx.freshName("result") | ||
ev.copy(code = | ||
code""" | ||
|${evals.map(_.code).mkString("\n")} | ||
|${setValues.mkString("\n")} | ||
|$resultType $resultTerm = ($resultType) $refEvaluator.evaluate(); | ||
|boolean ${ev.isNull} = $resultTerm == null; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* 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.sql.hive.execution; | ||
|
||
import org.apache.hadoop.hive.ql.exec.UDFArgumentException; | ||
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF; | ||
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; | ||
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; | ||
|
||
public class UDFCatchException extends GenericUDF { | ||
|
||
@Override | ||
public ObjectInspector initialize(ObjectInspector[] args) throws UDFArgumentException { | ||
if (args.length != 1) { | ||
throw new UDFArgumentException("Exactly one argument is expected."); | ||
} | ||
return PrimitiveObjectInspectorFactory.javaStringObjectInspector; | ||
} | ||
|
||
@Override | ||
public Object evaluate(GenericUDF.DeferredObject[] args) { | ||
if (args == null) { | ||
return null; | ||
} | ||
try { | ||
return args[0].get(); | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public String getDisplayString(String[] children) { | ||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* 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.sql.hive.execution; | ||
|
||
import org.apache.hadoop.hive.ql.exec.UDF; | ||
|
||
public class UDFThrowException extends UDF { | ||
public String evaluate(String data) { | ||
return Integer.valueOf(data).toString(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is slightly different from the interpreted path. In codegen we still eagerly execute the function inputs, but delay the throw of errors. I understand that it's hard to be truly lazy in codegen, can we update the interpreted path instead to make it the same as codegen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure and done. I have updated the code for non-codegen mode and the description for this pr.