Skip to content

Commit

Permalink
Deduplicate sorting keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ulysses-you committed Jul 5, 2024
1 parent 995145e commit ddc7785
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1911,4 +1911,10 @@ class TestOperator extends VeloxWholeStageTransformerSuite with AdaptiveSparkPla
val resultLength = df.collect().length
assert(resultLength > 25000 && resultLength < 35000)
}

test("Deduplicate sorting keys") {
runQueryAndCompare("select * from lineitem order by l_orderkey, l_orderkey") {
checkGlutenOperatorMatch[SortExecTransformer]
}
}
}
16 changes: 7 additions & 9 deletions cpp/velox/substrait/SubstraitToVeloxPlan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1046,17 +1046,15 @@ SubstraitToVeloxPlanConverter::processSortField(
const RowTypePtr& inputType) {
std::vector<core::FieldAccessTypedExprPtr> sortingKeys;
std::vector<core::SortOrder> sortingOrders;
sortingKeys.reserve(sortFields.size());
sortingOrders.reserve(sortFields.size());

std::unordered_set<std::string> uniqueKeys;
for (const auto& sort : sortFields) {
sortingOrders.emplace_back(toSortOrder(sort));

if (sort.has_expr()) {
auto expression = exprConverter_->toVeloxExpr(sort.expr(), inputType);
auto fieldExpr = std::dynamic_pointer_cast<const core::FieldAccessTypedExpr>(expression);
VELOX_USER_CHECK_NOT_NULL(fieldExpr, "Sort Operator only supports field sorting key");
GLUTEN_CHECK(sort.has_expr(), "Sort field must have expr");
auto expression = exprConverter_->toVeloxExpr(sort.expr(), inputType);
auto fieldExpr = std::dynamic_pointer_cast<const core::FieldAccessTypedExpr>(expression);
VELOX_USER_CHECK_NOT_NULL(fieldExpr, "Sort Operator only supports field sorting key");
if (uniqueKeys.insert(fieldExpr->name()).second) {
sortingKeys.emplace_back(fieldExpr);
sortingOrders.emplace_back(toSortOrder(sort));
}
}
return {sortingKeys, sortingOrders};
Expand Down
1 change: 1 addition & 0 deletions cpp/velox/substrait/SubstraitToVeloxPlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ class SubstraitToVeloxPlanConverter {

/// Helper Function to convert Substrait sortField to Velox sortingKeys and
/// sortingOrders.
/// Note that, this method would deduplicate the sorting keys which have the same field name.
std::pair<std::vector<core::FieldAccessTypedExprPtr>, std::vector<core::SortOrder>> processSortField(
const ::google::protobuf::RepeatedPtrField<::substrait::SortField>& sortField,
const RowTypePtr& inputType);
Expand Down

0 comments on commit ddc7785

Please sign in to comment.