Skip to content
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
Expand Up @@ -433,13 +433,6 @@ trait CheckAnalysis extends PredicateHelper with LookupCatalog {

case Offset(offsetExpr, _) => checkLimitLikeClause("offset", offsetExpr)

case o if !o.isInstanceOf[GlobalLimit] && !o.isInstanceOf[LocalLimit]
&& o.children.exists(_.isInstanceOf[Offset]) =>
failAnalysis(
s"""
|The OFFSET clause is allowed in the LIMIT clause or be the outermost node,
|but the OFFSET clause found in: ${o.nodeName}.""".stripMargin.replace("\n", " "))

case Tail(limitExpr, _) => checkLimitLikeClause("tail", limitExpr)

case _: Union | _: SetOperation if operator.children.length > 1 =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,13 +556,6 @@ class AnalysisErrorSuite extends AnalysisTest {
"The offset expression must be equal to or greater than 0, but got -1" :: Nil
)

errorTest(
"OFFSET clause in other node",
testRelation2.offset(Literal(10, IntegerType)).where('b > 1),
"The OFFSET clause is allowed in the LIMIT clause or be the outermost node," +
" but the OFFSET clause found in: Filter." :: Nil
)

errorTest(
"the sum of num_rows in limit clause and num_rows in offset clause less than Int.MaxValue",
testRelation.offset(Literal(2000000000, IntegerType)).limit(Literal(1000000000, IntegerType)),
Expand Down
10 changes: 10 additions & 0 deletions sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2102,6 +2102,16 @@ class Dataset[T] private[sql](
Limit(Literal(n), logicalPlan)
}

/**
* Returns a new Dataset by skipping the first `m` rows.
Copy link
Contributor

Choose a reason for hiding this comment

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

a typo? may be 'n'?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah. this is a minor issue. I will fix it by the way.

*
* @group typedrel
* @since 3.4.0
*/
def offset(n: Int): Dataset[T] = withTypedPlan {
Offset(Literal(n), logicalPlan)
}

/**
* Returns a new Dataset containing union of rows in this Dataset and another Dataset.
*
Expand Down
24 changes: 24 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/DataFrameSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,30 @@ class DataFrameSuite extends QueryTest
)
}

test("offset") {
checkAnswer(
testData.offset(90),
testData.collect().drop(90).toSeq)

checkAnswer(
arrayData.toDF().offset(99),
arrayData.collect().drop(99).map(r => Row.fromSeq(r.productIterator.toSeq)))

checkAnswer(
mapData.toDF().offset(99),
mapData.collect().drop(99).map(r => Row.fromSeq(r.productIterator.toSeq)))
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 test a few combinations? like limit followed by offset, and offset followed by limit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK

}

test("limit with offset") {
checkAnswer(
testData.limit(10).offset(5),
testData.take(10).drop(5).toSeq)

checkAnswer(
testData.offset(5).limit(10),
testData.take(15).drop(5).toSeq)
}

test("udf") {
val foo = udf((a: Int, b: String) => a.toString + b)

Expand Down