diff --git a/backends-velox/src/test/scala/org/apache/gluten/execution/VeloxWindowExpressionSuite.scala b/backends-velox/src/test/scala/org/apache/gluten/execution/VeloxWindowExpressionSuite.scala index 03b295f4983f..7af17c4cd151 100644 --- a/backends-velox/src/test/scala/org/apache/gluten/execution/VeloxWindowExpressionSuite.scala +++ b/backends-velox/src/test/scala/org/apache/gluten/execution/VeloxWindowExpressionSuite.scala @@ -73,6 +73,20 @@ class VeloxWindowExpressionSuite extends WholeStageTransformerSuite { } } + test("test overlapping partition and sorting keys") { + runAndCompare( + """ + |WITH t AS ( + |SELECT + | l_linenumber, + | row_number() over (partition by l_linenumber order by l_linenumber) as rn + |FROM lineitem + |) + |SELECT * FROM t WHERE rn = 1 + |""".stripMargin + ) {} + } + test("collect_list / collect_set") { withTable("t") { val data = Seq( diff --git a/cpp/velox/substrait/SubstraitToVeloxPlan.cc b/cpp/velox/substrait/SubstraitToVeloxPlan.cc index 1604c15e338a..bd545a960947 100644 --- a/cpp/velox/substrait/SubstraitToVeloxPlan.cc +++ b/cpp/velox/substrait/SubstraitToVeloxPlan.cc @@ -1024,6 +1024,14 @@ core::PlanNodePtr SubstraitToVeloxPlanConverter::toVeloxPlan( } } const std::optional rowNumberColumnName = std::nullopt; + + if (sortingKeys.empty()) { + // Handle if all sorting keys are also used as partition keys. + + return std::make_shared( + nextPlanNodeId(), partitionKeys, rowNumberColumnName, (int32_t)windowGroupLimitRel.limit(), childNode); + } + return std::make_shared( nextPlanNodeId(), partitionKeys,