forked from typelevel/frameless
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parameterize Spark actions over the effect type used (typelevel#172)
- Loading branch information
Showing
21 changed files
with
305 additions
and
52 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
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,24 @@ | ||
package frameless | ||
package cats | ||
|
||
import _root_.cats.effect.Sync | ||
import _root_.cats.implicits._ | ||
import _root_.cats.mtl.ApplicativeAsk | ||
import org.apache.spark.sql.SparkSession | ||
|
||
trait FramelessSyntax extends frameless.FramelessSyntax { | ||
implicit class SparkJobOps[F[_], A](fa: F[A])(implicit S: Sync[F], A: ApplicativeAsk[F, SparkSession]) { | ||
import S._, A._ | ||
|
||
def withLocalProperty(key: String, value: String): F[A] = | ||
for { | ||
session <- ask | ||
_ <- delay(session.sparkContext.setLocalProperty(key, value)) | ||
a <- fa | ||
} yield a | ||
|
||
def withGroupId(groupId: String): F[A] = withLocalProperty("spark.jobGroup.id", groupId) | ||
|
||
def withDescription(description: String) = withLocalProperty("spark.job.description", description) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
cats/src/main/scala/frameless/cats/SparkDelayInstances.scala
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,11 @@ | ||
package frameless | ||
package cats | ||
|
||
import _root_.cats.effect.Sync | ||
import org.apache.spark.sql.SparkSession | ||
|
||
trait SparkDelayInstances { | ||
implicit def framelessCatsSparkDelayForSync[F[_]](implicit S: Sync[F]): SparkDelay[F] = new SparkDelay[F] { | ||
def delay[A](a: => A)(implicit spark: SparkSession): F[A] = S.delay(a) | ||
} | ||
} |
File renamed without changes.
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
File renamed without changes.
63 changes: 63 additions & 0 deletions
63
cats/src/test/scala/frameless/cats/FramelessSyntaxTests.scala
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,63 @@ | ||
package frameless | ||
package cats | ||
|
||
import _root_.cats.data.ReaderT | ||
import _root_.cats.effect.{ IO, Sync } | ||
import frameless.{ TypedDataset, TypedDatasetSuite, TypedEncoder, X2 } | ||
import org.apache.spark.sql.SparkSession | ||
import org.scalacheck.Prop, Prop._ | ||
|
||
class FramelessSyntaxTests extends TypedDatasetSuite { | ||
override val sparkDelay = null | ||
|
||
def prop[A, B](data: Vector[X2[A, B]])( | ||
implicit ev: TypedEncoder[X2[A, B]] | ||
): Prop = { | ||
import implicits._ | ||
|
||
val dataset = TypedDataset.create(data).dataset | ||
val dataframe = dataset.toDF() | ||
|
||
val typedDataset = dataset.typed | ||
val typedDatasetFromDataFrame = dataframe.unsafeTyped[X2[A, B]] | ||
|
||
typedDataset.collect[IO]().unsafeRunSync().toVector ?= typedDatasetFromDataFrame.collect[IO]().unsafeRunSync().toVector | ||
} | ||
|
||
test("dataset typed - toTyped") { | ||
check(forAll(prop[Int, String] _)) | ||
} | ||
|
||
test("properties can be read back") { | ||
import implicits._ | ||
import _root_.cats.implicits._ | ||
import _root_.cats.mtl.implicits._ | ||
|
||
// We need this instance here because there is no cats.effect.Sync instance for ReaderT. | ||
// Hopefully the instance will be back before cats 1.0.0 and we'll be able to get rid of this. | ||
implicit val sync: Sync[ReaderT[IO, SparkSession, ?]] = new Sync[ReaderT[IO, SparkSession, ?]] { | ||
def suspend[A](thunk: => ReaderT[IO, SparkSession, A]) = thunk | ||
def pure[A](x: A): ReaderT[IO, SparkSession, A] = ReaderT.pure(x) | ||
def handleErrorWith[A](fa: ReaderT[IO, SparkSession, A])(f: Throwable => ReaderT[IO, SparkSession, A]): ReaderT[IO, SparkSession, A] = | ||
ReaderT(r => fa.run(r).handleErrorWith(e => f(e).run(r))) | ||
def raiseError[A](e: Throwable): ReaderT[IO, SparkSession, A] = ReaderT.lift(IO.raiseError(e)) | ||
def flatMap[A, B](fa: ReaderT[IO, SparkSession, A])(f: A => ReaderT[IO, SparkSession, B]): ReaderT[IO, SparkSession, B] = fa.flatMap(f) | ||
def tailRecM[A, B](a: A)(f: A => ReaderT[IO, SparkSession, Either[A, B]]): ReaderT[IO, SparkSession, B] = | ||
ReaderT.catsDataMonadForKleisli[IO, SparkSession].tailRecM(a)(f) | ||
} | ||
|
||
check { | ||
forAll { (k:String, v: String) => | ||
val scopedKey = "frameless.tests." + k | ||
1.pure[ReaderT[IO, SparkSession, ?]].withLocalProperty(scopedKey,v).run(session).unsafeRunSync() | ||
sc.getLocalProperty(scopedKey) ?= v | ||
|
||
1.pure[ReaderT[IO, SparkSession, ?]].withGroupId(v).run(session).unsafeRunSync() | ||
sc.getLocalProperty("spark.jobGroup.id") ?= v | ||
|
||
1.pure[ReaderT[IO, SparkSession, ?]].withDescription(v).run(session).unsafeRunSync() | ||
sc.getLocalProperty("spark.job.description") ?= v | ||
} | ||
} | ||
} | ||
} |
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
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,7 @@ | ||
package frameless | ||
|
||
import org.apache.spark.sql.SparkSession | ||
|
||
trait SparkDelay[F[_]] { | ||
def delay[A](a: => A)(implicit spark: SparkSession): F[A] | ||
} |
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
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,5 @@ | ||
package frameless | ||
|
||
package object syntax extends FramelessSyntax | ||
package object syntax extends FramelessSyntax { | ||
implicit val DefaultSparkDelay: SparkDelay[Job] = Job.framelessSparkDelayForJob | ||
} |
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
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
Oops, something went wrong.