Skip to content

Commit

Permalink
[FLINK-10230][table] Support 'SHOW CREATE VIEW' syntax to print the q…
Browse files Browse the repository at this point in the history
…uery of a view

This closes apache#17352
  • Loading branch information
RocMarshal committed Oct 26, 2021
1 parent fbc086f commit 0ed7847
Show file tree
Hide file tree
Showing 18 changed files with 759 additions and 158 deletions.
49 changes: 41 additions & 8 deletions docs/content.zh/docs/dev/table/sql/show.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ under the License.
# SHOW 语句


SHOW 语句用于列出其相应父对象中的对象,例如 catalog、database、table 和 view、column、function 和 module。有关详细信息和其他选项,请参见各个命令。

SHOW 语句用于列出所有的 catalog,或者列出当前 catalog 中所有的 database,或者列出当前 catalog 和当前 database 的所有表或视图,或者列出当前正在使用的 catalog 和 database, 或者列出创建指定表的语句,或者列出当前 catalog 和当前 database 中所有的 function,包括:系统 function 和用户定义的 function,或者仅仅列出当前 catalog 和当前 database 中用户定义的 function,或者列出当前环境所有激活的 module,或者列出当前环境所有加载的 module 及激活状态,或者根据可选的模糊查询语句列出给定表或视图的相应列
SHOW CREATE 语句用于打印给定对象的创建 DDL 语句。当前的 SHOW CREATE 语句仅在打印给定表和视图的 DDL 语句时可用

目前 Flink SQL 支持下列 SHOW 语句:
- SHOW CATALOGS
Expand All @@ -39,6 +40,7 @@ SHOW 语句用于列出所有的 catalog,或者列出当前 catalog 中所有
- SHOW CREATE TABLE
- SHOW COLUMNS
- SHOW VIEWS
- SHOW CREATE VIEW
- SHOW FUNCTIONS
- SHOW MODULES
- SHOW FULL MODULES
Expand Down Expand Up @@ -132,7 +134,7 @@ tEnv.executeSql("SHOW CREATE TABLE my_table").print();
// )

// show columns
tEnv.executeSql("SHOW COLUMNS FROM MY_TABLE LIKE '%f%'").print();
tEnv.executeSql("SHOW COLUMNS FROM my_table LIKE '%f%'").print();
// +--------+-------+------+-----+--------+-----------+
// | name | type | null | key | extras | watermark |
// +--------+-------+------+-----+--------+-----------+
Expand All @@ -141,7 +143,7 @@ tEnv.executeSql("SHOW COLUMNS FROM MY_TABLE LIKE '%f%'").print();


// create a view
tEnv.executeSql("CREATE VIEW my_view AS ...");
tEnv.executeSql("CREATE VIEW my_view AS SELECT * FROM my_table");
// show views
tEnv.executeSql("SHOW VIEWS").print();
// +-----------+
Expand All @@ -150,6 +152,12 @@ tEnv.executeSql("SHOW VIEWS").print();
// | my_view |
// +-----------+

// show create view
tEnv.executeSql("SHOW CREATE VIEW my_view").print();
// CREATE VIEW `default_catalog`.`default_db`.`my_view`(`field1`, `field2`, ...) as
// SELECT *
// FROM `default_catalog`.`default_database`.`my_table`

// show functions
tEnv.executeSql("SHOW FUNCTIONS").print();
// +---------------+
Expand Down Expand Up @@ -230,15 +238,15 @@ tEnv.executeSql("SHOW CREATE TABLE my_table").print()
// )

// show columns
tEnv.executeSql("SHOW COLUMNS FROM MY_TABLE LIKE '%f%'").print()
tEnv.executeSql("SHOW COLUMNS FROM my_table LIKE '%f%'").print()
// +--------+-------+------+-----+--------+-----------+
// | name | type | null | key | extras | watermark |
// +--------+-------+------+-----+--------+-----------+
// | field2 | BYTES | true | | | |
// +--------+-------+------+-----+--------+-----------+

// create a view
tEnv.executeSql("CREATE VIEW my_view AS ...")
tEnv.executeSql("CREATE VIEW my_view AS SELECT * FROM my_table")
// show views
tEnv.executeSql("SHOW VIEWS").print()
// +-----------+
Expand All @@ -247,6 +255,12 @@ tEnv.executeSql("SHOW VIEWS").print()
// | my_view |
// +-----------+

