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

    https://github.com/apache/spark/pull/6822#discussion_r33757090
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/math.scala
 ---
    @@ -160,6 +162,82 @@ case class Expm1(child: Expression) extends 
UnaryMathExpression(math.expm1, "EXP
     
     case class Floor(child: Expression) extends 
UnaryMathExpression(math.floor, "FLOOR")
     
    +object Factorial {
    +
    +  def factorial(n: Int): Long = {
    +    if (n < factorials.length) factorials(n) else Long.MaxValue
    +  }
    +
    +  private val factorials: Array[Long] = Array[Long](
    +    1,
    +    1,
    +    2,
    +    6,
    +    24,
    +    120,
    +    720,
    +    5040,
    +    40320,
    +    362880,
    +    3628800,
    +    39916800,
    +    479001600,
    +    6227020800L,
    +    87178291200L,
    +    1307674368000L,
    +    20922789888000L,
    +    355687428096000L,
    +    6402373705728000L,
    +    121645100408832000L,
    +    2432902008176640000L
    +  )
    +}
    +
    +case class Factorial(child: Expression)
    +  extends UnaryExpression with Serializable with ExpectsInputTypes {
    +
    +  override def inputTypes: Seq[Any] = Seq(IntegerType)
    +
    +  override def dataType: DataType = LongType
    +
    +  override def foldable: Boolean = child.foldable
    +
    +  // If the value not in the range of [0, 20], it still will be null, so 
set it to be true here.
    +  override def nullable: Boolean = true
    +
    +  override def toString: String = s"factorial($child)"
    +
    +  override def eval(input: InternalRow): Any = {
    +    val evalE = child.eval(input)
    +    if (evalE == null) {
    +      null
    +    } else {
    +      val input = evalE.asInstanceOf[Integer]
    +      if (input > 20 || input < 0) {
    +        null
    +      } else {
    +        Factorial.factorial(input)
    +      }
    +    }
    +  }
    +
    +  override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): 
String = {
    +    val eval = child.gen(ctx)
    +    eval.code + s"""
    +      boolean ${ev.isNull} = ${eval.isNull};
    +      ${ctx.javaType(dataType)} ${ev.primitive} = 
${ctx.defaultValue(dataType)};
    +      if (!${ev.isNull}) {
    +        if (${eval.primitive} > 20 || ${eval.primitive} < 0) {
    +           ${ev.isNull} = true;
    +        } else {
    +           ${ev.primitive} =
    +           
org.apache.spark.sql.catalyst.expressions.Factorial.factorial(${eval.primitive});
    --- End diff --
    
    can you indent this by 2 space


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