2222import org .apache .flink .sql .parser .ddl .constraint .SqlTableConstraint ;
2323
2424import org .apache .calcite .sql .SqlCharStringLiteral ;
25- import org .apache .calcite .sql .SqlCreate ;
2625import org .apache .calcite .sql .SqlIdentifier ;
2726import org .apache .calcite .sql .SqlIntervalLiteral ;
2827import org .apache .calcite .sql .SqlKind ;
3332import org .apache .calcite .sql .SqlSpecialOperator ;
3433import org .apache .calcite .sql .SqlWriter ;
3534import org .apache .calcite .sql .parser .SqlParserPos ;
36- import org .apache .calcite .util .ImmutableNullableList ;
3735
3836import javax .annotation .Nullable ;
3937
4038import java .util .Collections ;
41- import java .util .List ;
42- import java .util .Optional ;
43-
44- import static java .util .Objects .requireNonNull ;
4539
4640/** CREATE [OR ALTER] MATERIALIZED TABLE DDL sql call. */
47- public class SqlCreateOrAlterMaterializedTable extends SqlCreate {
48-
49- public static final SqlSpecialOperator CREATE_OPERATOR =
50- new SqlSpecialOperator ("CREATE MATERIALIZED TABLE" , SqlKind .CREATE_TABLE );
41+ public class SqlCreateOrAlterMaterializedTable extends SqlCreateMaterializedTable {
5142
5243 public static final SqlSpecialOperator CREATE_OR_ALTER_OPERATOR =
5344 new SqlSpecialOperator ("CREATE OR ALTER MATERIALIZED TABLE" , SqlKind .OTHER_DDL );
5445
55- private final SqlIdentifier tableName ;
56-
57- private final @ Nullable SqlTableConstraint tableConstraint ;
58-
59- private final @ Nullable SqlCharStringLiteral comment ;
60-
61- private final @ Nullable SqlDistribution distribution ;
62-
63- private final SqlNodeList partitionKeyList ;
64-
65- private final SqlNodeList propertyList ;
66-
67- private final @ Nullable SqlIntervalLiteral freshness ;
68-
69- private final @ Nullable SqlLiteral refreshMode ;
70-
71- private final SqlNode asQuery ;
72-
7346 private final boolean isOrAlter ;
7447
7548 public SqlCreateOrAlterMaterializedTable (
@@ -84,77 +57,24 @@ public SqlCreateOrAlterMaterializedTable(
8457 @ Nullable SqlLiteral refreshMode ,
8558 SqlNode asQuery ,
8659 boolean isOrAlter ) {
87- super (isOrAlter ? CREATE_OR_ALTER_OPERATOR : CREATE_OPERATOR , pos , false , false );
88- this .tableName = requireNonNull (tableName , "tableName should not be null" );
89- this .comment = comment ;
90- this .tableConstraint = tableConstraint ;
91- this .distribution = distribution ;
92- this .partitionKeyList =
93- requireNonNull (partitionKeyList , "partitionKeyList should not be null" );
94- this .propertyList = requireNonNull (propertyList , "propertyList should not be null" );
95- this .freshness = freshness ;
96- this .refreshMode = refreshMode ;
97- this .asQuery = requireNonNull (asQuery , "asQuery should not be null" );
98- this .isOrAlter = isOrAlter ;
99- }
100-
101- @ Override
102- public SqlOperator getOperator () {
103- return isOrAlter ? CREATE_OR_ALTER_OPERATOR : CREATE_OPERATOR ;
104- }
105-
106- @ Override
107- public List <SqlNode > getOperandList () {
108- return ImmutableNullableList .of (
60+ super (
61+ isOrAlter ? CREATE_OR_ALTER_OPERATOR : CREATE_OPERATOR ,
62+ pos ,
10963 tableName ,
110- comment ,
11164 tableConstraint ,
65+ comment ,
66+ distribution ,
11267 partitionKeyList ,
11368 propertyList ,
11469 freshness ,
70+ refreshMode ,
11571 asQuery );
72+ this .isOrAlter = isOrAlter ;
11673 }
11774
118- public SqlIdentifier getTableName () {
119- return tableName ;
120- }
121-
122- public String [] fullTableName () {
123- return tableName .names .toArray (new String [0 ]);
124- }
125-
126- public Optional <SqlCharStringLiteral > getComment () {
127- return Optional .ofNullable (comment );
128- }
129-
130- public Optional <SqlTableConstraint > getTableConstraint () {
131- return Optional .ofNullable (tableConstraint );
132- }
133-
134- public @ Nullable SqlDistribution getDistribution () {
135- return distribution ;
136- }
137-
138- public SqlNodeList getPartitionKeyList () {
139- return partitionKeyList ;
140- }
141-
142- public SqlNodeList getPropertyList () {
143- return propertyList ;
144- }
145-
146- @ Nullable
147- public SqlIntervalLiteral getFreshness () {
148- return freshness ;
149- }
150-
151- @ Nullable
152- public SqlLiteral getRefreshMode () {
153- return refreshMode ;
154- }
155-
156- public SqlNode getAsQuery () {
157- return asQuery ;
75+ @ Override
76+ public SqlOperator getOperator () {
77+ return isOrAlter ? CREATE_OR_ALTER_OPERATOR : CREATE_OPERATOR ;
15878 }
15979
16080 public boolean isOrAlter () {
@@ -168,67 +88,71 @@ public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
16888 writer .keyword ("OR ALTER" );
16989 }
17090 writer .keyword ("MATERIALIZED TABLE" );
171- tableName .unparse (writer , leftPrec , rightPrec );
172-
173- if (tableConstraint != null ) {
174- writer .newlineAndIndent ();
175- SqlUnparseUtils .unparseTableSchema (
176- writer ,
177- leftPrec ,
178- rightPrec ,
179- SqlNodeList .EMPTY ,
180- Collections .singletonList (tableConstraint ),
181- null );
182- }
183-
184- if (comment != null ) {
185- writer .newlineAndIndent ();
186- writer .keyword ("COMMENT" );
187- comment .unparse (writer , leftPrec , rightPrec );
188- }
189-
190- if (distribution != null ) {
91+ getTableName ().unparse (writer , leftPrec , rightPrec );
92+
93+ getTableConstraint ()
94+ .ifPresent (
95+ tableConstraint -> {
96+ writer .newlineAndIndent ();
97+ SqlUnparseUtils .unparseTableSchema (
98+ writer ,
99+ leftPrec ,
100+ rightPrec ,
101+ SqlNodeList .EMPTY ,
102+ Collections .singletonList (tableConstraint ),
103+ null );
104+ });
105+
106+ getComment ()
107+ .ifPresent (
108+ comment -> {
109+ writer .newlineAndIndent ();
110+ writer .keyword ("COMMENT" );
111+ comment .unparse (writer , leftPrec , rightPrec );
112+ });
113+
114+ if (getDistribution () != null ) {
191115 writer .newlineAndIndent ();
192- distribution .unparse (writer , leftPrec , rightPrec );
116+ getDistribution () .unparse (writer , leftPrec , rightPrec );
193117 }
194118
195- if (!partitionKeyList .isEmpty ()) {
119+ if (!getPartitionKeyList () .isEmpty ()) {
196120 writer .newlineAndIndent ();
197121 writer .keyword ("PARTITIONED BY" );
198122 SqlWriter .Frame partitionedByFrame = writer .startList ("(" , ")" );
199- partitionKeyList .unparse (writer , leftPrec , rightPrec );
123+ getPartitionKeyList () .unparse (writer , leftPrec , rightPrec );
200124 writer .endList (partitionedByFrame );
201125 }
202126
203- if (!propertyList .isEmpty ()) {
127+ if (!getPropertyList () .isEmpty ()) {
204128 writer .newlineAndIndent ();
205129 writer .keyword ("WITH" );
206130 SqlWriter .Frame withFrame = writer .startList ("(" , ")" );
207- for (SqlNode property : propertyList ) {
131+ for (SqlNode property : getPropertyList () ) {
208132 SqlUnparseUtils .printIndent (writer );
209133 property .unparse (writer , leftPrec , rightPrec );
210134 }
211135 writer .newlineAndIndent ();
212136 writer .endList (withFrame );
213137 }
214138
215- if (freshness != null ) {
139+ if (getFreshness () != null ) {
216140 writer .newlineAndIndent ();
217141 writer .keyword ("FRESHNESS" );
218142 writer .keyword ("=" );
219- freshness .unparse (writer , leftPrec , rightPrec );
143+ getFreshness () .unparse (writer , leftPrec , rightPrec );
220144 }
221145
222- if (refreshMode != null ) {
146+ if (getRefreshMode () != null ) {
223147 writer .newlineAndIndent ();
224148 writer .keyword ("REFRESH_MODE" );
225149 writer .keyword ("=" );
226- refreshMode .unparse (writer , leftPrec , rightPrec );
150+ getRefreshMode () .unparse (writer , leftPrec , rightPrec );
227151 }
228152
229153 writer .newlineAndIndent ();
230154 writer .keyword ("AS" );
231155 writer .newlineAndIndent ();
232- asQuery .unparse (writer , leftPrec , rightPrec );
156+ getAsQuery () .unparse (writer , leftPrec , rightPrec );
233157 }
234158}
0 commit comments