Skip to content

Commit 4e07d1a

Browse files
committed
typelevel#787 - spark 4 compat
1 parent 24bde95 commit 4e07d1a

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

dataset/src/test/scala/frameless/TypedDatasetSuite.scala

+39-16
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,35 @@ package frameless
22

33
import com.globalmentor.apache.hadoop.fs.BareLocalFileSystem
44
import org.apache.hadoop.fs.local.StreamingFS
5-
import org.apache.spark.{SparkConf, SparkContext}
6-
import org.apache.spark.sql.{SQLContext, SparkSession}
5+
import org.apache.spark.{ SparkConf, SparkContext }
6+
import org.apache.spark.sql.{ SQLContext, SparkSession }
77
import org.scalactic.anyvals.PosZInt
88
import org.scalatest.BeforeAndAfterAll
99
import org.scalatestplus.scalacheck.Checkers
1010
import org.scalacheck.Prop
1111
import org.scalacheck.Prop._
1212

13-
import scala.util.{Properties, Try}
13+
import scala.util.{ Properties, Try }
1414
import org.scalatest.funsuite.AnyFunSuite
1515

1616
trait SparkTesting { self: BeforeAndAfterAll =>
1717

18-
val appID: String = new java.util.Date().toString + math.floor(math.random * 10E4).toLong.toString
18+
val appID: String = new java.util.Date().toString + math
19+
.floor(math.random * 10e4)
20+
.toLong
21+
.toString
1922

2023
/**
2124
* Allows bare naked to be used instead of winutils for testing / dev
2225
*/
2326
def registerFS(sparkConf: SparkConf): SparkConf = {
2427
if (System.getProperty("os.name").startsWith("Windows"))
25-
sparkConf.set("spark.hadoop.fs.file.impl", classOf[BareLocalFileSystem].getName).
26-
set("spark.hadoop.fs.AbstractFileSystem.file.impl", classOf[StreamingFS].getName)
28+
sparkConf
29+
.set("spark.hadoop.fs.file.impl", classOf[BareLocalFileSystem].getName)
30+
.set(
31+
"spark.hadoop.fs.AbstractFileSystem.file.impl",
32+
classOf[StreamingFS].getName
33+
)
2734
else
2835
sparkConf
2936
}
@@ -33,16 +40,20 @@ trait SparkTesting { self: BeforeAndAfterAll =>
3340
.setAppName("test")
3441
.set("spark.ui.enabled", "false")
3542
.set("spark.app.id", appID)
43+
.set(
44+
"spark.sql.ansi.enabled",
45+
"false"
46+
) // 43 tests fail on overflow / casting issues
3647

3748
private var s: SparkSession = _
3849

3950
implicit def session: SparkSession = s
4051
implicit def sc: SparkContext = session.sparkContext
4152
implicit def sqlContext: SQLContext = session.sqlContext
4253

43-
def registerOptimizations(sqlContext: SQLContext): Unit = { }
54+
def registerOptimizations(sqlContext: SQLContext): Unit = {}
4455

45-
def addSparkConfigProperties(config: SparkConf): Unit = { }
56+
def addSparkConfigProperties(config: SparkConf): Unit = {}
4657

4758
override def beforeAll(): Unit = {
4859
assert(s == null)
@@ -59,11 +70,16 @@ trait SparkTesting { self: BeforeAndAfterAll =>
5970
}
6071
}
6172

73+
class TypedDatasetSuite
74+
extends AnyFunSuite
75+
with Checkers
76+
with BeforeAndAfterAll
77+
with SparkTesting {
6278

63-
class TypedDatasetSuite extends AnyFunSuite with Checkers with BeforeAndAfterAll with SparkTesting {
6479
// Limit size of generated collections and number of checks to avoid OutOfMemoryError
6580
implicit override val generatorDrivenConfig: PropertyCheckConfiguration = {
66-
def getPosZInt(name: String, default: PosZInt) = Properties.envOrNone(s"FRAMELESS_GEN_${name}")
81+
def getPosZInt(name: String, default: PosZInt) = Properties
82+
.envOrNone(s"FRAMELESS_GEN_${name}")
6783
.flatMap(s => Try(s.toInt).toOption)
6884
.flatMap(PosZInt.from)
6985
.getOrElse(default)
@@ -75,17 +91,24 @@ class TypedDatasetSuite extends AnyFunSuite with Checkers with BeforeAndAfterAll
7591

7692
implicit val sparkDelay: SparkDelay[Job] = Job.framelessSparkDelayForJob
7793

78-
def approximatelyEqual[A](a: A, b: A)(implicit numeric: Numeric[A]): Prop = {
94+
def approximatelyEqual[A](
95+
a: A,
96+
b: A
97+
)(implicit
98+
numeric: Numeric[A]
99+
): Prop = {
79100
val da = numeric.toDouble(a)
80101
val db = numeric.toDouble(b)
81-
val epsilon = 1E-6
102+
val epsilon = 1e-6
82103
// Spark has a weird behaviour concerning expressions that should return Inf
83104
// Most of the time they return NaN instead, for instance stddev of Seq(-7.827553978923477E227, -5.009124275715786E153)
84-
if((da.isNaN || da.isInfinity) && (db.isNaN || db.isInfinity)) proved
105+
if ((da.isNaN || da.isInfinity) && (db.isNaN || db.isInfinity)) proved
85106
else if (
86107
(da - db).abs < epsilon ||
87-
(da - db).abs < da.abs / 100)
88-
proved
89-
else falsified :| s"Expected $a but got $b, which is more than 1% off and greater than epsilon = $epsilon."
108+
(da - db).abs < da.abs / 100
109+
)
110+
proved
111+
else
112+
falsified :| s"Expected $a but got $b, which is more than 1% off and greater than epsilon = $epsilon."
90113
}
91114
}

0 commit comments

Comments
 (0)