Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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 @@ -402,23 +402,40 @@ case class DayOfMonth(child: Expression) extends UnaryExpression with ImplicitCa
}
}

// scalastyle:off line.size.limit
@ExpressionDescription(
usage = "_FUNC_(date) - Returns the week of the year of the given date.",
usage = "_FUNC_(date[, format]) - Returns the week of the year of the given date. Defaults to ISO 8601 standard, but can be gregorian specific",
extended = """
Examples:
> SELECT _FUNC_('2008-02-20');
8
> SELECT _FUNC_('2017-01-01', 'gregorian');
1
> SELECT _FUNC_('2017-01-01', 'iso');
52
> SELECT _FUNC_('2017-01-01');
52
""")
case class WeekOfYear(child: Expression) extends UnaryExpression with ImplicitCastInputTypes {
// scalastyle:on line.size.limit
case class WeekOfYear(child: Expression, format: Expression) extends
UnaryExpression with ImplicitCastInputTypes {

def this(child: Expression) = {
this(child, Literal("iso"))
}

override def inputTypes: Seq[AbstractDataType] = Seq(DateType)

override def dataType: DataType = IntegerType

@transient private lazy val minimalDays = {
if ("gregorian".equalsIgnoreCase(format.toString)) 1 else 4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How many formats the other DB/systems allow? Could you do a search?

Copy link
Contributor Author

@rubenljanssen rubenljanssen May 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a bit of research, and there seem to be no other formats. However, some systems (such as MySQL and Java), allow the first day of the week to be defined as well. Some countries in the middle east have weekends on Friday/Saturday, or even Thursday/Friday.
I will update the PR to allow users to override the first day of the week, as well as specify how the first week is defined (1 iso standard: week with more than half of the days, i.e. Thursday in a Monday-Sunday week. 2 gregorian: week with first day of the new year)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will still default to ISO stanards with Monday-Sunday week of course, but now users can override it in any way they would like

}

@transient private lazy val c = {
val c = Calendar.getInstance(DateTimeUtils.getTimeZone("UTC"))
c.setFirstDayOfWeek(Calendar.MONDAY)
c.setMinimalDaysInFirstWeek(4)
c.setMinimalDaysInFirstWeek(minimalDays)
c
}

Expand All @@ -436,7 +453,7 @@ case class WeekOfYear(child: Expression) extends UnaryExpression with ImplicitCa
s"""
$c = $cal.getInstance($dtu.getTimeZone("UTC"));
$c.setFirstDayOfWeek($cal.MONDAY);
$c.setMinimalDaysInFirstWeek(4);
$c.setMinimalDaysInFirstWeek($minimalDays);
""")
s"""
$c.setTimeInMillis($time * 1000L * 3600L * 24L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,24 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
}

test("WeekOfYear") {
checkEvaluation(WeekOfYear(Literal.create(null, DateType)), null)
checkEvaluation(WeekOfYear(Literal(d)), 15)
checkEvaluation(WeekOfYear(Cast(Literal(sdfDate.format(d)), DateType, gmtId)), 15)
checkEvaluation(WeekOfYear(Cast(Literal(ts), DateType, gmtId)), 45)
checkEvaluation(WeekOfYear(Cast(Literal("2011-05-06"), DateType, gmtId)), 18)
checkEvaluation(WeekOfYear(Literal(new Date(sdf.parse("1582-10-15 13:10:15").getTime))), 40)
checkEvaluation(WeekOfYear(Literal(new Date(sdf.parse("1582-10-04 13:10:15").getTime))), 40)
checkConsistencyBetweenInterpretedAndCodegen(WeekOfYear, DateType)
checkEvaluation(WeekOfYear(Literal.create(null, DateType), Literal("iso")), null)
checkEvaluation(WeekOfYear(Literal(d), Literal(false)), 15)
checkEvaluation(WeekOfYear(Cast(Literal(ts), DateType, gmtId), Literal("iso")), 45)
checkEvaluation(
WeekOfYear(Cast(Literal(sdfDate.format(d)), DateType, gmtId), Literal("iso")), 15)
checkEvaluation(
WeekOfYear(Literal(new Date(sdf.parse("1582-10-15 13:10:15").getTime)), Literal("iso")), 40)
checkEvaluation(
WeekOfYear(Literal(new Date(sdf.parse("1582-10-04 13:10:15").getTime)), Literal("iso")), 40)

checkEvaluation(
WeekOfYear(Cast(Literal("2017-01-01"), DateType, gmtId), Literal("iso")), 52)
checkEvaluation(
WeekOfYear(Cast(Literal("2017-01-01"), DateType, gmtId), Literal("gregorian")), 1)

checkConsistencyBetweenInterpretedAndCodegen(
(child: Expression) => WeekOfYear(child, Literal(true)), DateType)

}

test("DateFormat") {
Expand Down