Skip to content

Commit 8b71634

Browse files
authored
[FLINK-39302][table] Support SHOW CREATE OR ALTER MATERIALIZED TABLE
1 parent b975641 commit 8b71634

9 files changed

Lines changed: 197 additions & 145 deletions

File tree

docs/content.zh/docs/sql/reference/utility/show.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ SHOW CREATE 语句用于打印给定对象的创建 DDL 语句。当前的 SHOW
4141
- SHOW TABLES
4242
- SHOW CREATE TABLE
4343
- SHOW MATERIALIZED TABLES
44-
- SHOW CREATE MATERIALIZED TABLE
44+
- SHOW CREATE [OR ALTER ]MATERIALIZED TABLE
4545
- SHOW COLUMNS
4646
- SHOW PARTITIONS
4747
- SHOW PROCEDURES
@@ -993,14 +993,20 @@ The syntax of sql pattern in `LIKE` clause is the same as that of `MySQL` dialec
993993
* `%` matches any number of characters, including zero characters, `\%` matches one `%` character.
994994
* `_` matches exactly one character, `\_` matches one `_` character.
995995

996-
## SHOW CREATE MATERIALIZED TABLE
996+
## SHOW CREATE [OR ALTER ]MATERIALIZED TABLE
997997

998998
```sql
999999
SHOW CREATE MATERIALIZED TABLE [catalog_name.][db_name.]materialized_table_name
10001000
```
10011001

10021002
Show create materialized table statement for specified materialized table.
10031003

1004+
```sql
1005+
SHOW CREATE OR ALTER MATERIALIZED TABLE [catalog_name.][db_name.]materialized_table_name
1006+
```
1007+
1008+
Show create or alter materialized table statement for specified materialized table.
1009+
10041010
展示创建指定视图的 create 语句。
10051011

10061012
## SHOW FUNCTIONS

docs/content/docs/sql/reference/utility/show.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Flink SQL supports the following SHOW statements for now:
4545
- SHOW VIEWS
4646
- SHOW CREATE VIEW
4747
- SHOW MATERIALIZED TABLES
48-
- SHOW CREATE MATERIALIZED TABLE
48+
- SHOW CREATE [OR ALTER ]MATERIALIZED TABLE
4949
- SHOW FUNCTIONS
5050
- SHOW MODULES
5151
- SHOW JARS
@@ -1004,14 +1004,20 @@ The syntax of sql pattern in `LIKE` clause is the same as that of `MySQL` dialec
10041004
* `%` matches any number of characters, including zero characters, `\%` matches one `%` character.
10051005
* `_` matches exactly one character, `\_` matches one `_` character.
10061006

1007-
## SHOW CREATE MATERIALIZED TABLE
1007+
## SHOW CREATE [OR ALTER ]MATERIALIZED TABLE
10081008

10091009
```sql
10101010
SHOW CREATE MATERIALIZED TABLE [catalog_name.][db_name.]materialized_table_name
10111011
```
10121012

10131013
Show create materialized table statement for specified materialized table.
10141014

1015+
```sql
1016+
SHOW CREATE OR ALTER MATERIALIZED TABLE [catalog_name.][db_name.]materialized_table_name
1017+
```
1018+
1019+
Show create or alter materialized table statement for specified materialized table.
1020+
10151021
## SHOW FUNCTIONS
10161022

