提交 b2a70c1c authored 作者: Thomas Mueller Graf's avatar Thomas Mueller Graf

The condition in(select...) did not work correctly in some cases if the subquery was ordered

上级 804c05b1
...@@ -21,9 +21,11 @@ Change Log ...@@ -21,9 +21,11 @@ Change Log
<h2>Next Version (unreleased)</h2> <h2>Next Version (unreleased)</h2>
<ul> <ul>
<li>The condition "in(select...)" did not work correctly in some cases if the subquery had an "order by".
</li>
<li>Issue #184: The Platform-independent zip had Windows line endings in Linux scripts. <li>Issue #184: The Platform-independent zip had Windows line endings in Linux scripts.
</li> </li>
<li>The "script" command did not include sequences of temporary tables. <li>Issue #186: The "script" command did not include sequences of temporary tables.
</li> </li>
</ul> </ul>
......
...@@ -130,6 +130,13 @@ public abstract class Query extends Prepared { ...@@ -130,6 +130,13 @@ public abstract class Query extends Prepared {
*/ */
public abstract void setOrder(ArrayList<SelectOrderBy> order); public abstract void setOrder(ArrayList<SelectOrderBy> order);
/**
* Whether the query has an order.
*
* @return true if it has
*/
public abstract boolean hasOrder();
/** /**
* Set the 'for update' flag. * Set the 'for update' flag.
* *
......
...@@ -143,6 +143,11 @@ public class Select extends Query { ...@@ -143,6 +143,11 @@ public class Select extends Query {
orderList = order; orderList = order;
} }
@Override
public boolean hasOrder() {
return orderList != null || sort != null;
}
/** /**
* Add a condition to the list of conditions. * Add a condition to the list of conditions.
* *
......
...@@ -102,6 +102,11 @@ public class SelectUnion extends Query { ...@@ -102,6 +102,11 @@ public class SelectUnion extends Query {
orderList = order; orderList = order;
} }
@Override
public boolean hasOrder() {
return orderList != null || sort != null;
}
private Value[] convert(Value[] values, int columnCount) { private Value[] convert(Value[] values, int columnCount) {
Value[] newValues; Value[] newValues;
if (columnCount == values.length) { if (columnCount == values.length) {
......
...@@ -43,7 +43,9 @@ public class ConditionInSelect extends Condition { ...@@ -43,7 +43,9 @@ public class ConditionInSelect extends Condition {
@Override @Override
public Value getValue(Session session) { public Value getValue(Session session) {
query.setSession(session); query.setSession(session);
query.setDistinct(true); if (!query.hasOrder()) {
query.setDistinct(true);
}
LocalResult rows = query.query(0); LocalResult rows = query.query(0);
try { try {
Value l = left.getValue(session); Value l = left.getValue(session);
......
...@@ -206,13 +206,8 @@ public class LocalResult implements ResultInterface, ResultTarget { ...@@ -206,13 +206,8 @@ public class LocalResult implements ResultInterface, ResultTarget {
if (distinctRows == null) { if (distinctRows == null) {
distinctRows = ValueHashMap.newInstance(); distinctRows = ValueHashMap.newInstance();
for (Value[] row : rows) { for (Value[] row : rows) {
if (row.length > visibleColumnCount) { ValueArray array = getArrayOfVisible(row);
Value[] r2 = new Value[visibleColumnCount]; distinctRows.put(array, array.getList());
System.arraycopy(row, 0, r2, 0, visibleColumnCount);
row = r2;
}
ValueArray array = ValueArray.get(row);
distinctRows.put(array, row);
} }
} }
ValueArray array = ValueArray.get(values); ValueArray array = ValueArray.get(values);
...@@ -271,6 +266,15 @@ public class LocalResult implements ResultInterface, ResultTarget { ...@@ -271,6 +266,15 @@ public class LocalResult implements ResultInterface, ResultTarget {
} }
} }
private ValueArray getArrayOfVisible(Value[] values) {
if (values.length > visibleColumnCount) {
Value[] v2 = new Value[visibleColumnCount];
System.arraycopy(values, 0, v2, 0, visibleColumnCount);
values = v2;
}
return ValueArray.get(values);
}
/** /**
* Add a row to this object. * Add a row to this object.
* *
...@@ -281,7 +285,7 @@ public class LocalResult implements ResultInterface, ResultTarget { ...@@ -281,7 +285,7 @@ public class LocalResult implements ResultInterface, ResultTarget {
cloneLobs(values); cloneLobs(values);
if (distinct) { if (distinct) {
if (distinctRows != null) { if (distinctRows != null) {
ValueArray array = ValueArray.get(values); ValueArray array = getArrayOfVisible(values);
distinctRows.put(array, values); distinctRows.put(array, values);
rowCount = distinctRows.size(); rowCount = distinctRows.size();
if (rowCount > maxMemoryRows) { if (rowCount > maxMemoryRows) {
......
...@@ -3,6 +3,18 @@ ...@@ -3,6 +3,18 @@
-- Initial Developer: H2 Group -- Initial Developer: H2 Group
-- --
--- special grammar and test cases --------------------------------------------------------------------------------------------- --- special grammar and test cases ---------------------------------------------------------------------------------------------
create table test(id int) as select 1;
> ok
select * from test where id in (select id from test order by 'x');
> ID
> --
> 1
> rows (ordered): 1
drop table test;
> ok
select abs(cast(0.0 as double)) x; select abs(cast(0.0 as double)) x;
> X > X
> --- > ---
...@@ -62,7 +74,7 @@ LIMIT 2 ) ...@@ -62,7 +74,7 @@ LIMIT 2 )
AND studentID = 2; AND studentID = 2;
> SUM(POINTS) > SUM(POINTS)
> ----------- > -----------
> null > 30
> rows (ordered): 1 > rows (ordered): 1
SELECT eventID X FROM RESULTS SELECT eventID X FROM RESULTS
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论