提交 d840655a authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Fix RangeIndex.find() for non-default step

上级 5bbe364a
...@@ -47,20 +47,34 @@ public class RangeIndex extends BaseIndex { ...@@ -47,20 +47,34 @@ public class RangeIndex extends BaseIndex {
@Override @Override
public Cursor find(Session session, SearchRow first, SearchRow last) { public Cursor find(Session session, SearchRow first, SearchRow last) {
long min = rangeTable.getMin(session), start = min; long min = rangeTable.getMin(session);
long max = rangeTable.getMax(session), end = max; long max = rangeTable.getMax(session);
long step = rangeTable.getStep(session); long step = rangeTable.getStep(session);
try { try {
start = Math.max(min, first == null ? min : first.getValue(0).getLong()); long v = first.getValue(0).getLong();
if (step > 0) {
if (v > min) {
min += (v - min + step - 1) / step * step;
}
} else if (v > max) {
max = v;
}
} catch (Exception e) { } catch (Exception e) {
// error when converting the value - ignore // error when converting the value - ignore
} }
try { try {
end = Math.min(max, last == null ? max : last.getValue(0).getLong()); long v = last.getValue(0).getLong();
if (step > 0) {
if (v < max) {
max = v;
}
} else if (v < min) {
min -= (min - v - step - 1) / step * step;
}
} catch (Exception e) { } catch (Exception e) {
// error when converting the value - ignore // error when converting the value - ignore
} }
return new RangeCursor(session, start, end, step); return new RangeCursor(session, min, max, step);
} }
@Override @Override
......
...@@ -118,3 +118,57 @@ SELECT * FROM SYSTEM_RANGE(2, 1, 0); ...@@ -118,3 +118,57 @@ SELECT * FROM SYSTEM_RANGE(2, 1, 0);
SELECT COUNT(*) FROM SYSTEM_RANGE(2, 1, 0); SELECT COUNT(*) FROM SYSTEM_RANGE(2, 1, 0);
> exception > exception
SELECT * FROM SYSTEM_RANGE(1, 8, 2);
> X
> -
> 1
> 3
> 5
> 7
> rows: 4
SELECT * FROM SYSTEM_RANGE(1, 8, 2) WHERE X = 2;
> X
> -
> rows: 0
SELECT COUNT(*) FROM SYSTEM_RANGE(1, 8, 2) WHERE X = 2;
>> 0
SELECT * FROM SYSTEM_RANGE(1, 8, 2) WHERE X BETWEEN 2 AND 6;
> X
> -
> 3
> 5
> rows: 2
SELECT COUNT(*) FROM SYSTEM_RANGE(1, 8, 2) WHERE X BETWEEN 2 AND 6;
>> 2
SELECT * FROM SYSTEM_RANGE(8, 1, -2) ORDER BY X DESC;
> X
> -
> 8
> 6
> 4
> 2
> rows (ordered): 4
SELECT * FROM SYSTEM_RANGE(8, 1, -2) WHERE X = 3;
> X
> -
> rows: 0
SELECT COUNT(*) FROM SYSTEM_RANGE(8, 1, -2) WHERE X = 3;
>> 0
SELECT * FROM SYSTEM_RANGE(8, 1, -2) WHERE X BETWEEN 3 AND 7 ORDER BY 1 DESC;
> X
> -
> 6
> 4
> rows (ordered): 2
SELECT COUNT(*) FROM SYSTEM_RANGE(8, 1, -2) WHERE X BETWEEN 3 AND 7;
>> 2
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论