Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -726,23 +726,24 @@ case class TruncDate(date: Expression, format: Expression)
override def dataType: DataType = DateType
override def prettyName: String = "trunc"

lazy val minItemConst = DateTimeUtils.parseTruncLevel(format.eval().asInstanceOf[UTF8String])
private lazy val truncLevel: Int =
DateTimeUtils.parseTruncLevel(format.eval().asInstanceOf[UTF8String])

override def eval(input: InternalRow): Any = {
val minItem = if (format.foldable) {
minItemConst
val level = if (format.foldable) {
truncLevel
} else {
DateTimeUtils.parseTruncLevel(format.eval().asInstanceOf[UTF8String])
}
if (minItem == -1) {
if (level == -1) {
// unknown format
null
} else {
val d = date.eval(input)
if (d == null) {
null
} else {
DateTimeUtils.truncDate(d.asInstanceOf[Int], minItem)
DateTimeUtils.truncDate(d.asInstanceOf[Int], level)
}
}
}
Expand All @@ -751,7 +752,7 @@ case class TruncDate(date: Expression, format: Expression)
val dtu = DateTimeUtils.getClass.getName.stripSuffix("$")

if (format.foldable) {
if (minItemConst == -1) {
if (truncLevel == -1) {
s"""
boolean ${ev.isNull} = true;
${ctx.javaType(dataType)} ${ev.primitive} = ${ctx.defaultValue(dataType)};
Expand All @@ -763,7 +764,7 @@ case class TruncDate(date: Expression, format: Expression)
boolean ${ev.isNull} = ${d.isNull};
${ctx.javaType(dataType)} ${ev.primitive} = ${ctx.defaultValue(dataType)};
if (!${ev.isNull}) {
${ev.primitive} = $dtu.truncDate(${d.primitive}, $minItemConst);
${ev.primitive} = $dtu.truncDate(${d.primitive}, $truncLevel);
}
"""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,8 @@ object DateTimeUtils {
} else if (level == TRUNC_TO_MONTH) {
d - DateTimeUtils.getDayOfMonth(d) + 1
} else {
throw new Exception(s"Invalid trunc level: $level")
// caller make sure that this should never be reached
sys.error(s"Invalid trunc level: $level")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
package org.apache.spark.sql.catalyst.expressions

import org.scalactic.TripleEqualsSupport.Spread
import org.scalatest.Matchers._

import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.CatalystTypeConverters
import org.apache.spark.sql.catalyst.{CatalystTypeConverters, InternalRow}
import org.apache.spark.sql.catalyst.expressions.codegen._
import org.apache.spark.sql.catalyst.optimizer.DefaultOptimizer
import org.apache.spark.sql.catalyst.plans.logical.{OneRowRelation, Project}
Expand Down
3 changes: 3 additions & 0 deletions sql/core/src/main/scala/org/apache/spark/sql/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2192,6 +2192,9 @@ object functions {
/**
* Returns date truncated to the unit specified by the format.
*
* @param format: 'year', 'yyyy', 'yy' for truncate by year,
* or 'month', 'mon', 'mm' for truncate by month
*
* @group datetime_funcs
* @since 1.5.0
*/
Expand Down