spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tarekauel <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-8199][SPARK-8184][SPARK-8183][SPARK-818...
Date Tue, 30 Jun 2015 05:17:20 GMT
Github user tarekauel commented on a diff in the pull request:

    https://github.com/apache/spark/pull/6981#discussion_r33540988
  
    --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetime.scala
---
    @@ -0,0 +1,387 @@
    +/*
    + * 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 java.util.{Calendar, TimeZone}
    +import org.apache.spark.sql.catalyst.expressions.codegen.{GeneratedExpressionCode, CodeGenContext}
    +import org.apache.spark.sql.types._
    +import org.apache.spark.unsafe.types.UTF8String
    +
    +abstract class TimeFormatExpression extends UnaryExpression with ExpectsInputTypes {
    +  self: Product =>
    +
    +  protected val format: Int
    +
    +  protected val cntPerInterval: Int
    +
    +  override def expectedChildTypes: Seq[DataType] = Seq(TimestampType)
    +
    +  override def eval(input: InternalRow): Any = {
    +    val valueLeft = child.eval(input)
    +    if (valueLeft == null) {
    +      null
    +    } else {
    +      val time = valueLeft.asInstanceOf[Long] / 10000
    +      val utcTime: Long = time + TimeZone.getDefault.getOffset(time)
    +      ((utcTime / format) % cntPerInterval).toInt
    +    }
    +  }
    +
    +  override def genCode(
    +      ctx: CodeGenContext,
    +      ev: GeneratedExpressionCode): String = {
    +
    +    val tz = classOf[TimeZone].getName
    +
    +    defineCodeGen(ctx, ev, (c) =>
    +      s"""(${ctx.javaType(dataType)})
    +            ((($c / 10000) + $tz.getDefault().getOffset($c / 10000)) / $format % $cntPerInterval)"""
    +    )
    +  }
    +}
    +
    +case class Hour(child: Expression) extends TimeFormatExpression {
    +
    +  override protected val format: Int = 1000 * 3600
    +
    +  override protected val cntPerInterval: Int = 24
    +
    +  override def dataType: DataType = IntegerType
    +
    +  override def toString: String = s"Hour($child)"
    +}
    +
    +case class Minute(child: Expression) extends TimeFormatExpression {
    +
    +  override protected val format: Int = 1000 * 60
    +
    +  override protected val cntPerInterval: Int = 60
    +
    +  override def dataType: DataType = IntegerType
    +
    +  override def toString: String = s"Minute($child)"
    +}
    +
    +case class Second(child: Expression) extends TimeFormatExpression {
    +
    +  override protected val format: Int = 1000
    +
    +  override protected val cntPerInterval: Int = 60
    +
    +  override def dataType: DataType = IntegerType
    +
    +  override def toString: String = s"Second($child)"
    +}
    +
    +abstract class DateFormatExpression extends UnaryExpression with ExpectsInputTypes {
    +  self: Product =>
    +
    +  override def dataType: DataType = IntegerType
    +
    +  override def expectedChildTypes: Seq[DataType] = Seq(TimestampType)
    +
    +  protected def defineCodeGen(
    +    ctx: CodeGenContext,
    +    ev: GeneratedExpressionCode,
    +    f: (String, String) => String): String = {
    +
    +    val tz = classOf[TimeZone].getName
    +
    +    val utcTime = ctx.freshName("utcTime")
    +    val dayInYear = ctx.freshName("dayInYear")
    +    val days = ctx.freshName("days")
    +    val year = ctx.freshName("year")
    +
    +    val eval = child.gen(ctx)
    +    ev.isNull = eval.isNull
    +    eval.code + s"""
    +      ${ctx.javaType(dataType)} ${ev.primitive} = ${ctx.defaultValue(dataType)};
    +      if (!${ev.isNull}) {
    +        long $utcTime = ${eval.primitive} / 10000;
    +        long $days = $utcTime / 1000 / 3600 / 24;
    +        int $year = (int) ($days / 365.24);
    +        int $dayInYear = (int) ($days - $year * 365.24);
    +        ${f(dayInYear, utcTime)}
    +      }
    +    """
    +  }
    +
    +  def eval(input: InternalRow, f: (Int, Long) => Int): Any = {
    +    val valueLeft = child.eval(input)
    +    if (valueLeft == null) {
    +      null
    +    } else {
    +      val utcTime: Long = valueLeft.asInstanceOf[Long] / 10000
    +      val days = utcTime / 1000 / 3600 / 24
    +      val year = days / 365.24
    +      val dayInYear = days - year.toInt * 365.24
    +      f(dayInYear.toInt, utcTime)
    +    }
    +  }
    +
    +  override def toString: String = s"Year($child)"
    +}
    +
    +
    +case class Year(child: Expression) extends DateFormatExpression {
    +
    +  override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = {
    +
    +    val cal = classOf[Calendar].getName
    +    defineCodeGen(ctx, ev, (day, utc) =>
    +      s"""
    +         if ($day > 1 && $day < 360) {
    +           ${ev.primitive} = (int) (1970 + ($utc / 1000 / 3600 / 24 / 365.24));
    +         } else {
    +           $cal c = $cal.getInstance();
    +           c.setTimeInMillis($utc);
    +           ${ev.primitive} = c.get($cal.YEAR);
    +         }
    +       """)
    +  }
    +
    +  override def eval(input: InternalRow): Any = {
    +    eval(input, (dayInYear, utcTime) =>
    +      if (dayInYear > 1 && dayInYear < 360) {
    +        1970 + (utcTime / 1000 / 3600 / 24 / 365.24).toInt
    --- End diff --
    
    Is it okay to recalculate the result or should `Year` be independent of `DateFormatExpression`?


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

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


Mime
View raw message