in repl/src/main/scala/org/apache/livy/repl/AbstractSparkInterpreter.scala [191:236]
private def extractTableFromJValue(value: JValue): Interpreter.ExecuteResponse = {
// Convert the value into JSON and map it to a table.
val rows: List[JValue] = value match {
case JArray(arr) => arr
case _ => List(value)
}
try {
val headers = scala.collection.mutable.Map[String, Map[String, String]]()
val data = rows.map { case row =>
val cols: List[JField] = row match {
case JArray(arr: List[JValue]) =>
arr.zipWithIndex.map { case (v, index) => JField(index.toString, v) }
case JObject(obj) => obj.sortBy(_._1)
case value: JValue => List(JField("0", value))
}
cols.map { case (k, v) =>
val typeName = convertTableType(v)
headers.get(k) match {
case Some(header) =>
if (header.get("type").get != typeName) {
throw new TypesDoNotMatch
}
case None =>
headers.put(k, Map(
"type" -> typeName,
"name" -> k
))
}
v
}
}
Interpreter.ExecuteSuccess(
APPLICATION_LIVY_TABLE_JSON -> (
("headers" -> headers.toSeq.sortBy(_._1).map(_._2)) ~ ("data" -> data)
))
} catch {
case _: TypesDoNotMatch =>
Interpreter.ExecuteError("TypeError", "table rows have different types")
}
}