Skip to content

Commit

Permalink
[FLINK-24939][table] Introduce "SHOW CREATE CATALOG" Syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
liyubin117 committed Mar 19, 2024
1 parent 8ec5e7e commit 3929af6
Show file tree
Hide file tree
Showing 13 changed files with 410 additions and 10 deletions.
89 changes: 89 additions & 0 deletions docs/content.zh/docs/dev/table/sql/show.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ SHOW CREATE 语句用于打印给定对象的创建 DDL 语句。当前的 SHOW
目前 Flink SQL 支持下列 SHOW 语句:
- SHOW CATALOGS
- SHOW CURRENT CATALOG
- SHOW CREATE CATALOG
- SHOW DATABASES
- SHOW CURRENT DATABASE
- SHOW TABLES
Expand Down Expand Up @@ -102,6 +103,22 @@ tEnv.executeSql("SHOW CURRENT CATALOG").print();
// | default_catalog |
// +----------------------+

// create a catalog
tEnv.executeSql("CREATE CATALOG cat2 WITH (...)")

// show create catalog
tEnv.executeSql("SHOW CREATE CATALOG cat2").print();
// +---------------------------------------------------------------------------------------------+
// | result |
// +---------------------------------------------------------------------------------------------+
// | CREATE CATALOG `cat2` WITH (
// 'default-database' = 'db',
// 'type' = 'generic_in_memory'
// )
// |
// +---------------------------------------------------------------------------------------------+
// 1 row in set

// show databases
tEnv.executeSql("SHOW DATABASES").print();
// +------------------+
Expand Down Expand Up @@ -214,6 +231,22 @@ tEnv.executeSql("SHOW CATALOGS").print()
// | default_catalog |
// +-----------------+

// create a catalog
tEnv.executeSql("CREATE CATALOG cat2 WITH (...)")

// show create catalog
tEnv.executeSql("SHOW CREATE CATALOG cat2").print();
// +---------------------------------------------------------------------------------------------+
// | result |
// +---------------------------------------------------------------------------------------------+
// | CREATE CATALOG `cat2` WITH (
// 'default-database' = 'db',
// 'type' = 'generic_in_memory'
// )
// |
// +---------------------------------------------------------------------------------------------+
// 1 row in set

// show databases
tEnv.executeSql("SHOW DATABASES").print()
// +------------------+
Expand Down Expand Up @@ -316,6 +349,22 @@ table_env.execute_sql("SHOW CATALOGS").print()
# | default_catalog |
# +-----------------+

# create a catalog
table_env.execute_sql("CREATE CATALOG cat2 WITH (...)")

# show create catalog
table_env.execute_sql("SHOW CREATE CATALOG cat2").print();
# +---------------------------------------------------------------------------------------------+
# | result |
# +---------------------------------------------------------------------------------------------+
# | CREATE CATALOG `cat2` WITH (
# 'default-database' = 'db',
# 'type' = 'generic_in_memory'
# )
# |
# +---------------------------------------------------------------------------------------------+
# 1 row in set

# show databases
table_env.execute_sql("SHOW DATABASES").print()
# +------------------+
Expand Down Expand Up @@ -411,6 +460,14 @@ table_env.execute_sql("SHOW FULL MODULES").print()
Flink SQL> SHOW CATALOGS;
default_catalog

Flink SQL> create catalog cat2 WITH (...);
[INFO] Execute statement succeeded.

Flink SQL> show create catalog cat2;
CREATE CATALOG `cat2` WITH (
...
)

Flink SQL> SHOW DATABASES;
default_database

Expand Down Expand Up @@ -504,6 +561,38 @@ SHOW CURRENT CATALOG

显示当前正在使用的 catalog。

## SHOW CREATE CATALOG

```sql
SHOW CREATE CATALOG catalog_name
```

展示一个现有 catalog 的创建语句。

该语句的输出内容包括包括 catalog 的名称和相关属性,使您可以轻松地重用已创建好的 catalog。

