Skip to content
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

[FLINK-16464][sql-client]result-mode tableau may shift when content contains Chinese String in SQL CLI #11334

Merged
merged 5 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.commons.lang3.StringUtils;
import org.jline.terminal.Terminal;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -61,6 +62,7 @@ public class CliTableauResultView implements AutoCloseable {
private static final int DEFAULT_COLUMN_WIDTH = 20;
private static final String COLUMN_TRUNCATED_FLAG = "...";
private static final String CHANGEFLAG_COLUMN_NAME = "+/-";
private static final String DEFAULT_CHARSET = "UTF-8";

private final Terminal terminal;
private final Executor sqlExecutor;
Expand Down Expand Up @@ -274,12 +276,13 @@ private void printSingleRow(int[] colWidths, String[] cols) {
sb.append("|");
int idx = 0;
for (String col : cols) {
byte[] colBytes = getUTF8Bytes(col);
sb.append(" ");
if (col.length() <= colWidths[idx]) {
sb.append(StringUtils.repeat(' ', colWidths[idx] - col.length()));
if (colBytes.length <= colWidths[idx]) {
sb.append(StringUtils.repeat(' ', colWidths[idx] - colBytes.length));
sb.append(col);
} else {
sb.append(col, 0, colWidths[idx] - COLUMN_TRUNCATED_FLAG.length());
sb.append(subMaxString(col, colWidths[idx]));
sb.append(COLUMN_TRUNCATED_FLAG);
}
sb.append(" |");
Expand Down Expand Up @@ -389,7 +392,7 @@ private int[] columnWidthsByContent(List<TableColumn> columns, List<String[]> ro
// fill column width with real data
for (String[] row : rows) {
for (int i = 0; i < row.length; ++i) {
colWidths[i] = Math.max(colWidths[i], row[i].length());
colWidths[i] = Math.max(colWidths[i], getUTF8Bytes(row[i]).length);
}
}

Expand All @@ -401,4 +404,33 @@ private int[] columnWidthsByContent(List<TableColumn> columns, List<String[]> ro
return colWidths;
}

private byte[] getUTF8Bytes(String str) {
try {
return str.getBytes(DEFAULT_CHARSET);
} catch (UnsupportedEncodingException e) {
throw new SqlExecutionException("unknown charset", e);
}
}

private String subMaxString(String col, int colWidth) {
int bytesPos = 0;
int charPos = 0;
// avoid last char's bytes length is 2.
for (char ch: col.toCharArray()) {
bytesPos += getUTF8Bytes(String.valueOf(ch)).length;
if (bytesPos > colWidth - COLUMN_TRUNCATED_FLAG.length()) {
break;
}
charPos++;
}
String substring = col.substring(0, charPos);
int lackChars = colWidth - COLUMN_TRUNCATED_FLAG.length() - getUTF8Bytes(substring).length;

// fix with ' ' before result if lack char.
if (lackChars > 0){
substring = StringUtils.repeat(' ', lackChars) + substring;
}
return substring;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ public void setUp() {
null)
);

data.add(
Row.of(
null,
-1,
-1,
"这是一段中文",
BigDecimal.valueOf(-12345.06789),
Timestamp.valueOf("2020-03-04 18:39:14"))
);

streamingData = new ArrayList<>();
for (int i = 0; i < data.size(); ++i) {
streamingData.add(new Tuple2<>(i % 2 == 0, data.get(i)));
Expand Down Expand Up @@ -167,8 +177,9 @@ public void testBatchResult() {
"| false | -2147483648 | 9223372036854775807 | (NULL) | 12345.06789 | 2020-03-01 18:39:14.123 |\n" +
"| true | 100 | -9223372036854775808 | abcdefg111 | (NULL) | 2020-03-01 18:39:14.123456 |\n" +
"| (NULL) | -1 | -1 | abcdefghijklmnopqrstuvwxyz | -12345.06789 | (NULL) |\n" +
"| (NULL) | -1 | -1 | 这是一段中文 | -12345.06789 | 2020-03-04 18:39:14.0 |\n" +
leonardBang marked this conversation as resolved.
Show resolved Hide resolved
"+---------+-------------+----------------------+----------------------------+----------------+----------------------------+\n" +
"6 row in set\n",
"7 row in set\n",
terminalOutput.toString());
verify(mockExecutor, times(0)).cancelQuery(anyString(), anyString());
}
Expand Down Expand Up @@ -277,8 +288,9 @@ public void testStreamingResult() {
"| - | false | -2147483648 | 9223372036854775807 | (NULL) | 12345.06789 | 2020-03-01 18:39:14.123 |\n" +
"| + | true | 100 | -9223372036854775808 | abcdefg111 | (NULL) | 2020-03-01 18:39:14.123456 |\n" +
"| - | (NULL) | -1 | -1 | abcdefghijklmnopq... | -12345.06789 | (NULL) |\n" +
"| + | (NULL) | -1 | -1 | 这是一段中文 | -12345.06789 | 2020-03-04 18:39:14.0 |\n" +
"+-----+---------+-------------+----------------------+----------------------+----------------+----------------------------+\n" +
"Received a total of 6 rows\n",
"Received a total of 7 rows\n",
terminalOutput.toString());
verify(mockExecutor, times(0)).cancelQuery(anyString(), anyString());
}
Expand Down