Skip to content

Commit

Permalink
修复分页优化distinct搭配orderBy处理错误.
Browse files Browse the repository at this point in the history
  • Loading branch information
nieqiurong committed Apr 26, 2024
1 parent 745862d commit 74b5d38
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,6 @@ public String autoCountSql(IPage<?> page, String sql) {
return lowLevelCountSql(sql);
}
PlainSelect plainSelect = (PlainSelect) select;
Distinct distinct = plainSelect.getDistinct();
GroupByElement groupBy = plainSelect.getGroupBy();

// 包含 distinct、groupBy 不优化
if (null != distinct || null != groupBy) {
return lowLevelCountSql(select.toString());
}

// 优化 order by 在非分组情况下
List<OrderByElement> orderBy = plainSelect.getOrderByElements();
Expand All @@ -291,7 +284,12 @@ public String autoCountSql(IPage<?> page, String sql) {
plainSelect.setOrderByElements(null);
}
}

Distinct distinct = plainSelect.getDistinct();
GroupByElement groupBy = plainSelect.getGroupBy();
// 包含 distinct、groupBy 不优化
if (null != distinct || null != groupBy) {
return lowLevelCountSql(select.toString());
}
//#95 Github, selectItems contains #{} ${}, which will be translated to ?, and it may be in a function: power(#{myInt},2)
for (SelectItem item : plainSelect.getSelectItems()) {
if (item.toString().contains(StringPool.QUESTION_MARK)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ void optimizeCount() {

assertsCountSql("select * from user u LEFT JOIN role r ON r.id = u.role_id LEFT JOIN permission p on p.id = u.per_id WHERE u.xx = ?",
"SELECT COUNT(*) AS total FROM user u WHERE u.xx = ?");

assertsCountSql("select distinct id from table order by id", "SELECT COUNT(*) FROM (SELECT DISTINCT id FROM table) TOTAL");

assertsCountSql("select distinct id from table", "SELECT COUNT(*) FROM (SELECT DISTINCT id FROM table) TOTAL");
}

@Test
Expand Down

0 comments on commit 74b5d38

Please sign in to comment.