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

chengzhang 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 8b18d4e79d1 Fix npe cause by parse MySQL select window statement 
(#29817)
8b18d4e79d1 is described below

commit 8b18d4e79d10651dc97f59091b19e383708ac100
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Tue Jan 23 20:47:28 2024 +0800

    Fix npe cause by parse MySQL select window statement (#29817)
    
    * Fix npe cause by parse MySQL select window statement
    
    * Update MySQLContainer.java
---
 .../visitor/statement/MySQLStatementVisitor.java   |  6 +-
 .../src/main/resources/case/dml/select-window.xml  | 73 ++++++++++++++++++++++
 .../resources/sql/supported/dml/select-window.xml  | 21 +++++++
 3 files changed, 98 insertions(+), 2 deletions(-)

diff --git 
a/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java
 
b/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java
index 31a8bcda719..c38ccaca647 100644
--- 
a/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java
+++ 
b/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java
@@ -163,7 +163,6 @@ import 
org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSe
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.ColumnAssignmentSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.InsertValuesSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.SetAssignmentSegment;
-import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.ParenthesesSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.InsertColumnsSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.OnDuplicateKeyColumnsSegment;
@@ -215,6 +214,7 @@ import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DataTypeS
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DatabaseSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.ParameterMarkerSegment;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.ParenthesesSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WindowSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.WithSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.DeleteMultiTableSegment;
@@ -1042,7 +1042,9 @@ public abstract class MySQLStatementVisitor extends 
MySQLStatementBaseVisitor<AS
         super.visitWindowFunction(ctx);
         FunctionSegment result = new 
FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), 
ctx.windowingClause().OVER().getText(), getOriginalText(ctx));
         result.getParameters().add(new 
FunctionSegment(ctx.funcName.getStartIndex(), ctx.funcName.getStopIndex(), 
ctx.funcName.getText(), ctx.funcName.getText() + "()"));
-        
result.getParameters().addAll(getExpressions(ctx.windowingClause().windowSpecification().expr()));
+        if (null != ctx.windowingClause().windowSpecification()) {
+            
result.getParameters().addAll(getExpressions(ctx.windowingClause().windowSpecification().expr()));
+        }
         return result;
     }
     
diff --git a/test/it/parser/src/main/resources/case/dml/select-window.xml 
b/test/it/parser/src/main/resources/case/dml/select-window.xml
new file mode 100644
index 00000000000..e80f7d70815
--- /dev/null
+++ b/test/it/parser/src/main/resources/case/dml/select-window.xml
@@ -0,0 +1,73 @@
+<?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>
+    <select sql-case-id="select_window" parameters="1">
+        <projections start-index="7" stop-index="112">
+            <column-projection name="user_id" start-index="7" stop-index="13" 
/>
+            <expression-projection text="ROW_NUMBER() OVER w" start-index="16" 
stop-index="50" alias="row_number">
+                <expr>
+                    <!-- FIXME correct function-name parse -->
+                    <function start-index="16" stop-index="34" 
text="ROW_NUMBER() OVER w" function-name="OVER">
+                        <parameter>
+                            <function start-index="16" stop-index="25" 
text="ROW_NUMBER()" function-name="ROW_NUMBER" />
+                        </parameter>
+                    </function>
+                </expr>
+            </expression-projection>
+            <expression-projection text="RANK() OVER w" start-index="53" 
stop-index="75" alias="rank">
+                <expr>
+                    <!-- FIXME correct function-name parse -->
+                    <function start-index="53" stop-index="65" text="RANK() 
OVER w" function-name="OVER">
+                        <parameter>
+                            <function start-index="53" stop-index="56" 
text="RANK()" function-name="RANK" />
+                        </parameter>
+                    </function>
+                </expr>
+            </expression-projection>
+            <expression-projection text="DENSE_RANK() OVER w" start-index="78" 
stop-index="112" alias="dense_rank">
+                <expr>
+                    <!-- FIXME correct function-name parse -->
+                    <function start-index="78" stop-index="96" 
text="DENSE_RANK() OVER w" function-name="OVER">
+                        <parameter>
+                            <function start-index="78" stop-index="87" 
text="DENSE_RANK()" function-name="DENSE_RANK" />
+                        </parameter>
+                    </function>
+                </expr>
+            </expression-projection>
+        </projections>
+        <from>
+            <simple-table name="t_order" start-index="119" stop-index="125" />
+        </from>
+        <where start-index="127" stop-index="144">
+            <expr>
+                <binary-operation-expression start-index="133" 
stop-index="144">
+                    <left>
+                        <column name="order_id" start-index="133" 
stop-index="140" />
+                    </left>
+                    <operator>=</operator>
+                    <right>
+                        <literal-expression value="1" start-index="144" 
stop-index="144" />
+                        <parameter-marker-expression parameter-index="0" 
start-index="144" stop-index="144" />
+                    </right>
+                </binary-operation-expression>
+            </expr>
+        </where>
+        <window start-index="146" stop-index="175" />
+    </select>
+</sql-parser-test-cases>
diff --git 
a/test/it/parser/src/main/resources/sql/supported/dml/select-window.xml 
b/test/it/parser/src/main/resources/sql/supported/dml/select-window.xml
new file mode 100644
index 00000000000..46538255abc
--- /dev/null
+++ b/test/it/parser/src/main/resources/sql/supported/dml/select-window.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="select_window" value="SELECT user_id, ROW_NUMBER() OVER w AS 
'row_number', RANK() OVER w AS 'rank', DENSE_RANK() OVER w AS 'dense_rank' FROM 
t_order WHERE order_id = ? WINDOW w AS (ORDER BY user_id)" db-types="MySQL" />
+</sql-cases>

Reply via email to