-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-25108][SQL] Fix the show method to display the wide character alignment problem #22048
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
1b9b2e7
9aec12f
906c0ba
da37d2e
8737671
697ac04
3d65e6b
363de6b
3649de5
45ac272
52acfd5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -294,23 +294,25 @@ class Dataset[T] private[sql]( | |
| // We set a minimum column width at '3' | ||
| val minimumColWidth = 3 | ||
|
|
||
| //Regular expression matching full width characters | ||
| val fullWidthRegex = """[\u1100-\u115F\u2E80-\uA4CF\uAC00-\uD7A3\uF900-\uFAFF\uFE10-\uFE19\uFE30-\uFE6F\uFF00-\uFF60\uFFE0-\uFFE6]""".r | ||
|
||
| 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) | ||
| colWidths(i) = math.max(colWidths(i), cell.length + fullWidthRegex.findAllIn(cell).size) | ||
|
||
| } | ||
| } | ||
|
|
||
| val paddedRows = rows.map { row => | ||
| row.zipWithIndex.map { case (cell, i) => | ||
| if (truncate > 0) { | ||
| StringUtils.leftPad(cell, colWidths(i)) | ||
xuejianbest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| StringUtils.leftPad(cell, colWidths(i) - fullWidthRegex.findAllIn(cell).size) | ||
| } else { | ||
| StringUtils.rightPad(cell, colWidths(i)) | ||
| StringUtils.rightPad(cell, colWidths(i) - fullWidthRegex.findAllIn(cell).size) | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -332,12 +334,10 @@ class Dataset[T] private[sql]( | |
|
|
||
| // Compute the width of field name and data columns | ||
| val fieldNameColWidth = fieldNames.foldLeft(minimumColWidth) { case (curMax, fieldName) => | ||
| math.max(curMax, fieldName.length) | ||
| math.max(curMax, fieldName.length + fullWidthRegex.findAllIn(fieldName).size) | ||
| } | ||
| 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)) | ||
| math.max(curMax, row.map(cell => cell.length + fullWidthRegex.findAllIn(cell).size).max) | ||
| } | ||
|
|
||
| dataRows.zipWithIndex.foreach { case (row, i) => | ||
|
|
@@ -346,8 +346,8 @@ class Dataset[T] private[sql]( | |
| s"-RECORD $i", fieldNameColWidth + dataColWidth + 5, "-") | ||
| sb.append(rowHeader).append("\n") | ||
| row.zipWithIndex.map { case (cell, j) => | ||
| val fieldName = StringUtils.rightPad(fieldNames(j), fieldNameColWidth) | ||
| val data = StringUtils.rightPad(cell, dataColWidth) | ||
| val fieldName = StringUtils.rightPad(fieldNames(j), fieldNameColWidth - fullWidthRegex.findAllIn(fieldNames(j)).size) | ||
| val data = StringUtils.rightPad(cell, dataColWidth - fullWidthRegex.findAllIn(cell).size) | ||
| s" $fieldName | $data " | ||
| }.addString(sb, "", "\n", "\n") | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very small nit that might fail scalastyle -- space after comment slashes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, thanks.