1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.distsql.parser.core.kernel;
19
20 import org.antlr.v4.runtime.tree.ParseTree;
21 import org.apache.groovy.parser.antlr4.util.StringUtils;
22 import org.apache.shardingsphere.database.connector.core.metadata.database.enums.QuoteCharacter;
23 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementBaseVisitor;
24 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.AlgorithmDefinitionContext;
25 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.AlterComputeNodeContext;
26 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.AlterStorageUnitContext;
27 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.CheckPrivilegesContext;
28 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ConvertYamlConfigurationContext;
29 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.DatabaseNameContext;
30 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.DisableComputeNodeContext;
31 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.EnableComputeNodeContext;
32 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ExportDatabaseConfigurationContext;
33 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ExportMetaDataContext;
34 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ExportStorageNodesContext;
35 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.FromSegmentContext;
36 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.IgnoreBroadcastTablesContext;
37 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.IgnoreSingleAndBroadcastTablesContext;
38 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.IgnoreSingleTablesContext;
39 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ImportDatabaseConfigurationContext;
40 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ImportMetaDataContext;
41 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.InstanceIdContext;
42 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.LabelComputeNodeContext;
43 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.LockClusterContext;
44 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.PropertiesDefinitionContext;
45 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.PropertyContext;
46 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.RefreshDatabaseMetadataContext;
47 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.RefreshTableMetadataContext;
48 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.RegisterStorageUnitContext;
49 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.SetDistVariableContext;
50 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowComputeNodeInfoContext;
51 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowComputeNodeModeContext;
52 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowComputeNodesContext;
53 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowDistVariableContext;
54 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowDistVariablesContext;
55 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowKeyGenerateAlgorithmPluginsContext;
56 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowLoadBalanceAlgorithmPluginsContext;
57 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowLogicalTablesContext;
58 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowPluginImplementationsContext;
59 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowRulesUsedStorageUnitContext;
60 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowStorageUnitsContext;
61 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowTableMetadataContext;
62 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.StorageUnitDefinitionContext;
63 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.StorageUnitsDefinitionContext;
64 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.UnlabelComputeNodeContext;
65 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.UnlockClusterContext;
66 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.UnregisterStorageUnitContext;
67 import org.apache.shardingsphere.distsql.segment.AlgorithmSegment;
68 import org.apache.shardingsphere.distsql.segment.DataSourceSegment;
69 import org.apache.shardingsphere.distsql.segment.HostnameAndPortBasedDataSourceSegment;
70 import org.apache.shardingsphere.distsql.segment.URLBasedDataSourceSegment;
71 import org.apache.shardingsphere.distsql.statement.type.ral.queryable.convert.ConvertYamlConfigurationStatement;
72 import org.apache.shardingsphere.distsql.statement.type.ral.queryable.export.ExportDatabaseConfigurationStatement;
73 import org.apache.shardingsphere.distsql.statement.type.ral.queryable.export.ExportMetaDataStatement;
74 import org.apache.shardingsphere.distsql.statement.type.ral.queryable.export.ExportStorageNodesStatement;
75 import org.apache.shardingsphere.distsql.statement.type.ral.queryable.show.ShowComputeNodeInfoStatement;
76 import org.apache.shardingsphere.distsql.statement.type.ral.queryable.show.ShowComputeNodeModeStatement;
77 import org.apache.shardingsphere.distsql.statement.type.ral.queryable.show.ShowComputeNodesStatement;
78 import org.apache.shardingsphere.distsql.statement.type.ral.queryable.show.ShowDistVariableStatement;
79 import org.apache.shardingsphere.distsql.statement.type.ral.queryable.show.ShowDistVariablesStatement;
80 import org.apache.shardingsphere.distsql.statement.type.ral.queryable.show.ShowPluginsStatement;
81 import org.apache.shardingsphere.distsql.statement.type.ral.queryable.show.ShowTableMetaDataStatement;
82 import org.apache.shardingsphere.distsql.statement.type.ral.updatable.AlterComputeNodeStatement;
83 import org.apache.shardingsphere.distsql.statement.type.ral.updatable.ImportDatabaseConfigurationStatement;
84 import org.apache.shardingsphere.distsql.statement.type.ral.updatable.ImportMetaDataStatement;
85 import org.apache.shardingsphere.distsql.statement.type.ral.updatable.LabelComputeNodeStatement;
86 import org.apache.shardingsphere.distsql.statement.type.ral.updatable.LockClusterStatement;
87 import org.apache.shardingsphere.distsql.statement.type.ral.updatable.RefreshDatabaseMetaDataStatement;
88 import org.apache.shardingsphere.distsql.statement.type.ral.updatable.RefreshTableMetaDataStatement;
89 import org.apache.shardingsphere.distsql.statement.type.ral.updatable.SetComputeNodeStateStatement;
90 import org.apache.shardingsphere.distsql.statement.type.ral.updatable.SetDistVariableStatement;
91 import org.apache.shardingsphere.distsql.statement.type.ral.updatable.UnlabelComputeNodeStatement;
92 import org.apache.shardingsphere.distsql.statement.type.ral.updatable.UnlockClusterStatement;
93 import org.apache.shardingsphere.distsql.statement.type.rdl.resource.unit.type.AlterStorageUnitStatement;
94 import org.apache.shardingsphere.distsql.statement.type.rdl.resource.unit.type.RegisterStorageUnitStatement;
95 import org.apache.shardingsphere.distsql.statement.type.rdl.resource.unit.type.UnregisterStorageUnitStatement;
96 import org.apache.shardingsphere.distsql.statement.type.rql.resource.ShowLogicalTablesStatement;
97 import org.apache.shardingsphere.distsql.statement.type.rql.resource.ShowStorageUnitsStatement;
98 import org.apache.shardingsphere.distsql.statement.type.rql.rule.database.ShowRulesUsedStorageUnitStatement;
99 import org.apache.shardingsphere.sql.parser.api.ASTNode;
100 import org.apache.shardingsphere.sql.parser.api.visitor.SQLVisitor;
101 import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.FromDatabaseSegment;
102 import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.DatabaseSegment;
103 import org.apache.shardingsphere.sql.parser.statement.core.util.IdentifierValueUtils;
104 import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
105 import org.apache.shardingsphere.sql.parser.statement.core.value.literal.impl.StringLiteralValue;
106
107 import java.util.Collection;
108 import java.util.Collections;
109 import java.util.Objects;
110 import java.util.Properties;
111 import java.util.stream.Collectors;
112
113
114
115
116 public final class KernelDistSQLStatementVisitor extends KernelDistSQLStatementBaseVisitor<ASTNode> implements SQLVisitor<ASTNode> {
117
118 @Override
119 public ASTNode visitRegisterStorageUnit(final RegisterStorageUnitContext ctx) {
120 return new RegisterStorageUnitStatement(null != ctx.ifNotExists(), getStorageUnits(ctx.storageUnitsDefinition()), getExpectedPrivileges(ctx.checkPrivileges()));
121 }
122
123 @Override
124 public ASTNode visitAlterStorageUnit(final AlterStorageUnitContext ctx) {
125 return new AlterStorageUnitStatement(getStorageUnits(ctx.storageUnitsDefinition()), getExpectedPrivileges(ctx.checkPrivileges()));
126 }
127
128 private Collection<DataSourceSegment> getStorageUnits(final StorageUnitsDefinitionContext ctx) {
129 return ctx.storageUnitDefinition().stream().map(each -> (DataSourceSegment) visit(each)).collect(Collectors.toList());
130 }
131
132 private Collection<String> getExpectedPrivileges(final CheckPrivilegesContext ctx) {
133 return null == ctx ? Collections.emptySet() : ctx.privilegeType().stream().map(IdentifierValueUtils::getValue).collect(Collectors.toSet());
134 }
135
136 @Override
137 public ASTNode visitShowTableMetadata(final ShowTableMetadataContext ctx) {
138 Collection<String> tableNames = ctx.tableName().stream().map(IdentifierValueUtils::getValue).collect(Collectors.toSet());
139 return new ShowTableMetaDataStatement(tableNames, null == ctx.databaseName()
140 ? null
141 : new FromDatabaseSegment(ctx.FROM().getSymbol().getStartIndex(), (DatabaseSegment) visit(ctx.databaseName())));
142 }
143
144 @Override
145 public ASTNode visitStorageUnitDefinition(final StorageUnitDefinitionContext ctx) {
146 String user = IdentifierValueUtils.getValue(ctx.user());
147 String password = null == ctx.password() ? "" : new StringLiteralValue(StringUtils.replaceStandardEscapes(ctx.password().getText())).getValue();
148 Properties props = getProperties(ctx.propertiesDefinition());
149 return null == ctx.urlSource()
150 ? new HostnameAndPortBasedDataSourceSegment(IdentifierValueUtils.getValue(ctx.storageUnitName()),
151 IdentifierValueUtils.getValue(ctx.simpleSource().hostname()), ctx.simpleSource().port().getText(), IdentifierValueUtils.getValue(ctx.simpleSource().dbName()), user, password,
152 props)
153 : new URLBasedDataSourceSegment(IdentifierValueUtils.getValue(ctx.storageUnitName()), IdentifierValueUtils.getValue(ctx.urlSource().url()), user, password, props);
154 }
155
156 @Override
157 public ASTNode visitShowComputeNodes(final ShowComputeNodesContext ctx) {
158 return new ShowComputeNodesStatement();
159 }
160
161 @Override
162 public ASTNode visitShowComputeNodeInfo(final ShowComputeNodeInfoContext ctx) {
163 return new ShowComputeNodeInfoStatement();
164 }
165
166 @Override
167 public ASTNode visitShowComputeNodeMode(final ShowComputeNodeModeContext ctx) {
168 return new ShowComputeNodeModeStatement();
169 }
170
171 @Override
172 public ASTNode visitEnableComputeNode(final EnableComputeNodeContext ctx) {
173 return buildSetComputeNodeStateStatement(ctx.ENABLE().getText().toUpperCase(), ctx.instanceId());
174 }
175
176 @Override
177 public ASTNode visitDisableComputeNode(final DisableComputeNodeContext ctx) {
178 return buildSetComputeNodeStateStatement(ctx.DISABLE().getText().toUpperCase(), ctx.instanceId());
179 }
180
181 @Override
182 public ASTNode visitLabelComputeNode(final LabelComputeNodeContext ctx) {
183 Collection<String> labels = ctx.label().stream().map(IdentifierValueUtils::getValue).collect(Collectors.toList());
184 return new LabelComputeNodeStatement(null != ctx.RELABEL(), IdentifierValueUtils.getValue(ctx.instanceId()), labels);
185 }
186
187 @Override
188 public ASTNode visitUnlabelComputeNode(final UnlabelComputeNodeContext ctx) {
189 Collection<String> labels = ctx.label().stream().map(IdentifierValueUtils::getValue).collect(Collectors.toList());
190 return new UnlabelComputeNodeStatement(IdentifierValueUtils.getValue(ctx.instanceId()), labels);
191 }
192
193 private SetComputeNodeStateStatement buildSetComputeNodeStateStatement(final String status, final InstanceIdContext instanceIdContext) {
194 return new SetComputeNodeStateStatement(status, IdentifierValueUtils.getValue(instanceIdContext));
195 }
196
197 @Override
198 public ASTNode visitAlterComputeNode(final AlterComputeNodeContext ctx) {
199 return new AlterComputeNodeStatement(IdentifierValueUtils.getValue(ctx.instanceId()), IdentifierValueUtils.getValue(ctx.variableName()), IdentifierValueUtils.getValue(ctx.variableValues()));
200 }
201
202 private Properties getProperties(final PropertiesDefinitionContext ctx) {
203 Properties result = new Properties();
204 if (null == ctx || null == ctx.properties()) {
205 return result;
206 }
207 for (PropertyContext each : ctx.properties().property()) {
208 result.setProperty(QuoteCharacter.unwrapAndTrimText(each.key.getText()), QuoteCharacter.unwrapAndTrimText(each.value.getText()));
209 }
210 return result;
211 }
212
213 @Override
214 public ASTNode visitUnregisterStorageUnit(final UnregisterStorageUnitContext ctx) {
215 boolean ignoreSingleTables = ctx.ignoreTables() instanceof IgnoreSingleAndBroadcastTablesContext || ctx.ignoreTables() instanceof IgnoreSingleTablesContext;
216 boolean ignoreBroadcastTables = ctx.ignoreTables() instanceof IgnoreSingleAndBroadcastTablesContext || ctx.ignoreTables() instanceof IgnoreBroadcastTablesContext;
217 return new UnregisterStorageUnitStatement(null != ctx.ifExists(),
218 ctx.storageUnitName().stream().map(ParseTree::getText).map(each -> new IdentifierValue(each).getValue()).collect(Collectors.toList()),
219 ignoreSingleTables, ignoreBroadcastTables);
220 }
221
222 @Override
223 public ASTNode visitShowStorageUnits(final ShowStorageUnitsContext ctx) {
224 FromDatabaseSegment fromDatabase = null == ctx.databaseName() ? null : new FromDatabaseSegment(ctx.FROM().getSymbol().getStartIndex(), (DatabaseSegment) visit(ctx.databaseName()));
225 String likePattern = null == ctx.showLike() ? null : IdentifierValueUtils.getValue(ctx.showLike().likePattern());
226 return new ShowStorageUnitsStatement(fromDatabase, likePattern);
227 }
228
229 @Override
230 public ASTNode visitDatabaseName(final DatabaseNameContext ctx) {
231 return new DatabaseSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), new IdentifierValue(ctx.getText()));
232 }
233
234 @Override
235 public ASTNode visitSetDistVariable(final SetDistVariableContext ctx) {
236 return new SetDistVariableStatement(IdentifierValueUtils.getValue(ctx.variableName()), IdentifierValueUtils.getValue(ctx.variableValue()));
237 }
238
239 @Override
240 public ASTNode visitShowLogicalTables(final ShowLogicalTablesContext ctx) {
241 FromDatabaseSegment fromDatabase = null == ctx.databaseName() ? null : new FromDatabaseSegment(ctx.FROM().getSymbol().getStartIndex(), (DatabaseSegment) visit(ctx.databaseName()));
242 return new ShowLogicalTablesStatement(null != ctx.FULL(), fromDatabase, null == ctx.showLike() ? null : IdentifierValueUtils.getValue(ctx.showLike().likePattern()));
243 }
244
245 @Override
246 public ASTNode visitShowDistVariable(final ShowDistVariableContext ctx) {
247 return new ShowDistVariableStatement(Objects.requireNonNull(IdentifierValueUtils.getValue(ctx.variableName())).toUpperCase());
248 }
249
250 @Override
251 public ASTNode visitShowDistVariables(final ShowDistVariablesContext ctx) {
252 return new ShowDistVariablesStatement(null != ctx.TEMP(), null == ctx.showLike() ? null : IdentifierValueUtils.getValue(ctx.showLike().likePattern()));
253 }
254
255 @Override
256 public ASTNode visitRefreshDatabaseMetadata(final RefreshDatabaseMetadataContext ctx) {
257 return new RefreshDatabaseMetaDataStatement(null == ctx.databaseName() ? null : IdentifierValueUtils.getValue(ctx.databaseName()), null != ctx.FORCE());
258 }
259
260 @Override
261 public ASTNode visitRefreshTableMetadata(final RefreshTableMetadataContext ctx) {
262 if (null == ctx.refreshScope()) {
263 return new RefreshTableMetaDataStatement();
264 }
265 String storageUnitName = null;
266 String schemaName = null;
267 String tableName = IdentifierValueUtils.getValue(ctx.refreshScope().tableName());
268 if (null != ctx.refreshScope().fromSegment()) {
269 FromSegmentContext fromSegment = ctx.refreshScope().fromSegment();
270 storageUnitName = IdentifierValueUtils.getValue(fromSegment.storageUnitName());
271 schemaName = null == fromSegment.schemaName() ? null : IdentifierValueUtils.getValue(fromSegment.schemaName());
272 }
273 return new RefreshTableMetaDataStatement(tableName, storageUnitName, schemaName);
274 }
275
276 @Override
277 public ASTNode visitExportDatabaseConfiguration(final ExportDatabaseConfigurationContext ctx) {
278 return new ExportDatabaseConfigurationStatement(IdentifierValueUtils.getValue(ctx.filePath()),
279 null == ctx.databaseName() ? null : new FromDatabaseSegment(ctx.FROM().getSymbol().getStartIndex(), (DatabaseSegment) visit(ctx.databaseName())));
280 }
281
282 @Override
283 public ASTNode visitExportMetaData(final ExportMetaDataContext ctx) {
284 return new ExportMetaDataStatement(null == ctx.filePath() ? null : IdentifierValueUtils.getValue(ctx.filePath()));
285 }
286
287 @Override
288 public ASTNode visitExportStorageNodes(final ExportStorageNodesContext ctx) {
289 return new ExportStorageNodesStatement(
290 null == ctx.databaseName() ? null : IdentifierValueUtils.getValue(ctx.databaseName()), null == ctx.filePath() ? null : IdentifierValueUtils.getValue(ctx.filePath()));
291 }
292
293 @Override
294 public ASTNode visitConvertYamlConfiguration(final ConvertYamlConfigurationContext ctx) {
295 return new ConvertYamlConfigurationStatement(IdentifierValueUtils.getValue(ctx.filePath()));
296 }
297
298 @Override
299 public ASTNode visitShowRulesUsedStorageUnit(final ShowRulesUsedStorageUnitContext ctx) {
300 return new ShowRulesUsedStorageUnitStatement(IdentifierValueUtils.getValue(ctx.storageUnitName()),
301 null == ctx.databaseName() ? null : new FromDatabaseSegment(ctx.FROM().getSymbol().getStartIndex(), (DatabaseSegment) visit(ctx.databaseName())));
302 }
303
304 @Override
305 public ASTNode visitImportDatabaseConfiguration(final ImportDatabaseConfigurationContext ctx) {
306 return new ImportDatabaseConfigurationStatement(IdentifierValueUtils.getValue(ctx.filePath()));
307 }
308
309 @Override
310 public ASTNode visitImportMetaData(final ImportMetaDataContext ctx) {
311 return new ImportMetaDataStatement(null == ctx.metaDataValue() ? null : QuoteCharacter.unwrapText(ctx.metaDataValue().getText()), IdentifierValueUtils.getValue(ctx.filePath()));
312 }
313
314 @Override
315 public ASTNode visitAlgorithmDefinition(final AlgorithmDefinitionContext ctx) {
316 return new AlgorithmSegment(IdentifierValueUtils.getValue(ctx.algorithmTypeName()), buildProperties(ctx.propertiesDefinition()));
317 }
318
319 private Properties buildProperties(final PropertiesDefinitionContext ctx) {
320 Properties result = new Properties();
321 if (null == ctx) {
322 return result;
323 }
324 for (PropertyContext each : ctx.properties().property()) {
325 result.setProperty(QuoteCharacter.unwrapAndTrimText(each.key.getText()), QuoteCharacter.unwrapAndTrimText(each.value.getText()));
326 }
327 return result;
328 }
329
330 @Override
331 public ASTNode visitLockCluster(final LockClusterContext ctx) {
332 return new LockClusterStatement((AlgorithmSegment) visitAlgorithmDefinition(ctx.lockStrategy().algorithmDefinition()), Long.parseLong(IdentifierValueUtils.getValue(ctx.INT_())));
333 }
334
335 @Override
336 public ASTNode visitUnlockCluster(final UnlockClusterContext ctx) {
337 return new UnlockClusterStatement(Long.parseLong(IdentifierValueUtils.getValue(ctx.INT_())));
338 }
339
340 @Override
341 public ASTNode visitShowPluginImplementations(final ShowPluginImplementationsContext ctx) {
342 return new ShowPluginsStatement("COMMON", IdentifierValueUtils.getValue(ctx.pluginClass()));
343 }
344
345 @Override
346 public ASTNode visitShowKeyGenerateAlgorithmPlugins(final ShowKeyGenerateAlgorithmPluginsContext ctx) {
347 return new ShowPluginsStatement("KEY_GENERATE_ALGORITHM");
348 }
349
350 @Override
351 public ASTNode visitShowLoadBalanceAlgorithmPlugins(final ShowLoadBalanceAlgorithmPluginsContext ctx) {
352 return new ShowPluginsStatement("LOAD_BALANCE_ALGORITHM");
353 }
354 }