Skip to content

Commit d651f0b

Browse files
committed
[FLINK-38355] Revert deletion of SqlCreateMaterializedTable
1 parent d98845a commit d651f0b

File tree

2 files changed

+195
-121
lines changed

2 files changed

+195
-121
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.flink.sql.parser.ddl;
20+
21+
import org.apache.flink.sql.parser.ddl.constraint.SqlTableConstraint;
22+
23+
import org.apache.calcite.sql.SqlCharStringLiteral;
24+
import org.apache.calcite.sql.SqlCreate;
25+
import org.apache.calcite.sql.SqlIdentifier;
26+
import org.apache.calcite.sql.SqlIntervalLiteral;
27+
import org.apache.calcite.sql.SqlKind;
28+
import org.apache.calcite.sql.SqlLiteral;
29+
import org.apache.calcite.sql.SqlNode;
30+
import org.apache.calcite.sql.SqlNodeList;
31+
import org.apache.calcite.sql.SqlOperator;
32+
import org.apache.calcite.sql.SqlSpecialOperator;
33+
import org.apache.calcite.sql.parser.SqlParserPos;
34+
import org.apache.calcite.util.ImmutableNullableList;
35+
36+
import javax.annotation.Nullable;
37+
38+
import java.util.List;
39+
import java.util.Optional;
40+
41+
import static java.util.Objects.requireNonNull;
42+
43+
/** CREATE MATERIALIZED TABLE DDL sql call. */
44+
public class SqlCreateMaterializedTable extends SqlCreate {
45+
46+
public static final SqlSpecialOperator CREATE_OPERATOR =
47+
new SqlSpecialOperator("CREATE MATERIALIZED TABLE", SqlKind.CREATE_TABLE);
48+
49+
private final SqlIdentifier tableName;
50+
51+
private final @Nullable SqlTableConstraint tableConstraint;
52+
53+
private final @Nullable SqlCharStringLiteral comment;
54+
55+
private final @Nullable SqlDistribution distribution;
56+
57+
private final SqlNodeList partitionKeyList;
58+
59+
private final SqlNodeList propertyList;
60+
61+
private final @Nullable SqlIntervalLiteral freshness;
62+
63+
private final @Nullable SqlLiteral refreshMode;
64+
65+
private final SqlNode asQuery;
66+
67+
public SqlCreateMaterializedTable(
68+
SqlSpecialOperator operator,
69+
SqlParserPos pos,
70+
SqlIdentifier tableName,
71+
@Nullable SqlTableConstraint tableConstraint,
72+
@Nullable SqlCharStringLiteral comment,
73+
@Nullable SqlDistribution distribution,
74+
SqlNodeList partitionKeyList,
75+
SqlNodeList propertyList,
76+
@Nullable SqlIntervalLiteral freshness,
77+
@Nullable SqlLiteral refreshMode,
78+
SqlNode asQuery) {
79+
super(operator, pos, false, false);
80+
this.tableName = requireNonNull(tableName, "tableName should not be null");
81+
this.comment = comment;
82+
this.tableConstraint = tableConstraint;
83+
this.distribution = distribution;
84+
this.partitionKeyList =
85+
requireNonNull(partitionKeyList, "partitionKeyList should not be null");
86+
this.propertyList = requireNonNull(propertyList, "propertyList should not be null");
87+
this.freshness = freshness;
88+
this.refreshMode = refreshMode;
89+
this.asQuery = requireNonNull(asQuery, "asQuery should not be null");
90+
}
91+
92+
@Override
93+
public SqlOperator getOperator() {
94+
return CREATE_OPERATOR;
95+
}
96+
97+
@Override
98+
public List<SqlNode> getOperandList() {
99+
return ImmutableNullableList.of(
100+
tableName,
101+
comment,
102+
tableConstraint,
103+
partitionKeyList,
104+
propertyList,
105+
freshness,
106+
asQuery);
107+
}
108+
109+
public SqlIdentifier getTableName() {
110+
return tableName;
111+
}
112+
113+
public String[] fullTableName() {
114+
return tableName.names.toArray(new String[0]);
115+
}
116+
117+
public Optional<SqlCharStringLiteral> getComment() {
118+
return Optional.ofNullable(comment);
119+
}
120+
121+
public Optional<SqlTableConstraint> getTableConstraint() {
122+
return Optional.ofNullable(tableConstraint);
123+
}
124+
125+
public @Nullable SqlDistribution getDistribution() {
126+
return distribution;
127+
}
128+
129+
public SqlNodeList getPartitionKeyList() {
130+
return partitionKeyList;
131+
}
132+
133+
public SqlNodeList getPropertyList() {
134+
return propertyList;
135+
}
136+
137+
@Nullable
138+
public SqlIntervalLiteral getFreshness() {
139+
return freshness;
140+
}
141+
142+
@Nullable
143+
public SqlLiteral getRefreshMode() {
144+
return refreshMode;
145+
}
146+
147+
public SqlNode getAsQuery() {
148+
return asQuery;
149+
}
150+
}

flink-table/flink-sql-parser/src/main/java/org/apache/flink/sql/parser/ddl/SqlCreateOrAlterMaterializedTable.java

Lines changed: 45 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.apache.flink.sql.parser.ddl.constraint.SqlTableConstraint;
2323

2424
import org.apache.calcite.sql.SqlCharStringLiteral;
25-
import org.apache.calcite.sql.SqlCreate;
2625
import org.apache.calcite.sql.SqlIdentifier;
2726
import org.apache.calcite.sql.SqlIntervalLiteral;
2827
import org.apache.calcite.sql.SqlKind;
@@ -33,43 +32,17 @@
3332
import org.apache.calcite.sql.SqlSpecialOperator;
3433
import org.apache.calcite.sql.SqlWriter;
3534
import org.apache.calcite.sql.parser.SqlParserPos;
36-
import org.apache.calcite.util.ImmutableNullableList;
3735

3836
import javax.annotation.Nullable;
3937

4038
import 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

Comments
 (0)