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 @@ -1030,14 +1030,28 @@ abstract class TreeNode[BaseType <: TreeNode[BaseType]] extends Product with Tre
case p: Product if shouldConvertToJson(p) =>
try {
val fieldNames = getConstructorParameterNames(p.getClass)
val fieldValues = p.productIterator.toSeq
val fieldValues = {
if (p.productArity == fieldNames.length) {
p.productIterator.toSeq
} else {
val clazz = p.getClass
// Fallback to use reflection if length of product elements do not match
// constructor params.
fieldNames.map { fieldName =>
val field = clazz.getDeclaredField(fieldName)
field.setAccessible(true)
field.get(p)
}
}
}
assert(fieldNames.length == fieldValues.length, s"$simpleClassName fields: " +
fieldNames.mkString(", ") + s", values: " + fieldValues.mkString(", "))
("product-class" -> JString(p.getClass.getName)) :: fieldNames.zip(fieldValues).map {
case (name, value) => name -> parseToJson(value)
}.toList
} catch {
case _: RuntimeException => null
case _: ReflectiveOperationException => null
}
case _ => JNull
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ case class FakeLeafPlan(child: LogicalPlan)
override def output: Seq[Attribute] = child.output
}

case class FakeCurryingProduct(x: Expression)(val y: Int)

class TreeNodeSuite extends SparkFunSuite with SQLHelper {
test("top node changed") {
val after = Literal(1) transform { case Literal(1, _) => Literal(2) }
Expand Down Expand Up @@ -622,6 +624,21 @@ class TreeNodeSuite extends SparkFunSuite with SQLHelper {
"num-children" -> 0,
"arg" -> "1"
)))))

// Convert currying product contains TreeNode to JSON.
assertJSON(
FakeCurryingProduct(Literal(1))(1),
JObject(
"product-class" -> classOf[FakeCurryingProduct].getName,
"x" -> List(
JObject(
"class" -> JString(classOf[Literal].getName),
"num-children" -> 0,
"value" -> "1",
"dataType" -> "integer")),
"y" -> 1
)
)
}

test("toJSON should not throws java.lang.StackOverflowError") {
Expand Down