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 d5fc53e8346 Support flush statement SQL bind (#36036)
d5fc53e8346 is described below

commit d5fc53e834679ab037352236e7557fb9948170c8
Author: chakkk309 <[email protected]>
AuthorDate: Fri Dec 19 17:12:31 2025 +0800

    Support flush statement SQL bind (#36036)
    
    * Support MySQL flush statement SQL bind
    
    * refactor flushStatementBinder
    
    * fix checkstyle
    
    * remove useless blank line
    
    * update release notest
    
    * use commonSQLStatementContext
    
    * update release notes
    
    * remove useless import
    
    * fix EmptyLineSeparator checkstyle
    
    * fix spotless
    
    * refactor flushStatementBinder
    
    * fix: update release notes
    
    * fix: databaseType import in FlushStatement and FlushStatementBinder
---
 RELEASE-NOTES.md                                   |  1 +
 .../engine/statement/dal/FlushStatementBinder.java | 68 ++++++++++++++++++++++
 .../binder/engine/type/DALStatementBindEngine.java |  5 ++
 .../core/statement/type/dal/FlushStatement.java}   | 36 ++++++------
 .../statement/mysql/dal/MySQLFlushStatement.java   |  4 +-
 .../binder/src/test/resources/cases/dal/flush.xml  | 59 +++++++++++++++++++
 .../binder/src/test/resources/sqls/dal/flush.xml   | 26 +++++++++
 7 files changed, 178 insertions(+), 21 deletions(-)

diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md
index 3115ad24959..83b6121aa0d 100644
--- a/RELEASE-NOTES.md
+++ b/RELEASE-NOTES.md
@@ -151,6 +151,7 @@
 1. Sharding: Support GroupConcat function for aggregating multiple shards in 
MySQL, OpenGauss, Doris - 
[#33808](https://github.com/apache/shardingsphere/pull/33808)
 1. Encrypt: Add non-support checker for WITH, COMBINE, INSERT SELECT on 
encrypt feature  - [#34175](https://github.com/apache/shardingsphere/pull/34175)
 1. Encrypt: Support INSERT statement rewrite use quote 
[#34259](https://github.com/apache/shardingsphere/pull/34259)
+1. SQL Binder: Support Flush statement SQL bind - 
[#36036](https://github.com/apache/shardingsphere/pull/36036)
 
 ### Bug Fixes
 
diff --git 
a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/FlushStatementBinder.java
 
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/FlushStatementBinder.java
new file mode 100644
index 00000000000..d31f9f10dca
--- /dev/null
+++ 
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/FlushStatementBinder.java
@@ -0,0 +1,68 @@
+/*
+ * 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.dal;
+
+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.database.connector.core.type.DatabaseType;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.FlushStatement;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+/**
+ * Flush statement binder.
+ */
+public final class FlushStatementBinder implements 
SQLStatementBinder<FlushStatement> {
+    
+    @Override
+    public FlushStatement bind(final FlushStatement 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 copyFlushStatement(sqlStatement, boundTables);
+    }
+    
+    private FlushStatement copyFlushStatement(final FlushStatement 
sqlStatement, final Collection<SimpleTableSegment> tables) {
+        if (tables.equals(sqlStatement.getTables())) {
+            return sqlStatement;
+        }
+        try {
+            FlushStatement result = sqlStatement.getClass()
+                    .getDeclaredConstructor(DatabaseType.class, 
Collection.class, boolean.class)
+                    .newInstance(sqlStatement.getDatabaseType(), tables, 
sqlStatement.isFlushTable());
+            SQLStatementCopyUtils.copyAttributes(sqlStatement, result);
+            return result;
+        } catch (final ReflectiveOperationException ex) {
+            return sqlStatement;
+        }
+    }
+}
diff --git 
a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DALStatementBindEngine.java
 
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DALStatementBindEngine.java
index b01aa7c036d..9569f2b313f 100644
--- 
a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DALStatementBindEngine.java
+++ 
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DALStatementBindEngine.java
@@ -20,9 +20,11 @@ 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.dal.AnalyzeTableStatementBinder;
 import 
org.apache.shardingsphere.infra.binder.engine.statement.dal.ExplainStatementBinder;
+import 
org.apache.shardingsphere.infra.binder.engine.statement.dal.FlushStatementBinder;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.AnalyzeTableStatement;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.DALStatement;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.ExplainStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.FlushStatement;
 
 /**
  * DAL statement bind engine.
@@ -43,6 +45,9 @@ public final class DALStatementBindEngine {
         if (statement instanceof ExplainStatement) {
             return new ExplainStatementBinder().bind((ExplainStatement) 
statement, binderContext);
         }
+        if (statement instanceof FlushStatement) {
+            return new FlushStatementBinder().bind((FlushStatement) statement, 
binderContext);
+        }
         return statement;
     }
 }
diff --git 
a/parser/sql/statement/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/statement/mysql/dal/MySQLFlushStatement.java
 
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/type/dal/FlushStatement.java
similarity index 52%
copy from 
parser/sql/statement/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/statement/mysql/dal/MySQLFlushStatement.java
copy to 
parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/type/dal/FlushStatement.java
index de5c1751fb1..e37b98c54d8 100644
--- 
a/parser/sql/statement/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/statement/mysql/dal/MySQLFlushStatement.java
+++ 
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/type/dal/FlushStatement.java
@@ -15,35 +15,33 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.sql.parser.statement.mysql.dal;
+package org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal;
 
-import lombok.Getter;
 import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
 import 
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
-import 
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.SQLStatementAttributes;
-import 
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.type.TableSQLStatementAttribute;
-import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.DALStatement;
 
 import java.util.Collection;
 
 /**
- * Flush statement for MySQL.
+ * Flush statement.
  */
-@Getter
-public final class MySQLFlushStatement extends DALStatement {
+public abstract class FlushStatement extends DALStatement {
     
-    private final Collection<SimpleTableSegment> tables;
-    
-    private final boolean flushTable;
-    
-    public MySQLFlushStatement(final DatabaseType databaseType, final 
Collection<SimpleTableSegment> tables, final boolean flushTable) {
+    public FlushStatement(final DatabaseType databaseType) {
         super(databaseType);
-        this.tables = tables;
-        this.flushTable = flushTable;
     }
     
-    @Override
-    public SQLStatementAttributes getAttributes() {
-        return new SQLStatementAttributes(new 
TableSQLStatementAttribute(tables));
-    }
+    /**
+     * Get tables.
+     *
+     * @return tables
+     */
+    public abstract Collection<SimpleTableSegment> getTables();
+    
+    /**
+     * Is flush table.
+     *
+     * @return true if flush table, false otherwise
+     */
+    public abstract boolean isFlushTable();
 }
diff --git 
a/parser/sql/statement/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/statement/mysql/dal/MySQLFlushStatement.java
 
b/parser/sql/statement/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/statement/mysql/dal/MySQLFlushStatement.java
index de5c1751fb1..0d42290f238 100644
--- 
a/parser/sql/statement/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/statement/mysql/dal/MySQLFlushStatement.java
+++ 
b/parser/sql/statement/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/statement/mysql/dal/MySQLFlushStatement.java
@@ -22,7 +22,7 @@ import 
org.apache.shardingsphere.database.connector.core.type.DatabaseType;
 import 
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.SQLStatementAttributes;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.attribute.type.TableSQLStatementAttribute;
-import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.DALStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.FlushStatement;
 
 import java.util.Collection;
 
@@ -30,7 +30,7 @@ import java.util.Collection;
  * Flush statement for MySQL.
  */
 @Getter
-public final class MySQLFlushStatement extends DALStatement {
+public final class MySQLFlushStatement extends FlushStatement {
     
     private final Collection<SimpleTableSegment> tables;
     
diff --git a/test/it/binder/src/test/resources/cases/dal/flush.xml 
b/test/it/binder/src/test/resources/cases/dal/flush.xml
new file mode 100644
index 00000000000..a9a8fb224b5
--- /dev/null
+++ b/test/it/binder/src/test/resources/cases/dal/flush.xml
@@ -0,0 +1,59 @@
+<?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>
+    <flush sql-case-id="flush_tables" flush-table="true"></flush>
+
+    <flush sql-case-id="flush_tables_with_single_table" flush-table="true">
+        <table name="t_order" start-index="13" stop-index="19">
+            <table-bound>
+                <original-database name="foo_db_1" />
+                <original-schema name="foo_db_1" />
+            </table-bound>
+        </table>
+    </flush>
+
+    <flush sql-case-id="flush_tables_with_multiple_tables" flush-table="true">
+        <table name="t_order" start-index="13" stop-index="19">
+            <table-bound>
+                <original-database name="foo_db_1" />
+                <original-schema name="foo_db_1" />
+            </table-bound>
+        </table>
+        <table name="t_order_item" start-index="22" stop-index="33">
+            <table-bound>
+                <original-database name="foo_db_1" />
+                <original-schema name="foo_db_1" />
+            </table-bound>
+        </table>
+    </flush>
+
+    <flush sql-case-id="flush_tables_with_owner" flush-table="true">
+        <table name="t_product" start-delimiter="`" end-delimiter="`" 
start-index="13" stop-index="34">
+            <owner name="foo_db_2" start-delimiter="`" end-delimiter="`" 
start-index="13" stop-index="22" />
+            <table-bound>
+                <original-database name="foo_db_2" />
+                <original-schema name="foo_db_2" start-delimiter="`" 
end-delimiter="`" />
+            </table-bound>
+        </table>
+    </flush>
+
+    <flush sql-case-id="flush_logs" flush-table="false"></flush>
+
+    <flush sql-case-id="flush_privileges" flush-table="false"></flush>
+</sql-parser-test-cases>
diff --git a/test/it/binder/src/test/resources/sqls/dal/flush.xml 
b/test/it/binder/src/test/resources/sqls/dal/flush.xml
new file mode 100644
index 00000000000..240df68dcef
--- /dev/null
+++ b/test/it/binder/src/test/resources/sqls/dal/flush.xml
@@ -0,0 +1,26 @@
+<?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="flush_tables" value="FLUSH TABLES" db-types="MySQL" />
+    <sql-case id="flush_tables_with_single_table" value="FLUSH TABLES t_order" 
db-types="MySQL" />
+    <sql-case id="flush_tables_with_multiple_tables" value="FLUSH TABLES 
t_order, t_order_item" db-types="MySQL" />
+    <sql-case id="flush_tables_with_owner" value="FLUSH TABLES 
`foo_db_2`.`t_product`" db-types="MySQL" />
+    <sql-case id="flush_logs" value="FLUSH LOGS" db-types="MySQL" />
+    <sql-case id="flush_privileges" value="FLUSH PRIVILEGES" db-types="MySQL" 
/>
+</sql-cases>

Reply via email to