maropu commented on a change in pull request #29947:
URL: https://github.com/apache/spark/pull/29947#discussion_r500672056



##########
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")

Review comment:
       nit: `RuntimeException("null")` instead of `RuntimeException()`?

##########
File path: sql/core/src/test/resources/sql-tests/inputs/misc-functions.sql
##########
@@ -8,3 +8,12 @@ select typeof(cast(1.0 as float)), typeof(1.0D), typeof(1.2);
 select typeof(date '1986-05-23'),  typeof(timestamp '1986-05-23'), 
typeof(interval '23 days');
 select typeof(x'ABCD'), typeof('SPARK');
 select typeof(array(1, 2)), typeof(map(1, 2)), typeof(named_struct('a', 1, 
'b', 'spark'));
+
+-- Spark-32793: Rewrite AssertTrue with RaiseError

Review comment:
       Could you add tests like this?
   ```
   scala> sql("select * from t").show()
   +---+
   |  v|
   +---+
   |  1|
   |  8|
   |  2|
   +---+
   
   scala> sql("select if(v > 5, raise_error('error found: ' || v), v + 1) from 
t").show()
   java.lang.RuntimeException: error found: 8
     at org.apache.spark.sql.catalyst.expressions.RaiseError.eval(misc.scala:80)
   ```
   That is because I think it is a common usecase to print out an error message 
with an invalid value.

##########
File path: 
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CodeGenerationSuite.scala
##########
@@ -332,7 +332,7 @@ class CodeGenerationSuite extends SparkFunSuite with 
ExpressionEvalHelper {
   }
 
   test("SPARK-17160: field names are properly escaped by AssertTrue") {
-    GenerateUnsafeProjection.generate(AssertTrue(Cast(Literal("\""), 
BooleanType)) :: Nil)
+    GenerateUnsafeProjection.generate(new AssertTrue(Cast(Literal("\""), 
BooleanType)).child :: Nil)

Review comment:
       nit: we don't need `new` now.




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

Reply via email to