This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 0f3cf17f658 Support grant statement SQL bind (#36207)
0f3cf17f658 is described below
commit 0f3cf17f6583adb05d641dc502248df82770813f
Author: chakkk309 <[email protected]>
AuthorDate: Thu Jan 8 09:20:17 2026 +0800
Support grant statement SQL bind (#36207)
* Support grant statement SQL bind
* fix checkstyle
* resolve conflict
* update release notes
* fix: databaseType import in GrantStatementBinderTest
---
RELEASE-NOTES.md | 1 +
.../engine/statement/dcl/GrantStatementBinder.java | 62 +++++++++++++++
.../binder/engine/type/DCLStatementBindEngine.java | 5 ++
.../statement/dcl/GrantStatementBinderTest.java | 92 ++++++++++++++++++++++
.../binder/src/test/resources/cases/dcl/grant.xml | 28 +++++++
.../binder/src/test/resources/sqls/dcl/grant.xml | 21 +++++
6 files changed, 209 insertions(+)
diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md
index 4768e761a1f..ae56f2cab60 100644
--- a/RELEASE-NOTES.md
+++ b/RELEASE-NOTES.md
@@ -74,6 +74,7 @@
1. Pipeline: Support multi-columns unique key non-first column nullable -
[#37647](https://github.com/apache/shardingsphere/pull/37647)
1. Encrypt: Support handling show create view result decoration in encrypt -
[#37299](https://github.com/apache/shardingsphere/pull/37299)
1. JDBC: Enhance ResultSetUtils to support flexible string date/time
conversions - [37424](https://github.com/apache/shardingsphere/pull/37424)
+1. SQL Binder: Support Grant statement SQL bind -
[#36207](https://github.com/apache/shardingsphere/pull/36207)
### Bug Fixes
diff --git
a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dcl/GrantStatementBinder.java
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dcl/GrantStatementBinder.java
new file mode 100644
index 00000000000..28c988b982f
--- /dev/null
+++
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dcl/GrantStatementBinder.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.binder.engine.statement.dcl;
+
+import com.cedarsoftware.util.CaseInsensitiveMap.CaseInsensitiveString;
+import com.google.common.collect.LinkedHashMultimap;
+import com.google.common.collect.Multimap;
+import
org.apache.shardingsphere.infra.binder.engine.segment.dml.from.context.TableSegmentBinderContext;
+import
org.apache.shardingsphere.infra.binder.engine.segment.dml.from.type.SimpleTableSegmentBinder;
+import
org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinder;
+import
org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext;
+import
org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementCopyUtils;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dcl.GrantStatement;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+/**
+ * Grant statement binder.
+ */
+public final class GrantStatementBinder implements
SQLStatementBinder<GrantStatement> {
+
+ @Override
+ public GrantStatement bind(final GrantStatement sqlStatement, final
SQLStatementBinderContext binderContext) {
+ Collection<SimpleTableSegment> tables = sqlStatement.getTables();
+ if (tables.isEmpty()) {
+ return sqlStatement;
+ }
+ Multimap<CaseInsensitiveString, TableSegmentBinderContext>
tableBinderContexts = LinkedHashMultimap.create();
+ Collection<SimpleTableSegment> boundTables = new ArrayList<>();
+ for (SimpleTableSegment each : tables) {
+ boundTables.add(SimpleTableSegmentBinder.bind(each, binderContext,
tableBinderContexts));
+ }
+ return copyGrantStatement(sqlStatement, boundTables);
+ }
+
+ private GrantStatement copyGrantStatement(final GrantStatement
sqlStatement, final Collection<SimpleTableSegment> tables) {
+ if (tables.equals(sqlStatement.getTables())) {
+ return sqlStatement;
+ }
+ GrantStatement result = new
GrantStatement(sqlStatement.getDatabaseType());
+ result.getTables().addAll(tables);
+ SQLStatementCopyUtils.copyAttributes(sqlStatement, result);
+ return result;
+ }
+}
diff --git
a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DCLStatementBindEngine.java
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DCLStatementBindEngine.java
index a86c60b6879..7c71af57399 100644
---
a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DCLStatementBindEngine.java
+++
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DCLStatementBindEngine.java
@@ -19,8 +19,10 @@ package org.apache.shardingsphere.infra.binder.engine.type;
import
org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext;
import
org.apache.shardingsphere.infra.binder.engine.statement.dcl.RevokeStatementBinder;
+import
org.apache.shardingsphere.infra.binder.engine.statement.dcl.GrantStatementBinder;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dcl.DCLStatement;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dcl.RevokeStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dcl.GrantStatement;
/**
* DCL statement bind engine.
@@ -38,6 +40,9 @@ public final class DCLStatementBindEngine {
if (statement instanceof RevokeStatement) {
return new RevokeStatementBinder().bind((RevokeStatement)
statement, binderContext);
}
+ if (statement instanceof GrantStatement) {
+ return new GrantStatementBinder().bind((GrantStatement) statement,
binderContext);
+ }
return statement;
}
}
diff --git
a/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/engine/statement/dcl/GrantStatementBinderTest.java
b/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/engine/statement/dcl/GrantStatementBinderTest.java
new file mode 100644
index 00000000000..260757642d0
--- /dev/null
+++
b/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/engine/statement/dcl/GrantStatementBinderTest.java
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.binder.engine.statement.dcl;
+
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import
org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext;
+import org.apache.shardingsphere.infra.hint.HintValueContext;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
+import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dcl.GrantStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Collection;
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class GrantStatementBinderTest {
+
+ private final DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+
+ @Mock
+ private ShardingSphereMetaData metaData;
+
+ @Mock
+ private ShardingSphereDatabase database;
+
+ @Mock
+ private ShardingSphereSchema schema;
+
+ @Mock
+ private ShardingSphereTable table;
+
+ @Test
+ void assertBind() {
+ when(metaData.containsDatabase("foo_db")).thenReturn(true);
+ when(metaData.getDatabase("foo_db")).thenReturn(database);
+ when(database.containsSchema("foo_db")).thenReturn(true);
+ when(database.getSchema("foo_db")).thenReturn(schema);
+ when(schema.containsTable("test_table")).thenReturn(true);
+ when(schema.getTable("test_table")).thenReturn(table);
+ when(table.getAllColumns()).thenReturn(Collections.emptyList());
+ HintValueContext hintValueContext = new HintValueContext();
+ hintValueContext.setSkipMetadataValidate(true);
+ SimpleTableSegment tableSegment = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("test_table")));
+ GrantStatement original = new GrantStatement(databaseType);
+ original.getTables().add(tableSegment);
+ SQLStatementBinderContext binderContext = new
SQLStatementBinderContext(metaData, "foo_db", hintValueContext, original);
+ GrantStatement actual = new GrantStatementBinder().bind(original,
binderContext);
+ Collection<SimpleTableSegment> actualTables = actual.getTables();
+ assertThat(actualTables.size(), is(1));
+
assertThat(actualTables.iterator().next().getTableName().getIdentifier().getValue(),
is("test_table"));
+ }
+
+ @Test
+ void assertBindWithEmptyTables() {
+ GrantStatement original = new GrantStatement(databaseType);
+ HintValueContext hintValueContext = new HintValueContext();
+ hintValueContext.setSkipMetadataValidate(true);
+ SQLStatementBinderContext binderContext = new
SQLStatementBinderContext(metaData, "foo_db", hintValueContext, original);
+ GrantStatement actual = new GrantStatementBinder().bind(original,
binderContext);
+ Collection<SimpleTableSegment> actualTables = actual.getTables();
+ assertThat(actualTables.size(), is(0));
+ }
+}
diff --git a/test/it/binder/src/test/resources/cases/dcl/grant.xml
b/test/it/binder/src/test/resources/cases/dcl/grant.xml
new file mode 100644
index 00000000000..a20b46d2246
--- /dev/null
+++ b/test/it/binder/src/test/resources/cases/dcl/grant.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one or more
+ ~ contributor license agreements. See the NOTICE file distributed with
+ ~ this work for additional information regarding copyright ownership.
+ ~ The ASF licenses this file to You under the Apache License, Version 2.0
+ ~ (the "License"); you may not use this file except in compliance with
+ ~ the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+
+<sql-parser-test-cases>
+ <grant sql-case-id="grant_select_on_table">
+ <table name="t_order" start-index="16" stop-index="22">
+ <table-bound>
+ <original-database name="foo_db_1" />
+ <original-schema name="foo_db_1" />
+ </table-bound>
+ </table>
+ </grant>
+</sql-parser-test-cases>
diff --git a/test/it/binder/src/test/resources/sqls/dcl/grant.xml
b/test/it/binder/src/test/resources/sqls/dcl/grant.xml
new file mode 100644
index 00000000000..557e3db966d
--- /dev/null
+++ b/test/it/binder/src/test/resources/sqls/dcl/grant.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one or more
+ ~ contributor license agreements. See the NOTICE file distributed with
+ ~ this work for additional information regarding copyright ownership.
+ ~ The ASF licenses this file to You under the Apache License, Version 2.0
+ ~ (the "License"); you may not use this file except in compliance with
+ ~ the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+
+<sql-cases>
+ <sql-case id="grant_select_on_table" value="GRANT SELECT ON t_order TO
user1" db-types="MySQL"/>
+</sql-cases>