[SPARK-23602][SQL] PrintToStderr prints value also in interpreted mode
## What changes were proposed in this pull request? `PrintToStderr` was doing what is it supposed to only when code generation is enabled. The PR adds the same behavior in interpreted mode too. ## How was this patch tested? added UT Author: Marco Gaido <marcogaido91@gmail.com> Closes #20773 from mgaido91/SPARK-23602.
This commit is contained in:
parent
ea480990e7
commit
e7bbca8896
|
@ -31,7 +31,12 @@ case class PrintToStderr(child: Expression) extends UnaryExpression {
|
|||
|
||||
override def dataType: DataType = child.dataType
|
||||
|
||||
protected override def nullSafeEval(input: Any): Any = input
|
||||
protected override def nullSafeEval(input: Any): Any = {
|
||||
// scalastyle:off println
|
||||
System.err.println(outputPrefix + input)
|
||||
// scalastyle:on println
|
||||
input
|
||||
}
|
||||
|
||||
private val outputPrefix = s"Result of ${child.simpleString} is "
|
||||
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
package org.apache.spark.sql.catalyst.expressions
|
||||
|
||||
import java.io.PrintStream
|
||||
|
||||
import org.apache.spark.SparkFunSuite
|
||||
import org.apache.spark.sql.types._
|
||||
|
||||
|
@ -43,4 +45,27 @@ class MiscExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
|
|||
checkEvaluation(Length(Uuid()), 36)
|
||||
assert(evaluateWithoutCodegen(Uuid()) !== evaluateWithoutCodegen(Uuid()))
|
||||
}
|
||||
|
||||
test("PrintToStderr") {
|
||||
val inputExpr = Literal(1)
|
||||
val systemErr = System.err
|
||||
|
||||
val (outputEval, outputCodegen) = try {
|
||||
val errorStream = new java.io.ByteArrayOutputStream()
|
||||
System.setErr(new PrintStream(errorStream))
|
||||
// check without codegen
|
||||
checkEvaluationWithoutCodegen(PrintToStderr(inputExpr), 1)
|
||||
val outputEval = errorStream.toString
|
||||
errorStream.reset()
|
||||
// check with codegen
|
||||
checkEvaluationWithGeneratedMutableProjection(PrintToStderr(inputExpr), 1)
|
||||
val outputCodegen = errorStream.toString
|
||||
(outputEval, outputCodegen)
|
||||
} finally {
|
||||
System.setErr(systemErr)
|
||||
}
|
||||
|
||||
assert(outputCodegen.contains(s"Result of $inputExpr is 1"))
|
||||
assert(outputEval.contains(s"Result of $inputExpr is 1"))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue