Skip to content

Commit ffa1b59

Browse files
authored
Support for the ALTER RESOURCE statement (#37392)
* Support for the ALTER RESOURCE statement * Support for the ALTER RESOURCE statement * Support for the ALTER RESOURCE statement
1 parent 49a71f8 commit ffa1b59

16 files changed

Lines changed: 416 additions & 0 deletions

File tree

parser/sql/engine/core/src/main/java/org/apache/shardingsphere/sql/parser/engine/core/database/visitor/SQLVisitorRule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,8 @@ public enum SQLVisitorRule {
473473

474474
ALTER_RESOURCE_GROUP("AlterResourceGroup", SQLStatementType.DAL),
475475

476+
ALTER_RESOURCE("AlterResource", SQLStatementType.DAL),
477+
476478
DELIMITER("Delimiter", SQLStatementType.DAL),
477479

478480
CALL("Call", SQLStatementType.DML),

parser/sql/engine/dialect/doris/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,10 @@
4141
<artifactId>shardingsphere-parser-sql-statement-mysql</artifactId>
4242
<version>${project.version}</version>
4343
</dependency>
44+
<dependency>
45+
<groupId>org.apache.shardingsphere</groupId>
46+
<artifactId>shardingsphere-parser-sql-statement-doris</artifactId>
47+
<version>${project.version}</version>
48+
</dependency>
4449
</dependencies>
4550
</project>

parser/sql/engine/dialect/doris/src/main/antlr4/imports/doris/DALStatement.g4

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,30 @@ alterResourceGroup
329329
(ENABLE | DISABLE FORCE?)?
330330
;
331331

332+
alterResource
333+
: ALTER RESOURCE resourceName PROPERTIES LP_ propertyAssignments RP_
334+
;
335+
336+
resourceName
337+
: identifier | string_
338+
;
339+
340+
propertyAssignments
341+
: propertyAssignment (COMMA_ propertyAssignment)*
342+
;
343+
344+
propertyAssignment
345+
: propertyKey EQ_ propertyValue
346+
;
347+
348+
propertyKey
349+
: identifier | string_
350+
;
351+
352+
propertyValue
353+
: literals | identifier
354+
;
355+
332356
vcpuSpec
333357
: NUMBER_ | NUMBER_ MINUS_ NUMBER_
334358
;

parser/sql/engine/dialect/doris/src/main/antlr4/org/apache/shardingsphere/sql/parser/autogen/DorisStatement.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ execute
5151
| createTrigger
5252
| dropTrigger
5353
| alterResourceGroup
54+
| alterResource
5455
| createResourceGroup
5556
| dropResourceGroup
5657
| prepare

parser/sql/engine/dialect/doris/src/main/java/org/apache/shardingsphere/sql/parser/engine/doris/visitor/statement/type/DorisDALStatementVisitor.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@
120120
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.UninstallComponentContext;
121121
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.UninstallPluginContext;
122122
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.UseContext;
123+
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.AlterResourceContext;
124+
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.PropertyAssignmentContext;
125+
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.ResourceNameContext;
126+
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.PropertyKeyContext;
127+
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.PropertyValueContext;
123128
import org.apache.shardingsphere.sql.parser.engine.doris.visitor.statement.DorisStatementVisitor;
124129
import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.CacheTableIndexSegment;
125130
import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.CloneActionSegment;
@@ -153,6 +158,13 @@
153158
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
154159
import org.apache.shardingsphere.sql.parser.statement.core.value.literal.impl.NumberLiteralValue;
155160
import org.apache.shardingsphere.sql.parser.statement.core.value.literal.impl.StringLiteralValue;
161+
import org.apache.shardingsphere.sql.parser.statement.core.value.literal.LiteralValue;
162+
import org.apache.shardingsphere.sql.parser.statement.core.value.literal.impl.BooleanLiteralValue;
163+
import org.apache.shardingsphere.sql.parser.statement.core.value.literal.impl.DateTimeLiteralValue;
164+
import org.apache.shardingsphere.sql.parser.statement.core.value.literal.impl.NullLiteralValue;
165+
import org.apache.shardingsphere.sql.parser.statement.core.value.literal.impl.OtherLiteralValue;
166+
import org.apache.shardingsphere.sql.parser.statement.core.value.literal.impl.TemporalLiteralValue;
167+
import org.apache.shardingsphere.sql.parser.statement.doris.dal.DorisAlterResourceStatement;
156168
import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLCloneStatement;
157169
import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLCreateLoadableFunctionStatement;
158170
import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLDelimiterStatement;
@@ -233,6 +245,7 @@
233245
import java.util.LinkedList;
234246
import java.util.List;
235247
import java.util.Optional;
248+
import java.util.Properties;
236249
import java.util.stream.Collectors;
237250

238251
/**
@@ -902,6 +915,68 @@ public ASTNode visitAlterResourceGroup(final AlterResourceGroupContext ctx) {
902915
return new MySQLAlterResourceGroupStatement(getDatabaseType(), ((IdentifierValue) visit(ctx.groupName())).getValue());
903916
}
904917

918+
@Override
919+
public ASTNode visitAlterResource(final AlterResourceContext ctx) {
920+
String resourceName = getResourceName(ctx.resourceName());
921+
Properties properties = new Properties();
922+
for (PropertyAssignmentContext each : ctx.propertyAssignments().propertyAssignment()) {
923+
String key = getPropertyKey(each.propertyKey());
924+
String value = getPropertyValue(each.propertyValue());
925+
properties.setProperty(key, value);
926+
}
927+
return new DorisAlterResourceStatement(getDatabaseType(), resourceName, properties);
928+
}
929+
930+
private String getResourceName(final ResourceNameContext ctx) {
931+
if (null != ctx.identifier()) {
932+
return ((IdentifierValue) visit(ctx.identifier())).getValue();
933+
}
934+
return ((StringLiteralValue) visit(ctx.string_())).getValue();
935+
}
936+
937+
private String getPropertyKey(final PropertyKeyContext ctx) {
938+
if (null != ctx.identifier()) {
939+
return ((IdentifierValue) visit(ctx.identifier())).getValue();
940+
}
941+
return ((StringLiteralValue) visit(ctx.string_())).getValue();
942+
}
943+
944+
private String getPropertyValue(final PropertyValueContext ctx) {
945+
if (null != ctx.identifier()) {
946+
return ((IdentifierValue) visit(ctx.identifier())).getValue();
947+
}
948+
ASTNode result = visit(ctx.literals());
949+
if (result instanceof LiteralValue) {
950+
return getLiteralValueAsString((LiteralValue<?>) result);
951+
}
952+
return result.toString();
953+
}
954+
955+
private String getLiteralValueAsString(final LiteralValue<?> literalValue) {
956+
if (literalValue instanceof StringLiteralValue) {
957+
return ((StringLiteralValue) literalValue).getValue();
958+
}
959+
if (literalValue instanceof NumberLiteralValue) {
960+
return ((NumberLiteralValue) literalValue).getValue().toString();
961+
}
962+
if (literalValue instanceof BooleanLiteralValue) {
963+
return String.valueOf(((BooleanLiteralValue) literalValue).getValue());
964+
}
965+
if (literalValue instanceof NullLiteralValue) {
966+
return "NULL";
967+
}
968+
if (literalValue instanceof DateTimeLiteralValue) {
969+
return ((DateTimeLiteralValue) literalValue).getValue();
970+
}
971+
if (literalValue instanceof TemporalLiteralValue) {
972+
return ((TemporalLiteralValue) literalValue).getValue();
973+
}
974+
if (literalValue instanceof OtherLiteralValue) {
975+
return String.valueOf(((OtherLiteralValue) literalValue).getValue());
976+
}
977+
return String.valueOf(literalValue.getValue());
978+
}
979+
905980
@Override
906981
public ASTNode visitChangeMasterTo(final ChangeMasterToContext ctx) {
907982
return new MySQLChangeMasterStatement(getDatabaseType());
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to You under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ 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+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<parent>
22+
<groupId>org.apache.shardingsphere</groupId>
23+
<artifactId>shardingsphere-parser-sql-statement-dialect</artifactId>
24+
<version>5.5.3-SNAPSHOT</version>
25+
</parent>
26+
<artifactId>shardingsphere-parser-sql-statement-doris</artifactId>
27+
<name>${project.artifactId}</name>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.apache.shardingsphere</groupId>
32+
<artifactId>shardingsphere-parser-sql-statement-core</artifactId>
33+
<version>${project.version}</version>
34+
</dependency>
35+
</dependencies>
36+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shardingsphere.sql.parser.statement.doris.dal;
19+
20+
import lombok.Getter;
21+
import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
22+
import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.DALStatement;
23+
24+
import java.util.Properties;
25+
26+
/**
27+
* Alter resource statement for Doris.
28+
*/
29+
@Getter
30+
public final class DorisAlterResourceStatement extends DALStatement {
31+
32+
private final String resourceName;
33+
34+
private final Properties properties;
35+
36+
public DorisAlterResourceStatement(final DatabaseType databaseType, final String resourceName, final Properties properties) {
37+
super(databaseType);
38+
this.resourceName = resourceName;
39+
this.properties = properties;
40+
}
41+
}

parser/sql/statement/dialect/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@
3333
<module>sqlserver</module>
3434
<module>oracle</module>
3535
<module>hive</module>
36+
<module>doris</module>
3637
</modules>
3738
</project>

test/it/parser/src/main/java/org/apache/shardingsphere/test/it/sql/parser/internal/asserts/statement/dal/DALStatementAssert.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.dal.dialect.mysql.MySQLDALStatementAssert;
2525
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.dal.dialect.oracle.OracleDALStatementAssert;
2626
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.dal.dialect.postgresql.PostgreSQLDALStatementAssert;
27+
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.dal.dialect.doris.DorisDALStatementAssert;
2728
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.dal.standard.StandardDALStatementAssert;
2829
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.SQLParserTestCase;
2930

@@ -45,5 +46,6 @@ public static void assertIs(final SQLCaseAssertContext assertContext, final DALS
4546
MySQLDALStatementAssert.assertIs(assertContext, actual, expected);
4647
PostgreSQLDALStatementAssert.assertIs(assertContext, actual, expected);
4748
OracleDALStatementAssert.assertIs(assertContext, actual, expected);
49+
DorisDALStatementAssert.assertIs(assertContext, actual, expected);
4850
}
4951
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.dal.dialect.doris;
19+
20+
import lombok.AccessLevel;
21+
import lombok.NoArgsConstructor;
22+
import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.DALStatement;
23+
import org.apache.shardingsphere.sql.parser.statement.doris.dal.DorisAlterResourceStatement;
24+
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.SQLCaseAssertContext;
25+
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.dal.dialect.doris.type.DorisAlterResourceStatementAssert;
26+
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.SQLParserTestCase;
27+
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.dal.dialect.doris.DorisAlterResourceStatementTestCase;
28+
29+
/**
30+
* DAL statement assert for Doris.
31+
*/
32+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
33+
public final class DorisDALStatementAssert {
34+
35+
/**
36+
* Assert DAL statement is correct with expected parser result.
37+
*
38+
* @param assertContext assert context
39+
* @param actual actual DAL statement
40+
* @param expected expected DAL statement test case
41+
*/
42+
public static void assertIs(final SQLCaseAssertContext assertContext, final DALStatement actual, final SQLParserTestCase expected) {
43+
if (actual instanceof DorisAlterResourceStatement) {
44+
DorisAlterResourceStatementAssert.assertIs(assertContext, (DorisAlterResourceStatement) actual, (DorisAlterResourceStatementTestCase) expected);
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)