10171023
```sql

flink-table/flink-sql-parser/src/main/codegen/includes/parserImpls.ftl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,11 +792,13 @@ SqlShowCreate SqlShowCreate() :
792792
return new SqlShowCreateConnection(pos, sqlIdentifier);
793793
}
794794
|
795+
{boolean createOrAlter = false;}
796+
[ <OR> <ALTER> {createOrAlter = true;} ]
795797
<MATERIALIZED> <TABLE>
796798
{ pos = getPos(); }
797799
sqlIdentifier = CompoundIdentifier()
798800
{
799-
return new SqlShowCreateMaterializedTable(pos, sqlIdentifier);
801+
return new SqlShowCreateMaterializedTable(pos, sqlIdentifier, createOrAlter);
800802
}
801803
)
802804
}

flink-table/flink-sql-parser/src/main/java/org/apache/flink/sql/parser/dql/SqlShowCreateMaterializedTable.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,20 @@
2929
import java.util.Collections;
3030
import java.util.List;
3131

32-
/** SHOW CREATE MATERIALIZED TABLE sql call. */
32+
/** SHOW CREATE [OR ALTER ]MATERIALIZED TABLE sql call. */
3333
public class SqlShowCreateMaterializedTable extends SqlShowCreate {
34-
public static final SqlSpecialOperator OPERATOR =
34+
private static final SqlSpecialOperator SHOW_CREATE_OPERATOR =
3535
new SqlSpecialOperator("SHOW CREATE MATERIALIZED TABLE", SqlKind.OTHER_DDL);
3636

37-
public SqlShowCreateMaterializedTable(SqlParserPos pos, SqlIdentifier sqlIdentifier) {
37+
private static final SqlSpecialOperator SHOW_CREATE_OR_ALTER_OPERATOR =
38+
new SqlSpecialOperator("SHOW CREATE OR ALTER MATERIALIZED TABLE", SqlKind.OTHER_DDL);
39+
40+
private final boolean createOrAlter;
41+
42+
public SqlShowCreateMaterializedTable(
43+
SqlParserPos pos, SqlIdentifier sqlIdentifier, boolean createOrAlter) {
3844
super(pos, sqlIdentifier);
45+
this.createOrAlter = createOrAlter;
3946
}
4047

4148
public SqlIdentifier getMaterializedTableName() {
@@ -48,17 +55,21 @@ public String[] getFullMaterializedTableName() {
4855

4956
@Override
5057
public SqlOperator getOperator() {
51-
return OPERATOR;
58+
return createOrAlter ? SHOW_CREATE_OR_ALTER_OPERATOR : SHOW_CREATE_OPERATOR;
5259
}
5360

5461
@Override
5562
public List<SqlNode> getOperandList() {
5663
return Collections.singletonList(sqlIdentifier);
5764
}
5865

66+
public boolean isCreateOrAlter() {
67+
return createOrAlter;
68+
}
69+
5970
@Override
6071
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
61-
writer.keyword(OPERATOR.getName());
72+
writer.keyword(getOperator().getName());
6273
sqlIdentifier.unparse(writer, leftPrec, rightPrec);
6374
}
6475
}

flink-table/flink-sql-parser/src/test/java/org/apache/flink/sql/parser/MaterializedTableStatementParserTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,22 @@ void testStartMode() {
741741
+ " ");
742742
}
743743

744+
@Test
745+
void testShowCreateOrAlterMaterializedTable() {
746+
sql("show create materialized table mt1").ok("SHOW CREATE MATERIALIZED TABLE `MT1`");
747+
sql("show create materialized table db1.mt1")
748+
.ok("SHOW CREATE MATERIALIZED TABLE `DB1`.`MT1`");
749+
sql("show create materialized table catalog1.db1.mt1")
750+
.ok("SHOW CREATE MATERIALIZED TABLE `CATALOG1`.`DB1`.`MT1`");
751+
752+
sql("show create or alter materialized table mt1")
753+
.ok("SHOW CREATE OR ALTER MATERIALIZED TABLE `MT1`");
754+
sql("show create or alter materialized table db1.mt1")
755+
.ok("SHOW CREATE OR ALTER MATERIALIZED TABLE `DB1`.`MT1`");
756+
sql("show create or alter materialized table catalog1.db1.mt1")
757+
.ok("SHOW CREATE OR ALTER MATERIALIZED TABLE `CATALOG1`.`DB1`.`MT1`");
758+
}
759+
744760
public SqlParserFixture fixture() {
745761
return SqlParserFixture.DEFAULT.withConfig(
746762
c -> c.withParserFactory(FlinkSqlParserImpl.FACTORY));

flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/internal/ShowCreateUtil.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static String buildShowCreateModelRow(
6161
new StringBuilder()
6262
.append(
6363
buildCreateFormattedPrefix(
64-
"MODEL", isTemporary, modelIdentifier, false));
64+
"MODEL", isTemporary, modelIdentifier, false, false));
6565
extractFormattedColumns(model.getResolvedInputSchema())
6666
.ifPresent(
6767
c -> sb.append(String.format("INPUT (%s)%s", c, System.lineSeparator())));
@@ -93,7 +93,7 @@ public static String buildShowCreateTableRow(
9393
new StringBuilder()
9494
.append(
9595
buildCreateFormattedPrefix(
96-
"TABLE", isTemporary, tableIdentifier, true));
96+
"TABLE", isTemporary, tableIdentifier, false, true));
9797
sb.append(extractFormattedColumns(table, PRINT_INDENT));
9898
extractFormattedWatermarkSpecs(table, PRINT_INDENT, sqlFactory)
9999
.ifPresent(watermarkSpecs -> sb.append(",\n").append(watermarkSpecs));
@@ -117,13 +117,18 @@ public static String buildShowCreateMaterializedTableRow(
117117
ResolvedCatalogMaterializedTable table,
118118
ObjectIdentifier tableIdentifier,
119119
boolean isTemporary,
120+
boolean createOrAlter,
120121
SqlFactory sqlFactory) {
121122
validateTableKind(table, tableIdentifier, TableKind.MATERIALIZED_TABLE);
122123
StringBuilder sb =
123124
new StringBuilder()
124125
.append(
125126
buildCreateFormattedPrefix(
126-
"MATERIALIZED TABLE", isTemporary, tableIdentifier, true));
127+
"MATERIALIZED TABLE",
128+
isTemporary,
129+
tableIdentifier,
130+
createOrAlter,
131+
true));
127132
sb.append(extractFormattedColumns(table, PRINT_INDENT));
128133
extractFormattedWatermarkSpecs(table, PRINT_INDENT, sqlFactory)
129134
.ifPresent(watermarkSpecs -> sb.append(",\n").append(watermarkSpecs));
@@ -162,7 +167,7 @@ public static String buildShowCreateViewRow(
162167
new StringBuilder()
163168
.append(
164169
buildCreateFormattedPrefix(
165-
"VIEW", isTemporary, viewIdentifier, true));
170+
"VIEW", isTemporary, viewIdentifier, false, true));
166171
sb.append(extractFormattedColumnNames(view, PRINT_INDENT)).append("\n)\n");
167172
extractComment(view).ifPresent(c -> sb.append(formatComment(c)).append("\n"));
168173
sb.append("AS ").append(((CatalogView) origin).getExpandedQuery()).append("\n");
@@ -186,9 +191,11 @@ static String buildCreateFormattedPrefix(
186191
String type,
187192
boolean isTemporary,
188193
ObjectIdentifier identifier,
194+
boolean createOrAlter,
189195
boolean openParenthesis) {
190196
return String.format(
191-
"CREATE %s%s %s%s%s",
197+
"CREATE %s%s%s %s%s%s",
198+
createOrAlter ? "OR ALTER " : "",
192199
isTemporary ? "TEMPORARY " : "",
193200
type,
194201
identifier.asSerializableString(),

flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/operations/ShowCreateMaterializedTableOperation.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,23 @@
2727

2828
import static org.apache.flink.table.api.internal.TableResultUtils.buildStringArrayResult;
2929

30-
/** Operation to describe a SHOW CREATE MATERIALIZED TABLE statement. */
30+
/** Operation to describe a SHOW CREATE [OR ALTER ]MATERIALIZED TABLE statement. */
3131
@Internal
3232
public class ShowCreateMaterializedTableOperation implements ShowOperation {
3333
private final ObjectIdentifier tableIdentifier;
34+
private final boolean createOrAlter;
3435

35-
public ShowCreateMaterializedTableOperation(ObjectIdentifier sqlIdentifier) {
36+
public ShowCreateMaterializedTableOperation(
37+
ObjectIdentifier sqlIdentifier, boolean createOrAlter) {
3638
this.tableIdentifier = sqlIdentifier;
39+
this.createOrAlter = createOrAlter;
3740
}
3841

3942
@Override
4043
public String asSummaryString() {
4144
return String.format(
42-
"SHOW CREATE MATERIALIZED TABLE %s", tableIdentifier.asSummaryString());
45+
"SHOW CREATE %sMATERIALIZED TABLE %s",
46+
createOrAlter ? "OR ALTER " : "", tableIdentifier.asSummaryString());
4347
}
4448

4549
@Override
@@ -51,13 +55,15 @@ public TableResultInternal execute(Context ctx) {
5155
() ->
5256
new ValidationException(
5357
String.format(
54-
"Could not execute SHOW CREATE MATERIALIZED TABLE. Materialized table with identifier %s does not exist.",
58+
"Could not execute %s. Materialized table with identifier %s does not exist.",
59+
asSummaryString(),
5560
tableIdentifier.asSerializableString())));
5661
String resultRow =
5762
ShowCreateUtil.buildShowCreateMaterializedTableRow(
5863
table.getResolvedTable(),
5964
tableIdentifier,
6065
table.isTemporary(),
66+
createOrAlter,
6167
ctx.getCatalogManager().getSqlFactory());
6268

6369
return buildStringArrayResult("result", new String[] {resultRow});

0 commit comments

Comments
 (0)