提交 5eca85f1 authored 作者: Thomas Mueller's avatar Thomas Mueller

Statements with IN(..) conditions could produce the wrong result.

上级 165492ff
...@@ -49,7 +49,7 @@ public class IndexCursor implements Cursor { ...@@ -49,7 +49,7 @@ public class IndexCursor implements Cursor {
if (idxCols != null) { if (idxCols != null) {
for (int i = 0; i < columns.length; i++) { for (int i = 0; i < columns.length; i++) {
int idx = index.getColumnIndex(columns[i]); int idx = index.getColumnIndex(columns[i]);
if (idx >= 0) { if (idx == 0) {
indexColumns[i] = idxCols[idx]; indexColumns[i] = idxCols[idx];
} }
} }
...@@ -79,11 +79,9 @@ public class IndexCursor implements Cursor { ...@@ -79,11 +79,9 @@ public class IndexCursor implements Cursor {
this.inColumn = column; this.inColumn = column;
inList = condition.getCurrentValueList(session); inList = condition.getCurrentValueList(session);
inListIndex = 0; inListIndex = 0;
return;
} else if (condition.getCompareType() == Comparison.IN_QUERY) { } else if (condition.getCompareType() == Comparison.IN_QUERY) {
this.inColumn = column; this.inColumn = column;
inResult = condition.getCurrentResult(session); inResult = condition.getCurrentResult(session);
return;
} else { } else {
Value v = column.convert(condition.getCurrentValue(session)); Value v = column.convert(condition.getCurrentValue(session));
boolean isStart = condition.isStart(); boolean isStart = condition.isStart();
...@@ -103,8 +101,18 @@ public class IndexCursor implements Cursor { ...@@ -103,8 +101,18 @@ public class IndexCursor implements Cursor {
if (isEnd) { if (isEnd) {
end = getSearchRow(end, id, v, false); end = getSearchRow(end, id, v, false);
} }
if (isStart && isEnd) {
// an X=? condition will produce less rows than
// an X IN(..) condition
inColumn = null;
inList = null;
inResult = null;
}
} }
} }
if (inColumn != null) {
return;
}
if (!alwaysFalse) { if (!alwaysFalse) {
cursor = index.find(session, start, end); cursor = index.find(session, start, end);
} }
......
...@@ -39,6 +39,7 @@ public class TestOptimizations extends TestBase { ...@@ -39,6 +39,7 @@ public class TestOptimizations extends TestBase {
public void test() throws Exception { public void test() throws Exception {
testNestedInSelectAndLike(); testNestedInSelectAndLike();
testNestedInSelect();
testInSelectJoin(); testInSelectJoin();
testMinMaxNullOptimization(); testMinMaxNullOptimization();
if (config.networked) { if (config.networked) {
...@@ -58,6 +59,26 @@ public class TestOptimizations extends TestBase { ...@@ -58,6 +59,26 @@ public class TestOptimizations extends TestBase {
deleteDb("optimizations"); deleteDb("optimizations");
} }
private void testNestedInSelect() throws SQLException {
deleteDb("optimizations");
Connection conn = getConnection("optimizations");
Statement stat = conn.createStatement();
ResultSet rs;
stat.execute("create table test(id int primary key, name varchar) as select 1, 'Hello'");
stat.execute("select * from (select * from test) where id=1 and name in('Hello', 'World')");
stat.execute("drop table test");
stat.execute("create table test(id int, name varchar) as select 1, 'Hello'");
stat.execute("create index idx2 on test(id, name)");
rs = stat.executeQuery("select count(*) from test where id=1 and name in('Hello', 'x')");
rs.next();
assertEquals(1, rs.getInt(1));
conn.close();
}
private void testNestedInSelectAndLike() throws SQLException { private void testNestedInSelectAndLike() throws SQLException {
deleteDb("optimizations"); deleteDb("optimizations");
Connection conn = getConnection("optimizations"); Connection conn = getConnection("optimizations");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论