// show create view
tEnv.executeSql("SHOW CREATE VIEW my_view").print();
// CREATE VIEW `default_catalog`.`default_db`.`my_view`(`field1`, `field2`, ...) as
// SELECT *
// FROM `default_catalog`.`default_database`.`my_table`

// show functions
tEnv.executeSql("SHOW FUNCTIONS").print()
// +---------------+
Expand Down Expand Up @@ -325,15 +339,15 @@ table_env.executeSql("SHOW CREATE TABLE my_table").print()
# )

# show columns
table_env.execute_sql("SHOW COLUMNS FROM MY_TABLE LIKE '%f%'").print()
table_env.execute_sql("SHOW COLUMNS FROM my_table LIKE '%f%'").print()
# +--------+-------+------+-----+--------+-----------+
# | name | type | null | key | extras | watermark |
# +--------+-------+------+-----+--------+-----------+
# | field2 | BYTES | true | | | |
# +--------+-------+------+-----+--------+-----------+

# create a view
table_env.execute_sql("CREATE VIEW my_view AS ...")
table_env.execute_sql("CREATE VIEW my_view AS SELECT * FROM my_table")
# show views
table_env.execute_sql("SHOW VIEWS").print()
# +-----------+
Expand All @@ -342,6 +356,12 @@ table_env.execute_sql("SHOW VIEWS").print()
# | my_view |
# +-----------+

# show create view
table_env.execute_sql("SHOW CREATE VIEW my_view").print()
# CREATE VIEW `default_catalog`.`default_db`.`my_view`(`field1`, `field2`, ...) as
# SELECT *
# FROM `default_catalog`.`default_database`.`my_table`

# show functions
table_env.execute_sql("SHOW FUNCTIONS").print()
# +---------------+
Expand Down Expand Up @@ -414,12 +434,17 @@ Flink SQL> SHOW COLUMNS from MyUserTable LIKE '%f%';
1 row in set


Flink SQL> CREATE VIEW my_view AS ...;
Flink SQL> CREATE VIEW my_view AS SELECT * from my_table;
[INFO] View has been created.

Flink SQL> SHOW VIEWS;
my_view

Flink SQL> SHOW CREATE VIEW my_view;
CREATE VIEW `default_catalog`.`default_db`.`my_view`(`field1`, `field2`, ...) as
SELECT *
FROM `default_catalog`.`default_database`.`my_table`

Flink SQL> SHOW FUNCTIONS;
mod
sha256
Expand Down Expand Up @@ -606,6 +631,14 @@ SHOW VIEWS

展示当前 catalog 和当前 database 中所有的视图。

## SHOW CREATE VIEW

```sql
SHOW CREATE VIEW [catalog_name.][db_name.]view_name
```

展示创建指定视图的 create 语句。

## SHOW FUNCTIONS

