Skip to content

Commit

Permalink
[FLINK-21774][sql-client] Display all the SHOW command result in tabl…
Browse files Browse the repository at this point in the history
…eau format

This closes apache#15213
  • Loading branch information
SteNicholas committed Mar 17, 2021
1 parent b0467af commit 83d6698
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 271 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import org.apache.flink.table.client.gateway.ResultDescriptor;
import org.apache.flink.table.client.gateway.SqlExecutionException;
import org.apache.flink.table.utils.PrintUtils;
import org.apache.flink.types.Row;
import org.apache.flink.util.CollectionUtil;

import org.jline.reader.EndOfFileException;
import org.jline.reader.LineReader;
Expand All @@ -51,13 +49,9 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.apache.flink.util.Preconditions.checkNotNull;
import static org.apache.flink.util.Preconditions.checkState;

/** SQL CLI client. */
Expand Down Expand Up @@ -294,31 +288,15 @@ private void callCommand(SqlCommandCall cmdCall) {
callHelp();
break;
case SHOW_CATALOGS:
callShowCatalogs();
break;
case SHOW_CURRENT_CATALOG:
callShowCurrentCatalog();
break;
case SHOW_DATABASES:
callShowDatabases();
break;
case SHOW_CURRENT_DATABASE:
callShowCurrentDatabase();
break;
case SHOW_TABLES:
callShowTables();
break;
case SHOW_VIEWS:
callShowViews();
break;
case SHOW_FUNCTIONS:
callShowFunctions(cmdCall);
break;
case SHOW_MODULES:
callShowModules(cmdCall);
break;
case SHOW_PARTITIONS:
callShowPartitions(cmdCall);
case SHOW_CURRENT_CATALOG:
case SHOW_CURRENT_DATABASE:
callShow(cmdCall);
break;
case USE_CATALOG:
callUseCatalog(cmdCall);
Expand Down Expand Up @@ -478,145 +456,9 @@ private void callHelp() {
terminal.flush();
}

private void callShowCatalogs() {
final List<String> catalogs;
try {
catalogs = getShowResult("CATALOGS");
} catch (SqlExecutionException e) {
printExecutionException(e);
return;
}
if (catalogs.isEmpty()) {
terminal.writer().println(CliStrings.messageInfo(CliStrings.MESSAGE_EMPTY).toAnsi());
} else {
catalogs.forEach((v) -> terminal.writer().println(v));
}
terminal.flush();
}

private void callShowCurrentCatalog() {
String currentCatalog;
try {
Row result = executor.executeSql(sessionId, "SHOW CURRENT CATALOG").collect().next();
currentCatalog = checkNotNull(result.getField(0)).toString();
} catch (SqlExecutionException e) {
printExecutionException(e);
return;
}
terminal.writer().println(currentCatalog);
terminal.flush();
}

private void callShowDatabases() {
final List<String> dbs;
try {
dbs = getShowResult("DATABASES");
} catch (SqlExecutionException e) {
printExecutionException(e);
return;
}
if (dbs.isEmpty()) {
terminal.writer().println(CliStrings.messageInfo(CliStrings.MESSAGE_EMPTY).toAnsi());
} else {
dbs.forEach((v) -> terminal.writer().println(v));
}
terminal.flush();
}

private void callShowCurrentDatabase() {
String currentDatabase;
try {
Row result = executor.executeSql(sessionId, "SHOW CURRENT DATABASE").collect().next();
currentDatabase = checkNotNull(result.getField(0)).toString();
} catch (SqlExecutionException e) {
printExecutionException(e);
return;
}
terminal.writer().println(currentDatabase);
terminal.flush();
}

private void callShowTables() {
final List<String> tables;
try {
tables = getShowResult("TABLES");
} catch (SqlExecutionException e) {
printExecutionException(e);
return;
}
if (tables.isEmpty()) {
terminal.writer().println(CliStrings.messageInfo(CliStrings.MESSAGE_EMPTY).toAnsi());
} else {
tables.forEach((v) -> terminal.writer().println(v));
}
terminal.flush();
}

private void callShowViews() {
final List<String> views;
try {
views = getShowResult("VIEWS");
} catch (SqlExecutionException e) {
printExecutionException(e);
return;
}
if (views.isEmpty()) {
terminal.writer().println(CliStrings.messageInfo(CliStrings.MESSAGE_EMPTY).toAnsi());
} else {
views.forEach((v) -> terminal.writer().println(v));
}
terminal.flush();
}

private void callShowFunctions(SqlCommandCall cmdCall) {
final List<String> functions;
try {
functions = getShowResult(cmdCall);
} catch (SqlExecutionException e) {
printExecutionException(e);
return;
}
if (functions.isEmpty()) {
terminal.writer().println(CliStrings.messageInfo(CliStrings.MESSAGE_EMPTY).toAnsi());
} else {
Collections.sort(functions);
functions.forEach((v) -> terminal.writer().println(v));
}
terminal.flush();
}

private List<String> getShowResult(String objectToShow) {
TableResult tableResult = executor.executeSql(sessionId, "SHOW " + objectToShow);
return CollectionUtil.iteratorToList(tableResult.collect()).stream()
.map(r -> checkNotNull(r.getField(0)).toString())
.collect(Collectors.toList());
}

private List<String> getShowResult(SqlCommandCall cmdCall) {
TableResult tableResult = executor.executeSql(sessionId, cmdCall.operands[0]);
return CollectionUtil.iteratorToList(tableResult.collect()).stream()
.map(r -> checkNotNull(r.getField(0)).toString())
.collect(Collectors.toList());
}

private void callShowModules(SqlCommandCall cmdCall) {
getResultAsTableauForm(cmdCall.operands[0]);
}

private void callShowPartitions(SqlCommandCall cmdCall) {
final List<String> partitions;
try {
partitions = getShowResult(cmdCall);
} catch (SqlExecutionException e) {
printExecutionException(e);
return;
}
if (partitions.isEmpty()) {
terminal.writer().println(CliStrings.messageInfo(CliStrings.MESSAGE_EMPTY).toAnsi());
} else {
partitions.forEach((v) -> terminal.writer().println(v));
}
terminal.flush();
private void callShow(SqlCommandCall cmdCall) {
getResultAsTableauForm(
cmdCall.operands.length == 0 ? cmdCall.command.toString() : cmdCall.operands[0]);
}

private void callUseCatalog(SqlCommandCall cmdCall) {
Expand Down
Loading

0 comments on commit 83d6698

Please sign in to comment.