Skip to content

Commit

Permalink
[SPARK-12105] [SQL] add convenient show functions
Browse files Browse the repository at this point in the history
Author: Jean-Baptiste Onofré <[email protected]>

Closes #10130 from jbonofre/SPARK-12105.
  • Loading branch information
jbonofre authored and Andrew Or committed Dec 16, 2015
1 parent 2811265 commit 31b3910
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions sql/core/src/main/scala/org/apache/spark/sql/DataFrame.scala
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,24 @@ class DataFrame private[sql](
}
}

/**
* Compose the string representing rows for output
*/
def showString(): String = {
showString(20)
}

/**
* Compose the string representing rows for output
* @param _numRows Number of rows to show
* @param numRows Number of rows to show
* @param truncate Whether truncate long strings and align cells right
*/
private[sql] def showString(_numRows: Int, truncate: Boolean = true): String = {
val numRows = _numRows.max(0)
def showString(numRows: Int, truncate: Boolean = true): String = {
val _numRows = numRows.max(0)
val sb = new StringBuilder
val takeResult = take(numRows + 1)
val hasMoreData = takeResult.length > numRows
val data = takeResult.take(numRows)
val takeResult = take(_numRows + 1)
val hasMoreData = takeResult.length > _numRows
val data = takeResult.take(_numRows)
val numCols = schema.fieldNames.length

// For array values, replace Seq and Array with square brackets
Expand Down Expand Up @@ -224,10 +231,10 @@ class DataFrame private[sql](

sb.append(sep)

// For Data that has more than "numRows" records
// For Data that has more than "_numRows" records
if (hasMoreData) {
val rowsString = if (numRows == 1) "row" else "rows"
sb.append(s"only showing top $numRows $rowsString\n")
val rowsString = if (_numRows == 1) "row" else "rows"
sb.append(s"only showing top $_numRows $rowsString\n")
}

sb.toString()
Expand Down

0 comments on commit 31b3910

Please sign in to comment.