Github user tarekauel commented on a diff in the pull request:
https://github.com/apache/spark/pull/6981#discussion_r34337804
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeFunctions.scala
---
@@ -54,3 +60,341 @@ case class CurrentTimestamp() extends LeafExpression {
System.currentTimeMillis() * 1000L
}
}
+
+/**
+ * Abstract class for create time format expressions.
+ */
+abstract class TimeFormatExpression extends UnaryExpression with
ExpectsInputTypes {
+ self: Product =>
+
+ protected val factorToMilli: Int
+
+ protected val cntPerInterval: Int
+
+ override def inputTypes: Seq[AbstractDataType] = Seq(TimestampType)
+
+ override def dataType: DataType = IntegerType
+
+ override protected def nullSafeEval(timestamp: Any): Any = {
+ val time = timestamp.asInstanceOf[Long] / 1000
+ val longTime: Long = time + TimeZone.getDefault.getOffset(time)
+ ((longTime / factorToMilli) % cntPerInterval).toInt
+ }
+
+ override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
+ val tz = classOf[TimeZone].getName
+ defineCodeGen(ctx, ev, (c) =>
+ s"""(${ctx.javaType(dataType)})
+ ((($c / 1000) + $tz.getDefault().getOffset($c / 1000))
+ / $factorToMilli % $cntPerInterval)"""
+ )
+ }
+}
+
+case class Hour(child: Expression) extends TimeFormatExpression {
+
+ override protected val factorToMilli: Int = 1000 * 3600
+
+ override protected val cntPerInterval: Int = 24
+}
+
+case class Minute(child: Expression) extends TimeFormatExpression {
+
+ override protected val factorToMilli: Int = 1000 * 60
+
+ override protected val cntPerInterval: Int = 60
+}
+
+case class Second(child: Expression) extends UnaryExpression with
ExpectsInputTypes {
+
+ override def inputTypes: Seq[AbstractDataType] = Seq(TimestampType)
+
+ override def dataType: DataType = IntegerType
+
+ override protected def nullSafeEval(time: Any): Any = {
+ time.asInstanceOf[Long] / 1000L / 1000L % 60L
+ }
+
+ override protected def genCode(ctx: CodeGenContext, ev:
GeneratedExpressionCode): String = {
+ nullSafeCodeGen(ctx, ev, (time) => {
+ s"""${ev.primitive} = (${ctx.javaType(IntegerType)}) ($time / 1000L
/ 1000L % 60L);"""
+ })
+ }
+}
+
+private[sql] object DateFormatExpression {
+
+ def isLeapYear(year: Int): Boolean = {
+ (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0)
+ }
+
+ val yearBoundaries = new Array[Int](2330 - 1599)
+
+ var i = 0
+ yearBoundaries(i) = -135140
+ (1601 to 2330) foreach { year =>
+ i = i + 1
+ if (isLeapYear(year - 1)) {
+ yearBoundaries(i) = yearBoundaries(i - 1) + 366
+ } else {
+ yearBoundaries(i) = yearBoundaries(i - 1) + 365
+ }
+ }
+}
+
+abstract class DateFormatExpression extends UnaryExpression with
ExpectsInputTypes {
+ self: Product =>
+
+ override def dataType: DataType = IntegerType
+
+ override def inputTypes: Seq[AbstractDataType] = Seq(DateType)
+
+ protected def calculateYearAndDayInYear(daysIn: Int): (Int, Int) = {
+ var index: Int = 370 + (daysIn / 365.24).toInt - 1
+ while (DateFormatExpression.yearBoundaries(index) < daysIn + 1) {
+ index += 1
+ }
+ (index - 1 + 1600, daysIn - DateFormatExpression.yearBoundaries(index
- 1) + 1)
+ }
+
+ protected def codeGen(ctx: CodeGenContext, ev: GeneratedExpressionCode,
input: String,
+ f: (String, String) => String): String = {
+ val yb = ctx.freshName("yb")
+ val i = ctx.freshName("counter")
+ val x = ctx.freshName("counter")
+ val index = ctx.freshName("index")
+ val year = ctx.freshName("year")
+ val dayInYear = ctx.freshName("dayInYear")
+
+ s"""
+ int[] $yb = new int[2330 - 1599];
+ $yb[0] = -135140;
+ int $x = 1;
+ for(int $i = 1601; $i <= 2330; $i++, $x++) {
+ $yb[$x] = (($i - 1) % 4 == 0 && (($i - 1) % 100 != 0 || ($i - 1)
% 400 == 0)) ?
+ $yb[$x - 1] + 366 : $yb[$x - 1] + 365;
+ }
+ int $index = 370 + ((int) ($input / 365.24)) - 1;
+ while ($yb[$index] < $input + 1) {
+ $index++;
+ }
+ int $year = $index - 1 + 1600;
+ int $dayInYear = $input - $yb[$index - 1] + 1;
+ ${f(year, dayInYear)}
--- End diff --
@davies is there a way to provide `yb` somehow as static field?
---
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]