-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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-28330][SQL] Support ANSI SQL: result offset clause in query expression #25416
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -399,6 +399,7 @@ queryOrganization | |
(SORT BY sort+=sortItem (',' sort+=sortItem)*)? | ||
windowClause? | ||
(LIMIT (ALL | limit=expression))? | ||
(OFFSET offset=expression)? | ||
; | ||
|
||
multiInsertQueryBody | ||
|
@@ -1008,6 +1009,7 @@ ansiNonReserved | |
| NO | ||
| NULLS | ||
| OF | ||
| OFFSET | ||
| OPTION | ||
| OPTIONS | ||
| OUT | ||
|
@@ -1259,6 +1261,7 @@ nonReserved | |
| NULL | ||
| NULLS | ||
| OF | ||
| OFFSET | ||
| ONLY | ||
| OPTION | ||
| OPTIONS | ||
|
@@ -1520,6 +1523,7 @@ NOT: 'NOT' | '!'; | |
NULL: 'NULL'; | ||
NULLS: 'NULLS'; | ||
OF: 'OF'; | ||
OFFSET: 'OFFSET'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also update doc/sql-keywords.md |
||
ON: 'ON'; | ||
ONLY: 'ONLY'; | ||
OPTION: 'OPTION'; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* 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 | ||
|
||
import org.apache.spark.rdd.RDD | ||
import org.apache.spark.serializer.Serializer | ||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.catalyst.expressions.{Attribute, SortOrder} | ||
import org.apache.spark.sql.catalyst.plans.physical.{Partitioning, SinglePartition} | ||
|
||
|
||
/** | ||
* Skip the first `offset` elements and collect them to a single partition. | ||
* This operator will be used when a logical `Offset` operation is the final operator in an | ||
* logical plan, which happens when the user is collecting results back to the driver. | ||
*/ | ||
case class CollectOffsetExec(offset: Int, child: SparkPlan) extends UnaryExecNode { | ||
|
||
override def output: Seq[Attribute] = child.output | ||
|
||
override def outputPartitioning: Partitioning = SinglePartition | ||
|
||
override def outputOrdering: Seq[SortOrder] = child.outputOrdering | ||
|
||
override def executeCollect(): Array[InternalRow] = child.executeCollect.drop(offset) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we avoid collect everything into driver side? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have a good suggestion? Thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @HyukjinKwon I have resolved this issue collect results to driver. |
||
|
||
private val serializer: Serializer = new UnsafeRowSerializer(child.output.size) | ||
|
||
protected override def doExecute(): RDD[InternalRow] = { | ||
sparkContext.parallelize(executeCollect(), 1) | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Skip the first `offset` elements and collect them to a single partition. | ||
*/ | ||
case class OffsetExec(offset: Int, child: SparkPlan) extends UnaryExecNode { | ||
|
||
override def output: Seq[Attribute] = child.output | ||
|
||
override def outputOrdering: Seq[SortOrder] = child.outputOrdering | ||
|
||
protected override def doExecute(): RDD[InternalRow] = { | ||
val rdd = child.execute() | ||
val arr = rdd.take(offset) | ||
rdd.filter(!arr.contains(_)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need some design about how to implement OFFSET on a distributed data set. The current approach works, but it's not robust as it needs to broadcast We may need to follow how LIMIT is implemented in Spark. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think so. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A rough idea:
If we have accurate per-partition numRecords statistics, we can go step 4 directly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cloud-fan It is a better idea for me. I will reactor the current implement. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cloud-fan There are exists a problem the index of partition and the order of data are inconsistent.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, I want know how to assurance the order? |
||
} | ||
|
||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does SQL standard define the order of LIMIT and OFFSET clauses? Here we force users to write LIMIT clause first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, SQL standard defined the order for
LIMIT
andOFFSET
clauses.LIMIT
at the front ofOFFSET
.The behavior looks contrary to MySQL direction.
MySQL
supportLIMIT 10,10
.