-
-
Notifications
You must be signed in to change notification settings - Fork 137
[Can be replaced by parts of #225 - Do NOT merge] Adding orderBy methods for TypedDetasets #231
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
24 changes: 24 additions & 0 deletions
24
dataset/src/main/scala/frameless/ops/SortableColumnTypes.scala
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,24 @@ | ||
| package frameless.ops | ||
|
|
||
| import frameless.{CatalystOrdered, SortedTypedColumn} | ||
| import shapeless.{::, HList, HNil} | ||
|
|
||
| import scala.annotation.implicitNotFound | ||
|
|
||
| @implicitNotFound( | ||
| "Either one of the selected columns is not sortable (${U}), " + | ||
| "or no ordering (ascending/descending) has been selected. " + | ||
| "Select an ordering on any column (t) using the t.asc or t.desc methods.") | ||
| trait SortableColumnTypes[T, U <: HList] | ||
|
|
||
| object SortableColumnTypes { | ||
| implicit def deriveHNil[T]: SortableColumnTypes[T, HNil] = | ||
| new SortableColumnTypes[T, HNil] { type Out = HNil } | ||
|
|
||
| implicit def deriveCons[T, H, TT <: HList, V <: HList] | ||
| (implicit | ||
| order: CatalystOrdered[H], | ||
| tail: SortableColumnTypes[T, TT] | ||
| ): SortableColumnTypes[T, SortedTypedColumn[T, H] :: TT] = | ||
| new SortableColumnTypes[T, SortedTypedColumn[T, H] :: TT] {} | ||
| } | ||
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,77 @@ | ||
| package frameless | ||
|
|
||
| import org.scalacheck.Prop | ||
| import org.scalacheck.Prop._ | ||
| import org.scalatest.Matchers | ||
| import shapeless.test.illTyped | ||
|
|
||
| class OrderByTests extends TypedDatasetSuite with Matchers { | ||
| test("sorting single column descending") { | ||
| def prop[A: TypedEncoder : CatalystOrdered](data: Vector[X1[A]]): Prop = { | ||
| val ds = TypedDataset.create(data) | ||
|
|
||
| ds.dataset.orderBy(ds.dataset.col("a").desc).collect().toVector.?=( | ||
| ds.orderBy(ds('a).desc).collect().run().toVector) | ||
| } | ||
|
|
||
| check(forAll(prop[Int] _)) | ||
| check(forAll(prop[String] _)) | ||
| } | ||
|
|
||
| test("sorting single column ascending") { | ||
| def prop[A: TypedEncoder : CatalystOrdered](data: Vector[X1[A]]): Prop = { | ||
| val ds = TypedDataset.create(data) | ||
|
|
||
| ds.dataset.orderBy(ds.dataset.col("a").asc).collect().toVector.?=( | ||
| ds.orderBy(ds('a).asc).collect().run().toVector) | ||
| } | ||
|
|
||
| check(forAll(prop[Int] _)) | ||
| check(forAll(prop[String] _)) | ||
| check(forAll(prop[Boolean] _)) | ||
| } | ||
|
|
||
| test("sorting on two columns") { | ||
| def prop[A: TypedEncoder : CatalystOrdered, B: TypedEncoder : CatalystOrdered](data: Vector[X2[A,B]]): Prop = { | ||
| val ds = TypedDataset.create(data) | ||
|
|
||
| val vanillaSpark = ds.dataset.orderBy(ds.dataset.col("a").asc, ds.dataset.col("b").desc).collect().toVector | ||
| vanillaSpark.?=(ds.orderByMany(ds('a).asc, ds('b).desc).collect().run().toVector).&&( | ||
| vanillaSpark ?= ds.orderBy(ds('a).asc, ds('b).desc).collect().run().toVector | ||
| ) | ||
| } | ||
|
|
||
| check(forAll(prop[SQLDate, Long] _)) | ||
| check(forAll(prop[String, Boolean] _)) | ||
| check(forAll(prop[SQLTimestamp, Long] _)) | ||
| } | ||
|
|
||
| test("sorting on three columns") { | ||
| def prop[A: TypedEncoder : CatalystOrdered, B: TypedEncoder : CatalystOrdered] | ||
| (data: Vector[X3[A, B, A]]): Prop = { | ||
| val ds = TypedDataset.create(data) | ||
|
|
||
| val vanillaSpark = ds.dataset.orderBy( | ||
| ds.dataset.col("a").desc, | ||
| ds.dataset.col("b").desc, | ||
| ds.dataset.col("c").asc | ||
| ).collect().toVector | ||
|
|
||
| vanillaSpark.?=(ds.orderByMany(ds('a).desc, ds('b).desc, ds('c).asc).collect().run().toVector).&&( | ||
| vanillaSpark ?= ds.orderBy(ds('a).desc, ds('b).desc, ds('c).asc).collect().run().toVector) | ||
| } | ||
|
|
||
| check(forAll(prop[Int, Long] _)) | ||
| check(forAll(prop[String, SQLDate] _)) | ||
| check(forAll(prop[Boolean, Long] _)) | ||
| } | ||
|
|
||
| test("fail when selected column is not sortable") { | ||
| val d = TypedDataset.create(X2(1, List(1)) :: X2(2, List(2)) :: Nil) | ||
| d.orderBy(d('a).desc) | ||
| illTyped("""d.orderByDesc('b)""") | ||
| d.orderByMany(d('a).desc) | ||
| illTyped("""d.orderByMany(d('b).desc)""") | ||
| illTyped("""d.orderByMany(d('a))""") // column is correct, but no ordering is selected | ||
| } | ||
| } |
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.
I think there might be something in shapeless doing just that.
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.
Got it, it's called Comapped
Uh oh!
There was an error while loading. Please reload this page.
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.
Thank you for all the comments @OlivierBlanvillain. This one does two things, enforces that they are CatalystOrdered and that they are SortedTypedColumn. It also allows us to give the ability to write a custom error compilation error (using implicitNotFound). FInally, our ColumnTypes already is a small rework of Comapped and there @kanterov mentioned a performance reason why we prefer our own version rather than relying on Comapped.