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

[IOTDB-430] fix a bug when cache non-align resultSet #876

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 68 additions & 4 deletions client/src/main/java/org/apache/iotdb/client/AbstractClient.java
Expand Up @@ -531,8 +531,15 @@ private static void executeQuery(IoTDBConnection connection, String cmd) {
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int columnLength = resultSetMetaData.getColumnCount();
List<Integer> maxSizeList = new ArrayList<>(columnLength);
List<List<String>> lists = cacheResult(resultSet, maxSizeList, columnLength,
resultSetMetaData, zoneId);
List<List<String>> lists;
if (resultSet instanceof IoTDBQueryResultSet) {
lists = cacheResult(resultSet, maxSizeList, columnLength,
resultSetMetaData, zoneId);
}
else {
lists = cacheNonAlignResult(resultSet, maxSizeList, columnLength,
resultSetMetaData, zoneId);
}
output(lists, maxSizeList);
long costTime = System.currentTimeMillis() - startTime;
println(String.format("It costs %.3fs", costTime / 1000.0));
Expand All @@ -544,8 +551,14 @@ private static void executeQuery(IoTDBConnection connection, String cmd) {
try {
if (br.readLine().equals("")) {
maxSizeList = new ArrayList<>(columnLength);
lists = cacheResult(resultSet, maxSizeList, columnLength,
resultSetMetaData, zoneId);
if (resultSet instanceof IoTDBQueryResultSet) {
lists = cacheResult(resultSet, maxSizeList, columnLength,
resultSetMetaData, zoneId);
}
else {
lists = cacheNonAlignResult(resultSet, maxSizeList, columnLength,
resultSetMetaData, zoneId);
}
output(lists, maxSizeList);
} else {
break;
Expand Down Expand Up @@ -611,6 +624,57 @@ private static List<List<String>> cacheResult(ResultSet resultSet, List<Integer>
return lists;
}

/**
* cache all disable align results
*
* @param resultSet jdbc resultSet
* @param maxSizeList the longest result of every column
* @param columnCount the number of column
* @param resultSetMetaData jdbc resultSetMetaData
* @param zoneId your time zone
* @return List<List < String>> result
* @throws SQLException throw exception
*/
private static List<List<String>> cacheNonAlignResult(ResultSet resultSet, List<Integer> maxSizeList,
int columnCount, ResultSetMetaData resultSetMetaData, ZoneId zoneId) throws SQLException {

List<List<String>> lists = new ArrayList<>(columnCount);
for (int i = 1; i <= columnCount; i++) {
List<String> list = new ArrayList<>(maxPrintRowCount + 1);
if (resultSetMetaData.getColumnLabel(i).startsWith(TIMESTAMP_STR)) {
list.add(TIMESTAMP_STR);
maxSizeList.add(TIMESTAMP_STR.length());
} else {
list.add(resultSetMetaData.getColumnLabel(i));
maxSizeList.add(resultSetMetaData.getColumnLabel(i).length());
}
lists.add(list);
}
int j = 0;
if (cursorBeforeFirst) {
isReachEnd = !resultSet.next();
cursorBeforeFirst = false;
}
while (j < maxPrintRowCount && !isReachEnd) {
for (int i = 1; i <= columnCount; i++) {
String tmp = resultSet.getString(i);
if (tmp == null) {
tmp = NULL;
}
else if ((i & 1) == 1) { // for Time columns
tmp = formatDatetime(resultSet.getLong(i), zoneId);
}
lists.get(i - 1).add(tmp);
if (maxSizeList.get(i - 1) < tmp.length()) {
maxSizeList.set(i - 1, tmp.length());
}
}
j++;
isReachEnd = !resultSet.next();
}
return lists;
}

private static void output(List<List<String>> lists, List<Integer> maxSizeList) {
printBlockLine(maxSizeList);
printRow(lists, 0, maxSizeList);
Expand Down