Github user viirya commented on a diff in the pull request:
https://github.com/apache/spark/pull/13909#discussion_r87318216
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/complexTypeCreator.scala
---
@@ -133,50 +154,78 @@ case class CreateMap(children: Seq[Expression])
extends Expression {
new ArrayBasedMapData(new GenericArrayData(keyArray), new
GenericArrayData(valueArray))
}
+ private def getAccessors(ctx: CodegenContext, dt: DataType, array:
String,
+ isPrimitive : Boolean, size: Int): (String, String, String) = {
+ if (!isPrimitive) {
+ val arrayClass = classOf[GenericArrayData].getName
+ ctx.addMutableState("Object[]", array, s"this.$array = null;")
+ (s"new $arrayClass($array)",
+ s"$array = new Object[${size}];", s"this.$array = null;")
+ } else {
+ val unsafeArrayClass = classOf[UnsafeArrayData].getName
+ val javaDataType = ctx.javaType(dt)
+ ctx.addMutableState(s"${javaDataType}[]", array,
+ s"this.$array = new ${javaDataType}[${size}];")
+ (s"$unsafeArrayClass.fromPrimitiveArray($array)", "", "")
+ }
+ }
+
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
- val arrayClass = classOf[GenericArrayData].getName
val mapClass = classOf[ArrayBasedMapData].getName
val keyArray = ctx.freshName("keyArray")
val valueArray = ctx.freshName("valueArray")
- ctx.addMutableState("Object[]", keyArray, s"this.$keyArray = null;")
- ctx.addMutableState("Object[]", valueArray, s"this.$valueArray =
null;")
- val keyData = s"new $arrayClass($keyArray)"
- val valueData = s"new $arrayClass($valueArray)"
+ val MapType(keyDt, valueDt, _) = dataType
+ val evalKeys = keys.map(e => e.genCode(ctx))
+ val isPrimitiveArrayKey = ctx.isPrimitiveType(keyDt)
+ val evalValues = values.map(e => e.genCode(ctx))
+ val isPrimitiveArrayValue =
+ ctx.isPrimitiveType(valueDt) && evalValues.forall(_.isNull ==
"false")
+
+ val (keyData, keyArrayAllocate, keyArrayNullify) =
+ getAccessors(ctx, keyDt, keyArray, isPrimitiveArrayKey, keys.size)
+ val (valueData, valueArrayAllocate, valueArrayNullify) =
+ getAccessors(ctx, valueDt, valueArray, isPrimitiveArrayValue,
values.size)
ev.copy(code = s"""
final boolean ${ev.isNull} = false;
- $keyArray = new Object[${keys.size}];
- $valueArray = new Object[${values.size}];""" +
+ $keyArrayAllocate
+ $valueArrayAllocate""" +
ctx.splitExpressions(
ctx.INPUT_ROW,
- keys.zipWithIndex.map { case (key, i) =>
- val eval = key.genCode(ctx)
- s"""
- ${eval.code}
- if (${eval.isNull}) {
- throw new RuntimeException("Cannot use null as map key!");
+ evalKeys.zipWithIndex.map { case (eval, i) =>
+ eval.code +
+ (if (isPrimitiveArrayKey) {
+ s"$keyArray[$i] = ${eval.value};"
--- End diff --
Yes. That is what I think.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]