Skip to content

[SPARK-2498] [SQL] Synchronize on a lock when using scala reflection inside data type objects. #1423

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
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ package org.apache.spark.sql.catalyst.types

import java.sql.Timestamp

import scala.util.parsing.combinator.RegexParsers

import scala.reflect.ClassTag
import scala.reflect.runtime.universe.{typeTag, TypeTag, runtimeMirror}
import scala.util.parsing.combinator.RegexParsers

import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeReference, Expression}
import org.apache.spark.util.Utils

/**
*
* A JVM-global lock that should be used to prevent thread safety issues when using things in
* scala.reflect.*. Note that Scala Reflection API is made thread-safe in 2.11, but not yet for
* 2.10.* builds. See SI-6240 for more details.
*/
protected[catalyst] object ScalaReflectionLock

object DataType extends RegexParsers {
protected lazy val primitiveType: Parser[DataType] =
"StringType" ^^^ StringType |
Expand Down Expand Up @@ -62,7 +65,6 @@ object DataType extends RegexParsers {
"true" ^^^ true |
"false" ^^^ false


protected lazy val structType: Parser[DataType] =
"StructType\\([A-zA-z]*\\(".r ~> repsep(structField, ",") <~ "))" ^^ {
case fields => new StructType(fields)
Expand Down Expand Up @@ -106,30 +108,32 @@ abstract class NativeType extends DataType {
@transient val tag: TypeTag[JvmType]
val ordering: Ordering[JvmType]

@transient val classTag = {
@transient val classTag = ScalaReflectionLock.synchronized {
val mirror = runtimeMirror(Utils.getSparkClassLoader)
ClassTag[JvmType](mirror.runtimeClass(tag.tpe))
}
}

case object StringType extends NativeType with PrimitiveType {
type JvmType = String
@transient lazy val tag = typeTag[JvmType]
@transient lazy val tag = ScalaReflectionLock.synchronized { typeTag[JvmType] }
val ordering = implicitly[Ordering[JvmType]]
}

case object BinaryType extends DataType with PrimitiveType {
type JvmType = Array[Byte]
}

case object BooleanType extends NativeType with PrimitiveType {
type JvmType = Boolean
@transient lazy val tag = typeTag[JvmType]
@transient lazy val tag = ScalaReflectionLock.synchronized { typeTag[JvmType] }
val ordering = implicitly[Ordering[JvmType]]
}

case object TimestampType extends NativeType {
type JvmType = Timestamp

@transient lazy val tag = typeTag[JvmType]
@transient lazy val tag = ScalaReflectionLock.synchronized { typeTag[JvmType] }

val ordering = new Ordering[JvmType] {
def compare(x: Timestamp, y: Timestamp) = x.compareTo(y)
Expand Down Expand Up @@ -159,31 +163,31 @@ abstract class IntegralType extends NumericType {

case object LongType extends IntegralType {
type JvmType = Long
@transient lazy val tag = typeTag[JvmType]
@transient lazy val tag = ScalaReflectionLock.synchronized { typeTag[JvmType] }
val numeric = implicitly[Numeric[Long]]
val integral = implicitly[Integral[Long]]
val ordering = implicitly[Ordering[JvmType]]
}

case object IntegerType extends IntegralType {
type JvmType = Int
@transient lazy val tag = typeTag[JvmType]
@transient lazy val tag = ScalaReflectionLock.synchronized { typeTag[JvmType] }
val numeric = implicitly[Numeric[Int]]
val integral = implicitly[Integral[Int]]
val ordering = implicitly[Ordering[JvmType]]
}

case object ShortType extends IntegralType {
type JvmType = Short
@transient lazy val tag = typeTag[JvmType]
@transient lazy val tag = ScalaReflectionLock.synchronized { typeTag[JvmType] }
val numeric = implicitly[Numeric[Short]]
val integral = implicitly[Integral[Short]]
val ordering = implicitly[Ordering[JvmType]]
}

case object ByteType extends IntegralType {
type JvmType = Byte
@transient lazy val tag = typeTag[JvmType]
@transient lazy val tag = ScalaReflectionLock.synchronized { typeTag[JvmType] }
val numeric = implicitly[Numeric[Byte]]
val integral = implicitly[Integral[Byte]]
val ordering = implicitly[Ordering[JvmType]]
Expand All @@ -202,23 +206,23 @@ abstract class FractionalType extends NumericType {

case object DecimalType extends FractionalType {
type JvmType = BigDecimal
@transient lazy val tag = typeTag[JvmType]
@transient lazy val tag = ScalaReflectionLock.synchronized { typeTag[JvmType] }
val numeric = implicitly[Numeric[BigDecimal]]
val fractional = implicitly[Fractional[BigDecimal]]
val ordering = implicitly[Ordering[JvmType]]
}

case object DoubleType extends FractionalType {
type JvmType = Double
@transient lazy val tag = typeTag[JvmType]
@transient lazy val tag = ScalaReflectionLock.synchronized { typeTag[JvmType] }
val numeric = implicitly[Numeric[Double]]
val fractional = implicitly[Fractional[Double]]
val ordering = implicitly[Ordering[JvmType]]
}

case object FloatType extends FractionalType {
type JvmType = Float
@transient lazy val tag = typeTag[JvmType]
@transient lazy val tag = ScalaReflectionLock.synchronized { typeTag[JvmType] }
val numeric = implicitly[Numeric[Float]]
val fractional = implicitly[Fractional[Float]]
val ordering = implicitly[Ordering[JvmType]]
Expand Down