Skip to content

Commit 42d225f

Browse files
cloud-fanmarmbrus
authored andcommitted
[SPARK-11216][SQL][FOLLOW-UP] add encoder/decoder for external row
address comments in #9184 Author: Wenchen Fan <[email protected]> Closes #9212 from cloud-fan/encoder.
1 parent f6d06ad commit 42d225f

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,12 @@ case class ClassEncoder[T](
4848
private val dataType = ObjectType(clsTag.runtimeClass)
4949

5050
override def toRow(t: T): InternalRow = {
51-
if (t == null) {
52-
null
53-
} else {
54-
inputRow(0) = t
55-
extractProjection(inputRow)
56-
}
51+
inputRow(0) = t
52+
extractProjection(inputRow)
5753
}
5854

5955
override def fromRow(row: InternalRow): T = {
60-
if (row eq null) {
61-
null.asInstanceOf[T]
62-
} else {
63-
constructProjection(row).get(0, dataType).asInstanceOf[T]
64-
}
56+
constructProjection(row).get(0, dataType).asInstanceOf[T]
6557
}
6658

6759
override def bind(schema: Seq[Attribute]): ClassEncoder[T] = {

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ import org.apache.spark.sql.catalyst.util.DateTimeUtils
2626
import org.apache.spark.sql.types._
2727
import org.apache.spark.unsafe.types.UTF8String
2828

29+
/**
30+
* A factory for constructing encoders that convert external row to/from the Spark SQL
31+
* internal binary representation.
32+
*/
2933
object RowEncoder {
30-
3134
def apply(schema: StructType): ClassEncoder[Row] = {
3235
val cls = classOf[Row]
3336
val inputObject = BoundReference(0, ObjectType(cls), nullable = true)
@@ -136,7 +139,7 @@ object RowEncoder {
136139
constructorFor(BoundReference(i, f.dataType, f.nullable), f.dataType)
137140
)
138141
}
139-
CreateRow(fields)
142+
CreateExternalRow(fields)
140143
}
141144

142145
private def constructorFor(input: Expression, dataType: DataType): Expression = dataType match {
@@ -195,7 +198,7 @@ object RowEncoder {
195198
Literal.create(null, externalDataTypeFor(f.dataType)),
196199
constructorFor(getField(input, i, f.dataType), f.dataType))
197200
}
198-
CreateRow(convertedFields)
201+
CreateExternalRow(convertedFields)
199202
}
200203

201204
private def getField(

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/objects.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,13 @@ case class MapObjects(
456456
}
457457
}
458458

459-
case class CreateRow(children: Seq[Expression]) extends Expression {
459+
/**
460+
* Constructs a new external row, using the result of evaluating the specified expressions
461+
* as content.
462+
*
463+
* @param children A list of expression to use as content of the external row.
464+
*/
465+
case class CreateExternalRow(children: Seq[Expression]) extends Expression {
460466
override def dataType: DataType = ObjectType(classOf[Row])
461467

462468
override def nullable: Boolean = false

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class RowEncoderSuite extends SparkFunSuite {
7373
private def encodeDecodeTest(schema: StructType): Unit = {
7474
test(s"encode/decode: ${schema.simpleString}") {
7575
val encoder = RowEncoder(schema)
76-
val inputGenerator = RandomDataGenerator.forType(schema).get
76+
val inputGenerator = RandomDataGenerator.forType(schema, nullable = false).get
7777

7878
var input: Row = null
7979
try {

0 commit comments

Comments
 (0)