Github user chenghao-intel commented on a diff in the pull request:
https://github.com/apache/spark/pull/6822#discussion_r33107372
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/math.scala
---
@@ -160,6 +159,77 @@ 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 = {
+ require(n >= 0, "the input value should not be negative")
+ 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 expectedChildTypes: Seq[DataType] = Seq(IntegerType)
+ override def dataType: DataType = LongType
+ override def foldable: Boolean = child.foldable
+ 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) {
--- End diff --
`${eval.primitive} > 20` should be removed as it's a valid parameter.
---
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]