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 f50af23c253 Support comment statement SQL bind (#36012)
f50af23c253 is described below

commit f50af23c253ec9a71247e864b530e2a7a55414a5
Author: Chakkk <[email protected]>
AuthorDate: Thu Jul 17 14:03:44 2025 +0800

    Support comment statement SQL bind (#36012)
    
    * Support comment table statement context SQL bind
    
    * update release notes
    
    * remove useless blank line
---
 RELEASE-NOTES.md                                   |   1 +
 .../statement/ddl/CommentStatementBinder.java      |  57 +++++++++++
 .../binder/engine/type/DDLStatementBindEngine.java |   5 +
 .../statement/ddl/CommentStatementBinderTest.java  | 114 +++++++++++++++++++++
 .../src/test/resources/cases/ddl/comment.xml       |  47 +++++++++
 .../binder/src/test/resources/sqls/ddl/comment.xml |  23 +++++
 6 files changed, 247 insertions(+)

diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md
index d01678f1d0d..06a33a1a6ef 100644
--- a/RELEASE-NOTES.md
+++ b/RELEASE-NOTES.md
@@ -48,6 +48,7 @@
 1. SQL Binder: Support outer join expression bind - 
[#35019](https://github.com/apache/shardingsphere/pull/35019)
 1. SQL Binder: Support explain statement sql bind - 
[#35439](https://github.com/apache/shardingsphere/pull/35439)
 1. SQL Binder: Support AnalyzeTable statement SQL bind - 
[#35954](https://github.com/apache/shardingsphere/pull/35954)
+1. SQL Binder: Support Comment statement SQL bind - 
[#36012](https://github.com/apache/shardingsphere/pull/36012)
 1. SQL Binder: Add alter table metadata check - 
[#35877](https://github.com/apache/shardingsphere/pull/35877)
 1. SQL Router: Add check for select with union all routing to multi data 
sources - [#35037](https://github.com/apache/shardingsphere/pull/35037)
 1. SQL Router: Improve support for executing tableless SQL with single data 
source - [#35659](https://github.com/apache/shardingsphere/pull/35659)
diff --git 
a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/ddl/CommentStatementBinder.java
 
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/ddl/CommentStatementBinder.java
new file mode 100644
index 00000000000..12f6993d345
--- /dev/null
+++ 
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/ddl/CommentStatementBinder.java
@@ -0,0 +1,57 @@
+/*
+ * 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.ddl;
+
+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.dml.column.ColumnSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.CommentStatement;
+
+import java.util.Optional;
+
+/**
+ * Comment statement binder.
+ */
+public final class CommentStatementBinder implements 
SQLStatementBinder<CommentStatement> {
+    
+    @Override
+    public CommentStatement bind(final CommentStatement sqlStatement, final 
SQLStatementBinderContext binderContext) {
+        Multimap<CaseInsensitiveString, TableSegmentBinderContext> 
tableBinderContexts = LinkedHashMultimap.create();
+        Optional<SimpleTableSegment> boundTable = 
Optional.ofNullable(sqlStatement.getTable())
+                .map(each -> SimpleTableSegmentBinder.bind(each, 
binderContext, tableBinderContexts));
+        ColumnSegment boundColumn = sqlStatement.getColumn();
+        return copy(sqlStatement, boundTable.orElse(null), boundColumn);
+    }
+    
+    private CommentStatement copy(final CommentStatement sqlStatement, final 
SimpleTableSegment table, final ColumnSegment column) {
+        CommentStatement result = new 
CommentStatement(sqlStatement.getDatabaseType());
+        result.setTable(table);
+        result.setColumn(column);
+        result.setComment(sqlStatement.getComment());
+        result.setIndexType(sqlStatement.getIndexType().orElse(null));
+        SQLStatementCopyUtils.copyAttributes(sqlStatement, result);
+        return result;
+    }
+}
diff --git 
a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DDLStatementBindEngine.java
 
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DDLStatementBindEngine.java
index 8a8a7c8f9c8..332c546b178 100644
--- 
a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DDLStatementBindEngine.java
+++ 
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DDLStatementBindEngine.java
@@ -21,6 +21,7 @@ import 
org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinde
 import 
org.apache.shardingsphere.infra.binder.engine.statement.ddl.AlterIndexStatementBinder;
 import 
org.apache.shardingsphere.infra.binder.engine.statement.ddl.AlterTableStatementBinder;
 import 
org.apache.shardingsphere.infra.binder.engine.statement.ddl.AlterViewStatementBinder;
+import 
org.apache.shardingsphere.infra.binder.engine.statement.ddl.CommentStatementBinder;
 import 
org.apache.shardingsphere.infra.binder.engine.statement.ddl.CreateIndexStatementBinder;
 import 
org.apache.shardingsphere.infra.binder.engine.statement.ddl.CreateTableStatementBinder;
 import 
org.apache.shardingsphere.infra.binder.engine.statement.ddl.CreateViewStatementBinder;
@@ -30,6 +31,7 @@ import 
org.apache.shardingsphere.infra.binder.engine.statement.ddl.DropTableStat
 import 
org.apache.shardingsphere.infra.binder.engine.statement.ddl.DropViewStatementBinder;
 import 
org.apache.shardingsphere.infra.binder.engine.statement.ddl.RenameTableStatementBinder;
 import 
org.apache.shardingsphere.infra.binder.engine.statement.ddl.TruncateStatementBinder;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.CommentStatement;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.CursorStatement;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.DDLStatement;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.TruncateStatement;
@@ -93,6 +95,9 @@ public final class DDLStatementBindEngine {
         if (statement instanceof TruncateStatement) {
             return new TruncateStatementBinder().bind((TruncateStatement) 
statement, binderContext);
         }
+        if (statement instanceof CommentStatement) {
+            return new CommentStatementBinder().bind((CommentStatement) 
statement, binderContext);
+        }
         return statement;
     }
 }
diff --git 
a/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/engine/statement/ddl/CommentStatementBinderTest.java
 
b/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/engine/statement/ddl/CommentStatementBinderTest.java
new file mode 100644
index 00000000000..285a775500e
--- /dev/null
+++ 
b/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/engine/statement/ddl/CommentStatementBinderTest.java
@@ -0,0 +1,114 @@
+/*
+ * 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.ddl;
+
+import 
org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext;
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
+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.ddl.CommentStatement;
+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.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class CommentStatementBinderTest {
+    
+    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 assertBindWithTable() {
+        CommentStatement sqlStatement = new CommentStatement(databaseType);
+        SimpleTableSegment tableSegment = new SimpleTableSegment(new 
TableNameSegment(0, 0, new IdentifierValue("t_order")));
+        sqlStatement.setTable(tableSegment);
+        sqlStatement.setComment(new IdentifierValue("test comment"));
+        when(metaData.containsDatabase("foo_db_1")).thenReturn(true);
+        when(metaData.getDatabase("foo_db_1")).thenReturn(database);
+        when(database.containsSchema("foo_db_1")).thenReturn(true);
+        when(database.getSchema("foo_db_1")).thenReturn(schema);
+        when(schema.containsTable("t_order")).thenReturn(true);
+        when(schema.getTable("t_order")).thenReturn(table);
+        when(table.getAllColumns()).thenReturn(Collections.emptyList());
+        HintValueContext hintValueContext = new HintValueContext();
+        hintValueContext.setSkipMetadataValidate(true);
+        SQLStatementBinderContext binderContext = new 
SQLStatementBinderContext(metaData, "foo_db_1", hintValueContext, sqlStatement);
+        CommentStatement actual = new 
CommentStatementBinder().bind(sqlStatement, binderContext);
+        
assertThat(actual.getTable().getTableName().getIdentifier().getValue(), 
is("t_order"));
+        assertThat(actual.getComment().getValue(), is("test comment"));
+    }
+    
+    @Test
+    void assertBindWithoutTable() {
+        CommentStatement sqlStatement = new CommentStatement(databaseType);
+        sqlStatement.setComment(new IdentifierValue("test comment"));
+        HintValueContext hintValueContext = new HintValueContext();
+        hintValueContext.setSkipMetadataValidate(true);
+        SQLStatementBinderContext binderContext = new 
SQLStatementBinderContext(metaData, "foo_db_1", hintValueContext, sqlStatement);
+        CommentStatement actual = new 
CommentStatementBinder().bind(sqlStatement, binderContext);
+        assertNull(actual.getTable());
+        assertThat(actual.getComment().getValue(), is("test comment"));
+    }
+    
+    @Test
+    void assertBindWithColumn() {
+        CommentStatement sqlStatement = new CommentStatement(databaseType);
+        SimpleTableSegment tableSegment = new SimpleTableSegment(new 
TableNameSegment(0, 0, new IdentifierValue("t_order")));
+        sqlStatement.setTable(tableSegment);
+        sqlStatement.setComment(new IdentifierValue("column comment"));
+        when(metaData.containsDatabase("foo_db_1")).thenReturn(true);
+        when(metaData.getDatabase("foo_db_1")).thenReturn(database);
+        when(database.containsSchema("foo_db_1")).thenReturn(true);
+        when(database.getSchema("foo_db_1")).thenReturn(schema);
+        when(schema.containsTable("t_order")).thenReturn(true);
+        when(schema.getTable("t_order")).thenReturn(table);
+        when(table.getAllColumns()).thenReturn(Collections.emptyList());
+        HintValueContext hintValueContext = new HintValueContext();
+        hintValueContext.setSkipMetadataValidate(true);
+        SQLStatementBinderContext binderContext = new 
SQLStatementBinderContext(metaData, "foo_db_1", hintValueContext, sqlStatement);
+        CommentStatement actual = new 
CommentStatementBinder().bind(sqlStatement, binderContext);
+        
assertThat(actual.getTable().getTableName().getIdentifier().getValue(), 
is("t_order"));
+        assertThat(actual.getComment().getValue(), is("column comment"));
+    }
+}
diff --git a/test/it/binder/src/test/resources/cases/ddl/comment.xml 
b/test/it/binder/src/test/resources/cases/ddl/comment.xml
new file mode 100644
index 00000000000..4a91c3afa02
--- /dev/null
+++ b/test/it/binder/src/test/resources/cases/ddl/comment.xml
@@ -0,0 +1,47 @@
+<?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>
+    <comment sql-case-id="comment_table">
+        <table name="t_order" start-index="17" stop-index="23">
+            <table-bound>
+                <original-database name="foo_db_1" />
+                <original-schema name="public" />
+            </table-bound>
+        </table>
+    </comment>
+    
+    <comment sql-case-id="comment_column">
+        <table name="t_order" start-index="18" stop-index="24">
+            <table-bound>
+                <original-database name="foo_db_1" />
+                <original-schema name="public" />
+            </table-bound>
+        </table>
+        <column name="order_id" start-index="26" stop-index="33" />
+    </comment>
+    
+    <alter-table sql-case-id="comment_table_mysql">
+        <table name="t_order" start-index="12" stop-index="18">
+            <table-bound>
+                <original-database name="foo_db_1" />
+                <original-schema name="foo_db_1" />
+            </table-bound>
+        </table>
+    </alter-table>
+</sql-parser-test-cases>
diff --git a/test/it/binder/src/test/resources/sqls/ddl/comment.xml 
b/test/it/binder/src/test/resources/sqls/ddl/comment.xml
new file mode 100644
index 00000000000..6b35cbffeb3
--- /dev/null
+++ b/test/it/binder/src/test/resources/sqls/ddl/comment.xml
@@ -0,0 +1,23 @@
+<?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="comment_table" value="COMMENT ON TABLE t_order IS 'This is a 
table comment'" db-types="PostgreSQL"/>
+    <sql-case id="comment_column" value="COMMENT ON COLUMN t_order.order_id IS 
'This is a column comment'" db-types="PostgreSQL"/>
+    <sql-case id="comment_table_mysql" value="ALTER TABLE t_order COMMENT = 
'This is a table comment'" db-types="MySQL"/>
+</sql-cases>

Reply via email to