hqx871 commented on a change in pull request #2363:
URL: https://github.com/apache/calcite/pull/2363#discussion_r591354987
##########
File path: core/src/main/java/org/apache/calcite/rel/core/Sort.java
##########
@@ -124,11 +125,19 @@ public abstract Sort copy(RelTraitSet traitSet, RelNode
newInput,
@Override public @Nullable RelOptCost computeSelfCost(RelOptPlanner planner,
RelMetadataQuery mq) {
+ double rowCount = mq.getRowCount(this);
+ if (collation.getFieldCollations().isEmpty()) {
+ return planner.getCostFactory().makeCost(rowCount, 0, 0);
+ }
+ final int offsetValue = offset == null ? 0 : RexLiteral.intValue(offset);
+ final double inCount = mq.getRowCount(input);
// Higher cost if rows are wider discourages pushing a project through a
// sort.
- final double rowCount = mq.getRowCount(this);
final double bytesPerRow = getRowType().getFieldCount() * 4;
- final double cpu = Util.nLogN(rowCount) * bytesPerRow;
+ // When output count + offset is smaller than input count, we can use heap
sort,
+ // otherwise, we can use heap sort, quick sort and so on
+ final double heapSize = Math.min(rowCount + offsetValue, inCount);
Review comment:
I found duplicate code in EnumerableLimitSort. Shall I close the pr or
change the EnumerableLimitSort. code as follow
```
@Override public @Nullable RelOptCost computeSelfCost(RelOptPlanner planner,
RelMetadataQuery mq) {
final double rowCount = mq.getRowCount(this.input).doubleValue();
double toSort = getValue(this.fetch, rowCount);
if (this.offset != null) {
toSort += getValue(this.offset, rowCount);
}
// we need to sort at most rowCount rows
toSort = Math.min(rowCount, toSort);
// we need to process rowCount rows, and for every row
// we search the key in a TreeMap with at most toSort entries
final double lookup = Math.max(1., Math.log(toSort));
final double bytesPerRow = this.getRowType().getFieldCount() * 4.;
final double cpu = (rowCount * lookup) * bytesPerRow;
RelOptCost cost = planner.getCostFactory().makeCost(rowCount, cpu, 0);
return cost;
}
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]