Skip to content
35 changes: 29 additions & 6 deletions sql/core/src/main/scala/org/apache/spark/sql/DataFrame.scala
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class DataFrame private[sql](
* @param numRows Number of rows to show
*/
private[sql] def showString(numRows: Int): String = {
val sb = new StringBuilder
Copy link
Contributor

Choose a reason for hiding this comment

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

it would be great to create unit test for showString in DataFrameSuite.

val data = take(numRows)
val numCols = schema.fieldNames.length

Expand All @@ -194,12 +195,34 @@ class DataFrame private[sql](
}
}

// Pad the cells
rows.map { row =>
row.zipWithIndex.map { case (cell, i) =>
String.format(s"%-${colWidths(i)}s", cell)
}.mkString(" ")
}.mkString("\n")
// Create SeparateLine
val sep:String = {
"+" + colWidths.map {
size =>
val columnSep = new Array[Char](size)
Copy link
Contributor

Choose a reason for hiding this comment

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

this whole block is really just

"-" * size

for (i <- 0 until size)
columnSep(i) = '-'
String.valueOf(columnSep)
}.mkString("+") + "+\n"
}

sb.append(sep)

// append column names
sb.append("|").append(rows.head.zipWithIndex.map { case (cell, i) =>
String.format(s"%${colWidths(i)}s", cell)
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe use the padding function on string directly, rather than doing the format.

}.mkString("|")).append("|\n")
Copy link
Contributor

Choose a reason for hiding this comment

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

indentation here is off, and it is very confusing to have it wrapping this way


// append data
sb.append(rows.tail.map { row =>
"|" + row.zipWithIndex.map { case (cell, i) =>
String.format(s"%${colWidths(i)}s", cell)
}.mkString("|") + "|"
}.mkString("\n")).append("\n")

sb.append(sep)

sb.toString()
}

override def toString: String = {
Expand Down