-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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
[SPARK-16792][SQL] Dataset containing a Case Class with a List type causes a CompileException (converting sequence to list) #16240
Changes from 6 commits
c47f189
8c15b47
b04f46e
c4c0129
96f9d9d
b530bf5
f75a8f1
21d9e97
c45bee4
efd0801
2ad7eb0
68810c4
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 |
---|---|---|
|
@@ -30,7 +30,7 @@ import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder | |
* @since 1.6.0 | ||
*/ | ||
@InterfaceStability.Evolving | ||
abstract class SQLImplicits { | ||
abstract class SQLImplicits extends LowPrioritySQLImplicits { | ||
|
||
protected def _sqlContext: SQLContext | ||
|
||
|
@@ -45,9 +45,6 @@ abstract class SQLImplicits { | |
} | ||
} | ||
|
||
/** @since 1.6.0 */ | ||
implicit def newProductEncoder[T <: Product : TypeTag]: Encoder[T] = Encoders.product[T] | ||
|
||
// Primitives | ||
|
||
/** @since 1.6.0 */ | ||
|
@@ -100,31 +97,36 @@ abstract class SQLImplicits { | |
// Seqs | ||
|
||
/** @since 1.6.1 */ | ||
implicit def newIntSeqEncoder: Encoder[Seq[Int]] = ExpressionEncoder() | ||
implicit def newIntSeqEncoder[T <: Seq[Int] : TypeTag]: Encoder[T] = ExpressionEncoder() | ||
|
||
/** @since 1.6.1 */ | ||
implicit def newLongSeqEncoder: Encoder[Seq[Long]] = ExpressionEncoder() | ||
implicit def newLongSeqEncoder[T <: Seq[Long] : TypeTag]: Encoder[T] = ExpressionEncoder() | ||
|
||
/** @since 1.6.1 */ | ||
implicit def newDoubleSeqEncoder: Encoder[Seq[Double]] = ExpressionEncoder() | ||
implicit def newDoubleSeqEncoder[T <: Seq[Double] : TypeTag]: Encoder[T] = ExpressionEncoder() | ||
|
||
/** @since 1.6.1 */ | ||
implicit def newFloatSeqEncoder: Encoder[Seq[Float]] = ExpressionEncoder() | ||
implicit def newFloatSeqEncoder[T <: Seq[Float] : TypeTag]: Encoder[T] = ExpressionEncoder() | ||
|
||
/** @since 1.6.1 */ | ||
implicit def newByteSeqEncoder: Encoder[Seq[Byte]] = ExpressionEncoder() | ||
implicit def newByteSeqEncoder[T <: Seq[Byte] : TypeTag]: Encoder[T] = ExpressionEncoder() | ||
|
||
/** @since 1.6.1 */ | ||
implicit def newShortSeqEncoder: Encoder[Seq[Short]] = ExpressionEncoder() | ||
implicit def newShortSeqEncoder[T <: Seq[Short] : TypeTag]: Encoder[T] = ExpressionEncoder() | ||
|
||
/** @since 1.6.1 */ | ||
implicit def newBooleanSeqEncoder: Encoder[Seq[Boolean]] = ExpressionEncoder() | ||
implicit def newBooleanSeqEncoder[T <: Seq[Boolean] : TypeTag]: Encoder[T] = ExpressionEncoder() | ||
|
||
/** @since 1.6.1 */ | ||
implicit def newStringSeqEncoder: Encoder[Seq[String]] = ExpressionEncoder() | ||
implicit def newStringSeqEncoder[T <: Seq[String] : TypeTag]: Encoder[T] = ExpressionEncoder() | ||
|
||
/** @since 1.6.1 */ | ||
implicit def newProductSeqEncoder[A <: Product : TypeTag]: Encoder[Seq[A]] = ExpressionEncoder() | ||
implicit def newProductSeqEncoder[A <: Product : TypeTag, T <: Seq[A] : TypeTag]: Encoder[T] = | ||
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. This is my only concern now. Can you provide more details about it? 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. This one is the same as all the other ones, just with Product subclasses. If you were concerned about the |
||
ExpressionEncoder() | ||
|
||
// Workaround for implicit resolution problem for Seq.toDS (only supports Seq) | ||
implicit def newProductSeqOnlyEncoder[A <: Product : TypeTag]: Encoder[Seq[A]] = | ||
newProductSeqEncoder[A, Seq[A]] | ||
|
||
// Arrays | ||
|
||
|
@@ -180,3 +182,16 @@ abstract class SQLImplicits { | |
implicit def symbolToColumn(s: Symbol): ColumnName = new ColumnName(s.name) | ||
|
||
} | ||
|
||
/** | ||
* Lower priority implicit methods for converting Scala objects into [[Dataset]]s. | ||
* Conflicting implicits are placed here to disambiguate resolution. | ||
* | ||
* Reasons for including specific implicits: | ||
* newProductEncoder - to disambiguate for [[List]]s which are both [[Seq]] and [[Product]] | ||
*/ | ||
trait LowPrioritySQLImplicits { | ||
/** @since 1.6.0 */ | ||
implicit def newProductEncoder[T <: Product : TypeTag]: Encoder[T] = Encoders.product[T] | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,6 +130,34 @@ class DatasetPrimitiveSuite extends QueryTest with SharedSQLContext { | |
checkDataset(Seq(Array(Tuple1(1))).toDS(), Array(Tuple1(1))) | ||
} | ||
|
||
test("arbitrary sequences") { | ||
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. let's also test nested sequences, e.g. 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 added some sequence-product combination tests. |
||
import scala.collection.immutable.Queue | ||
checkDataset(Seq(Queue(1)).toDS(), Queue(1)) | ||
checkDataset(Seq(Queue(1.toLong)).toDS(), Queue(1.toLong)) | ||
checkDataset(Seq(Queue(1.toDouble)).toDS(), Queue(1.toDouble)) | ||
checkDataset(Seq(Queue(1.toFloat)).toDS(), Queue(1.toFloat)) | ||
checkDataset(Seq(Queue(1.toByte)).toDS(), Queue(1.toByte)) | ||
checkDataset(Seq(Queue(1.toShort)).toDS(), Queue(1.toShort)) | ||
checkDataset(Seq(Queue(true)).toDS(), Queue(true)) | ||
checkDataset(Seq(Queue("test")).toDS(), Queue("test")) | ||
// Implicit resolution problem - encoder needs to be provided explicitly | ||
implicit val queueEncoder = newProductSeqEncoder[Tuple1[Int], Queue[Tuple1[Int]]] | ||
checkDataset(Seq(Queue(Tuple1(1))).toDS(), Queue(Tuple1(1))) | ||
|
||
import scala.collection.mutable.ArrayBuffer | ||
checkDataset(Seq(ArrayBuffer(1)).toDS(), ArrayBuffer(1)) | ||
checkDataset(Seq(ArrayBuffer(1.toLong)).toDS(), ArrayBuffer(1.toLong)) | ||
checkDataset(Seq(ArrayBuffer(1.toDouble)).toDS(), ArrayBuffer(1.toDouble)) | ||
checkDataset(Seq(ArrayBuffer(1.toFloat)).toDS(), ArrayBuffer(1.toFloat)) | ||
checkDataset(Seq(ArrayBuffer(1.toByte)).toDS(), ArrayBuffer(1.toByte)) | ||
checkDataset(Seq(ArrayBuffer(1.toShort)).toDS(), ArrayBuffer(1.toShort)) | ||
checkDataset(Seq(ArrayBuffer(true)).toDS(), ArrayBuffer(true)) | ||
checkDataset(Seq(ArrayBuffer("test")).toDS(), ArrayBuffer("test")) | ||
// Implicit resolution problem - encoder needs to be provided explicitly | ||
implicit val arrayBufferEncoder = newProductSeqEncoder[Tuple1[Int], ArrayBuffer[Tuple1[Int]]] | ||
checkDataset(Seq(ArrayBuffer(Tuple1(1))).toDS(), ArrayBuffer(Tuple1(1))) | ||
} | ||
|
||
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.
spark code style discourage the usage of
Try
andSuccess
, can you refactor your code a little bit? i.e. movecls.getDeclaredMethod("canBuildFrom", classOf[ClassTag[_]])
out of theInvoke
code block.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.
Done. I tried looking up the code style you mentioned, but only found the Databricks' Scala Code Style Guide. And that is not mentioned in the Spark docs as far as I know.