Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-44340][SQL] Define the computing logic through PartitionEvaluator API and use it in WindowGroupLimitExec #41899

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.execution.window

import org.apache.spark.{PartitionEvaluator, PartitionEvaluatorFactory}
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.{Attribute, DenseRank, Expression, Rank, RowNumber, SortOrder}
import org.apache.spark.sql.execution.metric.SQLMetric

class WindowGroupLimitEvaluatorFactory(
partitionSpec: Seq[Expression],
orderSpec: Seq[SortOrder],
rankLikeFunction: Expression,
limit: Int,
childOutput: Seq[Attribute],
numOutputRows: SQLMetric)
extends PartitionEvaluatorFactory[InternalRow, InternalRow] {

override def createEvaluator(): PartitionEvaluator[InternalRow, InternalRow] = {
val limitFunc = rankLikeFunction match {
case _: RowNumber =>
(iter: Iterator[InternalRow]) => SimpleLimitIterator(iter, limit, numOutputRows)
case _: Rank =>
(iter: Iterator[InternalRow]) =>
RankLimitIterator(childOutput, iter, orderSpec, limit, numOutputRows)
case _: DenseRank =>
(iter: Iterator[InternalRow]) =>
DenseRankLimitIterator(childOutput, iter, orderSpec, limit, numOutputRows)
}

if (partitionSpec.isEmpty) {
new WindowGroupLimitPartitionEvaluator(limitFunc)
} else {
new WindowGroupLimitPartitionEvaluator(
input => new GroupedLimitIterator(input, childOutput, partitionSpec, limitFunc))
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we create the function in the match and then call `new WindowGroupLimitPartitionEvaluator(f) outside the match expression?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

class WindowGroupLimitPartitionEvaluator(f: Iterator[InternalRow] => Iterator[InternalRow])
extends PartitionEvaluator[InternalRow, InternalRow] {

override def eval(
partitionIndex: Int,
inputs: Iterator[InternalRow]*): Iterator[InternalRow] = {
f(inputs.head)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package org.apache.spark.sql.execution.window

import org.apache.spark.rdd.RDD
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.{Ascending, Attribute, DenseRank, Expression, Rank, RowNumber, SortOrder, UnsafeProjection, UnsafeRow}
import org.apache.spark.sql.catalyst.expressions.{Ascending, Attribute, Expression, SortOrder, UnsafeProjection, UnsafeRow}
import org.apache.spark.sql.catalyst.expressions.codegen.GenerateOrdering
import org.apache.spark.sql.catalyst.plans.physical.{AllTuples, ClusteredDistribution, Distribution, Partitioning}
import org.apache.spark.sql.execution.{SparkPlan, UnaryExecNode}
Expand Down Expand Up @@ -73,26 +73,23 @@ case class WindowGroupLimitExec(

protected override def doExecute(): RDD[InternalRow] = {
val numOutputRows = longMetric("numOutputRows")
rankLikeFunction match {
case _: RowNumber if partitionSpec.isEmpty =>
child.execute().mapPartitionsInternal(SimpleLimitIterator(_, limit, numOutputRows))
case _: RowNumber =>
child.execute().mapPartitionsInternal(new GroupedLimitIterator(_, output, partitionSpec,
(input: Iterator[InternalRow]) => SimpleLimitIterator(input, limit, numOutputRows)))
case _: Rank if partitionSpec.isEmpty =>
child.execute().mapPartitionsInternal(
RankLimitIterator(output, _, orderSpec, limit, numOutputRows))
case _: Rank =>
child.execute().mapPartitionsInternal(new GroupedLimitIterator(_, output, partitionSpec,
(input: Iterator[InternalRow]) =>
RankLimitIterator(output, input, orderSpec, limit, numOutputRows)))
case _: DenseRank if partitionSpec.isEmpty =>
child.execute().mapPartitionsInternal(
DenseRankLimitIterator(output, _, orderSpec, limit, numOutputRows))
case _: DenseRank =>
child.execute().mapPartitionsInternal(new GroupedLimitIterator(_, output, partitionSpec,
(input: Iterator[InternalRow]) =>
DenseRankLimitIterator(output, input, orderSpec, limit, numOutputRows)))

val evaluatorFactory =
new WindowGroupLimitEvaluatorFactory(
partitionSpec,
orderSpec,
rankLikeFunction,
limit,
child.output,
numOutputRows)

if (conf.usePartitionEvaluator) {
child.execute().mapPartitionsWithEvaluator(evaluatorFactory)
} else {
child.execute().mapPartitionsInternal { iter =>
val evaluator = evaluatorFactory.createEvaluator()
evaluator.eval(0, iter)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@beliefer, Can you please raise a follow-up PR to handle the partition index as this #42185

}
}
}

Expand Down
Loading