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

more fixes

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