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