-
Notifications
You must be signed in to change notification settings - Fork 138
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
TypedColumn#year and LocalDateTime generator #228
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
dca297f
added year function
Avasil f5c57da
changed Column#year type to Option[Int]
Avasil 7e8e8a6
cleanup
Avasil dd2f5a1
Formatting NonAggregateFunctionsTests
OlivierBlanvillain e4c0a12
Merge branch 'master' into column-year
Avasil 53b6dc3
fix merge conflicts
Avasil 36762fb
Merge branch 'master' into column-year
Avasil 21900fa
clean up after merge
Avasil 4086b26
removed curly braces from `year` function
Avasil 43b0073
Merge branch 'master' into column-year
Avasil 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 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 |
---|---|---|
@@ -1,10 +1,11 @@ | ||
package frameless | ||
package functions | ||
import java.time.{LocalDateTime => JavaLocalDateTime} | ||
|
||
import frameless.functions.nonAggregate._ | ||
import org.apache.spark.sql.{ Column, Encoder } | ||
import org.scalacheck.Gen | ||
import org.apache.spark.sql.{Column, Encoder, functions => untyped} | ||
import org.scalacheck.Prop._ | ||
import org.apache.spark.sql.{ functions => untyped } | ||
import org.scalacheck.{Gen, Prop} | ||
|
||
class NonAggregateFunctionsTests extends TypedDatasetSuite { | ||
|
||
|
@@ -611,23 +612,46 @@ class NonAggregateFunctionsTests extends TypedDatasetSuite { | |
check(stringFuncProp(upper, untyped.upper)) | ||
} | ||
|
||
def stringFuncProp[A : Encoder](strFunc: TypedColumn[X1[String], String] => TypedColumn[X1[String], A], sparkFunc: Column => Column) = { | ||
test("year") { | ||
val spark = session | ||
import spark.implicits._ | ||
|
||
check(dateTimeStringFuncProp(year, untyped.year)) | ||
} | ||
|
||
def stringFuncProp[A: Encoder](strFunc: TypedColumn[X1[String], String] => TypedColumn[X1[String], A], | ||
sparkFunc: Column => Column): Prop = { | ||
forAll { values: List[X1[String]] => | ||
val ds = TypedDataset.create(values) | ||
|
||
val sparkResult: List[A] = ds.toDF() | ||
.select(sparkFunc(untyped.col("a"))) | ||
.map(_.getAs[A](0)) | ||
.collect() | ||
.toList | ||
funcProp(ds)(strFunc, sparkFunc) | ||
} | ||
} | ||
|
||
val typed: List[A] = ds | ||
.select(strFunc(ds[String]('a))) | ||
.collect() | ||
.run() | ||
.toList | ||
def dateTimeStringFuncProp[A: Encoder](strFunc: TypedColumn[X1[String], String] => TypedColumn[X1[String], A], | ||
sparkFunc: Column => Column): Prop = | ||
forAll { values: List[JavaLocalDateTime] => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if it's not a nicely formatted date string? |
||
val ds = TypedDataset.create(values.map(v => X1[String](v.format(dateTimeFormatter)))) | ||
|
||
typed ?= sparkResult | ||
funcProp(ds)(strFunc, sparkFunc) | ||
} | ||
|
||
def funcProp[A: Encoder](ds: TypedDataset[X1[String]]) | ||
(strFunc: TypedColumn[X1[String], String] => TypedColumn[X1[String], A], | ||
sparkFunc: Column => Column): Prop = { | ||
val sparkResult = ds.toDF() | ||
.select(sparkFunc(untyped.col("a"))) | ||
.map(_.getAs[A](0)) | ||
.collect() | ||
.toList | ||
|
||
val typed = ds | ||
.select(strFunc(ds[String]('a))) | ||
.collect() | ||
.run() | ||
.toList | ||
|
||
typed ?= sparkResult | ||
} | ||
} | ||
|
||
} |
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
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.
This should probably
TypedColumn[T, Option[Int]]
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 agree here. This should fail when there is years to be extracted. Is Spark returns null in this case, then this should be encoded as @frosforever suggests. Should be a trivial change.