hqx871 commented on a change in pull request #2363:
URL: https://github.com/apache/calcite/pull/2363#discussion_r591302751
##########
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 make a simple query and found the LogicalSort will be implemented to
EnumerableLimit and EnumerableSort. The offset and fetch will be both null, so
the sort cost is still nLogN.
Query:
select * from foodmart.sales_fact_1997 order by cust_id limit 10
Plan:
EnumerableLimit(fetch=[10]): rowcount = 10.0, cumulative cost = {210.0 rows,
3795.1361487904733 cpu, 0.0 io}, id = 35
EnumerableSort(sort0=[$0], dir0=[ASC]): rowcount = 100.0, cumulative cost
= {200.0 rows, 3785.1361487904733 cpu, 0.0 io}, id = 34
EnumerableTableScan(table=[[foodmart, sales_fact_1997]]): rowcount =
100.0, cumulative cost = {100.0 rows, 101.0 cpu, 0.0 io}, id = 20
I also checked the EnumerableSort code, as follow:
public EnumerableSort(RelOptCluster cluster, RelTraitSet traitSet,
RelNode input, RelCollation collation, @Nullable RexNode offset,
@Nullable RexNode fetch) {
super(cluster, traitSet, input, collation, offset, fetch);
assert getConvention() instanceof EnumerableConvention;
assert getConvention() == input.getConvention();
assert fetch == null : "fetch must be null";
assert offset == null : "offset must be null";
}
----------------------------------------------------------------
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]