You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2025/10/13 09:35:38 UTC

(shardingsphere) branch master updated: Support substring negative parameter sql parsing (#36872)

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 3b0de6d783a Support substring negative parameter sql parsing (#36872)
3b0de6d783a is described below

commit 3b0de6d783a229cb4d0c0e69bc64aa8394330d79
Author: ZhangCheng <ch...@apache.org>
AuthorDate: Mon Oct 13 17:35:32 2025 +0800

    Support substring negative parameter sql parsing (#36872)
    
    * Support substring negative parameter sql parsing
    
    * Support substring negative parameter sql parsing
---
 .../dialect/mysql/src/main/antlr4/imports/mysql/BaseRule.g4   |  8 ++++++--
 .../engine/mysql/visitor/statement/MySQLStatementVisitor.java | 10 ++++++++--
 .../src/main/resources/case/dml/select-special-function.xml   | 11 +++++++----
 .../resources/sql/supported/dml/select-special-function.xml   |  2 +-
 4 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/parser/sql/engine/dialect/mysql/src/main/antlr4/imports/mysql/BaseRule.g4 b/parser/sql/engine/dialect/mysql/src/main/antlr4/imports/mysql/BaseRule.g4
index 2e4b7b57a39..920c50a3dce 100644
--- a/parser/sql/engine/dialect/mysql/src/main/antlr4/imports/mysql/BaseRule.g4
+++ b/parser/sql/engine/dialect/mysql/src/main/antlr4/imports/mysql/BaseRule.g4
@@ -682,8 +682,12 @@ positionFunction
     ;
 
 substringFunction
-    : (SUBSTRING | SUBSTR | MID) LP_ expr FROM NUMBER_ (FOR NUMBER_)? RP_
-    | (SUBSTRING | SUBSTR | MID) LP_ expr COMMA_ NUMBER_ (COMMA_ NUMBER_)? RP_
+    : (SUBSTRING | SUBSTR | MID) LP_ expr FROM substringParam (FOR substringParam)? RP_
+    | (SUBSTRING | SUBSTR | MID) LP_ expr COMMA_ substringParam (COMMA_ substringParam)? RP_
+    ;
+
+substringParam
+    : numberLiterals | expr
     ;
 
 extractFunction
diff --git a/parser/sql/engine/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/engine/mysql/visitor/statement/MySQLStatementVisitor.java b/parser/sql/engine/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/engine/mysql/visitor/statement/MySQLStatementVisitor.java
index 033890a1754..b515aaeb1fa 100644
--- a/parser/sql/engine/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/engine/mysql/visitor/statement/MySQLStatementVisitor.java
+++ b/parser/sql/engine/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/engine/mysql/visitor/statement/MySQLStatementVisitor.java
@@ -129,6 +129,7 @@ import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.StringL
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.String_Context;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SubqueryContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SubstringFunctionContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SubstringParamContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SystemVariableContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableAliasRefListContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableFactorContext;
@@ -1175,8 +1176,13 @@ public abstract class MySQLStatementVisitor extends MySQLStatementBaseVisitor<AS
     public final ASTNode visitSubstringFunction(final SubstringFunctionContext ctx) {
         FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), getFunctionName(ctx), getOriginalText(ctx));
         result.getParameters().add((ExpressionSegment) visit(ctx.expr()));
-        for (TerminalNode each : ctx.NUMBER_()) {
-            result.getParameters().add(new LiteralExpressionSegment(each.getSymbol().getStartIndex(), each.getSymbol().getStopIndex(), new NumberLiteralValue(each.getText()).getValue()));
+        for (SubstringParamContext each : ctx.substringParam()) {
+            if (null != each.numberLiterals()) {
+                result.getParameters()
+                        .add(new LiteralExpressionSegment(each.numberLiterals().start.getStartIndex(), each.numberLiterals().stop.getStopIndex(), new NumberLiteralValue(each.getText()).getValue()));
+            } else if (null != each.expr()) {
+                result.getParameters().add((ExpressionSegment) visit(each.expr()));
+            }
         }
         return result;
     }
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 7cc3cf4d461..1f5c4f62a95 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
@@ -199,15 +199,18 @@
         </projections>
     </select>
     <select sql-case-id="select_substring">
-        <projections start-index="7" stop-index="35">
-            <expression-projection text="SUBSTRING('foobarbar' from 4)" start-index="7" stop-index="35">
+        <projections start-index="7" stop-index="42">
+            <expression-projection text="SUBSTRING('foobarbar' from -4 for 2)" start-index="7" stop-index="42">
                 <expr>
-                    <function function-name="SUBSTRING" start-index="7" stop-index="35" text="SUBSTRING('foobarbar' from 4)" >
+                    <function function-name="SUBSTRING" start-index="7" stop-index="42" text="SUBSTRING('foobarbar' from -4 for 2)" >
                         <parameter>
                             <literal-expression value="foobarbar" start-index="17" stop-index="27" />
                         </parameter>
                         <parameter>
-                            <literal-expression value="4" start-index="34" stop-index="34" />
+                            <literal-expression value="-4" start-index="34" stop-index="35" />
+                        </parameter>
+                        <parameter>
+                            <literal-expression value="2" start-index="41" stop-index="41" />
                         </parameter>
                     </function>
                 </expr>
diff --git a/test/it/parser/src/main/resources/sql/supported/dml/select-special-function.xml b/test/it/parser/src/main/resources/sql/supported/dml/select-special-function.xml
index f5c6b791b18..3944d08c6e2 100644
--- a/test/it/parser/src/main/resources/sql/supported/dml/select-special-function.xml
+++ b/test/it/parser/src/main/resources/sql/supported/dml/select-special-function.xml
@@ -26,7 +26,7 @@
     <sql-case id="select_cast_multiset" value="select CAST(MULTISET(SELECT cust_address FROM customers c WHERE c.customer_id = cd.customer_id) as cust_address_tab_typ) from customer;" db-types="Oracle" />
     <sql-case id="select_convert_function" value="SELECT CONVERT('2020-10-01', DATE)" db-types="MySQL" />
     <sql-case id="select_position" value="SELECT POSITION('bar' IN 'foobarbar')" db-types="MySQL" />
-    <sql-case id="select_substring" value="SELECT SUBSTRING('foobarbar' from 4)" db-types="MySQL" />
+    <sql-case id="select_substring" value="SELECT SUBSTRING('foobarbar' from -4 for 2)" db-types="MySQL" />
     <sql-case id="select_substr" value="SELECT SUBSTR('foobarbar' from 4)" db-types="MySQL" />
     <sql-case id="select_extract" value="SELECT EXTRACT(YEAR FROM '2019-07-02')" db-types="MySQL" />
     <sql-case id="select_extract_from_column" value="SELECT EXTRACT(YEAR FROM o.creation_date) FROM t_order o" db-types="MySQL,Presto" />