-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
265 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
package doric.sem | ||
|
||
trait All extends AggregationOps with TransformOps with JoinOps with CollectOps | ||
trait All | ||
extends AggregationOps | ||
with TransformOps | ||
with JoinOps | ||
with CollectOps | ||
with SortingOps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package doric.sem | ||
|
||
import cats.implicits._ | ||
import doric.DoricColumn | ||
import org.apache.spark.sql.DataFrame | ||
|
||
private[sem] trait SortingOps { | ||
|
||
implicit class DataframeSortSyntax(df: DataFrame) { | ||
|
||
def sort(col: DoricColumn[_]*): DataFrame = | ||
col.toList | ||
.traverse(_.elem) | ||
.run(df) | ||
.map(col => df.sort(col: _*)) | ||
.returnOrThrow("sort") | ||
|
||
def orderBy(col: DoricColumn[_]*): DataFrame = sort(col: _*) | ||
|
||
def sortWithinPartitions(col: DoricColumn[_]*): DataFrame = | ||
col.toList | ||
.traverse(_.elem) | ||
.run(df) | ||
.map(col => df.sortWithinPartitions(col: _*)) | ||
.returnOrThrow("sortWithPartitions") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package doric.sem | ||
|
||
import doric.{DoricTestElements, colInt, colString} | ||
import org.apache.spark.sql.Row | ||
|
||
class SortingOpsSpec extends DoricTestElements { | ||
import spark.implicits._ | ||
|
||
describe("Sort") { | ||
it("sorts a dataframe with sort function on one column") { | ||
val df = List((1, "a"), (2, "b"), (3, "c"), (4, "d")).toDF("col1", "col2") | ||
|
||
val res = df.sort(colInt("col1").desc) | ||
val actual = List(Row(4, "d"), Row(3, "c"), Row(2, "b"), Row(1, "a")) | ||
|
||
res.collect().toList should contain theSameElementsInOrderAs actual | ||
} | ||
|
||
it("sorts a dataframe with sort function on multiple columns") { | ||
val df = List((1, "z"), (2, "n"), (3, "x"), (2, "f")).toDF("col1", "col2") | ||
|
||
val res = df.sort(colInt("col1").desc, colString("col2").asc) | ||
val actual = List(Row(3, "x"), Row(2, "f"), Row(2, "n"), Row(1, "z")) | ||
|
||
res.collect().toList should contain theSameElementsInOrderAs actual | ||
} | ||
} | ||
|
||
describe("Sort Within Partitions") { | ||
it("sorts dataframe partitions with sort function on one column") { | ||
val df = List((1, "a"), (2, "b"), (3, "c"), (4, "d")) | ||
.toDF("col1", "col2") | ||
.repartition(2) | ||
|
||
val res = df.sortWithinPartitions(colInt("col1").asc) | ||
val actual = List(Row(3, "c"), Row(4, "d"), Row(1, "a"), Row(2, "b")) | ||
|
||
res.collect().toList should contain theSameElementsInOrderAs actual | ||
} | ||
|
||
it("sorts dataframe partitions with sort function on multiple columns") { | ||
val df = List((1, "z"), (2, "n"), (3, "x"), (2, "f"), (2, "z")) | ||
.toDF("col1", "col2") | ||
.repartition(2) | ||
|
||
val res = | ||
df.sortWithinPartitions(colInt("col1").desc, colString("col2").asc) | ||
val actual = | ||
List(Row(2, "n"), Row(2, "z"), Row(1, "z"), Row(3, "x"), Row(2, "f")) | ||
|
||
res.collect().toList should contain theSameElementsInOrderAs actual | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters