This is an automated email from the ASF dual-hosted git repository.

zhangliang 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 358987bdd47 Add SQLStatementAttribute (#35771)
358987bdd47 is described below

commit 358987bdd47bd7ae129d237362060c70c8922e9c
Author: Liang Zhang <zhangli...@apache.org>
AuthorDate: Sat Jun 21 12:29:11 2025 +0800

    Add SQLStatementAttribute (#35771)
    
    * Add SQLStatementAttribute
    
    * Add SQLStatementAttribute
---
 .../statement/core/statement/SQLStatement.java     | 10 ++++
 ...tabaseSelectRequiredSQLStatementAttribute.java} |  6 +--
 .../SQLStatementAttribute.java}                    |  6 +--
 .../attribute/SQLStatementAttributes.java          | 62 ++++++++++++++++++++++
 .../statement/dal/ShowCreateTableStatement.java    | 11 ++--
 .../statement/dal/ShowTableStatusStatement.java    | 10 +++-
 .../core/statement/dal/ShowTablesStatement.java    | 10 +++-
 .../data/DatabaseBackendHandlerFactory.java        |  4 +-
 .../data/DatabaseBackendHandlerFactoryTest.java    |  2 +-
 9 files changed, 105 insertions(+), 16 deletions(-)

diff --git 
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/SQLStatement.java
 
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/SQLStatement.java
index 5458cd04891..6f04a4a5346 100644
--- 
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/SQLStatement.java
+++ 
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/SQLStatement.java
@@ -20,6 +20,7 @@ package 
org.apache.shardingsphere.sql.parser.statement.core.statement;
 import org.apache.shardingsphere.sql.parser.api.ASTNode;
 import 
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.CommentSegment;
 import 
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.ParameterMarkerSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.SQLStatementAttributes;
 
 import java.util.Collection;
 
@@ -62,4 +63,13 @@ public interface SQLStatement extends ASTNode {
      * @return comment segments
      */
     Collection<CommentSegment> getComments();
+    
+    /**
+     * Get SQL statement attributes.
+     *
+     * @return SQL statement attributes
+     */
+    default SQLStatementAttributes getAttributes() {
+        return new SQLStatementAttributes();
+    }
 }
diff --git 
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/required/DatabaseSelectRequiredSQLStatement.java
 
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/attribute/DatabaseSelectRequiredSQLStatementAttribute.java
similarity index 83%
copy from 
parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/required/DatabaseSelectRequiredSQLStatement.java
copy to 
parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/attribute/DatabaseSelectRequiredSQLStatementAttribute.java
index 64e40b45d7d..641f3aa2a61 100644
--- 
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/required/DatabaseSelectRequiredSQLStatement.java
+++ 
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/attribute/DatabaseSelectRequiredSQLStatementAttribute.java
@@ -15,10 +15,10 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.sql.parser.statement.core.statement.required;
+package 
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute;
 
 /**
- * Database select required SQL statement.
+ * Database select required SQL statement attribute.
  */
