Skip to content

Commit

Permalink
[FLINK-10229][Sql Client] Support listing of views
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghua committed Mar 11, 2019
1 parent 59afddb commit 5d9ba4d
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/dev/table/sqlClient.md
Expand Up @@ -504,6 +504,12 @@ Views created within a CLI session can also be removed again using the `DROP VIE
DROP VIEW MyNewView;
{% endhighlight %}

Displays all of the views in the current session using the `SHOW VIEWS` statement:

{% highlight text %}
SHOW VIEWS
{% endhighlight %}

<span class="label label-danger">Attention</span> The definition of views in the CLI is limited to the mentioned syntax above. Defining a schema for views or escaping whitespaces in table names will be supported in future versions.

{% top %}
Expand Down
Expand Up @@ -266,6 +266,9 @@ private void callCommand(SqlCommandCall cmdCall) {
case SHOW_TABLES:
callShowTables();
break;
case SHOW_VIEWS:
callShowViews();
break;
case SHOW_FUNCTIONS:
callShowFunctions();
break;
Expand Down Expand Up @@ -359,6 +362,23 @@ private void callShowTables() {
terminal.flush();
}

private void callShowViews() {
final List<String> views;
try {
views = executor.listViews(context);
} 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() {
final List<String> functions;
try {
Expand Down
Expand Up @@ -45,6 +45,7 @@ private CliStrings() {
.append(formatCommand(SqlCommand.CLEAR, "Clears the current terminal."))
.append(formatCommand(SqlCommand.HELP, "Prints the available commands."))
.append(formatCommand(SqlCommand.SHOW_TABLES, "Shows all registered tables."))
.append(formatCommand(SqlCommand.SHOW_VIEWS, "Shows all registered views."))
.append(formatCommand(SqlCommand.SHOW_FUNCTIONS, "Shows all registered user-defined functions."))
.append(formatCommand(SqlCommand.DESCRIBE, "Describes the schema of a table with the given name."))
.append(formatCommand(SqlCommand.EXPLAIN, "Describes the execution plan of a query or table with the given name."))
Expand Down
Expand Up @@ -87,6 +87,10 @@ enum SqlCommand {
"SHOW\\s+TABLES",
NO_OPERANDS),

SHOW_VIEWS(
"SHOW\\s+VIEWS",
NO_OPERANDS),

SHOW_FUNCTIONS(
"SHOW\\s+FUNCTIONS",
NO_OPERANDS),
Expand Down
Expand Up @@ -45,6 +45,11 @@ public interface Executor {
*/
List<String> listTables(SessionContext session) throws SqlExecutionException;

/**
* Lists all views known to the executor.
*/
List<String> listViews(SessionContext session) throws SqlExecutionException;

/**
* Lists all user-defined functions known to the executor.
*/
Expand Down
Expand Up @@ -199,6 +199,17 @@ public List<String> listTables(SessionContext session) throws SqlExecutionExcept
return Arrays.asList(tableEnv.listTables());
}

@Override
public List<String> listViews(SessionContext session) throws SqlExecutionException {
List<String> viewList = new ArrayList<>();
session.getViews().keySet().forEach((viewName) -> viewList.add(viewName));

//sort
Collections.sort(viewList);

return viewList;
}

@Override
public List<String> listUserDefinedFunctions(SessionContext session) throws SqlExecutionException {
final TableEnvironment tableEnv = getOrCreateExecutionContext(session)
Expand Down
Expand Up @@ -163,6 +163,11 @@ public List<String> listTables(SessionContext session) throws SqlExecutionExcept
return null;
}

@Override
public List<String> listViews(SessionContext session) throws SqlExecutionException {
return null;
}

@Override
public List<String> listUserDefinedFunctions(SessionContext session) throws SqlExecutionException {
return null;
Expand Down
Expand Up @@ -130,6 +130,11 @@ public List<String> listTables(SessionContext session) throws SqlExecutionExcept
return null;
}

@Override
public List<String> listViews(SessionContext session) throws SqlExecutionException {
return null;
}

@Override
public List<String> listUserDefinedFunctions(SessionContext session) throws SqlExecutionException {
return null;
Expand Down
Expand Up @@ -40,6 +40,8 @@ public void testCommands() {
testValidSqlCommand("CLEAR", new SqlCommandCall(SqlCommand.CLEAR));
testValidSqlCommand("SHOW TABLES", new SqlCommandCall(SqlCommand.SHOW_TABLES));
testValidSqlCommand(" SHOW TABLES ", new SqlCommandCall(SqlCommand.SHOW_TABLES));
testValidSqlCommand("SHOW VIEWS", new SqlCommandCall(SqlCommand.SHOW_VIEWS));
testValidSqlCommand(" SHOW VIEWS", new SqlCommandCall(SqlCommand.SHOW_VIEWS));
testValidSqlCommand("SHOW FUNCTIONS", new SqlCommandCall(SqlCommand.SHOW_FUNCTIONS));
testValidSqlCommand(" SHOW FUNCTIONS ", new SqlCommandCall(SqlCommand.SHOW_FUNCTIONS));
testValidSqlCommand("DESCRIBE MyTable", new SqlCommandCall(SqlCommand.DESCRIBE, new String[]{"MyTable"}));
Expand Down
Expand Up @@ -149,6 +149,26 @@ public void testValidateSession() throws Exception {
assertEquals(expectedTables, actualTables);
}

@Test
public void testListViews() throws Exception {
final Executor executor = createDefaultExecutor(clusterClient);
final SessionContext session = new SessionContext("test-session", new Environment());

session.addView(ViewEntry.create("TestView3", "SELECT 1"));
session.addView(ViewEntry.create("MyView1", "SELECT 1"));
session.addView(ViewEntry.create("aView1", "SELECT 1"));

final List<String> actualViews = executor.listViews(session);
final List<String> expectedViews = Arrays.asList(
"MyView1",
"TestView1",
"TestView2",
"TestView3",
"aView1");

assertEquals(expectedViews, actualViews);
}

@Test
public void testListTables() throws Exception {
final Executor executor = createDefaultExecutor(clusterClient);
Expand Down

0 comments on commit 5d9ba4d

Please sign in to comment.