viirya commented on a change in pull request #29947:
URL: https://github.com/apache/spark/pull/29947#discussion_r500711220
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/misc.scala
##########
@@ -53,51 +53,80 @@ case class PrintToStderr(child: Expression) extends
UnaryExpression {
}
/**
- * A function throws an exception if 'condition' is not true.
+ * Throw with the result of an expression (used for debugging).
*/
@ExpressionDescription(
- usage = "_FUNC_(expr) - Throws an exception if `expr` is not true.",
+ usage = "_FUNC_(expr) - Throws an exception with `expr`.",
examples = """
Examples:
- > SELECT _FUNC_(0 < 1);
- NULL
+ > SELECT _FUNC_('custom error message');
+ java.lang.RuntimeException
+ custom error message
""",
- since = "2.0.0")
-case class AssertTrue(child: Expression) extends UnaryExpression with
ImplicitCastInputTypes {
-
- override def nullable: Boolean = true
-
- override def inputTypes: Seq[DataType] = Seq(BooleanType)
+ since = "3.1.0")
+case class RaiseError(child: Expression) extends UnaryExpression with
ImplicitCastInputTypes {
+ override def foldable: Boolean = false
override def dataType: DataType = NullType
+ override def inputTypes: Seq[AbstractDataType] = Seq(StringType)
- override def prettyName: String = "assert_true"
+ override def prettyName: String = "raise_error"
- private val errMsg =
s"'${child.simpleString(SQLConf.get.maxToStringFields)}' is not true!"
-
- override def eval(input: InternalRow) : Any = {
- val v = child.eval(input)
- if (v == null || java.lang.Boolean.FALSE.equals(v)) {
- throw new RuntimeException(errMsg)
- } else {
- null
+ override def eval(input: InternalRow): Any = {
+ val value = child.eval(input)
+ if (value == null) {
+ throw new RuntimeException("null")
}
+ throw new RuntimeException(value.toString())
}
+ // if (true) is to avoid codegen compilation exception that statement is
unreachable
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
val eval = child.genCode(ctx)
+ ExprCode(
+ code = code"""${eval.code}
+ |if (true) {
+ | if (${eval.value} == null) {
+ | throw new RuntimeException("null");
+ | }
+ | throw new RuntimeException(${eval.value}.toString());
+ |}""".stripMargin,
+ isNull = TrueLiteral,
Review comment:
Oh, I see. It is `NullType`.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]