You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2026/08/31 16:22:47 UTC

(shardingsphere) branch master updated: Add DistSQLStringUtils (#39670)

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

terrymanu 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 d7a7b797485 Add DistSQLStringUtils (#39670)
d7a7b797485 is described below

commit d7a7b79748587328f6017c6d695bcea61fe8e51b
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Tue Sep 1 00:22:39 2026 +0800

    Add DistSQLStringUtils (#39670)
---
 .../core/kernel/KernelDistSQLStatementVisitor.java | 65 +--------------
 .../distsql/parser/util/DistSQLStringUtils.java    | 96 ++++++++++++++++++++++
 .../parser/util/DistSQLStringUtilsTest.java        | 53 ++++++++++++
 3 files changed, 151 insertions(+), 63 deletions(-)

diff --git a/parser/distsql/engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java b/parser/distsql/engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java
index 912f0049f2b..1375f3746e1 100644
--- a/parser/distsql/engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java
+++ b/parser/distsql/engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java
@@ -61,6 +61,7 @@ import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementPa
 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.StorageUnitsDefinitionContext;
 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.UnlockClusterContext;
 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.UnregisterStorageUnitContext;
+import org.apache.shardingsphere.distsql.parser.util.DistSQLStringUtils;
 import org.apache.shardingsphere.distsql.segment.AlgorithmSegment;
 import org.apache.shardingsphere.distsql.segment.DataSourceSegment;
 import org.apache.shardingsphere.distsql.segment.HostnameAndPortBasedDataSourceSegment;
@@ -139,7 +140,7 @@ public final class KernelDistSQLStatementVisitor extends KernelDistSQLStatementB
     @Override
     public ASTNode visitStorageUnitDefinition(final StorageUnitDefinitionContext ctx) {
         String user = IdentifierValueUtils.getValue(ctx.user());
-        String password = null == ctx.password() ? "" : new StringLiteralValue(replaceStandardEscapes(ctx.password().getText())).getValue();
+        String password = null == ctx.password() ? "" : new StringLiteralValue(DistSQLStringUtils.replaceStandardEscapes(ctx.password().getText())).getValue();
         Properties props = getProperties(ctx.propertiesDefinition());
         return null == ctx.urlSource()
                 ? new HostnameAndPortBasedDataSourceSegment(IdentifierValueUtils.getValue(ctx.storageUnitName()),
@@ -148,68 +149,6 @@ public final class KernelDistSQLStatementVisitor extends KernelDistSQLStatementB
                 : new URLBasedDataSourceSegment(IdentifierValueUtils.getValue(ctx.storageUnitName()), IdentifierValueUtils.getValue(ctx.urlSource().url()), user, password, props);
     }
     
-    private String replaceStandardEscapes(final String value) {
-        if (!value.contains("\\")) {
-            return value;
-        }
-        StringBuilder result = new StringBuilder(value.length());
-        int index = 0;
-        while (index < value.length()) {
-            if ('\\' != value.charAt(index)) {
-                result.append(value.charAt(index++));
-                continue;
-            }
-            int backslashEndIndex = index;
-            while (backslashEndIndex < value.length() && '\\' == value.charAt(backslashEndIndex)) {
-                backslashEndIndex++;
-            }
-            int backslashCount = backslashEndIndex - index;
-            int backslashPairCount = backslashCount / 2;
-            while (0 < backslashPairCount--) {
-                result.append('\\');
-            }
-            if (0 != backslashCount % 2) {
-                if (backslashEndIndex < value.length() && appendStandardEscape(value.charAt(backslashEndIndex), result)) {
-                    backslashEndIndex++;
-                } else {
-                    result.append('\\');
-                }
-            }
-            index = backslashEndIndex;
-        }
-        return result.toString();
-    }
-    
-    private boolean appendStandardEscape(final char value, final StringBuilder result) {
-        switch (value) {
-            case 'b':
-                result.append('\b');
-                break;
-            case 't':
-                result.append('\t');
-                break;
-            case 'n':
-                result.append('\n');
-                break;
-            case 'f':
-                result.append('\f');
-                break;
-            case 'r':
-                result.append('\r');
-                break;
-            case 's':
-                result.append(' ');
-                break;
-            case '"':
-            case '\'':
-                result.append(value);
-                break;
-            default:
-                return false;
-        }
-        return true;
-    }
-    
     @Override
     public ASTNode visitShowComputeNodes(final ShowComputeNodesContext ctx) {
         return new ShowComputeNodesStatement();
diff --git a/parser/distsql/engine/src/main/java/org/apache/shardingsphere/distsql/parser/util/DistSQLStringUtils.java b/parser/distsql/engine/src/main/java/org/apache/shardingsphere/distsql/parser/util/DistSQLStringUtils.java
new file mode 100644
index 00000000000..5740aa19733
--- /dev/null
+++ b/parser/distsql/engine/src/main/java/org/apache/shardingsphere/distsql/parser/util/DistSQLStringUtils.java
@@ -0,0 +1,96 @@
+/*
+ * 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.distsql.parser.util;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+/**
+ * DistSQL string utilities.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class DistSQLStringUtils {
+    
+    /**
+     * Replace standard escapes.
+     *
+     * @param value value to be replaced
+     * @return replaced value
+     */
+    public static String replaceStandardEscapes(final String value) {
+        if (!value.contains("\\")) {
+            return value;
+        }
+        StringBuilder result = new StringBuilder(value.length());
+        int index = 0;
+        while (index < value.length()) {
+            if ('\\' != value.charAt(index)) {
+                result.append(value.charAt(index++));
+                continue;
+            }
+            int backslashEndIndex = index;
+            while (backslashEndIndex < value.length() && '\\' == value.charAt(backslashEndIndex)) {
+                backslashEndIndex++;
+            }
+            int backslashCount = backslashEndIndex - index;
+            int backslashPairCount = backslashCount / 2;
+            while (0 < backslashPairCount--) {
+                result.append('\\');
+            }
+            if (0 != backslashCount % 2) {
+                if (backslashEndIndex < value.length() && appendStandardEscape(value.charAt(backslashEndIndex), result)) {
+                    backslashEndIndex++;
+                } else {
+                    result.append('\\');
+                }
+            }
+            index = backslashEndIndex;
+        }
+        return result.toString();
+    }
+    
+    private static boolean appendStandardEscape(final char value, final StringBuilder result) {
+        switch (value) {
+            case 'b':
+                result.append('\b');
+                break;
+            case 't':
+                result.append('\t');
+                break;
+            case 'n':
+                result.append('\n');
+                break;
+            case 'f':
+                result.append('\f');
+                break;
+            case 'r':
+                result.append('\r');
+                break;
+            case 's':
+                result.append(' ');
+                break;
+            case '"':
+            case '\'':
+                result.append(value);
+                break;
+            default:
+                return false;
+        }
+        return true;
+    }
+}
diff --git a/parser/distsql/engine/src/test/java/org/apache/shardingsphere/distsql/parser/util/DistSQLStringUtilsTest.java b/parser/distsql/engine/src/test/java/org/apache/shardingsphere/distsql/parser/util/DistSQLStringUtilsTest.java
new file mode 100644
index 00000000000..a8829fbbef7
--- /dev/null
+++ b/parser/distsql/engine/src/test/java/org/apache/shardingsphere/distsql/parser/util/DistSQLStringUtilsTest.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.distsql.parser.util;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.util.stream.Stream;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+class DistSQLStringUtilsTest {
+    
+    @ParameterizedTest(name = "{0}")
+    @MethodSource("standardEscapesArguments")
+    void assertReplaceStandardEscapes(final String input, final String expected) {
+        String actual = DistSQLStringUtils.replaceStandardEscapes(input);
+        assertThat(actual, is(expected));
+    }
+    
+    private static Stream<Arguments> standardEscapesArguments() {
+        return Stream.of(
+                Arguments.of("foo", "foo"),
+                Arguments.of("\\b", "\b"),
+                Arguments.of("\\t", "\t"),
+                Arguments.of("\\n", "\n"),
+                Arguments.of("\\f", "\f"),
+                Arguments.of("\\r", "\r"),
+                Arguments.of("\\s", " "),
+                Arguments.of("\\\"", "\""),
+                Arguments.of("\\'", "'"),
+                Arguments.of("\\x", "\\x"),
+                Arguments.of("\\\\", "\\"),
+                Arguments.of("\\", "\\"));
+    }
+}