Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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 @@ -1261,8 +1261,31 @@ case class InitializeJavaBean(beanInstance: Expression, setters: Map[String, Exp
override def children: Seq[Expression] = beanInstance +: setters.values.toSeq
override def dataType: DataType = beanInstance.dataType

override def eval(input: InternalRow): Any =
throw new UnsupportedOperationException("Only code-generated evaluation is supported.")
private lazy val resolvedSetters = {
val ObjectType(beanClass) = beanInstance.dataType
Copy link
Member

Choose a reason for hiding this comment

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

better to put assert(beanInstance.dataType.isInstanceOf[ObjectType]) in the constructor?

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.


setters.map { case (setterMethod, fieldExpr) =>
val foundMethods = beanClass.getMethods.filter { method =>
Copy link
Contributor

Choose a reason for hiding this comment

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

(Picking up our earlier conversation) You are not checking the argument?

Copy link
Member Author

Choose a reason for hiding this comment

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

Will updated later.

method.getName == setterMethod && Modifier.isPublic(method.getModifiers) &&
method.getParameterTypes.length == 1
}
assert(foundMethods.length == 1,
throw new RuntimeException("The Java Bean class should have only one " +
s"setter $setterMethod method, but ${foundMethods.length} methods found."))
(foundMethods.head, fieldExpr)
}
}

override def eval(input: InternalRow): Any = {
val instance = beanInstance.eval(input).asInstanceOf[Object]
Copy link
Member

Choose a reason for hiding this comment

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

super nit: better to put the cast inside if for avoiding unnecessary casts in case of many null cases?;

    val instance = beanInstance.eval(input)
    if (instance != null) {
      val obj = instance.asInstanceOf[Object]
      resolvedSetters.foreach {
        case (setter: Method, expr) =>
          setter.invoke(obj, expr.eval(input).asInstanceOf[Object])
      }
    }
    instance

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.

if (instance != null) {
resolvedSetters.foreach { case (setterMethod, fieldExpr) =>
val fieldValue = fieldExpr.eval(input).asInstanceOf[Object]
setterMethod.invoke(instance, fieldValue)
}
}
instance
}

override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
val instanceGen = beanInstance.genCode(ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ trait ExpressionEvalHelper extends GeneratorDrivenPropertyChecks {
expression: => Expression, expected: Any, inputRow: InternalRow = EmptyRow): Unit = {
val serializer = new JavaSerializer(new SparkConf()).newInstance
val resolver = ResolveTimeZone(new SQLConf)
val expr = resolver.resolveTimeZones(serializer.deserialize(serializer.serialize(expression)))
// Make it as method to obtain fresh expression everytime.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this change?

Copy link
Member Author

Choose a reason for hiding this comment

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

The content of bean instance will be changed after first evaluation of interpreted execution. For example, in the added unit test, the input bean of the later evaluation will become [1] not []. So the later evaluation result will be [1, 1].

Copy link
Contributor

Choose a reason for hiding this comment

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

Are we using a literal? Ok, makes sense.

def expr = resolver.resolveTimeZones(serializer.deserialize(serializer.serialize(expression)))
val catalystValue = CatalystTypeConverters.convertToCatalyst(expected)
checkEvaluationWithoutCodegen(expr, catalystValue, inputRow)
checkEvaluationWithGeneratedMutableProjection(expr, catalystValue, inputRow)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@ class ObjectExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
mapEncoder.serializer.head, mapExpected, mapInputRow)
}

test("SPARK-23593: InitializeJavaBean should support interpreted execution") {
val list = new java.util.LinkedList[Int]()
list.add(1)

val initializeBean = InitializeJavaBean(Literal.fromObject(new java.util.LinkedList[Int]),
Map("add" -> Literal(1)))
checkEvaluation(initializeBean, list, InternalRow.fromSeq(Seq()))

val errMsg = intercept[RuntimeException] {
val initializeWithNonexistingMethod = InitializeJavaBean(
Literal.fromObject(new java.util.LinkedList[Int]),
Map("nonexisting" -> Literal(1)))
evaluate(initializeWithNonexistingMethod, InternalRow.fromSeq(Seq()))
}.getMessage
assert(errMsg.contains("but 0 methods found."))
}

test("SPARK-23585: UnwrapOption should support interpreted execution") {
val cls = classOf[Option[Int]]
val inputObject = BoundReference(0, ObjectType(cls), nullable = true)
Expand Down