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 e697848af8e Add test for explain statement SQL bind (#36217)
e697848af8e is described below
commit e697848af8e12bd67f41bb36be10788f700b2150
Author: Chakkk <[email protected]>
AuthorDate: Mon Aug 11 16:56:42 2025 +0800
Add test for explain statement SQL bind (#36217)
* support explain statement SQL bind
* fix checkstyle
---
.../statement/dal/ExplainStatementBinderTest.java | 97 ++++++++++++++++++++++
.../src/test/resources/cases/dal/explain.xml | 47 +++++++++++
.../binder/src/test/resources/sqls/dal/explain.xml | 21 +++++
3 files changed, 165 insertions(+)
diff --git
a/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/ExplainStatementBinderTest.java
b/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/ExplainStatementBinderTest.java
new file mode 100644
index 00000000000..91a295c92a4
--- /dev/null
+++
b/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/engine/statement/dal/ExplainStatementBinderTest.java
@@ -0,0 +1,97 @@
+/*
+ * 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
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.statement.SQLStatement;
+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.dml.DMLStatement;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+@ExtendWith(MockitoExtension.class)
+class ExplainStatementBinderTest {
+
+ 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 assertBindWithDMLStatement() {
+ DMLStatement dmlStatement = new TestDMLStatement(databaseType);
+ ExplainStatement original = new ExplainStatement(databaseType,
dmlStatement);
+ HintValueContext hintValueContext = new HintValueContext();
+ hintValueContext.setSkipMetadataValidate(true);
+ SQLStatementBinderContext binderContext = new
SQLStatementBinderContext(metaData, "foo_db", hintValueContext, original);
+ ExplainStatement actual = new ExplainStatementBinder().bind(original,
binderContext);
+ assertNotSame(original, actual);
+ assertThat(actual.getExplainableSQLStatement().getClass(),
is(TestDMLStatement.class));
+ assertSame(dmlStatement, actual.getExplainableSQLStatement());
+ }
+
+ @Test
+ void assertBindWithNonDMLStatement() {
+ SQLStatement nonDMLStatement = new TestNonDMLStatement(databaseType);
+ ExplainStatement original = new ExplainStatement(databaseType,
nonDMLStatement);
+ HintValueContext hintValueContext = new HintValueContext();
+ hintValueContext.setSkipMetadataValidate(true);
+ SQLStatementBinderContext binderContext = new
SQLStatementBinderContext(metaData, "foo_db", hintValueContext, original);
+ ExplainStatement actual = new ExplainStatementBinder().bind(original,
binderContext);
+ assertNotSame(original, actual);
+ assertSame(nonDMLStatement, actual.getExplainableSQLStatement());
+ }
+
+ private static final class TestDMLStatement extends DMLStatement {
+
+ TestDMLStatement(final DatabaseType databaseType) {
+ super(databaseType);
+ }
+ }
+
+ private static final class TestNonDMLStatement extends DALStatement {
+
+ TestNonDMLStatement(final DatabaseType databaseType) {
+ super(databaseType);
+ }
+ }
+}
diff --git a/test/it/binder/src/test/resources/cases/dal/explain.xml
b/test/it/binder/src/test/resources/cases/dal/explain.xml
new file mode 100644
index 00000000000..e382ccdb162
--- /dev/null
+++ b/test/it/binder/src/test/resources/cases/dal/explain.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>
+ <explain sql-case-id="explain_select_from_table">
+ <projections start-index="8" stop-index="8">
+ <shorthand-projection start-index="8" stop-index="8" />
+ </projections>
+ <from>
+ <simple-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>
+ </simple-table>
+ </from>
+ <where start-index="23" stop-index="36" literal-stop-index="36">
+ <expr>
+ <binary-operation-expression start-index="29" stop-index="36"
literal-stop-index="36">
+ <left>
+ <column name="order_id" start-index="29"
stop-index="36" />
+ </left>
+ <operator>=</operator>
+ <right>
+ <literal-expression value="1" start-index="39"
stop-index="39" />
+ <parameter-marker-expression parameter-index="0"
start-index="39" stop-index="39" />
+ </right>
+ </binary-operation-expression>
+ </expr>
+ </where>
+ </explain>
+</sql-parser-test-cases>
diff --git a/test/it/binder/src/test/resources/sqls/dal/explain.xml
b/test/it/binder/src/test/resources/sqls/dal/explain.xml
new file mode 100644
index 00000000000..c2e262ad53e
--- /dev/null
+++ b/test/it/binder/src/test/resources/sqls/dal/explain.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="explain_select_from_table" value="EXPLAIN SELECT * FROM
t_order WHERE order_id = 1" db-types="MySQL,PostgreSQL,openGauss,SQLServer" />
+</sql-cases>