-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[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 5 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 |
|---|---|---|
|
|
@@ -342,6 +342,28 @@ object ScalaReflection extends ScalaReflection { | |
| mirror.runtimeClass(t.typeSymbol.asClass) | ||
| ) | ||
|
|
||
| // 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[scala.collection.Set[_]] => | ||
| val TypeRef(_, _, Seq(elementType)) = t | ||
| val Schema(dataType, elementNullable) = schemaFor(elementType) | ||
| val className = getClassNameFromType(elementType) | ||
| val newTypePath = s"""- set element class: "$className"""" +: walkedTypePath | ||
|
|
||
| val mapFunction: Expression => Expression = element => { | ||
| // upcast the array element to the data type the encoder expected. | ||
| val casted = upCastToExpectedType(element, dataType, newTypePath) | ||
| val converter = deserializerFor(elementType, Some(casted), newTypePath) | ||
| if (elementNullable) { | ||
| converter | ||
| } else { | ||
| AssertNotNull(converter, newTypePath) | ||
| } | ||
| } | ||
|
|
||
| val cls = mirror.runtimeClass(t.typeSymbol.asClass) | ||
| UnresolvedMapObjects(mapFunction, getPath, Some(cls)) | ||
|
|
||
| case t if t.typeSymbol.annotations.exists(_.tpe =:= typeOf[SQLUserDefinedType]) => | ||
| val udt = getClassFromType(t).getAnnotation(classOf[SQLUserDefinedType]).udt().newInstance() | ||
| val obj = NewInstance( | ||
|
|
@@ -498,6 +520,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 +737,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 |
|---|---|---|
|
|
@@ -460,6 +460,15 @@ class DataFrameAggregateSuite extends QueryTest with SharedSQLContext { | |
| df.select(collect_set($"a"), collect_set($"b")), | ||
| Seq(Row(Seq(1, 2, 3), Seq(2, 4))) | ||
| ) | ||
|
|
||
| checkDataset( | ||
| df.select(collect_set($"a").as("aSet")) | ||
| .as[Set[Int]], | ||
| Set(1, 2, 3)) | ||
| checkDataset( | ||
| df.select(collect_set($"b").as("bSet")) | ||
| .as[Set[Int]], | ||
| Set(2, 4)) | ||
|
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. can we add one more case?
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. Sure. |
||
| } | ||
|
|
||
| test("collect functions structs") { | ||
|
|
||
| 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.
can we merge it with the seq case? e.g.
case t if t <:< localTypeOf[Seq[_]] || t <:< localTypeOf[scala.collection.Set[_]]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.
I think we only need to update one place:
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.
This trick
companion.declaration...doesn't work forSet. You always getNoSymbol.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.
Anyway I will merge them.