提交 62f49f0b authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Do not sort index-sorted results with WITH TIES again

上级 bcdfb4a8
......@@ -774,7 +774,7 @@ public class Select extends Query {
}
boolean fetchPercent = this.fetchPercent;
if (fetchPercent) {
// Need to check it row, because negative limit has special treatment later
// Need to check it now, because negative limit has special treatment later
if (limitRows < 0 || limitRows > 100) {
throw DbException.getInvalidValueException("FETCH PERCENT", limitRows);
}
......@@ -803,7 +803,7 @@ public class Select extends Query {
}
// Do not add rows before OFFSET to result if possible
boolean quickOffset = !fetchPercent;
if (sort != null && (!sortUsingIndex || isAnyDistinct() || withTies)) {
if (sort != null && (!sortUsingIndex || isAnyDistinct())) {
result = createLocalResult(result);
result.setSortOrder(sort);
if (!sortUsingIndex) {
......@@ -886,7 +886,9 @@ public class Select extends Query {
if (limitRows >= 0) {
result.setLimit(limitRows);
result.setFetchPercent(fetchPercent);
result.setWithTies(withTies);
if (withTies) {
result.setWithTies(sort);
}
}
if (result != null) {
result.done();
......
......@@ -264,7 +264,9 @@ public class SelectUnion extends Query {
if (v != ValueNull.INSTANCE) {
result.setLimit(v.getInt());
result.setFetchPercent(fetchPercent);
result.setWithTies(withTies);
if (withTies) {
result.setWithTies(sort);
}
}
}
l.close();
......
......@@ -25,7 +25,10 @@ public interface LocalResult extends ResultInterface, ResultTarget {
public void setMaxMemoryRows(int maxValue);
/**
* @param sort Sort order.
* Sets sort order to be used by this result. When rows are presorted by the
* query this method should not be used.
*
* @param sort the sort order
*/
public void setSortOrder(SortOrder sort);
......@@ -82,9 +85,14 @@ public interface LocalResult extends ResultInterface, ResultTarget {
public void setFetchPercent(boolean fetchPercent);
/**
* @param withTies whether tied rows should be included in result too
* Enables inclusion of tied rows to result and sets the sort order for tied
* rows. The specified sort order must be the same as sort order if sort
* order was set. Passed value will be used if sort order was not set that
* is possible when rows are presorted.
*
* @param withTiesSortOrder the sort order for tied rows
*/
public void setWithTies(boolean withTies);
public void setWithTies(SortOrder withTiesSortOrder);
/**
* Set the offset of the first row to return.
......
......@@ -38,7 +38,7 @@ public class LocalResultImpl implements LocalResult {
private int offset;
private int limit = -1;
private boolean fetchPercent;
private boolean withTies;
private SortOrder withTiesSortOrder;
private boolean limitsWereApplied;
private ResultExternal external;
private boolean distinct;
......@@ -132,11 +132,6 @@ public class LocalResultImpl implements LocalResult {
return copy;
}
/**
* Set the sort order.
*
* @param sort the sort order
*/
@Override
public void setSortOrder(SortOrder sort) {
this.sort = sort;
......@@ -365,8 +360,8 @@ public class LocalResultImpl implements LocalResult {
if (isAnyDistinct()) {
rows = distinctRows.values();
}
if (sort != null && limit != 0) {
boolean withLimit = limit > 0 && !withTies;
if (sort != null && limit != 0 && !limitsWereApplied) {
boolean withLimit = limit > 0 && withTiesSortOrder == null;
if (offset > 0 || withLimit) {
sort.sort(rows, offset, withLimit ? limit : rows.size());
} else {
......@@ -412,9 +407,9 @@ public class LocalResultImpl implements LocalResult {
return;
}
int to = offset + limit;
if (withTies && sort != null) {
if (withTiesSortOrder != null) {
Value[] expected = rows.get(to - 1);
while (to < rows.size() && sort.compare(expected, rows.get(to)) == 0) {
while (to < rows.size() && withTiesSortOrder.compare(expected, rows.get(to)) == 0) {
to++;
rowCount++;
}
......@@ -448,9 +443,9 @@ public class LocalResultImpl implements LocalResult {
addRowsToDisk();
}
}
if (withTies && sort != null && row != null) {
if (withTiesSortOrder != null && row != null) {
Value[] expected = row;
while ((row = temp.next()) != null && sort.compare(expected, row) == 0) {
while ((row = temp.next()) != null && withTiesSortOrder.compare(expected, row) == 0) {
rows.add(row);
rowCount++;
if (rows.size() > maxMemoryRows) {
......@@ -497,12 +492,10 @@ public class LocalResultImpl implements LocalResult {
this.fetchPercent = fetchPercent;
}
/**
* @param withTies whether tied rows should be included in result too
*/
@Override
public void setWithTies(boolean withTies) {
this.withTies = withTies;
public void setWithTies(SortOrder withTiesSortOrder) {
assert sort == null || sort == withTiesSortOrder;
this.withTiesSortOrder = withTiesSortOrder;
}
@Override
......
......@@ -27,9 +27,9 @@ public interface ResultTarget {
int getRowCount();
/**
* A hint that offset and limit may be ignored by this result because they
* were applied during the query. This is useful for WITH TIES clause
* because result may contain tied rows above limit.
* A hint that sorting, offset and limit may be ignored by this result
* because they were applied during the query. This is useful for WITH TIES
* clause because result may contain tied rows above limit.
*/
void limitsWereApplied();
......
......@@ -806,3 +806,4 @@ econd irst bcef ordinality nord unnest
analyst occupation distributive josaph aor engineer sajeewa isuru randil kevin doctor businessman artist ashan
corrupts splitted disruption unintentional octets preconditions predicates subq objectweb insn opcodes
preserves masking holder unboxing avert iae transformed subtle reevaluate exclusions subclause ftbl rgr
presorted inclusion
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论