-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-21204][SQL] Add support for Scala Set collection types in serialization #18416
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 all commits
db1b91e
53b1dc8
31b7812
61f0bb6
4602689
56c1298
e2464c2
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 |
|---|---|---|
|
|
@@ -307,7 +307,10 @@ object ScalaReflection extends ScalaReflection { | |
| Invoke(arrayData, primitiveMethod, arrayCls, returnNullable = false) | ||
| } | ||
|
|
||
| case t if t <:< localTypeOf[Seq[_]] => | ||
| // We serialize a `Set` to Catalyst array. When we deserialize a Catalyst array | ||
| // to a `Set`, if there are duplicated elements, the elements will be de-duplicated. | ||
| case t if t <:< localTypeOf[Seq[_]] || | ||
| t <:< localTypeOf[scala.collection.Set[_]] => | ||
| val TypeRef(_, _, Seq(elementType)) = t | ||
| val Schema(dataType, elementNullable) = schemaFor(elementType) | ||
| val className = getClassNameFromType(elementType) | ||
|
|
@@ -325,8 +328,10 @@ object ScalaReflection extends ScalaReflection { | |
| } | ||
|
|
||
| val companion = t.normalize.typeSymbol.companionSymbol.typeSignature | ||
| val cls = companion.declaration(newTermName("newBuilder")) match { | ||
| case NoSymbol => classOf[Seq[_]] | ||
| val cls = companion.member(newTermName("newBuilder")) match { | ||
| case NoSymbol if t <:< localTypeOf[Seq[_]] => classOf[Seq[_]] | ||
| case NoSymbol if t <:< localTypeOf[scala.collection.Set[_]] => | ||
| classOf[scala.collection.Set[_]] | ||
| case _ => mirror.runtimeClass(t.typeSymbol.asClass) | ||
| } | ||
| UnresolvedMapObjects(mapFunction, getPath, Some(cls)) | ||
|
|
@@ -498,6 +503,19 @@ object ScalaReflection extends ScalaReflection { | |
| serializerFor(_, valueType, valuePath, seenTypeSet), | ||
| valueNullable = !valueType.typeSymbol.asClass.isPrimitive) | ||
|
|
||
| case t if t <:< localTypeOf[scala.collection.Set[_]] => | ||
| val TypeRef(_, _, Seq(elementType)) = t | ||
|
|
||
| // There's no corresponding Catalyst type for `Set`, we serialize a `Set` to Catalyst array. | ||
| // Note that the property of `Set` is only kept when manipulating the data as domain object. | ||
| val newInput = | ||
| Invoke( | ||
| inputObject, | ||
| "toSeq", | ||
| ObjectType(classOf[Seq[_]])) | ||
|
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. For primitive, calling |
||
|
|
||
| toCatalystArray(newInput, elementType) | ||
|
|
||
| case t if t <:< localTypeOf[String] => | ||
| StaticInvoke( | ||
| classOf[UTF8String], | ||
|
|
@@ -702,6 +720,10 @@ object ScalaReflection extends ScalaReflection { | |
| val Schema(valueDataType, valueNullable) = schemaFor(valueType) | ||
| Schema(MapType(schemaFor(keyType).dataType, | ||
| valueDataType, valueContainsNull = valueNullable), nullable = true) | ||
| case t if t <:< localTypeOf[Set[_]] => | ||
| val TypeRef(_, _, Seq(elementType)) = t | ||
| val Schema(dataType, nullable) = schemaFor(elementType) | ||
| Schema(ArrayType(dataType, containsNull = nullable), nullable = true) | ||
| case t if t <:< localTypeOf[String] => Schema(StringType, nullable = true) | ||
| case t if t <:< localTypeOf[java.sql.Timestamp] => Schema(TimestampType, nullable = true) | ||
| case t if t <:< localTypeOf[java.sql.Date] => Schema(DateType, nullable = true) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
|
|
||
| package org.apache.spark.sql | ||
|
|
||
| import scala.collection.immutable.{HashSet => HSet} | ||
| import scala.collection.immutable.Queue | ||
| import scala.collection.mutable.{LinkedHashMap => LHMap} | ||
| import scala.collection.mutable.ArrayBuffer | ||
|
|
@@ -339,6 +340,31 @@ class DatasetPrimitiveSuite extends QueryTest with SharedSQLContext { | |
| LHMapClass(LHMap(1 -> 2)) -> LHMap("test" -> MapClass(Map(3 -> 4)))) | ||
| } | ||
|
|
||
| test("arbitrary sets") { | ||
|
Member
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. Better to test null cases?
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. Added a test for it. |
||
| checkDataset(Seq(Set(1, 2, 3, 4)).toDS(), Set(1, 2, 3, 4)) | ||
| checkDataset(Seq(Set(1.toLong, 2.toLong)).toDS(), Set(1.toLong, 2.toLong)) | ||
| checkDataset(Seq(Set(1.toDouble, 2.toDouble)).toDS(), Set(1.toDouble, 2.toDouble)) | ||
| checkDataset(Seq(Set(1.toFloat, 2.toFloat)).toDS(), Set(1.toFloat, 2.toFloat)) | ||
| checkDataset(Seq(Set(1.toByte, 2.toByte)).toDS(), Set(1.toByte, 2.toByte)) | ||
| checkDataset(Seq(Set(1.toShort, 2.toShort)).toDS(), Set(1.toShort, 2.toShort)) | ||
| checkDataset(Seq(Set(true, false)).toDS(), Set(true, false)) | ||
| checkDataset(Seq(Set("test1", "test2")).toDS(), Set("test1", "test2")) | ||
| checkDataset(Seq(Set(Tuple1(1), Tuple1(2))).toDS(), Set(Tuple1(1), Tuple1(2))) | ||
|
|
||
| checkDataset(Seq(HSet(1, 2)).toDS(), HSet(1, 2)) | ||
| checkDataset(Seq(HSet(1.toLong, 2.toLong)).toDS(), HSet(1.toLong, 2.toLong)) | ||
| checkDataset(Seq(HSet(1.toDouble, 2.toDouble)).toDS(), HSet(1.toDouble, 2.toDouble)) | ||
| checkDataset(Seq(HSet(1.toFloat, 2.toFloat)).toDS(), HSet(1.toFloat, 2.toFloat)) | ||
| checkDataset(Seq(HSet(1.toByte, 2.toByte)).toDS(), HSet(1.toByte, 2.toByte)) | ||
| checkDataset(Seq(HSet(1.toShort, 2.toShort)).toDS(), HSet(1.toShort, 2.toShort)) | ||
| checkDataset(Seq(HSet(true, false)).toDS(), HSet(true, false)) | ||
| checkDataset(Seq(HSet("test1", "test2")).toDS(), HSet("test1", "test2")) | ||
| checkDataset(Seq(HSet(Tuple1(1), Tuple1(2))).toDS(), HSet(Tuple1(1), Tuple1(2))) | ||
|
|
||
| checkDataset(Seq(Seq(Some(1), None), Seq(Some(2))).toDF("c").as[Set[Integer]], | ||
| Seq(Set[Integer](1, null), Set[Integer](2)): _*) | ||
| } | ||
|
|
||
| test("nested sequences") { | ||
| checkDataset(Seq(Seq(Seq(1))).toDS(), Seq(Seq(1))) | ||
| checkDataset(Seq(List(Queue(1))).toDS(), List(Queue(1))) | ||
|
|
@@ -349,6 +375,11 @@ class DatasetPrimitiveSuite extends QueryTest with SharedSQLContext { | |
| checkDataset(Seq(LHMap(Map(1 -> 2) -> 3)).toDS(), LHMap(Map(1 -> 2) -> 3)) | ||
| } | ||
|
|
||
| test("nested set") { | ||
| checkDataset(Seq(Set(HSet(1, 2), HSet(3, 4))).toDS(), Set(HSet(1, 2), HSet(3, 4))) | ||
| checkDataset(Seq(HSet(Set(1, 2), Set(3, 4))).toDS(), HSet(Set(1, 2), Set(3, 4))) | ||
| } | ||
|
|
||
| test("package objects") { | ||
| import packageobject._ | ||
| checkDataset(Seq(PackageClass(1)).toDS(), PackageClass(1)) | ||
|
|
||
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.
does it work in scala 2.10?
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.
It works.