-public interface DatabaseSelectRequiredSQLStatement {
+public final class DatabaseSelectRequiredSQLStatementAttribute implements 
SQLStatementAttribute {
 }
diff --git 
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/required/DatabaseSelectRequiredSQLStatement.java
 
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/attribute/SQLStatementAttribute.java
similarity index 88%
rename from 
parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/required/DatabaseSelectRequiredSQLStatement.java
rename to 
parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/attribute/SQLStatementAttribute.java
index 64e40b45d7d..5f61c1c1406 100644
--- 
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/required/DatabaseSelectRequiredSQLStatement.java
+++ 
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/attribute/SQLStatementAttribute.java
@@ -15,10 +15,10 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.sql.parser.statement.core.statement.required;
+package 
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute;
 
 /**
- * Database select required SQL statement.
+ * SQL statement attribute.
  */
-public interface DatabaseSelectRequiredSQLStatement {
+public interface SQLStatementAttribute {
 }
diff --git 
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/attribute/SQLStatementAttributes.java
 
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/attribute/SQLStatementAttributes.java
new file mode 100644
index 00000000000..509b7055dba
--- /dev/null
+++ 
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/attribute/SQLStatementAttributes.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.sql.parser.statement.core.statement.attribute;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Optional;
+
+/**
+ * SQL statement attribute.
+ */
+public final class SQLStatementAttributes {
+    
+    private final Collection<SQLStatementAttribute> attributes;
+    
+    public SQLStatementAttributes(final SQLStatementAttribute... attributes) {
+        this.attributes = Arrays.asList(attributes);
+    }
+    
+    /**
+     * Find SQL statement attribute.
+     *
+     * @param attributeClass SQL statement attribute class
+     * @param <T> type of SQL statement attribute
+     * @return found SQL statement attribute
+     */
+    @SuppressWarnings("unchecked")
+    public <T extends SQLStatementAttribute> Optional<T> findAttribute(final 
Class<T> attributeClass) {
+        for (SQLStatementAttribute each : attributes) {
+            if (attributeClass.isAssignableFrom(each.getClass())) {
+                return Optional.of((T) each);
+            }
+        }
+        return Optional.empty();
+    }
+    
+    /**
+     * Get SQL statement attribute.
+     *
+     * @param attributeClass SQL statement attribute class
+     * @param <T> type of SQL statement attribute
+     * @return got SQL statement attribute
+     */
+    public <T extends SQLStatementAttribute> T getAttribute(final Class<T> 
attributeClass) {
+        return findAttribute(attributeClass).orElseThrow(() -> new 
IllegalStateException(String.format("Can not find SQL statement attribute: %s", 
attributeClass)));
+    }
+}
diff --git 
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dal/ShowCreateTableStatement.java
 
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dal/ShowCreateTableStatement.java
index 7f96998d860..b125a19d6e6 100644
--- 
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dal/ShowCreateTableStatement.java
+++ 
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dal/ShowCreateTableStatement.java
@@ -21,9 +21,10 @@ import lombok.Getter;
 import lombok.RequiredArgsConstructor;
 import 
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.AbstractSQLStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.DatabaseSelectRequiredSQLStatementAttribute;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.SQLStatementAttributes;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.available.TableAvailableSQLStatement;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.available.TableInfoInResultSetAvailableSQLStatement;
-import 
org.apache.shardingsphere.sql.parser.statement.core.statement.required.DatabaseSelectRequiredSQLStatement;
 
 import java.util.Collection;
 import java.util.Collections;
@@ -37,8 +38,7 @@ public final class ShowCreateTableStatement extends 
AbstractSQLStatement
         implements
             DALStatement,
             TableAvailableSQLStatement,
-            TableInfoInResultSetAvailableSQLStatement,
-            DatabaseSelectRequiredSQLStatement {
+            TableInfoInResultSetAvailableSQLStatement {
     
     private final SimpleTableSegment table;
     
@@ -51,4 +51,9 @@ public final class ShowCreateTableStatement extends 
AbstractSQLStatement
     public int getTableNameResultSetIndex() {
         return 2;
     }
+    
+    @Override
+    public SQLStatementAttributes getAttributes() {
+        return new SQLStatementAttributes(new 
DatabaseSelectRequiredSQLStatementAttribute());
+    }
 }
diff --git 
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dal/ShowTableStatusStatement.java
 
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dal/ShowTableStatusStatement.java
index 1cb10e071ef..218cfda4476 100644
--- 
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dal/ShowTableStatusStatement.java
+++ 
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dal/ShowTableStatusStatement.java
@@ -21,8 +21,9 @@ import lombok.RequiredArgsConstructor;
 import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dal.FromDatabaseSegment;
 import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dal.ShowFilterSegment;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.AbstractSQLStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.DatabaseSelectRequiredSQLStatementAttribute;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.SQLStatementAttributes;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.available.FromDatabaseAvailableSQLStatement;
-import 
org.apache.shardingsphere.sql.parser.statement.core.statement.required.DatabaseSelectRequiredSQLStatement;
 
 import java.util.Optional;
 
@@ -30,7 +31,7 @@ import java.util.Optional;
  * Show table status statement.
  */
 @RequiredArgsConstructor
-public final class ShowTableStatusStatement extends AbstractSQLStatement 
implements DALStatement, FromDatabaseAvailableSQLStatement, 
DatabaseSelectRequiredSQLStatement {
+public final class ShowTableStatusStatement extends AbstractSQLStatement 
implements DALStatement, FromDatabaseAvailableSQLStatement {
     
     private final FromDatabaseSegment fromDatabase;
     
@@ -49,4 +50,9 @@ public final class ShowTableStatusStatement extends 
AbstractSQLStatement impleme
     public Optional<ShowFilterSegment> getFilter() {
         return Optional.ofNullable(filter);
     }
+    
+    @Override
+    public SQLStatementAttributes getAttributes() {
+        return new SQLStatementAttributes(new 
DatabaseSelectRequiredSQLStatementAttribute());
+    }
 }
diff --git 
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dal/ShowTablesStatement.java
 
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dal/ShowTablesStatement.java
index 80d87af03a7..b83629f7442 100644
--- 
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dal/ShowTablesStatement.java
+++ 
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/dal/ShowTablesStatement.java
@@ -22,8 +22,9 @@ import lombok.RequiredArgsConstructor;
 import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dal.FromDatabaseSegment;
 import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dal.ShowFilterSegment;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.AbstractSQLStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.DatabaseSelectRequiredSQLStatementAttribute;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.SQLStatementAttributes;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.available.FromDatabaseAvailableSQLStatement;
-import 
org.apache.shardingsphere.sql.parser.statement.core.statement.required.DatabaseSelectRequiredSQLStatement;
 
 import java.util.Optional;
 
@@ -32,7 +33,7 @@ import java.util.Optional;
  */
 @RequiredArgsConstructor
 @Getter
-public final class ShowTablesStatement extends AbstractSQLStatement implements 
DALStatement, FromDatabaseAvailableSQLStatement, 
DatabaseSelectRequiredSQLStatement {
+public final class ShowTablesStatement extends AbstractSQLStatement implements 
DALStatement, FromDatabaseAvailableSQLStatement {
     
     private final FromDatabaseSegment fromDatabase;
     
@@ -53,4 +54,9 @@ public final class ShowTablesStatement extends 
AbstractSQLStatement implements D
     public Optional<ShowFilterSegment> getFilter() {
         return Optional.ofNullable(filter);
     }
+    
+    @Override
+    public SQLStatementAttributes getAttributes() {
+        return new SQLStatementAttributes(new 
DatabaseSelectRequiredSQLStatementAttribute());
+    }
 }
diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/data/DatabaseBackendHandlerFactory.java
 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/data/DatabaseBackendHandlerFactory.java
index 26588de385c..e9effd5607e 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/data/DatabaseBackendHandlerFactory.java
+++ 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/data/DatabaseBackendHandlerFactory.java
@@ -26,11 +26,11 @@ import 
org.apache.shardingsphere.proxy.backend.handler.data.impl.UnicastDatabase
 import 
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
 import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.DatabaseSelectRequiredSQLStatementAttribute;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.dal.DALStatement;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.dal.SetStatement;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DoStatement;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement;
-import 
org.apache.shardingsphere.sql.parser.statement.core.statement.required.DatabaseSelectRequiredSQLStatement;
 
 /**
  * Database backend handler factory.
@@ -62,7 +62,7 @@ public final class DatabaseBackendHandlerFactory {
     }
     
     private static boolean isNotDatabaseSelectRequiredDALStatement(final 
SQLStatement sqlStatement) {
-        return sqlStatement instanceof DALStatement && !(sqlStatement 
instanceof DatabaseSelectRequiredSQLStatement);
+        return sqlStatement instanceof DALStatement && 
!sqlStatement.getAttributes().findAttribute(DatabaseSelectRequiredSQLStatementAttribute.class).isPresent();
     }
     
     private static boolean isNotContainFromSelectStatement(final SQLStatement 
sqlStatement) {
diff --git 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/data/DatabaseBackendHandlerFactoryTest.java
 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/data/DatabaseBackendHandlerFactoryTest.java
index ad49656885c..455b5a3d67c 100644
--- 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/data/DatabaseBackendHandlerFactoryTest.java
+++ 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/data/DatabaseBackendHandlerFactoryTest.java
@@ -59,7 +59,7 @@ class DatabaseBackendHandlerFactoryTest {
     void assertNewInstanceReturnedUnicastDatabaseBackendHandlerWithDAL() {
         String sql = "DESC tbl";
         SQLStatementContext sqlStatementContext = 
mock(SQLStatementContext.class, RETURNS_DEEP_STUBS);
-        
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(DALStatement.class));
+        
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(DALStatement.class, 
RETURNS_DEEP_STUBS));
         
when(sqlStatementContext.getTablesContext().getDatabaseNames()).thenReturn(Collections.emptyList());
         DatabaseBackendHandler actual = 
DatabaseBackendHandlerFactory.newInstance(
                 new QueryContext(sqlStatementContext, sql, 
Collections.emptyList(), new HintValueContext(), mockConnectionContext(), 
mock(ShardingSphereMetaData.class)),

Reply via email to