Skip to content

Commit

Permalink
Fix npe cause by parse MySQL select window statement (#29817)
Browse files Browse the repository at this point in the history
* Fix npe cause by parse MySQL select window statement

* Update MySQLContainer.java
  • Loading branch information
strongduanmu committed Jan 23, 2024
1 parent 30c1ec4 commit 8b18d4e
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@
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;
Expand Down Expand Up @@ -215,6 +214,7 @@
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;
Expand Down Expand Up @@ -1042,7 +1042,9 @@ public final ASTNode visitWindowFunction(final WindowFunctionContext ctx) {
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;
}

Expand Down
73 changes: 73 additions & 0 deletions test/it/parser/src/main/resources/case/dml/select-window.xml
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit 8b18d4e

Please sign in to comment.