Github user nburoojy commented on a diff in the pull request:
https://github.com/apache/spark/pull/8592#discussion_r44176861
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/complexTypeExtractors.scala
---
@@ -227,6 +227,38 @@ case class GetArrayItem(child: Expression, ordinal:
Expression)
}
/**
+ * Combines two Arrays into one Array.
+ */
+case class ArrayUnion(left: Expression, right: Expression) extends
BinaryOperator {
+
+ override def inputType: AbstractDataType = ArrayType
+
+ override def symbol: String = "++"
+
+ private def inputArrType = left.dataType.asInstanceOf[ArrayType]
+ override def dataType: DataType = inputArrType
+
+ override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
+ val arrayClass = classOf[GenericArrayData].getName
+ val elementType = inputArrType.elementType
+ nullSafeCodeGen(ctx, ev, (eval1, eval2) => {
+ s"""
+ final int n1 = $eval1.numElements();
+ final int n2 = $eval2.numElements();
+ final Object[] unionValues = new Object[n1 + n2];
+ for (int j = 0; j < n1; j++) {
+ unionValues[j] = ${ctx.getValue(eval1, elementType, "j")};
+ }
+ for (int j = 0; j < n2; j++) {
+ unionValues[n1 + j] = ${ctx.getValue(eval2, elementType, "j")};
+ }
+ ${ev.primitive} = new $arrayClass(unionValues);
+ """
+ })
+ }
--- End diff --
You're absolutely right; since inserting an element into an array requires
copying the entire array, the running time is `O(n^2)` in the number of
elements in the destination array. N.B.: `n` is not necessarily the number of
elements in the source data; e.g. if the user groups by `id`.
---
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]