Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
marmbrus committed Jan 5, 2016
1 parent 0171b71 commit c187e2e
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 @@ -278,16 +280,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 c187e2e

Please sign in to comment.