Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1358,11 +1358,19 @@ case class GetExternalRowField(

override def dataType: DataType = ObjectType(classOf[Object])

override def eval(input: InternalRow): Any =
throw new UnsupportedOperationException("Only code-generated evaluation is supported")

private val errMsg = s"The ${index}th field '$fieldName' of input row cannot be null."

override def eval(input: InternalRow): Any = {
val inputRow = child.eval(input).asInstanceOf[Row]
if (inputRow == null) {
throw new RuntimeException("The input external row cannot be null.")
}
if (inputRow.isNullAt(index)) {
throw new RuntimeException(errMsg)
}
inputRow.get(index)
}

override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
// Use unnamed reference that doesn't create a local field here to reduce the number of fields
// because errMsgField is used only when the field is null.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.spark.sql.catalyst.expressions

import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.Row
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder
import org.apache.spark.sql.catalyst.expressions.objects._
Expand Down Expand Up @@ -84,4 +85,23 @@ class ObjectExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(wrapObject, expected, InternalRow.fromSeq(Seq(input)))
}
}

test("SPARK-23594 GetExternalRowField should support interpreted execution") {
val inputObject = BoundReference(0, ObjectType(classOf[Row]), nullable = true)
val getRowField = GetExternalRowField(inputObject, index = 0, fieldName = "c0")
Seq((Row(1), 1), (Row(3), 3)).foreach { case (input, expected) =>
checkEvaluation(getRowField, expected, InternalRow.fromSeq(Seq(input)))
}

// If an input row or a field are null, a runtime exception will be thrown
val errMsg1 = intercept[RuntimeException] {
evaluate(getRowField, InternalRow.fromSeq(Seq(null)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comment: This only tests the interpreted code path. Maybe we should add something to the ExpressionEvalHelper harness to test all paths in case of failure. Let me know if you want to pick this up, we can also spin that off into a separate PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, sound nice to me. Would it be better to add the helper function first before this pr?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just merge this, and add the helper in a follow-up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, thanks! I'll make a pr later for that.

}.getMessage
assert(errMsg1 === "The input external row cannot be null.")

val errMsg2 = intercept[RuntimeException] {
evaluate(getRowField, InternalRow.fromSeq(Seq(Row(null))))
}.getMessage
assert(errMsg2 === "The 0th field 'c0' of input row cannot be null.")
}
}