Github user mgaido91 commented on a diff in the pull request:

    https://github.com/apache/spark/pull/21024#discussion_r180414332
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala
 ---
    @@ -287,3 +287,61 @@ case class ArrayContains(left: Expression, right: 
Expression)
     
       override def prettyName: String = "array_contains"
     }
    +
    +
    +/**
    + * Returns the maximum value in the array.
    + */
    +@ExpressionDescription(
    +usage = "_FUNC_(array) - Returns the maximum value in the array.",
    +examples = """
    +    Examples:
    +      > SELECT _FUNC_(array(1, 20, null, 3));
    +       20
    +  """, since = "2.4.0")
    +case class ArrayMax(child: Expression) extends UnaryExpression with 
ImplicitCastInputTypes {
    +
    +  override def nullable: Boolean =
    +    child.nullable || child.dataType.asInstanceOf[ArrayType].containsNull
    +
    +  override def foldable: Boolean = child.foldable
    +
    +  override def inputTypes: Seq[AbstractDataType] = Seq(ArrayType)
    +
    +  private lazy val ordering = TypeUtils.getInterpretedOrdering(dataType)
    +
    +  override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): 
ExprCode = {
    +    val childGen = child.genCode(ctx)
    +    val javaType = CodeGenerator.javaType(dataType)
    +    val i = ctx.freshName("i")
    +    val item = ExprCode("",
    +      isNull = StatementValue(s"${childGen.value}.isNullAt($i)", 
"boolean"),
    +      value = StatementValue(CodeGenerator.getValue(childGen.value, 
dataType, i), javaType))
    +    ev.copy(code =
    +      s"""
    +         |${childGen.code}
    +         |boolean ${ev.isNull} = true;
    +         |$javaType ${ev.value} = ${CodeGenerator.defaultValue(dataType)};
    +         |if (!${childGen.isNull}) {
    +         |  for (int $i = 0; $i < ${childGen.value}.numElements(); $i ++) {
    +         |    ${ctx.reassignIfGreater(dataType, ev, item)}
    +         |  }
    +         |}
    +      """.stripMargin)
    +  }
    +
    +  override protected def nullSafeEval(input: Any): Any = {
    +    var max: Any = null
    +    input.asInstanceOf[ArrayData].foreach(dataType, (_, item) =>
    +      if (item != null && (max == null || ordering.gt(item, max))) {
    +        max = item
    +      }
    +    )
    +    max
    +  }
    +
    +  override def dataType: DataType = child.dataType match {
    +    case ArrayType(dt, _) => dt
    --- End diff --
    
    I added the check in the `checkInputDataTypes` method, thanks.


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to