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 39a9609451c Enhance SQLUtils getExpressionWithoutOutsideParentheses 
(#34804)
39a9609451c is described below

commit 39a9609451cc53dfa0ba646c13670e64e8d3cff9
Author: Yash-cor <[email protected]>
AuthorDate: Sun Nov 30 12:45:14 2025 +0530

    Enhance SQLUtils getExpressionWithoutOutsideParentheses (#34804)
    
    * added parser test cases for MySQL.
    
    * Made changes in SQLUtils getExpressionWithoutOutsideParentheses it gives 
wrong text output.
    
    * Merge with master and refactor getParenthesesOffset in SQLUtils.
---
 .../sql/parser/statement/core/util/SQLUtils.java   | 40 ++++++++++++++++++----
 .../src/main/resources/case/dml/select-join.xml    | 10 +++---
 .../resources/case/dml/select-special-function.xml |  2 +-
 .../parser/src/main/resources/case/dml/select.xml  |  2 +-
 4 files changed, 41 insertions(+), 13 deletions(-)

diff --git 
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/util/SQLUtils.java
 
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/util/SQLUtils.java
index 5007c24ca25..649022d9475 100644
--- 
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/util/SQLUtils.java
+++ 
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/util/SQLUtils.java
@@ -175,18 +175,25 @@ public final class SQLUtils {
      */
     public static String getExpressionWithoutOutsideParentheses(final String 
value) {
         int parenthesesOffset = getParenthesesOffset(value);
-        return 0 == parenthesesOffset ? value : 
value.substring(parenthesesOffset, value.length() - parenthesesOffset);
+        String result = 0 == parenthesesOffset ? value : 
value.substring(parenthesesOffset, value.length() - parenthesesOffset);
+        if (isValidParenthesis(result)) {
+            return result;
+        }
+        return value;
     }
     
     private static int getParenthesesOffset(final String value) {
-        int result = 0;
+        int left = 0;
         if (Strings.isNullOrEmpty(value)) {
-            return result;
+            return left;
         }
-        while (Paren.PARENTHESES.getLeftParen() == value.charAt(result)) {
-            result++;
+        
+        int right = value.length() - 1;
+        while (Paren.PARENTHESES.getLeftParen() == value.charAt(left) && 
Paren.PARENTHESES.getRightParen() == value.charAt(right)) {
+            left++;
+            right--;
         }
-        return result;
+        return left;
     }
     
     /**
@@ -278,4 +285,25 @@ public final class SQLUtils {
         }
         return result.trim();
     }
+    
+    /**
+     * Check for valid parenthesis in String.
+     *
+     * @param text to be checked for valid parenthesis
+     * @return true or false
+     */
+    public static boolean isValidParenthesis(final String text) {
+        int count = 0;
+        for (char c : text.toCharArray()) {
+            if (Paren.PARENTHESES.getLeftParen() == c) {
+                count++;
+            } else if (Paren.PARENTHESES.getRightParen() == c) {
+                if (count == 0) {
+                    return false;
+                }
+                count--;
+            }
+        }
+        return count == 0;
+    }
 }
diff --git a/test/it/parser/src/main/resources/case/dml/select-join.xml 
b/test/it/parser/src/main/resources/case/dml/select-join.xml
index 4433fdd5e49..983e33483a5 100644
--- a/test/it/parser/src/main/resources/case/dml/select-join.xml
+++ b/test/it/parser/src/main/resources/case/dml/select-join.xml
@@ -1388,7 +1388,7 @@
             <shorthand-projection name="*" start-index="86" stop-index="103">
                 <owner name="planForceDetails" start-index="86" 
stop-index="101"/>
             </shorthand-projection>
-            <expression-projection start-index="106" stop-index="255" 
text="regressedPlanExecutionCount + recommendedPlanExecutionCount) * 
(regressedPlanCpuTimeAverage - recommendedPlanCpuTimeAverage)/100000" 
alias="estimated_gain">
+            <expression-projection start-index="106" stop-index="255" 
text="(regressedPlanExecutionCount + recommendedPlanExecutionCount) * 
(regressedPlanCpuTimeAverage - recommendedPlanCpuTimeAverage)/1000000" 
alias="estimated_gain">
                 <expr>
                     <binary-operation-expression start-index="123" 
stop-index="255">
                         <left>
@@ -2116,7 +2116,7 @@
             <column-projection name="text" start-index="7" stop-index="12">
                 <owner name="t" start-index="7" stop-index="7"/>
             </column-projection>
-            <expression-projection start-index="15" stop-index="83" 
text="qs.total_elapsed_time/1000) / qs.execution_coun" alias="avg_elapsed_time">
+            <expression-projection start-index="15" stop-index="83" 
text="(qs.total_elapsed_time/1000) / qs.execution_count" 
alias="avg_elapsed_time">
                 <expr>
                     <binary-operation-expression start-index="15" 
stop-index="63">
                         <left>
@@ -2141,7 +2141,7 @@
                     </binary-operation-expression>
                 </expr>
             </expression-projection>
-            <expression-projection text="qs.total_worker_time/1000) / 
qs.execution_coun" start-index="86" stop-index="149" alias="avg_cpu_time">
+            <expression-projection text="(qs.total_worker_time/1000) / 
qs.execution_count" start-index="86" stop-index="149" alias="avg_cpu_time">
                 <expr>
                     <binary-operation-expression start-index="86" 
stop-index="133">
                         <left>
@@ -2166,7 +2166,7 @@
                     </binary-operation-expression>
                 </expr>
             </expression-projection>
-            <expression-projection text="qs.total_elapsed_time/1000) / 
qs.execution_count ) - ((qs.total_worker_time/1000) / qs.execution_coun" 
start-index="152" stop-index="273" alias="avg_wait_time">
+            <expression-projection text="((qs.total_elapsed_time/1000) / 
qs.execution_count ) - ((qs.total_worker_time/1000) / qs.execution_count)" 
start-index="152" stop-index="273" alias="avg_wait_time">
                 <expr>
                     <binary-operation-expression start-index="152" 
stop-index="256">
                         <left>
@@ -2823,7 +2823,7 @@
             <shorthand-projection start-index="83" stop-index="100">
                 <owner name="planForceDetails" start-index="83" 
stop-index="98" />
             </shorthand-projection>
-            <expression-projection alias="estimated_gain" 
text="regressedPlanExecutionCount + recommendedPlanExecutionCount) * 
(regressedPlanCpuTimeAverage - recommendedPlanCpuTimeAverage) / 100000" 
start-index="102" stop-index="253">
+            <expression-projection alias="estimated_gain" 
text="(regressedPlanExecutionCount + recommendedPlanExecutionCount) * 
(regressedPlanCpuTimeAverage - recommendedPlanCpuTimeAverage) / 1000000" 
start-index="102" stop-index="253">
                 <expr>
                     <binary-operation-expression start-index="119" 
stop-index="253">
                         <left>
diff --git 
a/test/it/parser/src/main/resources/case/dml/select-special-function.xml 
b/test/it/parser/src/main/resources/case/dml/select-special-function.xml
index 1f5c4f62a95..980c53490c4 100644
--- a/test/it/parser/src/main/resources/case/dml/select-special-function.xml
+++ b/test/it/parser/src/main/resources/case/dml/select-special-function.xml
@@ -5049,7 +5049,7 @@
 
     <select sql-case-id="select_not_in">
         <projections start-index="7" stop-index="33">
-            <expression-projection start-index="7" stop-index="33" text="3,4) 
NOT IN ((1,2), (3,4)">
+            <expression-projection start-index="7" stop-index="33" text="(3,4) 
NOT IN ((1,2), (3,4))">
                 <expr>
                     <in-expression start-index="7" stop-index="33">
                         <left>
diff --git a/test/it/parser/src/main/resources/case/dml/select.xml 
b/test/it/parser/src/main/resources/case/dml/select.xml
index 8e9f03d0641..a5bb4cca31b 100644
--- a/test/it/parser/src/main/resources/case/dml/select.xml
+++ b/test/it/parser/src/main/resources/case/dml/select.xml
@@ -7698,7 +7698,7 @@
             <column-projection name="empid" start-index="75" stop-index="81" 
literal-start-index="75" literal-stop-index="81">`
                 <owner name="a" start-index="75" stop-index="75" 
literal-start-index="75" literal-stop-index="75" />
             </column-projection>
-            <expression-projection text="a.enddate - term + term2 + 1) / 
(last_day(term) - term + 1" alias="cnt" start-index="8" stop-index="72" 
literal-start-index="8" literal-stop-index="72">
+            <expression-projection text="(a.enddate - term + term2 + 1) / 
(last_day(term) - term + 1)" alias="cnt" start-index="8" stop-index="72" 
literal-start-index="8" literal-stop-index="72">
                 <literalText>a.enddate - term + term2 + 1) / (last_day(term) - 
term + 1</literalText>
                 <expr>
                     <binary-operation-expression start-index="8" 
stop-index="67" literal-start-index="8" literal-stop-index="67">

Reply via email to