beliefer commented on a change in pull request #27237: [SPARK-28330][SQL]
Support ANSI SQL: result offset clause in query expression
URL: https://github.com/apache/spark/pull/27237#discussion_r367368537
##########
File path: sql/core/src/main/scala/org/apache/spark/sql/execution/limit.scala
##########
@@ -125,40 +138,92 @@ trait BaseLimitExec extends LimitExec with
CodegenSupport {
child.asInstanceOf[CodegenSupport].produce(ctx, this)
}
- override def doConsume(ctx: CodegenContext, input: Seq[ExprCode], row:
ExprCode): String = {
- // The counter name is already obtained by the upstream operators via
`limitNotReachedChecks`.
- // Here we have to inline it to not change its name. This is fine as we
won't have many limit
- // operators in one query.
- ctx.addMutableState(CodeGenerator.JAVA_INT, countTerm, forceInline = true,
useFreshName = false)
- s"""
- | if ($countTerm < $limit) {
- | $countTerm += 1;
- | ${consume(ctx, input)}
- | }
- """.stripMargin
- }
}
/**
- * Take the first `limit` elements of each child partition, but do not collect
or shuffle them.
+ * Skip the first `offset` elements then take the first `limit` of the
following elements in
+ * each child partition, but do not collect or shuffle them.
*/
-case class LocalLimitExec(limit: Int, child: SparkPlan) extends BaseLimitExec {
+case class LocalLimitExec(limit: Int, child: SparkPlan, offset: Int = 0)
extends BaseLimitExec {
override def outputOrdering: Seq[SortOrder] = child.outputOrdering
override def outputPartitioning: Partitioning = child.outputPartitioning
+
+ override def doExecute(): RDD[InternalRow] = {
+ if (limit == Limit.INVALID_LIMIT.value) {
+ child.execute()
+ } else {
+ child.execute().mapPartitions { iter => iter.take(limit + offset)}
+ }
+ }
+
+ override def doConsume(ctx: CodegenContext, input: Seq[ExprCode], row:
ExprCode): String = {
+ // The counter name is already obtained by the upstream operators via
`limitNotReachedChecks`.
+ // Here we have to inline it to not change its name. This is fine as we
won't have many limit
+ // operators in one query.
+ ctx.addMutableState(CodeGenerator.JAVA_INT, countTerm, forceInline = true,
useFreshName = false)
+ ctx.addMutableState(CodeGenerator.JAVA_INT, skipTerm, forceInline = true,
useFreshName = false)
+ if (limit == Limit.INVALID_LIMIT.value) {
+ s"${consume(ctx, input)}"
+ } else {
+ s"""
+ | if ($countTerm < ${limit + offset}) {
Review comment:
ditto
----------------------------------------------------------------
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]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]