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

    https://github.com/apache/spark/pull/6851#discussion_r35832525
  
    --- 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: Seq[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 (!${evalChildren(i).isNull} && (${ev.isNull} ||
    +          ${ctx.genComp(dataType, evalChildren(i).primitive, 
ev.primitive)} < 0)) {
    +          ${ev.isNull} = false;
    +          ${ev.primitive} = ${evalChildren(i).primitive};
    +        }
    +      """
    +    s"""
    +      ${evalChildren.map(_.code).mkString("\n")}
    +      boolean ${ev.isNull} = true;
    +      ${ctx.javaType(dataType)} ${ev.primitive} = 
${ctx.defaultValue(dataType)};
    +      ${(0 until children.length).map(updateEval).mkString("\n")}
    +    """
    +  }
    +}
    +
    +case class Greatest(children: Seq[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) {
    --- End diff --
    
    i think the problem is that null literals are not converted into any 
non-null type when it happens in greatest.



---
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]

Reply via email to