-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-12293][SQL] Support UnsafeRow in LocalTableScan #10283
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
Changes from 3 commits
ca4326a
a0a991a
f0e6ac0
ee89482
97a81c3
5b2a09c
97d390f
2500de3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,6 +61,7 @@ object ScalaReflection extends ScalaReflection { | |
| case t if t <:< definitions.ByteTpe => ByteType | ||
| case t if t <:< definitions.BooleanTpe => BooleanType | ||
| case t if t <:< localTypeOf[Array[Byte]] => BinaryType | ||
| case t if t <:< localTypeOf[Decimal] => DecimalType.SYSTEM_DEFAULT | ||
| case _ => | ||
| val className = getClassNameFromType(tpe) | ||
| className match { | ||
|
|
@@ -177,6 +178,7 @@ object ScalaReflection extends ScalaReflection { | |
| case _ => UpCast(expr, expected, walkedTypePath) | ||
| } | ||
|
|
||
| val className = getClassNameFromType(tpe) | ||
| tpe match { | ||
| case t if !dataTypeFor(t).isInstanceOf[ObjectType] => getPath | ||
|
|
||
|
|
@@ -372,6 +374,17 @@ object ScalaReflection extends ScalaReflection { | |
| } else { | ||
| newInstance | ||
| } | ||
|
|
||
| case t if Utils.classIsLoadable(className) && | ||
| Utils.classForName(className).isAnnotationPresent(classOf[SQLUserDefinedType]) => | ||
| val udt = Utils.classForName(className) | ||
| .getAnnotation(classOf[SQLUserDefinedType]).udt().newInstance() | ||
| val obj = NewInstance( | ||
| udt.userClass.getAnnotation(classOf[SQLUserDefinedType]).udt(), | ||
| Nil, | ||
| false, | ||
| dataType = ObjectType(udt.userClass.getAnnotation(classOf[SQLUserDefinedType]).udt())) | ||
| Invoke(obj, "deserialize", ObjectType(udt.userClass), getPath :: Nil) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -406,11 +419,16 @@ object ScalaReflection extends ScalaReflection { | |
| def toCatalystArray(input: Expression, elementType: `Type`): Expression = { | ||
| val externalDataType = dataTypeFor(elementType) | ||
| val Schema(catalystType, nullable) = silentSchemaFor(elementType) | ||
| if (isNativeType(catalystType)) { | ||
| NewInstance( | ||
|
|
||
| if (isNativeType(catalystType) && !(elementType <:< localTypeOf[Option[_]])) { | ||
|
Contributor
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. good catch!
Member
Author
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. Yes. Thanks. I propose this fixing as another pr #10391. |
||
| val array = NewInstance( | ||
| classOf[GenericArrayData], | ||
| input :: Nil, | ||
| dataType = ArrayType(catalystType, nullable)) | ||
| expressions.If( | ||
| IsNull(input), | ||
| expressions.Literal.create(null, ArrayType(catalystType, nullable)), | ||
| array) | ||
| } else { | ||
| val clsName = getClassNameFromType(elementType) | ||
| val newPath = s"""- array element class: "$clsName"""" +: walkedTypePath | ||
|
|
@@ -421,46 +439,75 @@ object ScalaReflection extends ScalaReflection { | |
| if (!inputObject.dataType.isInstanceOf[ObjectType]) { | ||
| inputObject | ||
| } else { | ||
| val className = getClassNameFromType(tpe) | ||
| tpe match { | ||
| case t if t <:< localTypeOf[Option[_]] => | ||
| val TypeRef(_, _, Seq(optType)) = t | ||
| optType match { | ||
| // For primitive types we must manually unbox the value of the object. | ||
| case t if t <:< definitions.IntTpe => | ||
| Invoke( | ||
| UnwrapOption(ObjectType(classOf[java.lang.Integer]), inputObject), | ||
| "intValue", | ||
| IntegerType) | ||
| val unwrapped = UnwrapOption(ObjectType(classOf[java.lang.Integer]), inputObject) | ||
| expressions.If( | ||
| IsNull(unwrapped), | ||
| expressions.Literal.create(null, silentSchemaFor(optType).dataType), | ||
| Invoke( | ||
| unwrapped, | ||
|
Contributor
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. If the I think we don't need this extra
Member
Author
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. I think you are right. I add this to make sure it is not possibly creating wrong values when I debug. I will remove these |
||
| "intValue", | ||
| IntegerType)) | ||
| case t if t <:< definitions.LongTpe => | ||
| Invoke( | ||
| UnwrapOption(ObjectType(classOf[java.lang.Long]), inputObject), | ||
| "longValue", | ||
| LongType) | ||
| val unwrapped = UnwrapOption(ObjectType(classOf[java.lang.Long]), inputObject) | ||
| expressions.If( | ||
| IsNull(unwrapped), | ||
| expressions.Literal.create(null, silentSchemaFor(optType).dataType), | ||
| Invoke( | ||
| unwrapped, | ||
| "longValue", | ||
| LongType)) | ||
| case t if t <:< definitions.DoubleTpe => | ||
| Invoke( | ||
| UnwrapOption(ObjectType(classOf[java.lang.Double]), inputObject), | ||
| "doubleValue", | ||
| DoubleType) | ||
| val unwrapped = UnwrapOption(ObjectType(classOf[java.lang.Double]), inputObject) | ||
| expressions.If( | ||
| IsNull(unwrapped), | ||
| expressions.Literal.create(null, silentSchemaFor(optType).dataType), | ||
| Invoke( | ||
| unwrapped, | ||
| "doubleValue", | ||
| DoubleType)) | ||
| case t if t <:< definitions.FloatTpe => | ||
| Invoke( | ||
| UnwrapOption(ObjectType(classOf[java.lang.Float]), inputObject), | ||
| "floatValue", | ||
| FloatType) | ||
| val unwrapped = UnwrapOption(ObjectType(classOf[java.lang.Float]), inputObject) | ||
| expressions.If( | ||
| IsNull(unwrapped), | ||
| expressions.Literal.create(null, silentSchemaFor(optType).dataType), | ||
| Invoke( | ||
| unwrapped, | ||
| "floatValue", | ||
| FloatType)) | ||
| case t if t <:< definitions.ShortTpe => | ||
| Invoke( | ||
| UnwrapOption(ObjectType(classOf[java.lang.Short]), inputObject), | ||
| "shortValue", | ||
| ShortType) | ||
| val unwrapped = UnwrapOption(ObjectType(classOf[java.lang.Short]), inputObject) | ||
| expressions.If( | ||
| IsNull(unwrapped), | ||
| expressions.Literal.create(null, silentSchemaFor(optType).dataType), | ||
| Invoke( | ||
| unwrapped, | ||
| "shortValue", | ||
| ShortType)) | ||
| case t if t <:< definitions.ByteTpe => | ||
| Invoke( | ||
| UnwrapOption(ObjectType(classOf[java.lang.Byte]), inputObject), | ||
| "byteValue", | ||
| ByteType) | ||
| val unwrapped = UnwrapOption(ObjectType(classOf[java.lang.Byte]), inputObject) | ||
| expressions.If( | ||
| IsNull(unwrapped), | ||
| expressions.Literal.create(null, silentSchemaFor(optType).dataType), | ||
| Invoke( | ||
| unwrapped, | ||
| "byteValue", | ||
| ByteType)) | ||
| case t if t <:< definitions.BooleanTpe => | ||
| Invoke( | ||
| UnwrapOption(ObjectType(classOf[java.lang.Boolean]), inputObject), | ||
| "booleanValue", | ||
| BooleanType) | ||
| val unwrapped = UnwrapOption(ObjectType(classOf[java.lang.Boolean]), inputObject) | ||
| expressions.If( | ||
| IsNull(unwrapped), | ||
| expressions.Literal.create(null, silentSchemaFor(optType).dataType), | ||
| Invoke( | ||
| unwrapped, | ||
| "booleanValue", | ||
| BooleanType)) | ||
|
|
||
| // For non-primitives, we can just extract the object from the Option and then recurse. | ||
| case other => | ||
|
|
@@ -589,6 +636,17 @@ object ScalaReflection extends ScalaReflection { | |
| case t if t <:< localTypeOf[java.lang.Boolean] => | ||
| Invoke(inputObject, "booleanValue", BooleanType) | ||
|
|
||
| case t if Utils.classIsLoadable(className) && | ||
| Utils.classForName(className).isAnnotationPresent(classOf[SQLUserDefinedType]) => | ||
| val udt = Utils.classForName(className) | ||
| .getAnnotation(classOf[SQLUserDefinedType]).udt().newInstance() | ||
| val obj = NewInstance( | ||
| udt.userClass.getAnnotation(classOf[SQLUserDefinedType]).udt(), | ||
| Nil, | ||
| false, | ||
| dataType = ObjectType(udt.userClass.getAnnotation(classOf[SQLUserDefinedType]).udt())) | ||
| Invoke(obj, "serialize", udt.sqlType, inputObject :: Nil) | ||
|
|
||
| case other => | ||
| throw new UnsupportedOperationException( | ||
| s"No Encoder found for $tpe\n" + walkedTypePath.mkString("\n")) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -937,8 +937,9 @@ object DecimalAggregates extends Rule[LogicalPlan] { | |
| object ConvertToLocalRelation extends Rule[LogicalPlan] { | ||
| def apply(plan: LogicalPlan): LogicalPlan = plan transform { | ||
| case Project(projectList, LocalRelation(output, data)) => | ||
| val projection = new InterpretedProjection(projectList, output) | ||
| LocalRelation(projectList.map(_.toAttribute), data.map(projection)) | ||
| val projection = UnsafeProjection.create(projectList, output) | ||
| LocalRelation(projectList.map(_.toAttribute), | ||
| data.map(projection(_).copy().asInstanceOf[UnsafeRow])) | ||
|
Contributor
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. The return type of |
||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,8 @@ | |
| package org.apache.spark.sql.catalyst.plans.logical | ||
|
|
||
| import org.apache.spark.sql.Row | ||
| import org.apache.spark.sql.catalyst.expressions.Attribute | ||
| import org.apache.spark.sql.catalyst.encoders.{ExpressionEncoder, RowEncoder} | ||
| import org.apache.spark.sql.catalyst.expressions.{Attribute, BindReferences, UnsafeProjection, UnsafeRow} | ||
| import org.apache.spark.sql.catalyst.{CatalystTypeConverters, InternalRow, analysis} | ||
| import org.apache.spark.sql.types.{StructField, StructType} | ||
|
|
||
|
|
@@ -29,20 +30,27 @@ object LocalRelation { | |
| new LocalRelation(StructType(output1 +: output).toAttributes) | ||
| } | ||
|
|
||
| def fromInternalRows(output: Seq[Attribute], data: Seq[InternalRow]): LocalRelation = { | ||
| val projection = UnsafeProjection.create(output.map(_.dataType).toArray) | ||
| new LocalRelation(output, data.map(projection(_).copy())) | ||
| } | ||
|
|
||
| def fromExternalRows(output: Seq[Attribute], data: Seq[Row]): LocalRelation = { | ||
| val schema = StructType.fromAttributes(output) | ||
| val converter = CatalystTypeConverters.createToCatalystConverter(schema) | ||
| LocalRelation(output, data.map(converter(_).asInstanceOf[InternalRow])) | ||
| val encoder = RowEncoder(schema) | ||
| LocalRelation(output, data.map(encoder.toRow(_).copy().asInstanceOf[UnsafeRow])) | ||
| } | ||
|
|
||
| def fromProduct(output: Seq[Attribute], data: Seq[Product]): LocalRelation = { | ||
| def fromProduct[T <: Product : ExpressionEncoder]( | ||
| output: Seq[Attribute], | ||
| data: Seq[T]): LocalRelation = { | ||
| val encoder = implicitly[ExpressionEncoder[T]] | ||
|
Contributor
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. we can use |
||
| val schema = StructType.fromAttributes(output) | ||
| val converter = CatalystTypeConverters.createToCatalystConverter(schema) | ||
| LocalRelation(output, data.map(converter(_).asInstanceOf[InternalRow])) | ||
| new LocalRelation(output, data.map(encoder.toRow(_).copy().asInstanceOf[UnsafeRow])) | ||
| } | ||
| } | ||
|
|
||
| case class LocalRelation(output: Seq[Attribute], data: Seq[InternalRow] = Nil) | ||
| case class LocalRelation(output: Seq[Attribute], data: Seq[UnsafeRow] = Nil) | ||
| extends LeafNode with analysis.MultiInstanceRelation { | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,14 +18,30 @@ | |
| package org.apache.spark.sql.catalyst.util | ||
|
|
||
| import scala.collection.JavaConverters._ | ||
| import scala.collection.mutable.WrappedArray | ||
|
|
||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.types.{DataType, Decimal} | ||
| import org.apache.spark.unsafe.types.{CalendarInterval, UTF8String} | ||
|
|
||
| object GenericArrayData { | ||
| def processSeq(seq: Seq[Any]): Array[Any] = { | ||
| seq match { | ||
| case wArray: WrappedArray[_] => | ||
| if (wArray.array == null) { | ||
| null | ||
| } else { | ||
| wArray.toArray[Any] | ||
| } | ||
| case null => null | ||
| case _ => seq.toArray | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class GenericArrayData(val array: Array[Any]) extends ArrayData { | ||
|
|
||
| def this(seq: Seq[Any]) = this(seq.toArray) | ||
| def this(seq: Seq[Any]) = this(GenericArrayData.processSeq(seq)) | ||
|
Contributor
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. instead of handling null here, I think a better way is not passing null to it.
Member
Author
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. Thanks. I propose the fixing as pr #10401. |
||
| def this(list: java.util.List[Any]) = this(list.asScala) | ||
|
|
||
| // TODO: This is boxing. We should specialize. | ||
|
|
@@ -39,7 +55,11 @@ class GenericArrayData(val array: Array[Any]) extends ArrayData { | |
|
|
||
| override def copy(): ArrayData = new GenericArrayData(array.clone()) | ||
|
|
||
| override def numElements(): Int = array.length | ||
| override def numElements(): Int = if (array != null) { | ||
| array.length | ||
| } else { | ||
| 0 | ||
| } | ||
|
|
||
| private def getAs[T](ordinal: Int) = array(ordinal).asInstanceOf[T] | ||
| override def isNullAt(ordinal: Int): Boolean = getAs[AnyRef](ordinal) eq null | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ import org.apache.spark.scheduler.{SparkListener, SparkListenerApplicationEnd} | |
| import org.apache.spark.sql.SQLConf.SQLConfEntry | ||
| import org.apache.spark.sql.catalyst.analysis._ | ||
| import org.apache.spark.sql.catalyst.encoders.encoderFor | ||
| import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder | ||
| import org.apache.spark.sql.catalyst.errors.DialectException | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.optimizer.{DefaultOptimizer, Optimizer} | ||
|
|
@@ -426,6 +427,7 @@ class SQLContext private[sql]( | |
| */ | ||
| @Experimental | ||
| def createDataFrame[A <: Product : TypeTag](data: Seq[A]): DataFrame = { | ||
| implicit def encoder[T : TypeTag]: ExpressionEncoder[T] = ExpressionEncoder() | ||
| SQLContext.setActive(self) | ||
| val schema = ScalaReflection.schemaFor[A].dataType.asInstanceOf[StructType] | ||
| val attributeSeq = schema.toAttributes | ||
|
|
@@ -501,7 +503,7 @@ class SQLContext private[sql]( | |
| def createDataset[T : Encoder](data: Seq[T]): Dataset[T] = { | ||
| val enc = encoderFor[T] | ||
| val attributes = enc.schema.toAttributes | ||
| val encoded = data.map(d => enc.toRow(d).copy()) | ||
| val encoded = data.map(d => enc.toRow(d).copy().asInstanceOf[UnsafeRow]) | ||
| val plan = new LocalRelation(attributes, encoded) | ||
|
|
||
| new Dataset[T](this, plan) | ||
|
|
@@ -604,7 +606,9 @@ class SQLContext private[sql]( | |
| val className = beanClass.getName | ||
| val beanInfo = Introspector.getBeanInfo(beanClass) | ||
| val rows = SQLContext.beansToRows(data.asScala.iterator, beanInfo, attrSeq) | ||
| DataFrame(self, LocalRelation(attrSeq, rows.toSeq)) | ||
| val projection = UnsafeProjection.create(attrSeq) | ||
| DataFrame(self, | ||
| LocalRelation(attrSeq, rows.toSeq.map(projection(_).copy().asInstanceOf[UnsafeRow]))) | ||
|
Contributor
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.
|
||
| } | ||
|
|
||
|
|
||
|
|
||
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.
Normally
Decimalshould only be used inside spark SQL as the internal representation of decimal type, and we don't need to catch it here. Do we break it in tests?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.
Yes.
constructorForwill calldataTypeForto determine if a type isObjectTypeor not. If there is not case forDecimal, it will be recognized asObjectTypeand causes bug.