提交 b24cbdf5 authored 作者: S.Vladykin's avatar S.Vladykin

more tests and fixes

上级 9ad1ffae
...@@ -4729,8 +4729,14 @@ public class Parser { ...@@ -4729,8 +4729,14 @@ public class Parser {
.substring(parseIndex)); .substring(parseIndex));
read("AS"); read("AS");
try { try {
Query query = parseSelect(); Query query;
session.setParsingView(true);
try {
query = parseSelect();
query.prepare(); query.prepare();
} finally {
session.setParsingView(false);
}
command.setSelect(query); command.setSelect(query);
} catch (DbException e) { } catch (DbException e) {
if (force) { if (force) {
......
...@@ -88,7 +88,7 @@ public class ViewIndex extends BaseIndex implements SpatialIndex { ...@@ -88,7 +88,7 @@ public class ViewIndex extends BaseIndex implements SpatialIndex {
this.indexMasks = masks; this.indexMasks = masks;
this.createSession = session; this.createSession = session;
columns = new Column[0]; columns = new Column[0];
if (!recursive) { if (!recursive && filters != null) {
query = getQuery(session, masks, filters, filter, sortOrder); query = getQuery(session, masks, filters, filter, sortOrder);
} }
} }
...@@ -136,7 +136,7 @@ public class ViewIndex extends BaseIndex implements SpatialIndex { ...@@ -136,7 +136,7 @@ public class ViewIndex extends BaseIndex implements SpatialIndex {
@Override @Override
public synchronized double getCost(Session session, int[] masks, public synchronized double getCost(Session session, int[] masks,
TableFilter[] filters, int filter, SortOrder sortOrder) { TableFilter[] filters, int filter, SortOrder sortOrder) {
if (recursive) { if (recursive || filters == null) {
return 1000; return 1000;
} }
IntArray masksArray = new IntArray(masks == null ? IntArray masksArray = new IntArray(masks == null ?
...@@ -177,6 +177,10 @@ public class ViewIndex extends BaseIndex implements SpatialIndex { ...@@ -177,6 +177,10 @@ public class ViewIndex extends BaseIndex implements SpatialIndex {
String sql = q.getPlanSQL(); String sql = q.getPlanSQL();
q = prepareSubQuery(sql, session, masks, filters, filter, sortOrder, false); q = prepareSubQuery(sql, session, masks, filters, filter, sortOrder, false);
} }
if (query == null) {
// this is possible when scan index has been taken and the cost was recalculated
query = q;
}
double cost = q.getCost(); double cost = q.getCost();
cachedCost = new CostElement(); cachedCost = new CostElement();
cachedCost.evaluatedAt = System.currentTimeMillis(); cachedCost.evaluatedAt = System.currentTimeMillis();
......
...@@ -41,6 +41,7 @@ import org.h2.result.Row; ...@@ -41,6 +41,7 @@ import org.h2.result.Row;
import org.h2.result.SearchRow; import org.h2.result.SearchRow;
import org.h2.result.SortOrder; import org.h2.result.SortOrder;
import org.h2.table.IndexColumn; import org.h2.table.IndexColumn;
import org.h2.table.SubQueryInfo;
import org.h2.table.Table; import org.h2.table.Table;
import org.h2.table.TableBase; import org.h2.table.TableBase;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
...@@ -350,11 +351,20 @@ public class TestTableEngines extends TestBase { ...@@ -350,11 +351,20 @@ public class TestTableEngines extends TestBase {
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
stat.execute("create table SUB_QUERY_TEST(id int primary key, name varchar) ENGINE \"" + stat.execute("create table SUB_QUERY_TEST(id int primary key, name varchar) ENGINE \"" +
TreeSetIndexTableEngine.class.getName() + "\""); TreeSetIndexTableEngine.class.getName() + "\"");
stat.execute("create table t1(id int, birthday date)"); // test sub-queries
stat.executeQuery("select t1.birthday from t1, " stat.executeQuery("select * from "
+ "(select t2.id from sub_query_test t2, " + "(select t2.id from "
+ "(select t3.id from sub_query_test t3 where t3.name = '') t4 " + "(select t3.id from sub_query_test t3 where t3.name = '') t4, "
+ "where t2.id = t4.id) t5 where t1.id = t5.id"); + "sub_query_test t2 "
+ "where t2.id = t4.id) t5").next();
// test view 1
stat.execute("create view t4 as (select t3.id from sub_query_test t3 where t3.name = '')");
stat.executeQuery("select * from "
+ "(select t2.id from t4, sub_query_test t2 where t2.id = t4.id) t5").next();
// test view 2
stat.execute("create view t5 as "
+ "(select t2.id from t4, sub_query_test t2 where t2.id = t4.id)");
stat.executeQuery("select * from t5").next();
deleteDb("testSubQueryInfo"); deleteDb("testSubQueryInfo");
} }
...@@ -478,6 +488,12 @@ public class TestTableEngines extends TestBase { ...@@ -478,6 +488,12 @@ public class TestTableEngines extends TestBase {
} }
} }
private static void assert0(boolean condition, String message) {
if (!condition) {
throw new AssertionError(message);
}
}
private static void setBatchSize(ArrayList<TreeSetTable> tables, int... batchSizes) { private static void setBatchSize(ArrayList<TreeSetTable> tables, int... batchSizes) {
for (int i = 0; i < batchSizes.length; i++) { for (int i = 0; i < batchSizes.length; i++) {
int batchSize = batchSizes[i]; int batchSize = batchSizes[i];
...@@ -1219,13 +1235,26 @@ public class TestTableEngines extends TestBase { ...@@ -1219,13 +1235,26 @@ public class TestTableEngines extends TestBase {
return new IteratorCursor(subSet.iterator()); return new IteratorCursor(subSet.iterator());
} }
private void checkInfo(SubQueryInfo info) {
if (info.getUpper() == null) {
// check 1st level info
assert0(info.getFilters().length == 1, "getFilters().length " + info.getFilters().length);
String alias = info.getFilters()[info.getFilter()].getTableAlias();
assert0("T5".equals(alias), "alias: " + alias);
} else {
// check 2nd level info
assert0(info.getFilters().length == 2, "getFilters().length " + info.getFilters().length);
String alias = info.getFilters()[info.getFilter()].getTableAlias();
assert0("T4".equals(alias), "alias: " + alias);
checkInfo(info.getUpper());
}
}
@Override @Override
public double getCost(Session session, int[] masks, public double getCost(Session session, int[] masks,
TableFilter[] filters, int filter, SortOrder sortOrder) { TableFilter[] filters, int filter, SortOrder sortOrder) {
if (getTable().getName().equals("SUB_QUERY_TEST")) { if (getTable().getName().equals("SUB_QUERY_TEST")) {
if (session.getSubQueryInfo() == null) { checkInfo(session.getSubQueryInfo());
throw new IllegalStateException("No qubquery info found.");
}
} }
return getCostRangeIndex(masks, set.size(), filters, filter, sortOrder); return getCostRangeIndex(masks, set.size(), filters, filter, sortOrder);
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论