假设 `cat2` 是按如下方式创建的:
```sql
create catalog cat2 WITH (
'type'='generic_in_memory',
'default-database'='db'
);
```
展示 catalog 创建语句。
```sql
show create catalog cat2;
+---------------------------------------------------------------------------------------------+
| result |
+---------------------------------------------------------------------------------------------+
| CREATE CATALOG `cat2` WITH (
'default-database' = 'db',
'type' = 'generic_in_memory'
)
|
+---------------------------------------------------------------------------------------------+
1 row in set
```

## SHOW DATABASES

```sql
Expand Down
89 changes: 89 additions & 0 deletions docs/content/docs/dev/table/sql/show.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ SHOW CREATE statements are used to print a DDL statement with which a given obje
Flink SQL supports the following SHOW statements for now:
- SHOW CATALOGS
- SHOW CURRENT CATALOG
- SHOW CREATE CATALOG
- SHOW DATABASES
- SHOW CURRENT DATABASE
- SHOW TABLES
Expand Down Expand Up @@ -102,6 +103,22 @@ tEnv.executeSql("SHOW CURRENT CATALOG").print();
// | default_catalog |
// +----------------------+

// create a catalog
tEnv.executeSql("CREATE CATALOG cat2 WITH (...)")

// show create catalog
tEnv.executeSql("SHOW CREATE CATALOG cat2").print();
// +---------------------------------------------------------------------------------------------+
// | result |
// +---------------------------------------------------------------------------------------------+
// | CREATE CATALOG `cat2` WITH (
// 'default-database' = 'db',
// 'type' = 'generic_in_memory'
// )
// |
// +---------------------------------------------------------------------------------------------+
// 1 row in set

// show databases
tEnv.executeSql("SHOW DATABASES").print();
// +------------------+
Expand Down Expand Up @@ -214,6 +231,22 @@ tEnv.executeSql("SHOW CATALOGS").print()
// | default_catalog |
// +-----------------+

// create a catalog
tEnv.executeSql("CREATE CATALOG cat2 WITH (...)")

// show create catalog
tEnv.executeSql("SHOW CREATE CATALOG cat2").print();
// +---------------------------------------------------------------------------------------------+
// | result |
// +---------------------------------------------------------------------------------------------+
// | CREATE CATALOG `cat2` WITH (
// 'default-database' = 'db',
// 'type' = 'generic_in_memory'
// )
// |
// +---------------------------------------------------------------------------------------------+
// 1 row in set

// show databases
tEnv.executeSql("SHOW DATABASES").print()
// +------------------+
Expand Down Expand Up @@ -316,6 +349,22 @@ table_env.execute_sql("SHOW CATALOGS").print()
# | default_catalog |
# +-----------------+

# create a catalog
table_env.execute_sql("CREATE CATALOG cat2 WITH (...)")

# show create catalog
table_env.execute_sql("SHOW CREATE CATALOG cat2").print();
# +---------------------------------------------------------------------------------------------+
# | result |
# +---------------------------------------------------------------------------------------------+
# | CREATE CATALOG `cat2` WITH (
# 'default-database' = 'db',
# 'type' = 'generic_in_memory'
# )
# |
# +---------------------------------------------------------------------------------------------+
# 1 row in set

# show databases
table_env.execute_sql("SHOW DATABASES").print()
# +------------------+
Expand Down Expand Up @@ -411,6 +460,14 @@ table_env.execute_sql("SHOW FULL MODULES").print()
Flink SQL> SHOW CATALOGS;
default_catalog

Flink SQL> create catalog cat2 WITH (...);
[INFO] Execute statement succeeded.

Flink SQL> show create catalog cat2;
CREATE CATALOG `cat2` WITH (
...
)

Flink SQL> SHOW DATABASES;
default_database

Expand Down Expand Up @@ -504,6 +561,38 @@ SHOW CURRENT CATALOG

Show current catalog.

## SHOW CREATE CATALOG

```sql
SHOW CREATE CATALOG catalog_name
```

Show creation statement for an existing catalog.

The output includes the catalog's name and relevant properties, which allows you to easily reuse the created catalogs.

Assumes that the catalog `cat2` is created as follows:
```sql
create catalog cat2 WITH (
'type'='generic_in_memory',
'default-database'='db'
);
```
Shows the creation statement.
```sql
show create catalog cat2;
+---------------------------------------------------------------------------------------------+
| result |
+---------------------------------------------------------------------------------------------+
| CREATE CATALOG `cat2` WITH (
'default-database' = 'db',
'type' = 'generic_in_memory'
)
|
+---------------------------------------------------------------------------------------------+
1 row in set
```

## SHOW DATABASES

```sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -686,3 +686,19 @@ show tables from db1 like 'p_r%';
+------------+
1 row in set
!ok

