Skip to content

Commit

Permalink
Merge pull request #1171 from tuohai666/dev
Browse files Browse the repository at this point in the history
 #569, fix RowCountToken beginPosition
  • Loading branch information
terrymanu authored Aug 23, 2018
2 parents cc6837d + 3e4e4e1 commit c9e7383
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 2 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

#### Core

1. [ISSUE #569](https://github.com/sharding-sphere/sharding-sphere/issues/569) Failed to parse SQL for Oracle when ROWNUM is not at end
1. [ISSUE #628](https://github.com/sharding-sphere/sharding-sphere/issues/628) Support data type jsonb for PostgreSQL
1. [ISSUE #646](https://github.com/sharding-sphere/sharding-sphere/issues/646) When aliases in `SELECT ITEMS` correspond to the real column names of `GROUP BY` or `ORDER BY`, there is no need to generate derived columns
1. [ISSUE #806](https://github.com/sharding-sphere/sharding-sphere/issues/806) `NOT IN` parse exception
Expand Down
1 change: 1 addition & 0 deletions RELEASE-NOTES_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

#### 内核

1. [ISSUE #569](https://github.com/sharding-sphere/sharding-sphere/issues/569) 当Oracle的SQL中ROWNUM不在语句尾部时解析错误
1. [ISSUE #628](https://github.com/sharding-sphere/sharding-sphere/issues/628) 支持PostgreSQL的数据类型jsonb
1. [ISSUE #646](https://github.com/sharding-sphere/sharding-sphere/issues/646) 当SELECT ITEMS中的别名与GROUP BY或ORDER BY的真实列名对应时无需补列
1. [ISSUE #806](https://github.com/sharding-sphere/sharding-sphere/issues/806) `NOT IN`解析异常
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,15 @@ private boolean isRowNumberCondition(final List<SelectItem> items, final SQLExpr
protected abstract boolean isRowNumberCondition(List<SelectItem> items, String columnLabel);

private void parseRowCountCondition(final SelectStatement selectStatement, final boolean includeRowCount) {
int endPosition = lexerEngine.getCurrentToken().getEndPosition();
SQLExpression sqlExpression = basicExpressionParser.parse(selectStatement);
if (null == selectStatement.getLimit()) {
selectStatement.setLimit(new Limit(databaseType));
}
if (sqlExpression instanceof SQLNumberExpression) {
int rowCount = ((SQLNumberExpression) sqlExpression).getNumber().intValue();
selectStatement.getLimit().setRowCount(new LimitValue(rowCount, -1, includeRowCount));
selectStatement.getSqlTokens().add(new RowCountToken(
lexerEngine.getCurrentToken().getEndPosition() - String.valueOf(rowCount).length() - lexerEngine.getCurrentToken().getLiterals().length(), rowCount));
selectStatement.getSqlTokens().add(new RowCountToken(endPosition - String.valueOf(rowCount).length(), rowCount));
} else if (sqlExpression instanceof SQLPlaceholderExpression) {
selectStatement.getLimit().setRowCount(new LimitValue(-1, ((SQLPlaceholderExpression) sqlExpression).getIndex(), includeRowCount));
}
Expand Down
15 changes: 15 additions & 0 deletions sharding-core/src/test/resources/parser/select_pagination.xml
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,19 @@
</order-by-columns>
<limit offset="3" offset-index="5" row-count-index="4" row-count="5" />
</parser-result>

<parser-result sql-case-id="select_pagination_with_row_number_not_at_end" parameters="20">
<tables>
<table name="t_order"/>
</tables>
<tokens>
<table-token begin-position="14" original-literals="t_order"/>
<row-count-token begin-position="38" row-count="20"/>
</tokens>
<order-by-columns>
<order-by-column name="order_id" order-direction="ASC" />
</order-by-columns>
<limit row-count="20" row-count-index="0"/>
</parser-result>

</parser-result-sets>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<dataset>
<metadata>
<column name="order_id" />
<column name="user_id" />
<column name="status" />
</metadata>
<row values="1000, 10, init" />
<row values="1001, 10, init" />
<row values="1100, 11, init" />
<row values="1101, 11, init" />
<row values="1200, 12, init" />
<row values="1201, 12, init" />
<row values="1300, 13, init" />
<row values="1301, 13, init" />
<row values="1400, 14, init" />
<row values="1401, 14, init" />
<row values="1500, 15, init" />
<row values="1501, 15, init" />
<row values="1600, 16, init" />
<row values="1601, 16, init" />
<row values="1700, 17, init" />
<row values="1701, 17, init" />
<row values="1800, 18, init" />
<row values="1801, 18, init" />
<row values="1900, 19, init" />
<row values="1901, 19, init" />
</dataset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<dataset>
<metadata>
<column name="order_id" />
<column name="user_id" />
<column name="status" />
</metadata>
<row values="1000, 10, init_slave" />
<row values="1001, 10, init_slave" />
<row values="1002, 10, init_slave" />
<row values="1003, 10, init_slave" />
<row values="1004, 10, init_slave" />
<row values="1005, 10, init_slave" />
<row values="1006, 10, init_slave" />
<row values="1007, 10, init_slave" />
<row values="1008, 10, init_slave" />
<row values="1009, 10, init_slave" />
<row values="1100, 11, init_slave" />
<row values="1101, 11, init_slave" />
<row values="1102, 11, init_slave" />
<row values="1103, 11, init_slave" />
<row values="1104, 11, init_slave" />
<row values="1105, 11, init_slave" />
<row values="1106, 11, init_slave" />
<row values="1107, 11, init_slave" />
<row values="1108, 11, init_slave" />
<row values="1109, 11, init_slave" />
</dataset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<dataset>
<metadata>
<column name="order_id" />
<column name="user_id" />
<column name="status" />
</metadata>
<row values="1000, 10, init_slave" />
<row values="1001, 10, init_slave" />
<row values="1100, 11, init_slave" />
<row values="1101, 11, init_slave" />
<row values="1200, 12, init_slave" />
<row values="1201, 12, init_slave" />
<row values="1300, 13, init_slave" />
<row values="1301, 13, init_slave" />
<row values="1400, 14, init_slave" />
<row values="1401, 14, init_slave" />
<row values="1500, 15, init_slave" />
<row values="1501, 15, init_slave" />
<row values="1600, 16, init_slave" />
<row values="1601, 16, init_slave" />
<row values="1700, 17, init_slave" />
<row values="1701, 17, init_slave" />
<row values="1800, 18, init_slave" />
<row values="1801, 18, init_slave" />
<row values="1900, 19, init_slave" />
<row values="1901, 19, init_slave" />
</dataset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<dataset>
<metadata>
<column name="order_id" />
<column name="user_id" />
<column name="status" />
</metadata>
<row values="1000, 10, init" />
<row values="1001, 10, init" />
<row values="1002, 10, init" />
<row values="1003, 10, init" />
<row values="1004, 10, init" />
<row values="1005, 10, init" />
<row values="1006, 10, init" />
<row values="1007, 10, init" />
<row values="1008, 10, init" />
<row values="1009, 10, init" />
<row values="1100, 11, init" />
<row values="1101, 11, init" />
<row values="1102, 11, init" />
<row values="1103, 11, init" />
<row values="1104, 11, init" />
<row values="1105, 11, init" />
<row values="1106, 11, init" />
<row values="1107, 11, init" />
<row values="1108, 11, init" />
<row values="1109, 11, init" />
</dataset>
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@
<assertion parameters="10:int, 19:int, 1000:int, 1909:int, 4:int, 3:int" expected-data-file="select_pagination_with_row_number_and_limit.xml" />
</dql-test-case>

<dql-test-case sql-case-id="select_pagination_with_row_number_not_at_end">
<assertion parameters="20:int" expected-data-file="select_pagination_with_row_number_not_at_end.xml" />
</dql-test-case>

<dql-test-case sql-case-id="select_inner_join_related_with_alias">
<assertion parameters="1000:int" expected-data-file="select_inner_join.xml" />
</dql-test-case>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
<sql-case id="select_pagination_with_top_for_greater_than_and_equal" value="SELECT * FROM (SELECT TOP (?) row_number() OVER (ORDER BY i.item_id DESC) AS rownum_, i.item_id, o.order_id as order_id, o.status as status, o.user_id as user_id FROM t_order o JOIN t_order_item i ON o.user_id = i.user_id AND o.order_id = i.order_id WHERE o.user_id IN (?, ?) AND o.order_id BETWEEN ? AND ?) AS row_ WHERE row_.rownum_ &gt;= ?" db-types="SQLServer" />
<sql-case id="select_pagination_with_row_number_for_greater_than" value="SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT order0_.order_id as order_id, order0_.status as status, order0_.user_id as user_id FROM t_order order0_ JOIN t_order_item i ON order0_.user_id = i.user_id AND order0_.order_id = i.order_id WHERE order0_.user_id IN (?, ?) AND order0_.order_id BETWEEN ? AND ? ORDER BY i.item_id DESC) row_ WHERE rownum &lt;= ?) t WHERE t.rownum_ &gt; ?" db-types="Oracle" />
<sql-case id="select_pagination_with_row_number_for_greater_than_and_equal" value="SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT order0_.order_id as order_id, order0_.status as status, order0_.user_id as user_id FROM t_order order0_ JOIN t_order_item i ON order0_.user_id = i.user_id AND order0_.order_id = i.order_id WHERE order0_.user_id IN (?, ?) AND order0_.order_id BETWEEN ? AND ? ORDER BY i.item_id DESC) row_ WHERE rownum &lt;= ?) t WHERE t.rownum_ &gt;= ?" db-types="Oracle" />
<sql-case id="select_pagination_with_row_number_not_at_end" value="SELECT * FROM t_order WHERE ROWNUM &lt;= ? ORDER BY order_id" db-types="Oracle" />
</sql-cases>

0 comments on commit c9e7383

Please sign in to comment.