-
-
Notifications
You must be signed in to change notification settings - Fork 137
Sort columns #236
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
Merged
Merged
Sort columns #236
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e2c4b9e
Add dataset orderBy
frosforever 4448951
flush out tests a bit
frosforever 66e5029
fail compile test on unsortable
frosforever cf40f4b
add comment about currently failling tests with CatalystOrdered
frosforever c434660
clean up
frosforever 5d82a06
gut option and collection sorting
frosforever 1be4288
use frameless internals rather than explicit SortOrder
frosforever 7df2ae6
add sortWithinPartitionN and Many
frosforever 22d432b
add sortWithinPartitionsTest
frosforever 5102003
add tests for default ordering in Many sorts
frosforever e866091
clean up untyped expr
frosforever cc288aa
and another removal of explicit SortOrder
frosforever File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,161 @@ | ||
| package frameless | ||
|
|
||
| import org.scalacheck.Prop | ||
| import org.scalacheck.Prop._ | ||
| import org.scalatest.Matchers | ||
| import shapeless.test.illTyped | ||
| import org.apache.spark.sql.Column | ||
|
|
||
| class OrderByTests extends TypedDatasetSuite with Matchers { | ||
| def sortings[A : CatalystOrdered, T]: Seq[(TypedColumn[T, A] => SortedTypedColumn[T, A], Column => Column)] = Seq( | ||
| (_.desc, _.desc), | ||
| (_.asc, _.asc), | ||
| (t => t, t => t) //default ascending | ||
| ) | ||
|
|
||
| test("single column non nullable orderBy") { | ||
| def prop[A: TypedEncoder : CatalystOrdered](data: Vector[X1[A]]): Prop = { | ||
| val ds = TypedDataset.create(data) | ||
|
|
||
| sortings[A, X1[A]].map { case (typ, untyp) => | ||
| ds.dataset.orderBy(untyp(ds.dataset.col("a"))).collect().toVector.?=( | ||
| ds.orderBy(typ(ds('a))).collect().run().toVector) | ||
| }.reduce(_ && _) | ||
| } | ||
|
|
||
| check(forAll(prop[Int] _)) | ||
| check(forAll(prop[Boolean] _)) | ||
| check(forAll(prop[Byte] _)) | ||
| check(forAll(prop[Short] _)) | ||
| check(forAll(prop[Long] _)) | ||
| check(forAll(prop[Float] _)) | ||
| check(forAll(prop[Double] _)) | ||
| check(forAll(prop[SQLDate] _)) | ||
| check(forAll(prop[SQLTimestamp] _)) | ||
| check(forAll(prop[String] _)) | ||
| } | ||
|
|
||
| test("single column non nullable partition sorting") { | ||
| def prop[A: TypedEncoder : CatalystOrdered](data: Vector[X1[A]]): Prop = { | ||
| val ds = TypedDataset.create(data) | ||
|
|
||
| sortings[A, X1[A]].map { case (typ, untyp) => | ||
| ds.dataset.sortWithinPartitions(untyp(ds.dataset.col("a"))).collect().toVector.?=( | ||
| ds.sortWithinPartitions(typ(ds('a))).collect().run().toVector) | ||
| }.reduce(_ && _) | ||
| } | ||
|
|
||
| check(forAll(prop[Int] _)) | ||
| check(forAll(prop[Boolean] _)) | ||
| check(forAll(prop[Byte] _)) | ||
| check(forAll(prop[Short] _)) | ||
| check(forAll(prop[Long] _)) | ||
| check(forAll(prop[Float] _)) | ||
| check(forAll(prop[Double] _)) | ||
| check(forAll(prop[SQLDate] _)) | ||
| check(forAll(prop[SQLTimestamp] _)) | ||
| check(forAll(prop[String] _)) | ||
| } | ||
|
|
||
| test("two columns non nullable orderBy") { | ||
| def prop[A: TypedEncoder : CatalystOrdered, B: TypedEncoder : CatalystOrdered](data: Vector[X2[A,B]]): Prop = { | ||
| val ds = TypedDataset.create(data) | ||
|
|
||
| sortings[A, X2[A, B]].reverse.zip(sortings[B, X2[A, B]]).map { case ((typA, untypA), (typB, untypB)) => | ||
| val vanillaSpark = ds.dataset.orderBy(untypA(ds.dataset.col("a")), untypB(ds.dataset.col("b"))).collect().toVector | ||
| vanillaSpark.?=(ds.orderBy(typA(ds('a)), typB(ds('b))).collect().run().toVector).&&( | ||
| vanillaSpark ?= ds.orderByMany(typA(ds('a)), typB(ds('b))).collect().run().toVector | ||
| ) | ||
| }.reduce(_ && _) | ||
| } | ||
|
|
||
| check(forAll(prop[SQLDate, Long] _)) | ||
| check(forAll(prop[String, Boolean] _)) | ||
| check(forAll(prop[SQLTimestamp, Long] _)) | ||
| } | ||
|
|
||
| test("two columns non nullable partition sorting") { | ||
| def prop[A: TypedEncoder : CatalystOrdered, B: TypedEncoder : CatalystOrdered](data: Vector[X2[A,B]]): Prop = { | ||
| val ds = TypedDataset.create(data) | ||
|
|
||
| sortings[A, X2[A, B]].reverse.zip(sortings[B, X2[A, B]]).map { case ((typA, untypA), (typB, untypB)) => | ||
| val vanillaSpark = ds.dataset.sortWithinPartitions(untypA(ds.dataset.col("a")), untypB(ds.dataset.col("b"))).collect().toVector | ||
| vanillaSpark.?=(ds.sortWithinPartitions(typA(ds('a)), typB(ds('b))).collect().run().toVector).&&( | ||
| vanillaSpark ?= ds.sortWithinPartitionsMany(typA(ds('a)), typB(ds('b))).collect().run().toVector | ||
| ) | ||
| }.reduce(_ && _) | ||
| } | ||
|
|
||
| check(forAll(prop[SQLDate, Long] _)) | ||
| check(forAll(prop[String, Boolean] _)) | ||
| check(forAll(prop[SQLTimestamp, Long] _)) | ||
| } | ||
|
|
||
| test("three columns non nullable orderBy") { | ||
| def prop[A: TypedEncoder : CatalystOrdered, B: TypedEncoder : CatalystOrdered](data: Vector[X3[A,B,A]]): Prop = { | ||
| val ds = TypedDataset.create(data) | ||
|
|
||
| sortings[A, X3[A, B, A]].reverse | ||
| .zip(sortings[B, X3[A, B, A]]) | ||
| .zip(sortings[A, X3[A, B, A]]) | ||
| .map { case (((typA, untypA), (typB, untypB)), (typA2, untypA2)) => | ||
| val vanillaSpark = ds.dataset | ||
| .orderBy(untypA(ds.dataset.col("a")), untypB(ds.dataset.col("b")), untypA2(ds.dataset.col("c"))) | ||
| .collect().toVector | ||
|
|
||
| vanillaSpark.?=(ds.orderBy(typA(ds('a)), typB(ds('b)), typA2(ds('c))).collect().run().toVector).&&( | ||
| vanillaSpark ?= ds.orderByMany(typA(ds('a)), typB(ds('b)), typA2(ds('c))).collect().run().toVector | ||
| ) | ||
| }.reduce(_ && _) | ||
| } | ||
|
|
||
| check(forAll(prop[SQLDate, Long] _)) | ||
| check(forAll(prop[String, Boolean] _)) | ||
| check(forAll(prop[SQLTimestamp, Long] _)) | ||
| } | ||
|
|
||
| test("three columns non nullable partition sorting") { | ||
| def prop[A: TypedEncoder : CatalystOrdered, B: TypedEncoder : CatalystOrdered](data: Vector[X3[A,B,A]]): Prop = { | ||
| val ds = TypedDataset.create(data) | ||
|
|
||
| sortings[A, X3[A, B, A]].reverse | ||
| .zip(sortings[B, X3[A, B, A]]) | ||
| .zip(sortings[A, X3[A, B, A]]) | ||
| .map { case (((typA, untypA), (typB, untypB)), (typA2, untypA2)) => | ||
| val vanillaSpark = ds.dataset | ||
| .sortWithinPartitions(untypA(ds.dataset.col("a")), untypB(ds.dataset.col("b")), untypA2(ds.dataset.col("c"))) | ||
| .collect().toVector | ||
|
|
||
| vanillaSpark.?=(ds.sortWithinPartitions(typA(ds('a)), typB(ds('b)), typA2(ds('c))).collect().run().toVector).&&( | ||
| vanillaSpark ?= ds.sortWithinPartitionsMany(typA(ds('a)), typB(ds('b)), typA2(ds('c))).collect().run().toVector | ||
| ) | ||
| }.reduce(_ && _) | ||
| } | ||
|
|
||
| check(forAll(prop[SQLDate, Long] _)) | ||
| check(forAll(prop[String, Boolean] _)) | ||
| check(forAll(prop[SQLTimestamp, Long] _)) | ||
| } | ||
|
|
||
| test("sort support for mixed default and explicit ordering") { | ||
| def prop[A: TypedEncoder : CatalystOrdered, B: TypedEncoder : CatalystOrdered](data: Vector[X2[A, B]]): Prop = { | ||
| val ds = TypedDataset.create(data) | ||
|
|
||
| ds.dataset.orderBy(ds.dataset.col("a"), ds.dataset.col("b").desc).collect().toVector.?=( | ||
| ds.orderByMany(ds('a), ds('b).desc).collect().run().toVector) && | ||
| ds.dataset.sortWithinPartitions(ds.dataset.col("a"), ds.dataset.col("b").desc).collect().toVector.?=( | ||
| ds.sortWithinPartitionsMany(ds('a), ds('b).desc).collect().run().toVector) | ||
| } | ||
|
|
||
| check(forAll(prop[SQLDate, Long] _)) | ||
| check(forAll(prop[String, Boolean] _)) | ||
| check(forAll(prop[SQLTimestamp, Long] _)) | ||
| } | ||
|
|
||
| test("fail when selected column is not sortable") { | ||
| val d = TypedDataset.create(X2(1, Map(1 -> 2)) :: X2(2, Map(2 -> 2)) :: Nil) | ||
| d.orderBy(d('a).desc) | ||
| illTyped("""d.orderBy(d('b).desc)""") | ||
| illTyped("""d.sortWithinPartitions(d('b).desc)""") | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@frosforever, we can do something similar to what we did with
orderByManyforsortWithinPartitions? Maybe even call itsortWithinPartitionsManyand have some sortWithinPartitions overloaded for up to 3 args.