# ==========================================================================
# test show create catalog
# ==========================================================================

show create catalog catalog1;
+--------------------------------------------------------------------+
| result |
+--------------------------------------------------------------------+
| CREATE CATALOG `catalog1` WITH (
'type' = 'generic_in_memory'
)
|
+--------------------------------------------------------------------+
1 row in set
!ok
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
"org.apache.flink.sql.parser.dql.SqlShowCreate"
"org.apache.flink.sql.parser.dql.SqlShowCreateTable"
"org.apache.flink.sql.parser.dql.SqlShowCreateView"
"org.apache.flink.sql.parser.dql.SqlShowCreateCatalog"
"org.apache.flink.sql.parser.dql.SqlShowViews"
"org.apache.flink.sql.parser.dql.SqlRichDescribeTable"
"org.apache.flink.sql.parser.dql.SqlUnloadModule"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,8 @@ SqlShowViews SqlShowViews() :
}

/**
* SHOW TABLES FROM [catalog.] database sql call.
* Parses a show tables statement.
* SHOW TABLES [ ( FROM | IN ) [catalog_name.]database_name ] [ [NOT] LIKE pattern ];
*/
SqlShowTables SqlShowTables() :
{
Expand Down Expand Up @@ -653,7 +654,7 @@ SqlShowColumns SqlShowColumns() :
}

/**
* Parse a "Show Create Table" query and "Show Create View" query commands.
* Parse a "Show Create Table" query and "Show Create View" and "Show Create Catalog" query commands.
*/
SqlShowCreate SqlShowCreate() :
{
Expand All @@ -676,6 +677,13 @@ SqlShowCreate SqlShowCreate() :
{
return new SqlShowCreateView(pos, sqlIdentifier);
}
|
<CATALOG>
{ pos = getPos(); }
sqlIdentifier = SimpleIdentifier()
{
return new SqlShowCreateCatalog(pos, sqlIdentifier);
}
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.flink.sql.parser.dql;

import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlSpecialOperator;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.parser.SqlParserPos;

import java.util.Collections;
import java.util.List;

/** SHOW CREATE CATALOG sql call. */
public class SqlShowCreateCatalog extends SqlShowCreate {

public static final SqlSpecialOperator OPERATOR =
new SqlSpecialOperator("SHOW CREATE CATALOG", SqlKind.OTHER_DDL);

protected final SqlIdentifier catalogName;

public SqlShowCreateCatalog(SqlParserPos pos, SqlIdentifier catalogName) {
super(pos, catalogName);
this.catalogName = catalogName;
}

public SqlIdentifier getCatalogName() {
return catalogName;
}

@Override
public SqlOperator getOperator() {
return OPERATOR;
}

@Override
public List<SqlNode> getOperandList() {
return Collections.singletonList(sqlIdentifier);
}

@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
writer.keyword("SHOW CREATE CATALOG");
sqlIdentifier.unparse(writer, leftPrec, rightPrec);
}

public String catalogName() {
return catalogName.getSimple();
}
}
Loading

0 comments on commit 3929af6

Please sign in to comment.