vinodkc commented on code in PR #38419: URL: https://github.com/apache/spark/pull/38419#discussion_r1089829197
########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala: ########## @@ -331,6 +332,247 @@ case class RoundCeil(child: Expression, scale: Expression) copy(child = newLeft, scale = newRight) } +case class TruncNumber(child: Expression, scale: Expression) + extends BaseBinaryExpression with NullIntolerant { + + override protected def withNewChildrenInternal( + newLeft: Expression, + newRight: Expression): TruncNumber = copy(child = newLeft, scale = newRight) + + /** + * Returns Java source code that can be compiled to evaluate this expression. The default + * behavior is to call the eval method of the expression. Concrete expression implementations + * should override this to do actual code generation. + * + * @param ctx + * a [[CodegenContext]] + * @param ev + * an [[ExprCode]] with unique terms. + * @return + * an [[ExprCode]] containing the Java source code to generate the given expression + */ + override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = + defineCodeGen( + ctx, + ev, + (input, _) => { + dataType match { + case ByteType if (scaleValue <= 0) => + s"""(byte)(org.apache.spark.sql.catalyst.expressions.TruncNumber.trunc( + |(long)$input, $scaleValue))""".stripMargin + case ShortType if (scaleValue <= 0) => + s"""(short)(org.apache.spark.sql.catalyst.expressions.TruncNumber.trunc( + |(long)$input, $scaleValue))""".stripMargin + case IntegerType if (scaleValue <= 0) => + s"""(int)(org.apache.spark.sql.catalyst.expressions.TruncNumber.trunc( + |(long)$input, $scaleValue))""".stripMargin + case LongType if (scaleValue <= 0) => + s"""(org.apache.spark.sql.catalyst.expressions.TruncNumber.trunc( + |$input, $scaleValue))""".stripMargin + case FloatType if (scaleValue <= 0) => + s"""org.apache.spark.sql.catalyst.expressions.TruncNumber.trunc( + |$input, $scaleValue).floatValue()""".stripMargin + case DoubleType if (scaleValue <= 0) => + s"""org.apache.spark.sql.catalyst.expressions.TruncNumber.trunc( + |$input, $scaleValue).doubleValue()""".stripMargin + case DecimalType.Fixed(p, s) => + s"""Decimal.apply( + |org.apache.spark.sql.catalyst.expressions.TruncNumber.trunc( Review Comment: Done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org