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-21774][sql-client] Do not display column names when return set is emtpy in SQL Client #15213

Merged
merged 3 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -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