22
22
import org .apache .flink .table .api .TableException ;
23
23
import org .apache .flink .table .catalog .CatalogBaseTable ;
24
24
import org .apache .flink .table .catalog .CatalogDescriptor ;
25
+ import org .apache .flink .table .catalog .CatalogMaterializedTable ;
25
26
import org .apache .flink .table .catalog .CatalogView ;
26
27
import org .apache .flink .table .catalog .Column ;
27
28
import org .apache .flink .table .catalog .ObjectIdentifier ;
28
29
import org .apache .flink .table .catalog .QueryOperationCatalogView ;
29
30
import org .apache .flink .table .catalog .ResolvedCatalogBaseTable ;
31
+ import org .apache .flink .table .catalog .ResolvedCatalogMaterializedTable ;
30
32
import org .apache .flink .table .catalog .ResolvedCatalogModel ;
31
33
import org .apache .flink .table .catalog .ResolvedCatalogTable ;
32
34
import org .apache .flink .table .catalog .ResolvedSchema ;
@@ -87,6 +89,9 @@ public static String buildShowCreateTableRow(
87
89
String .format (
88
90
"SHOW CREATE TABLE is only supported for tables, but %s is a view. Please use SHOW CREATE VIEW instead." ,
89
91
tableIdentifier .asSerializableString ()));
92
+ } else if (table .getTableKind () == CatalogBaseTable .TableKind .MATERIALIZED_TABLE ) {
93
+ return buildShowCreateMaterializedTableRow (
94
+ (ResolvedCatalogMaterializedTable ) table , tableIdentifier , isTemporary );
90
95
}
91
96
StringBuilder sb =
92
97
new StringBuilder ()
@@ -110,6 +115,37 @@ public static String buildShowCreateTableRow(
110
115
return sb .toString ();
111
116
}
112
117
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
+
113
149
/** Show create view statement only for views. */
114
150
public static String buildShowCreateViewRow (
115
151
ResolvedCatalogBaseTable <?> view ,
@@ -161,6 +197,15 @@ static String buildCreateFormattedPrefix(
161
197
System .lineSeparator ());
162
198
}
163
199
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
+
164
209
static Optional <String > extractFormattedPrimaryKey (
165
210
ResolvedCatalogBaseTable <?> table , String printIndent ) {
166
211
Optional <UniqueConstraint > primaryKey = table .getResolvedSchema ().getPrimaryKey ();
@@ -269,6 +314,31 @@ static Optional<String> extractFormattedPartitionedInfo(ResolvedCatalogTable cat
269
314
.collect (Collectors .joining (", " )));
270
315
}
271
316
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
+
272
342
static Optional <String > extractFormattedOptions (Map <String , String > conf , String printIndent ) {
273
343
if (Objects .isNull (conf ) || conf .isEmpty ()) {
274
344
return Optional .empty ();
0 commit comments