Skip to content

Commit 4a80377

Browse files
author
Yucai Yu
committed
Create setValue to update a variable for a given DataType
1 parent eb03b2a commit 4a80377

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeGenerator.scala

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,20 @@ class CodegenContext {
318318
}
319319
}
320320

321+
/**
322+
* Returns the code to update a variable for a given DataType.
323+
*/
324+
def setValue(target: String, dataType: DataType, value: String): String = {
325+
val jt = javaType(dataType)
326+
val codes = dataType match {
327+
case _ if isPrimitiveType(jt) => value
328+
case StringType => s"$value.clone()"
329+
case _: StructType | _: ArrayType | _: MapType => s"$value.copy()"
330+
case _ => value
331+
}
332+
s"$target = $codes"
333+
}
334+
321335
/**
322336
* Returns the specialized code to set a given value in a column vector for a given `DataType`.
323337
*/
@@ -375,16 +389,6 @@ class CodegenContext {
375389
}
376390
}
377391

378-
def copyValue(value: String, dataType: DataType): String = {
379-
val jt = javaType(dataType)
380-
val safeCopy = s"$value == null ? null :"
381-
dataType match {
382-
case _ if isPrimitiveType(jt) => value
383-
case _: StructType => s"$safeCopy $value.copy()"
384-
case _ => s"$safeCopy $value.clone()"
385-
}
386-
}
387-
388392
/**
389393
* Returns the name used in accessor and setter for a Java primitive type.
390394
*/

sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/SortAggregateExec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ case class SortAggregateExec(
186186
s"""
187187
| ${bufVars(i).isNull} = ${ev.isNull};
188188
| if (${bufVars(i).value} != ${ev.value})
189-
| ${bufVars(i).value} = ${ctx.copyValue(ev.value, bufVarsType(i))};
189+
| ${ctx.setValue(bufVars(i).value, bufVarsType(i), ev.value)};
190190
""".stripMargin
191191
}
192192
s"""

0 commit comments

Comments
 (0)