Unverified 提交 0f7a451f authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov 提交者: GitHub

Merge pull request #988 from katzyn/range_table

Fix RangeTable.getRowCount() for non-default step
...@@ -139,7 +139,19 @@ public class RangeTable extends Table { ...@@ -139,7 +139,19 @@ public class RangeTable extends Table {
@Override @Override
public long getRowCount(Session session) { public long getRowCount(Session session) {
return Math.max(0, getMax(session) - getMin(session) + 1); long step = getStep(session);
if (step == 0L) {
throw DbException.get(ErrorCode.STEP_SIZE_MUST_NOT_BE_ZERO);
}
long delta = getMax(session) - getMin(session);
if (step > 0) {
if (delta < 0) {
return 0;
}
} else if (delta > 0) {
return 0;
}
return delta / step + 1;
} }
@Override @Override
......
...@@ -85,6 +85,7 @@ public class TestScript extends TestBase { ...@@ -85,6 +85,7 @@ public class TestScript extends TestBase {
testScript("derived-column-names.sql"); testScript("derived-column-names.sql");
testScript("information_schema.sql"); testScript("information_schema.sql");
testScript("joins.sql"); testScript("joins.sql");
testScript("range_table.sql");
testScript("altertable-index-reuse.sql"); testScript("altertable-index-reuse.sql");
testScript("default-and-on_update.sql"); testScript("default-and-on_update.sql");
testScript("query-optimisations.sql"); testScript("query-optimisations.sql");
......
-- Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
-- and the EPL 1.0 (http://h2database.com/html/license.html).
-- Initial Developer: H2 Group
--
SELECT * FROM SYSTEM_RANGE(1, 10) ORDER BY 1;
> X
> --
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 10
> rows (ordered): 10
SELECT COUNT(*) FROM SYSTEM_RANGE(1, 10);
>> 10
SELECT * FROM SYSTEM_RANGE(1, 10, 2) ORDER BY 1;
> X
> -
> 1
> 3
> 5
> 7
> 9
> rows (ordered): 5
SELECT COUNT(*) FROM SYSTEM_RANGE(1, 10, 2);
>> 5
SELECT * FROM SYSTEM_RANGE(1, 9, 2) ORDER BY 1;
> X
> -
> 1
> 3
> 5
> 7
> 9
> rows (ordered): 5
SELECT COUNT(*) FROM SYSTEM_RANGE(1, 9, 2);
>> 5
SELECT * FROM SYSTEM_RANGE(10, 1, -2) ORDER BY 1 DESC;
> X
> --
> 10
> 8
> 6
> 4
> 2
> rows (ordered): 5
SELECT COUNT(*) FROM SYSTEM_RANGE(10, 1, -2);
>> 5
SELECT * FROM SYSTEM_RANGE(10, 2, -2) ORDER BY 1 DESC;
> X
> --
> 10
> 8
> 6
> 4
> 2
> rows (ordered): 5
SELECT COUNT(*) FROM SYSTEM_RANGE(10, 2, -2);
>> 5
SELECT * FROM SYSTEM_RANGE(1, 1);
> X
> -
> 1
> rows: 1
SELECT COUNT(*) FROM SYSTEM_RANGE(1, 1);
>> 1
SELECT * FROM SYSTEM_RANGE(1, 1, -1);
> X
> -
> 1
> rows: 1
SELECT COUNT(*) FROM SYSTEM_RANGE(1, 1, -1);
>> 1
SELECT * FROM SYSTEM_RANGE(2, 1);
> X
> -
> rows: 0
SELECT COUNT(*) FROM SYSTEM_RANGE(2, 1);
>> 0
SELECT * FROM SYSTEM_RANGE(2, 1, 2);
> X
> -
> rows: 0
SELECT COUNT(*) FROM SYSTEM_RANGE(2, 1, 2);
>> 0
SELECT * FROM SYSTEM_RANGE(1, 2, 0);
> exception
SELECT COUNT(*) FROM SYSTEM_RANGE(1, 2, 0);
> exception
SELECT * FROM SYSTEM_RANGE(2, 1, 0);
> exception
SELECT COUNT(*) FROM SYSTEM_RANGE(2, 1, 0);
> exception
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论