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

    https://github.com/apache/spark/pull/6981#discussion_r33514278
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetime.scala
 ---
    @@ -0,0 +1,255 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.sql.catalyst.expressions
    +
    +import java.sql.Date
    +import java.text.SimpleDateFormat
    +
    +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
    +import 
org.apache.spark.sql.catalyst.expressions.codegen.{GeneratedExpressionCode, 
CodeGenContext}
    +import org.apache.spark.sql.catalyst.util.DateTimeUtils
    +import org.apache.spark.sql.types._
    +import org.apache.spark.unsafe.types.UTF8String
    +
    +abstract class DateFormatExpression extends UnaryExpression with 
ExpectsInputTypes {
    +  self: Product =>
    +
    +  protected val format: String
    +
    +  override def expectedChildTypes: Seq[DataType] = Seq(TimestampType)
    +
    +  override def eval(input: InternalRow): Any = {
    +    val valueLeft = child.eval(input)
    +    if (valueLeft == null) {
    +      null
    +    } else {
    +      if (format == null) {
    +        null
    +      } else {
    +        val sdf = new SimpleDateFormat(format)
    +        UTF8String.fromString(sdf.format(new 
Date(valueLeft.asInstanceOf[Long] / 10000)))
    +      }
    +    }
    +  }
    +
    +  override protected def defineCodeGen(
    +      ctx: CodeGenContext,
    +      ev: GeneratedExpressionCode,
    +      f: String => String): String = {
    +
    +    val sdf = classOf[SimpleDateFormat].getName
    +    super.defineCodeGen(ctx, ev, (x) => {
    +      f(s"""${ctx.stringType}.fromString((new $sdf("$format"))
    +            .format(new java.sql.Date($x / 10000)))""")
    +    })
    +  }
    +
    +}
    +
    +case class DateFormatClass(left: Expression, right: Expression) extends 
BinaryExpression
    +    with ExpectsInputTypes {
    +
    +  override def dataType: DataType = StringType
    +
    +  override def toString: String = s"DateFormat($left, $right)"
    +
    +  override def expectedChildTypes: Seq[DataType] = Seq(TimestampType, 
StringType)
    +
    +  override def eval(input: InternalRow): Any = {
    +    val valueLeft = left.eval(input)
    +    if (valueLeft == null) {
    +      null
    +    } else {
    +      val valueRight = right.eval(input)
    +      if (valueRight == null) {
    +        null
    +      } else {
    +        val sdf = new SimpleDateFormat(valueRight.toString)
    +        UTF8String.fromString(sdf.format(new 
Date(valueLeft.asInstanceOf[Long] / 10000)))
    +      }
    +    }
    +  }
    +
    +  override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): 
String = {
    +    val sdf = classOf[SimpleDateFormat].getName
    +    defineCodeGen(ctx, ev, (x, y) => {
    +      s"""${ctx.stringType}.fromString((new $sdf($y.toString()))
    +          .format(new java.sql.Date($x / 10000)))"""
    +    })
    +  }
    +}
    +
    +case class Year(child: Expression) extends DateFormatExpression with 
ExpectsInputTypes {
    +
    +  override protected val format: String = "y"
    +
    +  override def dataType: DataType = IntegerType
    +
    +  override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): 
String = {
    +    defineCodeGen(ctx, ev, c => s"Integer.parseInt($c.toString())")
    --- End diff --
    
    Since @adrian-wang also work on datetime functions, can you two work 
together to avoid duplicates?


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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

Reply via email to