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

    https://github.com/apache/spark/pull/20015#discussion_r157678588
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeExpressions.scala
 ---
    @@ -1295,87 +1295,184 @@ case class ParseToTimestamp(left: Expression, 
format: Option[Expression], child:
       override def dataType: DataType = TimestampType
     }
     
    -/**
    - * Returns date truncated to the unit specified by the format.
    - */
    -// scalastyle:off line.size.limit
    -@ExpressionDescription(
    -  usage = "_FUNC_(date, fmt) - Returns `date` with the time portion of the 
day truncated to the unit specified by the format model `fmt`.",
    -  examples = """
    -    Examples:
    -      > SELECT _FUNC_('2009-02-12', 'MM');
    -       2009-02-01
    -      > SELECT _FUNC_('2015-10-27', 'YEAR');
    -       2015-01-01
    -  """,
    -  since = "1.5.0")
    -// scalastyle:on line.size.limit
    -case class TruncDate(date: Expression, format: Expression)
    -  extends BinaryExpression with ImplicitCastInputTypes {
    -  override def left: Expression = date
    -  override def right: Expression = format
    -
    -  override def inputTypes: Seq[AbstractDataType] = Seq(DateType, 
StringType)
    -  override def dataType: DataType = DateType
    +trait TruncTime extends BinaryExpression with ImplicitCastInputTypes {
    +  val time: Expression
    +  val format: Expression
       override def nullable: Boolean = true
    -  override def prettyName: String = "trunc"
     
       private lazy val truncLevel: Int =
         DateTimeUtils.parseTruncLevel(format.eval().asInstanceOf[UTF8String])
     
    -  override def eval(input: InternalRow): Any = {
    +  /**
    +   *
    +   * @param input
    +   * @param maxLevel Maximum level that can be used for truncation (e.g 
MONTH for Date input)
    +   * @param truncFunc
    +   * @tparam T
    +   * @return
    +   */
    +  protected def evalHelper[T](input: InternalRow, maxLevel: Int)(
    +    truncFunc: (Any, Int) => T): Any = {
         val level = if (format.foldable) {
           truncLevel
         } else {
           DateTimeUtils.parseTruncLevel(format.eval().asInstanceOf[UTF8String])
         }
    -    if (level == -1) {
    +    if (level == DateTimeUtils.TRUNC_INVALID || level > maxLevel) {
           // unknown format
           null
         } else {
    -      val d = date.eval(input)
    +      val d = time.eval(input)
           if (d == null) {
             null
           } else {
    -        DateTimeUtils.truncDate(d.asInstanceOf[Int], level)
    +        truncFunc(d, level)
           }
         }
       }
     
    -  override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
    +  protected def codeGenHelper[T](
    --- End diff --
    
    Why do we need a type parameter `T`?


---

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

Reply via email to