提交 c58cd967 authored 作者: christian.peter.io's avatar christian.peter.io

Certain queries were not sorted if subselects were involved

上级 6bac611f
...@@ -22,6 +22,7 @@ Change Log ...@@ -22,6 +22,7 @@ Change Log
The diagrams are HTML. The diagrams are HTML.
</li><li>The documentation is no longer available in Japanese because the </li><li>The documentation is no longer available in Japanese because the
translation was too much out of sync. Please use the Google translation instead. translation was too much out of sync. Please use the Google translation instead.
</li><li>Certain queries were not sorted if subselect queries were involved
</li></ul> </li></ul>
<h2>Version 1.2.121 (2009-10-11)</h2> <h2>Version 1.2.121 (2009-10-11)</h2>
......
...@@ -760,9 +760,10 @@ public class Select extends Query { ...@@ -760,9 +760,10 @@ public class Select extends Query {
Index current = topTableFilter.getIndex(); Index current = topTableFilter.getIndex();
if (index != null && (current.getIndexType().isScan() || current == index)) { if (index != null && (current.getIndexType().isScan() || current == index)) {
topTableFilter.setIndex(index); topTableFilter.setIndex(index);
if (!distinct || isDistinctQuery) { if ((!distinct || isDistinctQuery) && (!topTableFilter.hasInComparisons())) {
// sort using index would not work correctly for distinct result sets // - sort using index would not work correctly for distinct result sets
// because it would break too early when limit is used // because it would break too early when limit is used
// - in(select ...) and in(1,2,3) indices are unsorted
sortUsingIndex = true; sortUsingIndex = true;
} }
} }
......
...@@ -13,6 +13,7 @@ import org.h2.command.dml.Select; ...@@ -13,6 +13,7 @@ import org.h2.command.dml.Select;
import org.h2.constant.SysProperties; import org.h2.constant.SysProperties;
import org.h2.engine.Right; import org.h2.engine.Right;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.expression.Comparison;
import org.h2.expression.ConditionAndOr; import org.h2.expression.ConditionAndOr;
import org.h2.expression.Expression; import org.h2.expression.Expression;
import org.h2.expression.ExpressionColumn; import org.h2.expression.ExpressionColumn;
...@@ -693,4 +694,23 @@ public class TableFilter implements ColumnResolver { ...@@ -693,4 +694,23 @@ public class TableFilter implements ColumnResolver {
return hashCode; return hashCode;
} }
/**
* Returns if there are in(...) comparisons involved
*
* @see Comparison.IN_LIST
* @see Comparison.IN_QUERY
*
* @return if there are in(...) comparisons involved
*/
public boolean hasInComparisons() {
for (int i = 0; i < indexConditions.size(); i++) {
if ((indexConditions.get(i).getCompareType() == Comparison.IN_QUERY)
||
(indexConditions.get(i).getCompareType() == Comparison.IN_LIST)) {
return true;
}
}
return false;
}
} }
...@@ -10,6 +10,7 @@ import java.io.File; ...@@ -10,6 +10,7 @@ import java.io.File;
import java.io.StringReader; import java.io.StringReader;
import java.sql.Connection; import java.sql.Connection;
import java.sql.Date; import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
...@@ -35,6 +36,7 @@ public class TestCases extends TestBase { ...@@ -35,6 +36,7 @@ public class TestCases extends TestBase {
} }
public void test() throws Exception { public void test() throws Exception {
testOrderByWithSubselect();
testInsertDeleteRollback(); testInsertDeleteRollback();
testLargeRollback(); testLargeRollback();
testConstraintAlterTable(); testConstraintAlterTable();
...@@ -880,4 +882,34 @@ public class TestCases extends TestBase { ...@@ -880,4 +882,34 @@ public class TestCases extends TestBase {
c0.close(); c0.close();
} }
private void testOrderByWithSubselect() throws SQLException {
Connection c = DriverManager.getConnection("jdbc:h2:mem:testdb");
Statement s = c.createStatement();
s.execute("create table master(id number primary key, name varchar2(30));");
s.execute("create table detail(id number references master(id), location varchar2(30));");
s.execute("Insert into master values(1,'a');");
s.execute("Insert into master values(2,'b');");
s.execute("Insert into master values(3,'c');");
s.execute("commit;");
s.execute("Insert into detail values(1,'a');");
s.execute("Insert into detail values(2,'b');");
s.execute("Insert into detail values(3,'c');");
s.execute("commit;");
ResultSet rs = s.executeQuery(
"select master.id, master.name " +
"from master " +
"where master.id in (select detail.id from detail) " +
"order by master.id");
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
assertTrue(rs.next());
assertEquals(3, rs.getInt(1));
c.close();
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论