提交 661504d9 authored 作者: Thomas Mueller's avatar Thomas Mueller

Added documentation and renamed method and fields, to fix the problem of the H2SpeedTest

上级 689e006c
...@@ -391,7 +391,7 @@ public class Select extends Query { ...@@ -391,7 +391,7 @@ public class Select extends Query {
return null; return null;
} }
ArrayList<Column> sortColumns = New.arrayList(); ArrayList<Column> sortColumns = New.arrayList();
for (int idx : sort.getIndexes()) { for (int idx : sort.getQueryColumnIndexes()) {
if (idx < 0 || idx >= expressions.size()) { if (idx < 0 || idx >= expressions.size()) {
throw DbException.getInvalidValueException("ORDER BY", idx + 1); throw DbException.getInvalidValueException("ORDER BY", idx + 1);
} }
......
...@@ -198,19 +198,23 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index { ...@@ -198,19 +198,23 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index {
columnSortTypes[i] = indexColumns[i].sortType; columnSortTypes[i] = indexColumns[i].sortType;
} }
boolean sortOrderMatches = true; boolean sortOrderMatches = true;
int[] sortOrderIndexes = sortOrder.getIndexes(); int[] sortOrderQueryColumnIndexes = sortOrder.getQueryColumnIndexes();
int coveringCount = 0; int coveringCount = 0;
for (int i = 0, len = sortOrderIndexes.length; i < len; i++) {
int theFollowingNeedsToBeFixedAndTested;
// see also the method Select.getSortIndex()
for (int i = 0, len = sortOrderQueryColumnIndexes.length; i < len; i++) {
if (i >= columnIndexes.length) { if (i >= columnIndexes.length) {
// we can still use this index if we are sorting by more than it's columns // we can still use this index if we are sorting by more than it's columns
break; break;
} }
if (columnIndexes[i] != sortOrderIndexes[i] || columnSortTypes[i] != sortOrder.getSortTypes()[i]) { if (columnIndexes[i] != sortOrderQueryColumnIndexes[i] || columnSortTypes[i] != sortOrder.getSortTypes()[i]) {
sortOrderMatches = false; sortOrderMatches = false;
break; break;
} }
coveringCount++; coveringCount++;
} }
if (sortOrderMatches) { if (sortOrderMatches) {
// "coveringCount" makes sure that when we have two // "coveringCount" makes sure that when we have two
// or more covering indexes, we choose the one // or more covering indexes, we choose the one
......
...@@ -51,19 +51,24 @@ public class SortOrder implements Comparator<Value[]> { ...@@ -51,19 +51,24 @@ public class SortOrder implements Comparator<Value[]> {
private static final int DEFAULT_NULL_SORT = SysProperties.SORT_NULLS_HIGH ? 1 : -1; private static final int DEFAULT_NULL_SORT = SysProperties.SORT_NULLS_HIGH ? 1 : -1;
private final Database database; private final Database database;
private final int[] indexes;
/**
* The column indexes of the order by expressions within the query.
*/
private final int[] queryColumnIndexes;
private final int[] sortTypes; private final int[] sortTypes;
/** /**
* Construct a new sort order object. * Construct a new sort order object.
* *
* @param database the database * @param database the database
* @param index the column index list * @param queryColumnIndexes the column index list
* @param sortType the sort order bit masks * @param sortType the sort order bit masks
*/ */
public SortOrder(Database database, int[] index, int[] sortType) { public SortOrder(Database database, int[] queryColumnIndexes, int[] sortType) {
this.database = database; this.database = database;
this.indexes = index; this.queryColumnIndexes = queryColumnIndexes;
this.sortTypes = sortType; this.sortTypes = sortType;
} }
...@@ -78,7 +83,7 @@ public class SortOrder implements Comparator<Value[]> { ...@@ -78,7 +83,7 @@ public class SortOrder implements Comparator<Value[]> {
public String getSQL(Expression[] list, int visible) { public String getSQL(Expression[] list, int visible) {
StatementBuilder buff = new StatementBuilder(); StatementBuilder buff = new StatementBuilder();
int i = 0; int i = 0;
for (int idx : indexes) { for (int idx : queryColumnIndexes) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
if (idx < visible) { if (idx < visible) {
buff.append(idx + 1); buff.append(idx + 1);
...@@ -127,8 +132,8 @@ public class SortOrder implements Comparator<Value[]> { ...@@ -127,8 +132,8 @@ public class SortOrder implements Comparator<Value[]> {
*/ */
@Override @Override
public int compare(Value[] a, Value[] b) { public int compare(Value[] a, Value[] b) {
for (int i = 0, len = indexes.length; i < len; i++) { for (int i = 0, len = queryColumnIndexes.length; i < len; i++) {
int idx = indexes[i]; int idx = queryColumnIndexes[i];
int type = sortTypes[i]; int type = sortTypes[i];
Value ao = a[idx]; Value ao = a[idx];
Value bo = b[idx]; Value bo = b[idx];
...@@ -186,12 +191,18 @@ public class SortOrder implements Comparator<Value[]> { ...@@ -186,12 +191,18 @@ public class SortOrder implements Comparator<Value[]> {
} }
/** /**
* Get the column index list. * Get the column index list. This is the column indexes of the order by
* * expressions within the query.
* <p>
* For the query "select name, id from test order by id, name" this is {1,
* 0} as the first order by expression (the column "id") is the second
* column of the query, and the second order by expression ("name") is the
* first column of the query.
*
* @return the list * @return the list
*/ */
public int[] getIndexes() { public int[] getQueryColumnIndexes() {
return indexes; return queryColumnIndexes;
} }
/** /**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论