Skip to content
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 @@ -49,8 +49,6 @@ object GenerateSafeProjection extends CodeGenerator[Seq[Expression], Projection]
val tmpInput = ctx.freshName("tmpInput")
val output = ctx.freshName("safeRow")
val values = ctx.freshName("values")
// These expressions could be split into multiple functions
ctx.addMutableState("Object[]", values, s"$values = null;")

val rowClass = classOf[GenericInternalRow].getName

Expand All @@ -66,15 +64,15 @@ object GenerateSafeProjection extends CodeGenerator[Seq[Expression], Projection]
val allFields = ctx.splitExpressions(
expressions = fieldWriters,
funcName = "writeFields",
arguments = Seq("InternalRow" -> tmpInput)
arguments = Seq("InternalRow" -> tmpInput, "Object[]" -> values)
)
val code = s"""
final InternalRow $tmpInput = $input;
$values = new Object[${schema.length}];
$allFields
final InternalRow $output = new $rowClass($values);
$values = null;
"""
val code =
s"""
|final InternalRow $tmpInput = $input;
|final Object[] $values = new Object[${schema.length}];
|$allFields
|final InternalRow $output = new $rowClass($values);
""".stripMargin

ExprCode(code, "false", output)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,15 @@ class GeneratedProjectionSuite extends SparkFunSuite {
unsafeProj.apply(InternalRow(InternalRow(UTF8String.fromString("b"))))
assert(row.getStruct(0, 1).getString(0).toString == "a")
}

test("SPARK-22699: GenerateSafeProjection should not use global variables for struct") {
val safeProj = GenerateSafeProjection.generate(
Seq(BoundReference(0, new StructType().add("i", IntegerType), true)))
val globalVariables = safeProj.getClass.getDeclaredFields
// We need always 3 variables:
// - one is a reference to this
// - one is the references object
// - one is the mutableRow
assert(globalVariables.length == 3)
}
}