Skip to content

Commit

Permalink
[SPARK-12568][SQL] Add BINARY to Encoders
Browse files Browse the repository at this point in the history
Author: Michael Armbrust <[email protected]>

Closes #10516 from marmbrus/datasetCleanup.

(cherry picked from commit 53beddc)
Signed-off-by: Michael Armbrust <[email protected]>
  • Loading branch information
marmbrus committed Jan 5, 2016
1 parent 8950482 commit d9e4438
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ object Encoders {
*/
def TIMESTAMP: Encoder[java.sql.Timestamp] = ExpressionEncoder()

/**
* An encoder for arrays of bytes.
* @since 1.6.1
*/
def BINARY: Encoder[Array[Byte]] = ExpressionEncoder()

/**
* Creates an encoder for Java Bean of type T.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,15 @@ case class ExpressionEncoder[T](
@transient
private lazy val constructProjection = GenerateSafeProjection.generate(fromRowExpression :: Nil)

/**
* Returns this encoder where it has been bound to its own output (i.e. no remaping of columns
* is performed).
*/
def defaultBinding: ExpressionEncoder[T] = {
val attrs = schema.toAttributes
resolve(attrs, OuterScopes.outerScopes).bind(attrs)
}

/**
* Returns an encoded version of `t` as a Spark SQL row. Note that multiple calls to
* toRow are allowed to return the same actual [[InternalRow]] object. Thus, the caller should
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class JavaSerializable(val value: Int) extends Serializable {
}

class ExpressionEncoderSuite extends SparkFunSuite {
OuterScopes.outerScopes.put(getClass.getName, this)

implicit def encoder[T : TypeTag]: ExpressionEncoder[T] = ExpressionEncoder()

// test flat encoders
Expand Down Expand Up @@ -274,16 +276,14 @@ class ExpressionEncoderSuite extends SparkFunSuite {
}
}

private val outers: ConcurrentMap[String, AnyRef] = new MapMaker().weakValues().makeMap()
outers.put(getClass.getName, this)
private def encodeDecodeTest[T : ExpressionEncoder](
input: T,
testName: String): Unit = {
test(s"encode/decode for $testName: $input") {
val encoder = implicitly[ExpressionEncoder[T]]
val row = encoder.toRow(input)
val schema = encoder.schema.toAttributes
val boundEncoder = encoder.resolve(schema, outers).bind(schema)
val boundEncoder = encoder.defaultBinding
val convertedBack = try boundEncoder.fromRow(row) catch {
case e: Exception =>
fail(
Expand Down

0 comments on commit d9e4438

Please sign in to comment.