Github user mn-mikke commented on a diff in the pull request: https://github.com/apache/spark/pull/21045#discussion_r188543281 --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala --- @@ -90,6 +90,117 @@ case class MapKeys(child: Expression) override def prettyName: String = "map_keys" } +@ExpressionDescription( + usage = """_FUNC_(a1, a2, ...) - Returns a merged array containing in the N-th position the + N-th value of each array given.""", + examples = """ + Examples: + > SELECT _FUNC_(array(1, 2, 3), array(2, 3, 4)); + [[1, 2], [2, 3], [3, 4]] + > SELECT _FUNC_(array(1, 2), array(2, 3), array(3, 4)); + [[1, 2, 3], [2, 3, 4]] + """, + since = "2.4.0") +case class Zip(children: Seq[Expression]) extends Expression with ExpectsInputTypes { + override def inputTypes: Seq[AbstractDataType] = Seq.fill(children.length)(ArrayType) + + def mountSchema(): StructType = { + val arrayAT = children.map(_.dataType.asInstanceOf[ArrayType]) + val fields = arrayAT.zipWithIndex.foldRight(List[StructField]()) { + (item, list) => { + val (arr, idx) = item + StructField(s"_$idx", arr.elementType, arr.containsNull) :: list + } + } + StructType(fields) + } + + override def dataType: DataType = ArrayType(mountSchema()) + + override def prettyName: String = "zip" + + override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { + val genericArrayData = classOf[GenericArrayData].getName + val genericInternalRow = classOf[GenericInternalRow].getName + + val evals = children.map(_.genCode(ctx)) + val numArrs = evals.length + + val arrCardinality = ctx.freshName("args") + val arrVals = ctx.freshName("arrVals") + val inputs = evals.zipWithIndex.map { case (eval, index) => + s""" + |${eval.code} + |if (!${eval.isNull}) { + | $arrVals[$index] = ${eval.value}; + |} + """.stripMargin + }.mkString("\n") + + val myobject = ctx.freshName("myobject") + val biggestCardinality = ctx.freshName("biggestCardinality") + val j = ctx.freshName("j") + val i = ctx.freshName("i") + val args = ctx.freshName("args") + + ev.copy(s""" + |ArrayData[] $arrVals = new ArrayData[$numArrs]; + |int[] $arrCardinality = new int[$numArrs]; + |$inputs + |int $biggestCardinality = 0; + |for (int $i = 0; $i < $numArrs; $i ++) { + | $arrCardinality[$i] = $arrVals[$i].numElements(); + | $biggestCardinality = Math.max($biggestCardinality, $arrCardinality[$i]); + |} + |Object[] $args = new Object[$biggestCardinality]; + |for (int $i = 0; $i < $biggestCardinality; $i ++) { + | Object[] $myobject = new Object[$numArrs]; + | for (int $j = 0; $j < $numArrs; $j ++) { + | if ($arrCardinality[$j] > $i) { + | $myobject[$j] = $arrVals[$j].getInt(0); --- End diff -- What about `null` elements? if type is primitive a regular value is returned. So you have to do a check `$arrVals[$j].isNullAt($i)`.
--- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org