```sql
Expand Down
50 changes: 42 additions & 8 deletions docs/content/docs/dev/table/sql/show.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ under the License.

# SHOW Statements

SHOW statements are used to list all catalogs, or list all databases in the current catalog, or list all tables/views in the current catalog and the current database, or show current catalog and database, or show create statement for specified table, or list all functions including system functions and user-defined functions in the current catalog and current database, or list only user-defined functions in the current catalog and current database, or list enabled module names, or list all loaded modules with enabled status in the current session, or list the columns of the table or the view with the given name and the optional like clause.
SHOW statements are used to list objects within their corresponding parent, such as catalogs, databases, tables and views, columns, functions, and modules. See the individual commands for more details and additional options.

SHOW CREATE statements are used to print a DDL statement with which a given object can be created. The currently 'SHOW CREATE' statement is only available in printing DDL statement of the given table and view.

Flink SQL supports the following SHOW statements for now:
- SHOW CATALOGS
Expand All @@ -37,6 +39,7 @@ Flink SQL supports the following SHOW statements for now:
- SHOW CREATE TABLE
- SHOW COLUMNS
- SHOW VIEWS
- SHOW CREATE VIEW
- SHOW FUNCTIONS
- SHOW MODULES
- SHOW JARS
Expand Down Expand Up @@ -131,7 +134,7 @@ tEnv.executeSql("SHOW CREATE TABLE my_table").print();
// )

// show columns
tEnv.executeSql("SHOW COLUMNS FROM MY_TABLE LIKE '%f%'").print();
tEnv.executeSql("SHOW COLUMNS FROM my_table LIKE '%f%'").print();
// +--------+-------+------+-----+--------+-----------+
// | name | type | null | key | extras | watermark |
// +--------+-------+------+-----+--------+-----------+
Expand All @@ -140,7 +143,7 @@ tEnv.executeSql("SHOW COLUMNS FROM MY_TABLE LIKE '%f%'").print();


// create a view
tEnv.executeSql("CREATE VIEW my_view AS ...");
tEnv.executeSql("CREATE VIEW my_view AS SELECT * FROM my_table");
// show views
tEnv.executeSql("SHOW VIEWS").print();
// +-----------+
Expand All @@ -149,6 +152,12 @@ tEnv.executeSql("SHOW VIEWS").print();
// | my_view |
// +-----------+

// show create view
tEnv.executeSql("SHOW CREATE VIEW my_view").print();
// CREATE VIEW `default_catalog`.`default_db`.`my_view`(`field1`, `field2`, ...) as
// SELECT *
// FROM `default_catalog`.`default_database`.`my_table`

// show functions
tEnv.executeSql("SHOW FUNCTIONS").print();
// +---------------+
Expand Down Expand Up @@ -229,15 +238,15 @@ tEnv.executeSql("SHOW CREATE TABLE my_table").print()
// )

// show columns
tEnv.executeSql("SHOW COLUMNS FROM MY_TABLE LIKE '%f%'").print()
tEnv.executeSql("SHOW COLUMNS FROM my_table LIKE '%f%'").print()
// +--------+-------+------+-----+--------+-----------+
// | name | type | null | key | extras | watermark |
// +--------+-------+------+-----+--------+-----------+
// | field2 | BYTES | true | | | |
// +--------+-------+------+-----+--------+-----------+

// create a view
tEnv.executeSql("CREATE VIEW my_view AS ...")
tEnv.executeSql("CREATE VIEW my_view AS SELECT * FROM my_table")
// show views
tEnv.executeSql("SHOW VIEWS").print()
// +-----------+
Expand All @@ -246,6 +255,12 @@ tEnv.executeSql("SHOW VIEWS").print()
// | my_view |
// +-----------+

// show create view
tEnv.executeSql("SHOW CREATE VIEW my_view").print();
// CREATE VIEW `default_catalog`.`default_db`.`my_view`(`field1`, `field2`, ...) as
// SELECT *
// FROM `default_catalog`.`default_database`.`my_table`

// show functions
tEnv.executeSql("SHOW FUNCTIONS").print()
// +---------------+
Expand Down Expand Up @@ -324,15 +339,15 @@ table_env.executeSql("SHOW CREATE TABLE my_table").print()
# )

# show columns
table_env.execute_sql("SHOW COLUMNS FROM MY_TABLE LIKE '%f%'").print()
table_env.execute_sql("SHOW COLUMNS FROM my_table LIKE '%f%'").print()
# +--------+-------+------+-----+--------+-----------+
# | name | type | null | key | extras | watermark |
# +--------+-------+------+-----+--------+-----------+
# | field2 | BYTES | true | | | |
# +--------+-------+------+-----+--------+-----------+

# create a view
table_env.execute_sql("CREATE VIEW my_view AS ...")
table_env.execute_sql("CREATE VIEW my_view AS SELECT * FROM my_table")
# show views
table_env.execute_sql("SHOW VIEWS").print()
# +-----------+
Expand All @@ -341,6 +356,12 @@ table_env.execute_sql("SHOW VIEWS").print()
# | my_view |
# +-----------+

# show create view
table_env.execute_sql("SHOW CREATE VIEW my_view").print()
# CREATE VIEW `default_catalog`.`default_db`.`my_view`(`field1`, `field2`, ...) as
# SELECT *
# FROM `default_catalog`.`default_database`.`my_table`

# show functions
table_env.execute_sql("SHOW FUNCTIONS").print()
# +---------------+
Expand Down Expand Up @@ -413,12 +434,17 @@ Flink SQL> SHOW COLUMNS from MyUserTable LIKE '%f%';
1 row in set


Flink SQL> CREATE VIEW my_view AS ...;
Flink SQL> CREATE VIEW my_view AS SELECT * from my_table;
[INFO] View has been created.

Flink SQL> SHOW VIEWS;
my_view

Flink SQL> SHOW CREATE VIEW my_view;
CREATE VIEW `default_catalog`.`default_db`.`my_view`(`field1`, `field2`, ...) as
SELECT *
FROM `default_catalog`.`default_database`.`my_table`

Flink SQL> SHOW FUNCTIONS;
mod
sha256
Expand Down Expand Up @@ -605,6 +631,14 @@ SHOW VIEWS

Show all views in the current catalog and the current database.

## SHOW CREATE VIEW

```sql
SHOW CREATE VIEW [catalog_name.][db_name.]view_name
```

Show create view statement for specified view.

## SHOW FUNCTIONS

```sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.flink.table.operations.Operation;
import org.apache.flink.table.operations.QueryOperation;
import org.apache.flink.table.operations.ShowCreateTableOperation;
import org.apache.flink.table.operations.ShowCreateViewOperation;
import org.apache.flink.table.operations.UnloadModuleOperation;
import org.apache.flink.table.operations.UseOperation;
import org.apache.flink.table.operations.command.AddJarOperation;
Expand Down Expand Up @@ -443,6 +444,9 @@ private void callOperation(Operation operation, ExecutionMode mode) {
} else if (operation instanceof ShowCreateTableOperation) {
// SHOW CREATE TABLE
callShowCreateTable((ShowCreateTableOperation) operation);
} else if (operation instanceof ShowCreateViewOperation) {
// SHOW CREATE VIEW
callShowCreateView((ShowCreateViewOperation) operation);
} else {
// fallback to default implementation
executeOperation(operation);
Expand Down Expand Up @@ -593,6 +597,10 @@ public void callShowCreateTable(ShowCreateTableOperation operation) {
printRawContent(operation);
}

public void callShowCreateView(ShowCreateViewOperation operation) {
printRawContent(operation);
}

public void printRawContent(Operation operation) {
TableResult tableResult = executor.executeOperation(sessionId, operation);
// show raw content instead of tableau style
Expand Down
6 changes: 6 additions & 0 deletions flink-table/flink-sql-client/src/test/resources/sql/table.q
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,12 @@ CREATE TABLE `default_catalog`.`default_database`.`orders2` (
!ok
# test SHOW CREATE VIEW for tables
show create view orders2;
[ERROR] Could not execute SQL statement. Reason:
org.apache.flink.table.api.TableException: SHOW CREATE VIEW is only supported for views, but `default_catalog`.`default_database`.`orders2` is a table. Please use SHOW CREATE TABLE instead.
!error
# test explain plan to verify the table source cannot be created
explain plan for select * from orders2;
[ERROR] Could not execute SQL statement. Reason:
Expand Down
33 changes: 32 additions & 1 deletion flink-table/flink-sql-client/src/test/resources/sql/view.q
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ create temporary view if not exists v2 as select * from v1;
[INFO] Execute statement succeed.
!info

# test show create a temporary view
show create view v1;
CREATE TEMPORARY VIEW `default_catalog`.`default_database`.`v1`(`user`, `product`, `amount`, `ts`, `ptime`) as
SELECT *
FROM `default_catalog`.`default_database`.`orders`
!ok

# test show create a temporary view reference another view
show create view v2;
CREATE TEMPORARY VIEW `default_catalog`.`default_database`.`v2`(`user`, `product`, `amount`, `ts`, `ptime`) as
SELECT *
FROM `default_catalog`.`default_database`.`v1`
!ok

show tables;
+------------+
| table name |
Expand All @@ -75,7 +89,7 @@ show views;
# test SHOW CREATE TABLE for views
show create table v1;
[ERROR] Could not execute SQL statement. Reason:
org.apache.flink.table.api.TableException: SHOW CREATE TABLE does not support showing CREATE VIEW statement with identifier `default_catalog`.`default_database`.`v1`.
org.apache.flink.table.api.TableException: SHOW CREATE TABLE is only supported for tables, but `default_catalog`.`default_database`.`v1` is a view. Please use SHOW CREATE VIEW instead.
!error

# ==== test permanent view =====
Expand All @@ -91,6 +105,23 @@ create view v1 as select * from orders;
org.apache.flink.table.catalog.exceptions.TableAlreadyExistException: Table (or view) default_database.v1 already exists in Catalog default_catalog.
!error

# test show create a permanent view
create view permanent_v1 as select * from orders;
[INFO] Execute statement succeed.
!info

# test show create a permanent view
show create view permanent_v1;
CREATE VIEW `default_catalog`.`default_database`.`permanent_v1`(`user`, `product`, `amount`, `ts`, `ptime`) as
SELECT *
FROM `default_catalog`.`default_database`.`orders`
!ok

# remove permanent_v1 view
drop view permanent_v1;
[INFO] Execute statement succeed.
!info

# we didn't distinguish the temporary v1 and permanent v1 for now
show views;
+-----------+
Expand Down
Loading

0 comments on commit 0ed7847

Please sign in to comment.