Skip to content

Commit a10e1b6

Browse files
author
dujie
committed
[FLINK-37827][table] Fix use SHOW CREATE TABLE for MATERIALIZED_TABLE
1 parent 5eeb2fc commit a10e1b6

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
import org.apache.flink.table.api.TableException;
2323
import org.apache.flink.table.catalog.CatalogBaseTable;
2424
import org.apache.flink.table.catalog.CatalogDescriptor;
25+
import org.apache.flink.table.catalog.CatalogMaterializedTable;
2526
import org.apache.flink.table.catalog.CatalogView;
2627
import org.apache.flink.table.catalog.Column;
2728
import org.apache.flink.table.catalog.ObjectIdentifier;
2829
import org.apache.flink.table.catalog.QueryOperationCatalogView;
2930
import org.apache.flink.table.catalog.ResolvedCatalogBaseTable;
31+
import org.apache.flink.table.catalog.ResolvedCatalogMaterializedTable;
3032
import org.apache.flink.table.catalog.ResolvedCatalogModel;
3133
import org.apache.flink.table.catalog.ResolvedCatalogTable;
3234
import org.apache.flink.table.catalog.ResolvedSchema;
@@ -87,6 +89,9 @@ public static String buildShowCreateTableRow(
8789
String.format(
8890
"SHOW CREATE TABLE is only supported for tables, but %s is a view. Please use SHOW CREATE VIEW instead.",
8991
tableIdentifier.asSerializableString()));
92+
} else if (table.getTableKind() == CatalogBaseTable.TableKind.MATERIALIZED_TABLE) {
93+
return buildShowCreateMaterializedTableRow(
94+
(ResolvedCatalogMaterializedTable) table, tableIdentifier, isTemporary);
9095
}
9196
StringBuilder sb =
9297
new StringBuilder()
@@ -110,6 +115,37 @@ public static String buildShowCreateTableRow(
110115
return sb.toString();
111116
}
112117

118+
private static String buildShowCreateMaterializedTableRow(
119+
ResolvedCatalogMaterializedTable table,
120+
ObjectIdentifier tableIdentifier,
121+
boolean isTemporary) {
122+
StringBuilder sb =
123+
new StringBuilder()
124+
.append(
125+
buildCreateMaterializedTableFormattedPrefix(
126+
isTemporary, tableIdentifier));
127+
extractFormattedPrimaryKey(table, PRINT_INDENT)
128+
.ifPresent(pk -> sb.append(" (").append(pk).append(")\n"));
129+
130+
extractComment(table).ifPresent(c -> sb.append(formatComment(c)).append("\n"));
131+
132+
extractFormattedPartitionedInfo(table)
133+
.ifPresent(
134+
partitionedInfoFormatted ->
135+
sb.append("PARTITIONED BY (")
136+
.append(partitionedInfoFormatted)
137+
.append(")\n"));
138+
139+
extractFormattedOptions(table.getOptions(), PRINT_INDENT)
140+
.ifPresent(v -> sb.append("WITH (\n").append(v).append("\n)\n"));
141+
142+
sb.append(extractMaterializedTablefreshNess(table)).append("\n");
143+
extractMaterializedTableRefreshMode(table).ifPresent(mode -> sb.append(mode).append("\n"));
144+
145+
sb.append("AS ").append(table.getDefinitionQuery());
146+
return sb.toString();
147+
}
148+
113149
/** Show create view statement only for views. */
114150
public static String buildShowCreateViewRow(
115151
ResolvedCatalogBaseTable<?> view,
@@ -161,6 +197,15 @@ static String buildCreateFormattedPrefix(
161197
System.lineSeparator());
162198
}
163199

200+
static String buildCreateMaterializedTableFormattedPrefix(
201+
boolean isTemporary, ObjectIdentifier identifier) {
202+
return String.format(
203+
"CREATE %sMATERIALIZED TABLE %s %s",
204+
isTemporary ? "TEMPORARY " : "",
205+
identifier.asSerializableString(),
206+
System.lineSeparator());
207+
}
208+
164209
static Optional<String> extractFormattedPrimaryKey(
165210
ResolvedCatalogBaseTable<?> table, String printIndent) {
166211
Optional<UniqueConstraint> primaryKey = table.getResolvedSchema().getPrimaryKey();
@@ -269,6 +314,31 @@ static Optional<String> extractFormattedPartitionedInfo(ResolvedCatalogTable cat
269314
.collect(Collectors.joining(", ")));
270315
}
271316

317+
static Optional<String> extractFormattedPartitionedInfo(
318+
ResolvedCatalogMaterializedTable catalogTable) {
319+
if (!catalogTable.isPartitioned()) {
320+
return Optional.empty();
321+
}
322+
return Optional.of(
323+
catalogTable.getPartitionKeys().stream()
324+
.map(EncodingUtils::escapeIdentifier)
325+
.collect(Collectors.joining(", ")));
326+
}
327+
328+
static String extractMaterializedTablefreshNess(ResolvedCatalogMaterializedTable table) {
329+
return String.format("FRESHNESS = %s ", table.getDefinitionFreshness().toString());
330+
}
331+
332+
static Optional<String> extractMaterializedTableRefreshMode(
333+
ResolvedCatalogMaterializedTable table) {
334+
CatalogMaterializedTable.LogicalRefreshMode logicalRefreshMode =
335+
table.getLogicalRefreshMode();
336+
if (logicalRefreshMode == CatalogMaterializedTable.LogicalRefreshMode.AUTOMATIC) {
337+
return Optional.empty();
338+
}
339+
return Optional.of(String.format("REFRESH_MODE = %s", table.getRefreshMode().name()));
340+
}
341+
272342
static Optional<String> extractFormattedOptions(Map<String, String> conf, String printIndent) {
273343
if (Objects.isNull(conf) || conf.isEmpty()) {
274344
return Optional.empty();

0 commit comments

Comments
 (0)