Skip to content

Commit

Permalink
[native] Fix 'Number of sorting keys must be greater than zero'
Browse files Browse the repository at this point in the history
A query with row_number() window function using same partitioning and sorting
keys and a limit on row number values used to fail with

```
Caused by: java.lang.RuntimeException: sortingKeys_.size() > 0 (0 vs. 0) Number of sorting keys must be greater than zero
```

This happened because Prestissimo dropped sorting keys from Presto's
TopNRowNumberNode and tried to create TopNRowNumberNode in Velox with no
sorting keys. The fix is to use Velox's RowNumberNode when all sorting keys are
present as partitioning keys.
  • Loading branch information
mbasmanova committed Feb 29, 2024
1 parent 61794f0 commit cec0968
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2683,7 +2683,7 @@ VeloxQueryPlanConverterBase::toVeloxQueryPlan(
toVeloxQueryPlan(node->source, tableWriteInfo, taskId));
}

std::shared_ptr<const velox::core::TopNRowNumberNode>
std::shared_ptr<const velox::core::PlanNode>
VeloxQueryPlanConverterBase::toVeloxQueryPlan(
const std::shared_ptr<const protocol::TopNRowNumberNode>& node,
const std::shared_ptr<protocol::TableWriteInfo>& tableWriteInfo,
Expand All @@ -2705,6 +2705,18 @@ VeloxQueryPlanConverterBase::toVeloxQueryPlan(
if (!node->partial) {
rowNumberColumnName = node->rowNumberVariable.name;
}

if (sortFields.empty()) {
// May happen if all sorting keys are also used as partition keys.

return std::make_shared<core::RowNumberNode>(
node->id,
partitionFields,
rowNumberColumnName,
node->maxRowCountPerPartition,
toVeloxQueryPlan(node->source, tableWriteInfo, taskId));
}

return std::make_shared<core::TopNRowNumberNode>(
node->id,
partitionFields,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class VeloxQueryPlanConverterBase {
const std::shared_ptr<protocol::TableWriteInfo>& tableWriteInfo,
const protocol::TaskId& taskId);

std::shared_ptr<const velox::core::TopNRowNumberNode> toVeloxQueryPlan(
std::shared_ptr<const velox::core::PlanNode> toVeloxQueryPlan(
const std::shared_ptr<const protocol::TopNRowNumberNode>& node,
const std::shared_ptr<protocol::TableWriteInfo>& tableWriteInfo,
const protocol::TaskId& taskId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,8 @@ public void testOverlappingPartitionAndSortingKeys()
assertQuery("SELECT row_number() OVER (PARTITION BY orderdate ORDER BY orderdate) FROM orders");
assertQuery("SELECT min(orderkey) OVER (PARTITION BY orderdate ORDER BY orderdate, totalprice) FROM orders");
assertQuery("SELECT * FROM (SELECT row_number() over(partition by orderstatus order by orderkey, orderstatus) rn, * from orders) WHERE rn = 1");

assertQuery("WITH t AS (SELECT linenumber, row_number() over (partition by linenumber order by linenumber) as rn FROM lineitem) " +
"SELECT * FROM t WHERE rn = 1");
}
}

0 comments on commit cec0968

Please sign in to comment.