This is an automated email from the ASF dual-hosted git repository.
jianglongtao 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 437c447139e Fix alter sql_parser rule not refresh parser engine and
support preview hint with sqlcommentParseEnabled=false. (#26664)
437c447139e is described below
commit 437c447139e0367b043eeb7846ad58e2deb3f60e
Author: Chuxin Chen <[email protected]>
AuthorDate: Mon Jul 3 14:22:02 2023 +0800
Fix alter sql_parser rule not refresh parser engine and support preview
hint with sqlcommentParseEnabled=false. (#26664)
* Fix alter sql_parser rule not refresh parser engine.
* support preview hint with sqlCommentParseEnabled=false
---
.../infra/parser/sql/SQLStatementParserEngine.java | 13 +++++++
.../sql/SQLStatementParserEngineFactory.java | 3 ++
.../sql/SQLStatementParserEngineFactoryTest.java | 41 ++++++++++++++++++++++
.../shardingsphere/sql/parser/api/CacheOption.java | 2 ++
.../handler/distsql/rul/sql/PreviewExecutor.java | 8 +++--
...preview_sql_hint_comment_parse_enable_false.xml | 24 +++++++++++++
.../resources/cases/ral/ral-integration-alter.xml | 26 ++++++++++++++
7 files changed, 115 insertions(+), 2 deletions(-)
diff --git
a/infra/parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngine.java
b/infra/parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngine.java
index 1449105de25..f0ba50a17b2 100644
---
a/infra/parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngine.java
+++
b/infra/parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngine.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.infra.parser.sql;
import com.github.benmanes.caffeine.cache.LoadingCache;
+import lombok.Getter;
import org.apache.shardingsphere.infra.parser.cache.SQLStatementCacheBuilder;
import org.apache.shardingsphere.sql.parser.api.CacheOption;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
@@ -31,9 +32,21 @@ public final class SQLStatementParserEngine {
private final LoadingCache<String, SQLStatement> sqlStatementCache;
+ @Getter
+ private final CacheOption sqlStatementCacheOption;
+
+ @Getter
+ private final CacheOption parseTreeCacheOption;
+
+ @Getter
+ private final boolean isParseComment;
+
public SQLStatementParserEngine(final String databaseType, final
CacheOption sqlStatementCacheOption, final CacheOption parseTreeCacheOption,
final boolean isParseComment) {
sqlStatementParserExecutor = new
SQLStatementParserExecutor(databaseType, parseTreeCacheOption, isParseComment);
sqlStatementCache = SQLStatementCacheBuilder.build(databaseType,
sqlStatementCacheOption, parseTreeCacheOption, isParseComment);
+ this.sqlStatementCacheOption = sqlStatementCacheOption;
+ this.parseTreeCacheOption = parseTreeCacheOption;
+ this.isParseComment = isParseComment;
}
/**
diff --git
a/infra/parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngineFactory.java
b/infra/parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngineFactory.java
index 7f14ed1aa6e..e7e5de1abdd 100644
---
a/infra/parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngineFactory.java
+++
b/infra/parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngineFactory.java
@@ -46,6 +46,9 @@ public final class SQLStatementParserEngineFactory {
SQLStatementParserEngine result = ENGINES.get(databaseType);
if (null == result) {
result = ENGINES.computeIfAbsent(databaseType, key -> new
SQLStatementParserEngine(key, sqlStatementCacheOption, parseTreeCacheOption,
isParseComment));
+ } else if
(!result.getSqlStatementCacheOption().equals(sqlStatementCacheOption) ||
!result.getParseTreeCacheOption().equals(parseTreeCacheOption)
+ || result.isParseComment() != isParseComment) {
+ ENGINES.put(databaseType, result = new
SQLStatementParserEngine(databaseType, sqlStatementCacheOption,
parseTreeCacheOption, isParseComment));
}
return result;
}
diff --git
a/infra/parser/src/test/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngineFactoryTest.java
b/infra/parser/src/test/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngineFactoryTest.java
new file mode 100644
index 00000000000..e7f219d4ecb
--- /dev/null
+++
b/infra/parser/src/test/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngineFactoryTest.java
@@ -0,0 +1,41 @@
+/*
+ * 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.parser.sql;
+
+import org.apache.shardingsphere.sql.parser.api.CacheOption;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+class SQLStatementParserEngineFactoryTest {
+
+ @Test
+ void assertGetSQLStatementParserEngineNotSame() {
+ SQLStatementParserEngine before =
SQLStatementParserEngineFactory.getSQLStatementParserEngine("MySQL", new
CacheOption(2000, 65535L), new CacheOption(128, 1024L), false);
+ SQLStatementParserEngine after =
SQLStatementParserEngineFactory.getSQLStatementParserEngine("MySQL", new
CacheOption(2000, 65535L), new CacheOption(128, 1024L), true);
+ assertNotSame(before, after);
+ }
+
+ @Test
+ void assertGetSQLStatementParserEngineSame() {
+ SQLStatementParserEngine before =
SQLStatementParserEngineFactory.getSQLStatementParserEngine("MySQL", new
CacheOption(2000, 65535L), new CacheOption(128, 1024L), false);
+ SQLStatementParserEngine after =
SQLStatementParserEngineFactory.getSQLStatementParserEngine("MySQL", new
CacheOption(2000, 65535L), new CacheOption(128, 1024L), false);
+ assertSame(before, after);
+ }
+}
diff --git
a/parser/sql/engine/src/main/java/org/apache/shardingsphere/sql/parser/api/CacheOption.java
b/parser/sql/engine/src/main/java/org/apache/shardingsphere/sql/parser/api/CacheOption.java
index 07038235229..a4720c3301b 100644
---
a/parser/sql/engine/src/main/java/org/apache/shardingsphere/sql/parser/api/CacheOption.java
+++
b/parser/sql/engine/src/main/java/org/apache/shardingsphere/sql/parser/api/CacheOption.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.sql.parser.api;
+import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@@ -25,6 +26,7 @@ import lombok.RequiredArgsConstructor;
*/
@RequiredArgsConstructor
@Getter
+@EqualsAndHashCode
public final class CacheOption {
private final int initialCapacity;
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/PreviewExecutor.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/PreviewExecutor.java
index 07f9084f0ef..b38ff1239eb 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/PreviewExecutor.java
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/PreviewExecutor.java
@@ -43,6 +43,8 @@ import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.dr
import
org.apache.shardingsphere.infra.executor.sql.prepare.driver.DriverExecutionPrepareEngine;
import
org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.JDBCDriverType;
import
org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.StatementOption;
+import org.apache.shardingsphere.infra.hint.HintValueContext;
+import org.apache.shardingsphere.infra.hint.SQLHintUtils;
import
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
@@ -91,9 +93,11 @@ public final class PreviewExecutor implements
ConnectionSessionRequiredRULExecut
String databaseType =
DatabaseTypeEngine.getTrunkDatabaseTypeName(metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType());
ShardingSphereRuleMetaData globalRuleMetaData =
metaDataContexts.getMetaData().getGlobalRuleMetaData();
SQLParserRule sqlParserRule =
globalRuleMetaData.getSingleRule(SQLParserRule.class);
- SQLStatement previewedStatement =
sqlParserRule.getSQLParserEngine(databaseType).parse(sqlStatement.getSql(),
false);
+ String sql = sqlParserRule.isSqlCommentParseEnabled() ?
sqlStatement.getSql() : SQLHintUtils.removeHint(sqlStatement.getSql());
+ SQLStatement previewedStatement =
sqlParserRule.getSQLParserEngine(databaseType).parse(sql, false);
SQLStatementContext sqlStatementContext =
SQLStatementContextFactory.newInstance(metaDataContexts.getMetaData(),
previewedStatement, databaseName);
- QueryContext queryContext = new QueryContext(sqlStatementContext,
sqlStatement.getSql(), Collections.emptyList());
+ HintValueContext hintValueContext =
sqlParserRule.isSqlCommentParseEnabled() ? new HintValueContext() :
SQLHintUtils.extractHint(sqlStatement.getSql()).orElseGet(HintValueContext::new);
+ QueryContext queryContext = new QueryContext(sqlStatementContext, sql,
Collections.emptyList(), hintValueContext);
connectionSession.setQueryContext(queryContext);
if (sqlStatementContext instanceof CursorAvailable &&
sqlStatementContext instanceof CursorDefinitionAware) {
setUpCursorDefinition(sqlStatementContext, connectionSession);
diff --git
a/test/e2e/sql/src/test/resources/cases/ral/dataset/empty_rules/preview_sql_hint_comment_parse_enable_false.xml
b/test/e2e/sql/src/test/resources/cases/ral/dataset/empty_rules/preview_sql_hint_comment_parse_enable_false.xml
new file mode 100644
index 00000000000..ee49f0d6735
--- /dev/null
+++
b/test/e2e/sql/src/test/resources/cases/ral/dataset/empty_rules/preview_sql_hint_comment_parse_enable_false.xml
@@ -0,0 +1,24 @@
+<!--
+ ~ 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.
+ -->
+
+<dataset>
+ <metadata>
+ <column name="data_source_name" />
+ <column name="actual_sql" />
+ </metadata>
+ <row values="write_ds_0| SELECT * FROM t_user_item" />
+</dataset>
diff --git
a/test/e2e/sql/src/test/resources/cases/ral/ral-integration-alter.xml
b/test/e2e/sql/src/test/resources/cases/ral/ral-integration-alter.xml
new file mode 100644
index 00000000000..b298bc81fdd
--- /dev/null
+++ b/test/e2e/sql/src/test/resources/cases/ral/ral-integration-alter.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.
+ -->
+
+<integration-test-cases>
+ <test-case sql="ALTER SQL_PARSER RULE (SQL_COMMENT_PARSE_ENABLED=false,
PARSE_TREE_CACHE(INITIAL_CAPACITY=128, MAXIMUM_SIZE=1024),
SQL_STATEMENT_CACHE(INITIAL_CAPACITY=2000, MAXIMUM_SIZE=65535));">
+ <assertion
expected-data-file="preview_sql_hint_comment_parse_enable_false.xml">
+ <assertion-sql sql="PREVIEW /* SHARDINGSPHERE_HINT:
DATA_SOURCE_NAME = write_ds_0, SKIP_SQL_REWRITE = true */ SELECT * FROM
t_user_item" />
+ <destroy-sql sql="ALTER SQL_PARSER RULE
(SQL_COMMENT_PARSE_ENABLED=true, PARSE_TREE_CACHE(INITIAL_CAPACITY=128,
MAXIMUM_SIZE=1024), SQL_STATEMENT_CACHE(INITIAL_CAPACITY=2000,
MAXIMUM_SIZE=65535));" />
+ </assertion>
+ </test-case>
+</integration-test-cases>