fixed failure on incorrect limit and offsets with sorting, need to add tests

上级 95b5a4a5
...@@ -336,7 +336,7 @@ public class LocalResult implements ResultInterface, ResultTarget { ...@@ -336,7 +336,7 @@ public class LocalResult implements ResultInterface, ResultTarget {
} else { } else {
if (sort != null) { if (sort != null) {
if (offset > 0 || limit > 0) { if (offset > 0 || limit > 0) {
sort.sort(rows, offset, limit == -1 ? rows.size() : limit); sort.sort(rows, offset, limit < 0 ? rows.size() : limit);
} else { } else {
sort.sort(rows); sort.sort(rows);
} }
......
...@@ -163,16 +163,23 @@ public class SortOrder implements Comparator<Value[]> { ...@@ -163,16 +163,23 @@ public class SortOrder implements Comparator<Value[]> {
* @param limit the limit * @param limit the limit
*/ */
public void sort(ArrayList<Value[]> rows, int offset, int limit) { public void sort(ArrayList<Value[]> rows, int offset, int limit) {
if (rows.isEmpty()) { int rowsSize = rows.size();
if (rows.isEmpty() || offset >= rowsSize || limit == 0) {
return; return;
} }
if (offset < 0) {
offset = 0;
}
if (offset + limit > rowsSize) {
limit = rowsSize - offset;
}
if (limit == 1 && offset == 0) { if (limit == 1 && offset == 0) {
rows.set(0, Collections.min(rows, this)); rows.set(0, Collections.min(rows, this));
return; return;
} }
Value[][] arr = rows.toArray(new Value[rows.size()][]); Value[][] arr = rows.toArray(new Value[rowsSize][]);
Utils.sortTopN(arr, offset, limit, this); Utils.sortTopN(arr, offset, limit, this);
for (int i = 0, end = Math.min(offset + limit, arr.length); i < end; i++) { for (int i = 0, end = Math.min(offset + limit, rowsSize); i < end; i++) {
rows.set(i, arr[i]); rows.set(i, arr[i]);
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论