Skip to content

Commit 53beddc

Browse files
committed
[SPARK-12568][SQL] Add BINARY to Encoders
Author: Michael Armbrust <[email protected]> Closes apache#10516 from marmbrus/datasetCleanup.
1 parent 7058dc1 commit 53beddc

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/Encoder.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ object Encoders {
157157
*/
158158
def TIMESTAMP: Encoder[java.sql.Timestamp] = ExpressionEncoder()
159159

160+
/**
161+
* An encoder for arrays of bytes.
162+
* @since 1.6.1
163+
*/
164+
def BINARY: Encoder[Array[Byte]] = ExpressionEncoder()
165+
160166
/**
161167
* Creates an encoder for Java Bean of type T.
162168
*

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/encoders/ExpressionEncoder.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,15 @@ case class ExpressionEncoder[T](
198198
@transient
199199
private lazy val constructProjection = GenerateSafeProjection.generate(fromRowExpression :: Nil)
200200

201+
/**
202+
* Returns this encoder where it has been bound to its own output (i.e. no remaping of columns
203+
* is performed).
204+
*/
205+
def defaultBinding: ExpressionEncoder[T] = {
206+
val attrs = schema.toAttributes
207+
resolve(attrs, OuterScopes.outerScopes).bind(attrs)
208+
}
209+
201210
/**
202211
* Returns an encoded version of `t` as a Spark SQL row. Note that multiple calls to
203212
* toRow are allowed to return the same actual [[InternalRow]] object. Thus, the caller should

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/encoders/ExpressionEncoderSuite.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ class JavaSerializable(val value: Int) extends Serializable {
7777
}
7878

7979
class ExpressionEncoderSuite extends SparkFunSuite {
80+
OuterScopes.outerScopes.put(getClass.getName, this)
81+
8082
implicit def encoder[T : TypeTag]: ExpressionEncoder[T] = ExpressionEncoder()
8183

8284
// test flat encoders
@@ -278,16 +280,14 @@ class ExpressionEncoderSuite extends SparkFunSuite {
278280
}
279281
}
280282

281-
private val outers: ConcurrentMap[String, AnyRef] = new MapMaker().weakValues().makeMap()
282-
outers.put(getClass.getName, this)
283283
private def encodeDecodeTest[T : ExpressionEncoder](
284284
input: T,
285285
testName: String): Unit = {
286286
test(s"encode/decode for $testName: $input") {
287287
val encoder = implicitly[ExpressionEncoder[T]]
288288
val row = encoder.toRow(input)
289289
val schema = encoder.schema.toAttributes
290-
val boundEncoder = encoder.resolve(schema, outers).bind(schema)
290+
val boundEncoder = encoder.defaultBinding
291291
val convertedBack = try boundEncoder.fromRow(row) catch {
292292
case e: Exception =>
293293
fail(

0 commit comments

Comments
 (0)