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

    https://github.com/apache/spark/pull/6851#discussion_r34417815
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/conditionals.scala
 ---
    @@ -312,3 +313,103 @@ case class CaseKeyWhen(key: Expression, branches: 
Seq[Expression]) extends CaseW
         }.mkString
       }
     }
    +
    +case class Least(children: Expression*) extends Expression {
    +  require(children.length > 1, "LEAST requires at least 2 arguments, got " 
+ children.length)
    +
    +  override def nullable: Boolean = children.forall(_.nullable)
    +  override def foldable: Boolean = children.forall(_.foldable)
    +
    +  private lazy val ordering = TypeUtils.getOrdering(dataType)
    +
    +  override def checkInputDataTypes(): TypeCheckResult = {
    +    if (children.map(_.dataType).distinct.count(_ != NullType) > 1) {
    +      TypeCheckResult.TypeCheckFailure(
    +        s"The expressions should all have the same type," +
    +          s" got LEAST (${children.map(_.dataType)}).")
    +    } else {
    +      TypeUtils.checkForOrderingExpr(dataType, "function " + prettyName)
    +    }
    +  }
    +
    +  override def dataType: DataType = children.head.dataType
    +
    +  override def eval(input: InternalRow): Any = {
    +    children.foldLeft[Any](null)((r, c) => {
    +      val evalc = c.eval(input)
    +      if (evalc != null) {
    +        if (r == null || ordering.lt(evalc, r)) evalc else r
    +      } else {
    +        r
    +      }
    +    })
    +  }
    +
    +  override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): 
String = {
    +    val evalChildren = children.map(_.gen(ctx))
    +    def updateEval(i: Int): String =
    +      s"""
    +        if (${ev.isNull} || (!${evalChildren(i).isNull} && ${
    +          ctx.genComp(dataType, evalChildren(i).primitive, ev.primitive)} 
< 0)) {
    +          ${ev.isNull} = ${evalChildren(i).isNull};
    +          ${ev.primitive} = ${evalChildren(i).primitive};
    +        }
    +      """
    +    s"""
    +      ${evalChildren.map(_.code).mkString("\n")}
    +      boolean ${ev.isNull} = true;
    +      ${ctx.javaType(dataType)} ${ev.primitive} = 
${ctx.defaultValue(dataType)};
    +      ${(0 to children.length - 1).map(updateEval).mkString("\n")}
    +    """
    +  }
    +}
    +
    +case class Greatest(children: Expression*) extends Expression {
    +  require(children.length > 1, "GREATEST requires at least 2 arguments, 
got " + children.length)
    +
    +  override def nullable: Boolean = children.forall(_.nullable)
    +  override def foldable: Boolean = children.forall(_.foldable)
    +
    +  private lazy val ordering = TypeUtils.getOrdering(dataType)
    +
    +  override def checkInputDataTypes(): TypeCheckResult = {
    +    if (children.map(_.dataType).distinct.count(_ != NullType) > 1) {
    +      TypeCheckResult.TypeCheckFailure(
    +        s"The expressions should all have the same type," +
    +          s" got GREATEST (${children.map(_.dataType)}).")
    +    } else {
    +      TypeUtils.checkForOrderingExpr(dataType, "function " + prettyName)
    +    }
    +  }
    +
    +  override def dataType: DataType = children.head.dataType
    +
    +  override def eval(input: InternalRow): Any = {
    +    children.foldLeft[Any](null)((r, c) => {
    +      val evalc = c.eval(input)
    +      if (evalc != null) {
    +        if (r == null || ordering.gt(evalc, r)) evalc else r
    +      } else {
    +        r
    +      }
    +    })
    +  }
    +
    +  override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): 
String = {
    +    val evalChildren = children.map(_.gen(ctx))
    +    def updateEval(i: Int): String =
    +      s"""
    +        if (${ev.isNull} || (!${evalChildren(i).isNull} && ${
    +        ctx.genComp(dataType, evalChildren(i).primitive, ev.primitive)} > 
0)) {
    +          ${ev.isNull} = ${evalChildren(i).isNull};
    +          ${ev.primitive} = ${evalChildren(i).primitive};
    +        }
    +      """
    +    s"""
    +      ${evalChildren.map(_.code).mkString("\n")}
    +      boolean ${ev.isNull} = true;
    +      ${ctx.javaType(dataType)} ${ev.primitive} = 
${ctx.defaultValue(dataType)};
    +      ${(0 to children.length - 1).map(updateEval).mkString("\n")}
    --- End diff --
    
    `0 until children.length`


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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

Reply via email to