Skip to content

[SPARK-4625] [SQL] Add sort by for DSL & SimpleSqlParser #3481

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

Closed
wants to merge 1 commit 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
Expand Up @@ -85,6 +85,7 @@ class SqlParser extends AbstractSparkSQLParser {
protected val ON = Keyword("ON")
protected val OR = Keyword("OR")
protected val ORDER = Keyword("ORDER")
protected val SORT = Keyword("SORT")
protected val OUTER = Keyword("OUTER")
protected val OVERWRITE = Keyword("OVERWRITE")
protected val REGEXP = Keyword("REGEXP")
Expand Down Expand Up @@ -140,7 +141,7 @@ class SqlParser extends AbstractSparkSQLParser {
(WHERE ~> expression).? ~
(GROUP ~ BY ~> rep1sep(expression, ",")).? ~
(HAVING ~> expression).? ~
(ORDER ~ BY ~> ordering).? ~
sortType.? ~
(LIMIT ~> expression).? ^^ {
case d ~ p ~ r ~ f ~ g ~ h ~ o ~ l =>
val base = r.getOrElse(NoRelation)
Expand All @@ -150,7 +151,7 @@ class SqlParser extends AbstractSparkSQLParser {
.getOrElse(Project(assignAliases(p), withFilter))
val withDistinct = d.map(_ => Distinct(withProjection)).getOrElse(withProjection)
val withHaving = h.map(Filter(_, withDistinct)).getOrElse(withDistinct)
val withOrder = o.map(Sort(_, withHaving)).getOrElse(withHaving)
val withOrder = o.map(_(withHaving)).getOrElse(withHaving)
val withLimit = l.map(Limit(_, withOrder)).getOrElse(withOrder)
withLimit
}
Expand Down Expand Up @@ -202,6 +203,11 @@ class SqlParser extends AbstractSparkSQLParser {
| FULL ~ OUTER.? ^^^ FullOuter
)

protected lazy val sortType: Parser[LogicalPlan => LogicalPlan] =
( ORDER ~ BY ~> ordering ^^ { case o => l: LogicalPlan => Sort(o, l) }
| SORT ~ BY ~> ordering ^^ { case o => l: LogicalPlan => SortPartitions(o, l) }
)

protected lazy val ordering: Parser[Seq[SortOrder]] =
( rep1sep(singleOrder, ",")
| rep1sep(expression, ",") ~ direction.? ^^ {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ package object dsl {

def orderBy(sortExprs: SortOrder*) = Sort(sortExprs, logicalPlan)

def sortBy(sortExprs: SortOrder*) = SortPartitions(sortExprs, logicalPlan)

def groupBy(groupingExprs: Expression*)(aggregateExprs: Expression*) = {
val aliasedExprs = aggregateExprs.map {
case ne: NamedExpression => ne
Expand Down
13 changes: 13 additions & 0 deletions sql/core/src/main/scala/org/apache/spark/sql/SchemaRDD.scala
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,19 @@ class SchemaRDD(
def orderBy(sortExprs: SortOrder*): SchemaRDD =
new SchemaRDD(sqlContext, Sort(sortExprs, logicalPlan))

/**
* Sorts the results by the given expressions within partition.
* {{{
* schemaRDD.sortBy('a)
* schemaRDD.sortBy('a, 'b)
* schemaRDD.sortBy('a.asc, 'b.desc)
* }}}
*
* @group Query
*/
def sortBy(sortExprs: SortOrder*): SchemaRDD =
new SchemaRDD(sqlContext, SortPartitions(sortExprs, logicalPlan))

@deprecated("use limit with integer argument", "1.1.0")
def limit(limitExpr: Expression): SchemaRDD =
new SchemaRDD(sqlContext, Limit(limitExpr, logicalPlan))
Expand Down
18 changes: 18 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/DslQuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ class DslQuerySuite extends QueryTest {
mapData.collect().sortBy(_.data(1)).reverse.toSeq)
}

test("sorting #2") {
checkAnswer(
testData2.sortBy('a.asc, 'b.asc),
Seq((1,1), (1,2), (2,1), (2,2), (3,1), (3,2)))

checkAnswer(
testData2.sortBy('a.asc, 'b.desc),
Seq((1,2), (1,1), (2,2), (2,1), (3,2), (3,1)))

checkAnswer(
testData2.sortBy('a.desc, 'b.desc),
Seq((3,2), (3,1), (2,2), (2,1), (1,2), (1,1)))

checkAnswer(
testData2.sortBy('a.desc, 'b.asc),
Seq((3,1), (3,2), (2,1), (2,2), (1,1), (1,2)))
}

test("limit") {
checkAnswer(
testData.limit(10),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class SQLQuerySuite extends QueryTest with BeforeAndAfterAll {
TimeZone.setDefault(origZone)
}

test("SPARK-4625 support SORT BY in SimpleSQLParser & DSL") {
checkAnswer(
sql("SELECT a FROM testData2 SORT BY a"),
Seq(1, 1, 2 ,2 ,3 ,3).map(Seq(_))
)
}

test("grouping on nested fields") {
jsonRDD(sparkContext.parallelize("""{"nested": {"attribute": 1}, "value": 2}""" :: Nil))
.registerTempTable("rows")
Expand Down