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 5521c8a0f3a Support optimize table sql bind and add test case (#34242)
5521c8a0f3a is described below

commit 5521c8a0f3ab78b15a832c116c9a3877b8a72f90
Author: Chakkk <[email protected]>
AuthorDate: Mon Jan 6 16:21:58 2025 +0800

    Support optimize table sql bind and add test case (#34242)
    
    * support optimize table SQL bind and add test case
    
    * update release note
    
    * Use CaseInsensitiveString static import
---
 RELEASE-NOTES.md                                   |  1 +
 .../infra/binder/engine/SQLBindEngine.java         |  5 ++
 .../dal/OptimizeTableStatementBinder.java          | 51 +++++++++++++++++++++
 .../binder/engine/type/DALStatementBindEngine.java | 53 ++++++++++++++++++++++
 .../test/resources/cases/dal/optimize-table.xml    | 34 ++++++++++++++
 .../src/test/resources/sqls/dal/optimize-table.xml | 21 +++++++++
 6 files changed, 165 insertions(+)

diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md
index 84fff669925..01261a82b65 100644
--- a/RELEASE-NOTES.md
+++ b/RELEASE-NOTES.md
@@ -50,6 +50,7 @@
 1. Agent: Simplify the use of Agent's Docker Image - 
[#33356](https://github.com/apache/shardingsphere/pull/33356)
 1. Mode: Support modifying Hikari-CP configurations via props in standalone 
mode [#34185](https://github.com/apache/shardingsphere/pull/34185)
 1. Encrypt: Support insert statement rewrite use quote 
[#34259](https://github.com/apache/shardingsphere/pull/34259)
+1. SQL Binder: Support optimize table sql bind and add test case - 
[#34242](https://github.com/apache/shardingsphere/pull/34242)
 
 ### Bug Fixes
 
diff --git 
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/SQLBindEngine.java
 
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/SQLBindEngine.java
index 191d4b73382..57dedf85d83 100644
--- 
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/SQLBindEngine.java
+++ 
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/SQLBindEngine.java
@@ -20,12 +20,14 @@ package org.apache.shardingsphere.infra.binder.engine;
 import lombok.RequiredArgsConstructor;
 import 
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
 import 
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContextFactory;
+import 
org.apache.shardingsphere.infra.binder.engine.type.DALStatementBindEngine;
 import 
org.apache.shardingsphere.infra.binder.engine.type.DDLStatementBindEngine;
 import 
org.apache.shardingsphere.infra.binder.engine.type.DMLStatementBindEngine;
 import org.apache.shardingsphere.infra.hint.HintManager;
 import org.apache.shardingsphere.infra.hint.HintValueContext;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.dal.DALStatement;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DDLStatement;
 import 
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DMLStatement;
 
@@ -66,6 +68,9 @@ public final class SQLBindEngine {
         if (statement instanceof DDLStatement) {
             return new DDLStatementBindEngine(metaData, currentDatabaseName, 
hintValueContext).bind((DDLStatement) statement);
         }
+        if (statement instanceof DALStatement) {
+            return new DALStatementBindEngine(metaData, currentDatabaseName, 
hintValueContext).bind((DALStatement) statement);
+        }
         return statement;
     }
 }
diff --git 
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/OptimizeTableStatementBinder.java
 
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/OptimizeTableStatementBinder.java
new file mode 100644
index 00000000000..35d76e80b24
--- /dev/null
+++ 
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/OptimizeTableStatementBinder.java
@@ -0,0 +1,51 @@
+/*
+ * 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 lombok.SneakyThrows;
+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.sql.parser.statement.core.statement.dal.OptimizeTableStatement;
+
+/**
+ * Optimize table statement binder.
+ */
+public final class OptimizeTableStatementBinder implements 
SQLStatementBinder<OptimizeTableStatement> {
+    
+    @Override
+    public OptimizeTableStatement bind(final OptimizeTableStatement 
sqlStatement, final SQLStatementBinderContext binderContext) {
+        OptimizeTableStatement result = copy(sqlStatement);
+        Multimap<CaseInsensitiveString, TableSegmentBinderContext> 
tableBinderContexts = LinkedHashMultimap.create();
+        sqlStatement.getTables().forEach(each -> 
result.getTables().add(SimpleTableSegmentBinder.bind(each, binderContext, 
tableBinderContexts)));
+        return result;
+    }
+    
+    @SneakyThrows(ReflectiveOperationException.class)
+    private static OptimizeTableStatement copy(final OptimizeTableStatement 
sqlStatement) {
+        OptimizeTableStatement result = 
sqlStatement.getClass().getDeclaredConstructor().newInstance();
+        
result.addParameterMarkerSegments(sqlStatement.getParameterMarkerSegments());
+        result.getCommentSegments().addAll(sqlStatement.getCommentSegments());
+        result.getVariableNames().addAll(sqlStatement.getVariableNames());
+        return result;
+    }
+}
diff --git 
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DALStatementBindEngine.java
 
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DALStatementBindEngine.java
new file mode 100644
index 00000000000..2ab63622ff8
--- /dev/null
+++ 
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/engine/type/DALStatementBindEngine.java
@@ -0,0 +1,53 @@
+/*
+ * 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.type;
+
+import lombok.RequiredArgsConstructor;
+import 
org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext;
+import 
org.apache.shardingsphere.infra.binder.engine.statement.dal.OptimizeTableStatementBinder;
+import org.apache.shardingsphere.infra.hint.HintValueContext;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.dal.DALStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.dal.OptimizeTableStatement;
+
+/**
+ * DAL statement bind engine.
+ */
+@RequiredArgsConstructor
+public final class DALStatementBindEngine {
+    
+    private final ShardingSphereMetaData metaData;
+    
+    private final String currentDatabaseName;
+    
+    private final HintValueContext hintValueContext;
+    
+    /**
+     * Bind DAL statement.
+     *
+     * @param statement to be bound DAL statement
+     * @return bound DAL statement
+     */
+    public DALStatement bind(final DALStatement statement) {
+        SQLStatementBinderContext binderContext = new 
SQLStatementBinderContext(metaData, currentDatabaseName, hintValueContext, 
statement);
+        if (statement instanceof OptimizeTableStatement) {
+            return new 
OptimizeTableStatementBinder().bind((OptimizeTableStatement) statement, 
binderContext);
+        }
+        return statement;
+    }
+}
diff --git a/test/it/binder/src/test/resources/cases/dal/optimize-table.xml 
b/test/it/binder/src/test/resources/cases/dal/optimize-table.xml
new file mode 100644
index 00000000000..82e015a5c1b
--- /dev/null
+++ b/test/it/binder/src/test/resources/cases/dal/optimize-table.xml
@@ -0,0 +1,34 @@
+<?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>
+    <optimize-table sql-case-id="optimize_table">
+        <table name="t_order" start-index="15" stop-index="21">
+            <table-bound>
+                <original-database name="foo_db_1" />
+                <original-schema name="foo_db_1" />
+            </table-bound>
+        </table>
+        <table name="t_user" start-index="24" stop-index="29">
+            <table-bound>
+                <original-database name="foo_db_1" />
+                <original-schema name="foo_db_1" />
+            </table-bound>
+        </table>
+    </optimize-table>
+</sql-parser-test-cases>
diff --git a/test/it/binder/src/test/resources/sqls/dal/optimize-table.xml 
b/test/it/binder/src/test/resources/sqls/dal/optimize-table.xml
new file mode 100644
index 00000000000..1fc86840acd
--- /dev/null
+++ b/test/it/binder/src/test/resources/sqls/dal/optimize-table.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="optimize_table" value="OPTIMIZE TABLE t_order, t_user" 
db-types="MySQL,Doris" />
+</sql-cases>

Reply via email to