spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gatorsmile <...@git.apache.org>
Subject [GitHub] spark pull request #17733: [SPARK-20425][SQL] Support a vertical display mod...
Date Wed, 26 Apr 2017 05:57:59 GMT
Github user gatorsmile commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17733#discussion_r113370773
  
    --- Diff: sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala ---
    @@ -277,43 +279,73 @@ class Dataset[T] private[sql](
     
         val sb = new StringBuilder
         val numCols = schema.fieldNames.length
    +    // We set a minimum column width at '3'
    +    val minimumColWidth = 3
     
    -    // Initialise the width of each column to a minimum value of '3'
    -    val colWidths = Array.fill(numCols)(3)
    +    if (!vertical) {
    +      // Initialise the width of each column to a minimum value
    +      val colWidths = Array.fill(numCols)(minimumColWidth)
     
    -    // Compute the width of each column
    -    for (row <- rows) {
    -      for ((cell, i) <- row.zipWithIndex) {
    -        colWidths(i) = math.max(colWidths(i), cell.length)
    -      }
    -    }
    -
    -    // Create SeparateLine
    -    val sep: String = colWidths.map("-" * _).addString(sb, "+", "+", "+\n").toString()
    -
    -    // column names
    -    rows.head.zipWithIndex.map { case (cell, i) =>
    -      if (truncate > 0) {
    -        StringUtils.leftPad(cell, colWidths(i))
    -      } else {
    -        StringUtils.rightPad(cell, colWidths(i))
    +      // Compute the width of each column
    +      for (row <- rows) {
    +        for ((cell, i) <- row.zipWithIndex) {
    +          colWidths(i) = math.max(colWidths(i), cell.length)
    +        }
           }
    -    }.addString(sb, "|", "|", "|\n")
     
    -    sb.append(sep)
    +      // Create SeparateLine
    +      val sep: String = colWidths.map("-" * _).addString(sb, "+", "+", "+\n").toString()
     
    -    // data
    -    rows.tail.map {
    -      _.zipWithIndex.map { case (cell, i) =>
    +      // column names
    +      rows.head.zipWithIndex.map { case (cell, i) =>
             if (truncate > 0) {
    -          StringUtils.leftPad(cell.toString, colWidths(i))
    +          StringUtils.leftPad(cell, colWidths(i))
             } else {
    -          StringUtils.rightPad(cell.toString, colWidths(i))
    +          StringUtils.rightPad(cell, colWidths(i))
             }
           }.addString(sb, "|", "|", "|\n")
    -    }
     
    -    sb.append(sep)
    +      sb.append(sep)
    +
    +      // data
    +      rows.tail.foreach {
    +        _.zipWithIndex.map { case (cell, i) =>
    +          if (truncate > 0) {
    +            StringUtils.leftPad(cell.toString, colWidths(i))
    +          } else {
    +            StringUtils.rightPad(cell.toString, colWidths(i))
    +          }
    +        }.addString(sb, "|", "|", "|\n")
    +      }
    +
    +      sb.append(sep)
    +    } else {
    +      // Extended display mode enabled
    +      val fieldNames = rows.head
    +      val dataRows = rows.tail
    +
    +      // Compute the width of field name and data columns
    +      val fieldNameColWidth = fieldNames.foldLeft(minimumColWidth) { case (curMax, fieldName)
=>
    +        math.max(curMax, fieldName.length)
    +      }
    +      val dataColWidth = dataRows.foldLeft(minimumColWidth) { case (curMax, row) =>
    +        math.max(curMax, row.map(_.length).reduceLeftOption[Int] { case (cellMax, cell)
=>
    +          math.max(cellMax, cell)
    +        }.getOrElse(0))
    +      }
    +
    +      dataRows.zipWithIndex.foreach { case (row, i) =>
    --- End diff --
    
    When no row exists, we at least need to output the column names.
    
    ```Scala
    df.limit(0).show(20, 0, true)
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message