-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
AlterTableStatementAnalyzer.java
215 lines (197 loc) · 11.1 KB
/
AlterTableStatementAnalyzer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// This file is licensed under the Elastic License 2.0. Copyright 2021-present, StarRocks Limited.
package com.starrocks.sql.analyzer;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.starrocks.alter.AlterOpType;
import com.starrocks.analysis.AlterClause;
import com.starrocks.analysis.AlterTableStmt;
import com.starrocks.analysis.CreateIndexClause;
import com.starrocks.analysis.IndexDef;
import com.starrocks.analysis.ModifyPartitionClause;
import com.starrocks.analysis.ModifyTablePropertiesClause;
import com.starrocks.analysis.TableName;
import com.starrocks.analysis.TableRenameClause;
import com.starrocks.catalog.CatalogUtils;
import com.starrocks.catalog.DataProperty;
import com.starrocks.catalog.Index;
import com.starrocks.catalog.MaterializedView;
import com.starrocks.catalog.Table;
import com.starrocks.catalog.TableProperty;
import com.starrocks.common.AnalysisException;
import com.starrocks.common.ErrorCode;
import com.starrocks.common.ErrorReport;
import com.starrocks.common.FeConstants;
import com.starrocks.common.FeNameFormat;
import com.starrocks.common.util.DynamicPartitionUtil;
import com.starrocks.common.util.PropertyAnalyzer;
import com.starrocks.qe.ConnectContext;
import com.starrocks.sql.ast.AstVisitor;
import com.starrocks.sql.common.MetaUtils;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import java.util.List;
import java.util.Map;
public class AlterTableStatementAnalyzer {
public static void analyze(AlterTableStmt statement, ConnectContext context) {
TableName tbl = statement.getTbl();
MetaUtils.normalizationTableName(context, tbl);
Table table = MetaUtils.getTable(context, tbl);
if (table instanceof MaterializedView) {
throw new SemanticException(
"The '%s' cannot be alter by 'ALTER TABLE', because '%s' is a materialized view," +
"you can use 'ALTER MATERIALIZED VIEW' to alter it.",
tbl.getTbl(), tbl.getTbl());
}
try {
CatalogUtils.checkIsLakeTable(tbl.getDb(), tbl.getTbl());
} catch (AnalysisException e) {
ErrorReport.reportSemanticException(ErrorCode.ERR_NO_TABLES_USED);
}
List<AlterClause> alterClauseList = statement.getOps();
if (alterClauseList == null || alterClauseList.isEmpty()) {
ErrorReport.reportSemanticException(ErrorCode.ERR_NO_ALTER_OPERATION);
}
AlterTableClauseAnalyzerVisitor alterTableClauseAnalyzerVisitor = new AlterTableClauseAnalyzerVisitor();
for (AlterClause alterClause : alterClauseList) {
alterTableClauseAnalyzerVisitor.analyze(alterClause, context);
}
}
static class AlterTableClauseAnalyzerVisitor extends AstVisitor<Void, ConnectContext> {
public void analyze(AlterClause statement, ConnectContext session) {
visit(statement, session);
}
@Override
public Void visitCreateIndexClause(CreateIndexClause clause, ConnectContext context) {
IndexDef indexDef = clause.getIndexDef();
indexDef.analyze();
clause.setIndex(new Index(indexDef.getIndexName(), indexDef.getColumns(),
indexDef.getIndexType(), indexDef.getComment()));
return null;
}
@Override
public Void visitTableRenameClause(TableRenameClause clause, ConnectContext context) {
String newTableName = clause.getNewTableName();
if (Strings.isNullOrEmpty(newTableName)) {
throw new SemanticException("New Table name is not set");
}
try {
FeNameFormat.checkTableName(newTableName);
} catch (AnalysisException e) {
ErrorReport.reportSemanticException(ErrorCode.ERR_WRONG_TABLE_NAME, newTableName);
}
return null;
}
@Override
public Void visitModifyTablePropertiesClause(ModifyTablePropertiesClause clause, ConnectContext context) {
@CheckForNull
Map<String, String> properties = clause.getProperties();
if (properties.isEmpty()) {
ErrorReport.reportSemanticException(ErrorCode.ERR_COMMON_ERROR, "Properties is not set");
}
if (properties.size() != 1
&& !TableProperty.isSamePrefixProperties(properties, TableProperty.DYNAMIC_PARTITION_PROPERTY_PREFIX)) {
ErrorReport.reportSemanticException(ErrorCode.ERR_COMMON_ERROR, "Can only set one table property at a time");
}
if (properties.containsKey(PropertyAnalyzer.PROPERTIES_COLOCATE_WITH)) {
clause.setNeedTableStable(false);
} else if (properties.containsKey(PropertyAnalyzer.PROPERTIES_STORAGE_TYPE)) {
if (!properties.get(PropertyAnalyzer.PROPERTIES_STORAGE_TYPE).equalsIgnoreCase("column")) {
ErrorReport.reportSemanticException(ErrorCode.ERR_COMMON_ERROR, "Can only change storage type to COLUMN");
}
} else if (properties.containsKey(PropertyAnalyzer.PROPERTIES_DISTRIBUTION_TYPE)) {
if (!properties.get(PropertyAnalyzer.PROPERTIES_DISTRIBUTION_TYPE).equalsIgnoreCase("hash")) {
ErrorReport.reportSemanticException(ErrorCode.ERR_COMMON_ERROR, "Can only change distribution type to HASH");
}
clause.setNeedTableStable(false);
} else if (properties.containsKey(PropertyAnalyzer.PROPERTIES_SEND_CLEAR_ALTER_TASK)) {
if (!properties.get(PropertyAnalyzer.PROPERTIES_SEND_CLEAR_ALTER_TASK).equalsIgnoreCase("true")) {
ErrorReport.reportSemanticException(ErrorCode.ERR_COMMON_ERROR,
"Property " + PropertyAnalyzer.PROPERTIES_SEND_CLEAR_ALTER_TASK + " should be set to true");
}
clause.setNeedTableStable(false);
} else if (properties.containsKey(PropertyAnalyzer.PROPERTIES_BF_COLUMNS)
|| properties.containsKey(PropertyAnalyzer.PROPERTIES_BF_FPP)) {
// do nothing, these 2 properties will be analyzed when creating alter job
} else if (properties.containsKey(PropertyAnalyzer.PROPERTIES_STORAGE_FORMAT)) {
if (!properties.get(PropertyAnalyzer.PROPERTIES_STORAGE_FORMAT).equalsIgnoreCase("v2")) {
ErrorReport.reportSemanticException(ErrorCode.ERR_COMMON_ERROR,
"Property " + PropertyAnalyzer.PROPERTIES_STORAGE_FORMAT + " should be v2");
}
} else if (DynamicPartitionUtil.checkDynamicPartitionPropertiesExist(properties)) {
// do nothing, dynamic properties will be analyzed in SchemaChangeHandler.process
} else if (properties.containsKey(PropertyAnalyzer.PROPERTIES_REPLICATION_NUM)) {
try {
PropertyAnalyzer.analyzeReplicationNum(properties, false);
} catch (AnalysisException e) {
ErrorReport.reportSemanticException(ErrorCode.ERR_COMMON_ERROR, e.getMessage());
}
} else if (properties.containsKey("default." + PropertyAnalyzer.PROPERTIES_REPLICATION_NUM)) {
short defaultReplicationNum = 0;
try {
defaultReplicationNum = PropertyAnalyzer.analyzeReplicationNum(properties, true);
} catch (AnalysisException e) {
ErrorReport.reportSemanticException(ErrorCode.ERR_COMMON_ERROR, e.getMessage());
}
properties.put(PropertyAnalyzer.PROPERTIES_REPLICATION_NUM, Short.toString(defaultReplicationNum));
} else if (properties.containsKey(PropertyAnalyzer.PROPERTIES_INMEMORY)) {
clause.setNeedTableStable(false);
clause.setOpType(AlterOpType.MODIFY_TABLE_PROPERTY_SYNC);
} else if (properties.containsKey(PropertyAnalyzer.PROPERTIES_ENABLE_PERSISTENT_INDEX)) {
if (!properties.get(PropertyAnalyzer.PROPERTIES_ENABLE_PERSISTENT_INDEX).equalsIgnoreCase("true") &&
!properties.get(PropertyAnalyzer.PROPERTIES_ENABLE_PERSISTENT_INDEX).equalsIgnoreCase("false")) {
ErrorReport.reportSemanticException(ErrorCode.ERR_COMMON_ERROR,
"Property " + PropertyAnalyzer.PROPERTIES_ENABLE_PERSISTENT_INDEX +
" must be bool type(false/true)");
}
clause.setNeedTableStable(false);
clause.setOpType(AlterOpType.MODIFY_TABLE_PROPERTY_SYNC);
} else if (properties.containsKey(PropertyAnalyzer.PROPERTIES_TABLET_TYPE)) {
ErrorReport.reportSemanticException(ErrorCode.ERR_COMMON_ERROR, "Alter tablet type not supported");
} else {
ErrorReport.reportSemanticException(ErrorCode.ERR_COMMON_ERROR, "Unknown table property: " + properties.keySet());
}
return null;
}
@Override
public Void visitModifyPartitionClause(ModifyPartitionClause clause, ConnectContext context) {
final List<String> partitionNames = clause.getPartitionNames();
final boolean needExpand = clause.isNeedExpand();
final Map<String, String> properties = clause.getProperties();
if (partitionNames == null || (!needExpand && partitionNames.isEmpty())) {
throw new SemanticException("Partition names is not set or empty");
}
if (partitionNames.stream().anyMatch(Strings::isNullOrEmpty)) {
throw new SemanticException("there are empty partition name");
}
if (properties == null || properties.isEmpty()) {
throw new SemanticException("Properties is not set");
}
// check properties here
try {
checkProperties(Maps.newHashMap(properties));
} catch (AnalysisException e) {
throw new SemanticException("check properties error: %s", e.getMessage());
}
return null;
}
// Check the following properties' legality before modifying partition.
// 1. replication_num
// 2. storage_medium && storage_cooldown_time
// 3. in_memory
// 4. tablet type
private void checkProperties(Map<String, String> properties) throws AnalysisException {
// 1. data property
DataProperty newDataProperty = null;
newDataProperty = PropertyAnalyzer.analyzeDataProperty(properties, DataProperty.DEFAULT_DATA_PROPERTY);
Preconditions.checkNotNull(newDataProperty);
// 2. replication num
short newReplicationNum;
newReplicationNum = PropertyAnalyzer.analyzeReplicationNum(properties, FeConstants.default_replication_num);
Preconditions.checkState(newReplicationNum != (short) -1);
// 3. in memory
PropertyAnalyzer.analyzeBooleanProp(properties, PropertyAnalyzer.PROPERTIES_INMEMORY, false);
// 4. tablet type
PropertyAnalyzer.analyzeTabletType(properties);
}
}
}