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

more fixes

上级 b24cbdf5
......@@ -900,8 +900,8 @@ public class Select extends Query {
}
if (sort != null && !isQuickAggregateQuery && !isGroupQuery) {
Index index = getSortIndex();
if (index != null) {
Index current = topTableFilter.getIndex();
if (index != null && current != null) {
if (current.getIndexType().isScan() || current == index) {
topTableFilter.setIndex(index);
if (!topTableFilter.hasInComparisons()) {
......@@ -934,7 +934,7 @@ public class Select extends Query {
getGroupByExpressionCount() > 0) {
Index index = getGroupSortedIndex();
Index current = topTableFilter.getIndex();
if (index != null && (current.getIndexType().isScan() ||
if (index != null && current != null && (current.getIndexType().isScan() ||
current == index)) {
topTableFilter.setIndex(index);
isGroupSortedQuery = true;
......
......@@ -88,7 +88,7 @@ public class ViewIndex extends BaseIndex implements SpatialIndex {
this.indexMasks = masks;
this.createSession = session;
columns = new Column[0];
if (!recursive && filters != null) {
if (!recursive) {
query = getQuery(session, masks, filters, filter, sortOrder);
}
}
......@@ -136,7 +136,7 @@ public class ViewIndex extends BaseIndex implements SpatialIndex {
@Override
public synchronized double getCost(Session session, int[] masks,
TableFilter[] filters, int filter, SortOrder sortOrder) {
if (recursive || filters == null) {
if (recursive) {
return 1000;
}
IntArray masksArray = new IntArray(masks == null ?
......@@ -177,10 +177,6 @@ public class ViewIndex extends BaseIndex implements SpatialIndex {
String sql = q.getPlanSQL();
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();
cachedCost = new CostElement();
cachedCost.evaluatedAt = System.currentTimeMillis();
......@@ -201,6 +197,7 @@ public class ViewIndex extends BaseIndex implements SpatialIndex {
private static Query prepareSubQuery(String sql, Session session, int[] masks,
TableFilter[] filters, int filter, SortOrder sortOrder, boolean preliminary) {
assert filters != null;
Prepared p;
SubQueryInfo upper = session.getSubQueryInfo();
SubQueryInfo info = new SubQueryInfo(upper,
......
......@@ -236,6 +236,11 @@ public abstract class Table extends SchemaObjectBase {
*/
public abstract Index getScanIndex(Session session);
public Index getScanIndex(Session session, int[] masks,
TableFilter[] filters, int filter, SortOrder sortOrder) {
return getScanIndex(session);
}
/**
* Get any unique index for this table if one exists.
*
......
......@@ -202,10 +202,14 @@ public class TableFilter implements ColumnResolver {
*/
public PlanItem getBestPlanItem(Session s, TableFilter[] filters, int filter) {
PlanItem item;
SortOrder sortOrder = null;
if (select != null) {
sortOrder = select.getSortOrder();
}
if (indexConditions.size() == 0) {
item = new PlanItem();
item.setIndex(table.getScanIndex(s));
item.cost = item.getIndex().getCost(s, null, filters, filter, null);
item.setIndex(table.getScanIndex(s, null, filters, filter, sortOrder));
item.cost = item.getIndex().getCost(s, null, filters, filter, sortOrder);
} else {
int len = table.getColumns().length;
int[] masks = new int[len];
......@@ -221,10 +225,6 @@ public class TableFilter implements ColumnResolver {
}
}
}
SortOrder sortOrder = null;
if (select != null) {
sortOrder = select.getSortOrder();
}
item = table.getBestPlanItem(s, masks, filters, filter, sortOrder);
item.setMasks(masks);
// The more index conditions, the earlier the table.
......@@ -241,8 +241,9 @@ public class TableFilter implements ColumnResolver {
}
if (join != null) {
setEvaluatable(join);
filter += nestedJoin == null ? 1 : 2;
assert filters[filter] == join;
do {
filter++;
} while (filters[filter] != join);
item.setJoinPlan(join.getBestPlanItem(s, filters, filter));
// TODO optimizer: calculate cost of a join: should use separate
// expected row number and lookup cost
......
......@@ -435,12 +435,18 @@ public class TableView extends Table {
@Override
public Index getScanIndex(Session session) {
return getBestPlanItem(session, null, null, -1, null).getIndex();
}
@Override
public Index getScanIndex(Session session, int[] masks,
TableFilter[] filters, int filter, SortOrder sortOrder) {
if (createException != null) {
String msg = createException.getMessage();
throw DbException.get(ErrorCode.VIEW_IS_INVALID_2,
createException, getSQL(), msg);
}
PlanItem item = getBestPlanItem(session, null, null, -1, null);
PlanItem item = getBestPlanItem(session, masks, filters, filter, sortOrder);
return item.getIndex();
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论