[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:
Marco Gaido 2018-03-08 22:02:28 +01:00 committed by Herman van Hovell
parent ea480990e7
commit e7bbca8896
2 changed files with 31 additions and 1 deletions

View file

@ -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 "

View file

@ -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"))
}
}