Skip to content
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-12568] [SQL] Add BINARY to Encoders #10516

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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