Skip to content

Commit

Permalink
[SPARK-8199] day_of_month alias
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekbecker committed Jul 17, 2015
1 parent d01b977 commit 2259299
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
20 changes: 16 additions & 4 deletions python/pyspark/sql/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ def date_format(dateCol, format):
[Row(date=u'04/08/2015')]
"""
sc = SparkContext._active_spark_context
return Column(sc._jvm.functions.dateFormat(dateCol, format))
return Column(sc._jvm.functions.date_format(dateCol, format))


@since(1.5)
Expand Down Expand Up @@ -715,14 +715,26 @@ def month(col):
@since(1.5)
def day(col):
"""
Extract the day of a given date as integer.
Extract the day of the month of a given date as integer.
>>> sqlContext.createDataFrame([('2015-04-08',)], ['a']).select(day('a').alias('day')).collect()
[Row(day=8)]
"""
sc = SparkContext._active_spark_context
return Column(sc._jvm.functions.day(col))


@since(1.5)
def day_of_month(col):
"""
Extract the day of the month of a given date as integer.
>>> df0 = sqlContext.createDataFrame([('2015-04-08',)], ['a'])
>>> df0.select(day_of_month('a').alias('day')).collect()
[Row(day=8)]
"""
sc = SparkContext._active_spark_context
return Column(sc._jvm.functions.day_of_month(col))


@since(1.5)
def day_in_year(col):
"""
Expand All @@ -732,7 +744,7 @@ def day_in_year(col):
[Row(day=98)]
"""
sc = SparkContext._active_spark_context
return Column(sc._jvm.functions.day(col))
return Column(sc._jvm.functions.day_in_year(col))


@since(1.5)
Expand Down Expand Up @@ -784,7 +796,7 @@ def week_of_year(col):
[Row(week=15)]
"""
sc = SparkContext._active_spark_context
return Column(sc._jvm.functions.weekOfYear(col))
return Column(sc._jvm.functions.week_of_year(col))


class UserDefinedFunction(object):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ object FunctionRegistry {
expression[Quarter]("quarter"),
expression[Month]("month"),
expression[Day]("day"),
expression[Day]("day_of_month"),
expression[DayInYear]("day_in_year"),
expression[Hour]("hour"),
expression[Minute]("minute"),
Expand Down
18 changes: 16 additions & 2 deletions sql/core/src/main/scala/org/apache/spark/sql/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1779,19 +1779,33 @@ object functions {
def month(columnName: String): Column = month(Column(columnName))

/**
* Extracts the day as an integer from a given date/timestamp/string.
* Extracts the day of the month as an integer from a given date/timestamp/string.
* @group datetime_funcs
* @since 1.5.0
*/
def day(e: Column): Column = Day(e.expr)

/**
* Extracts the day as an integer from a given date/timestamp/string.
* Extracts the day of the month as an integer from a given date/timestamp/string.
* @group datetime_funcs
* @since 1.5.0
*/
def day(columnName: String): Column = day(Column(columnName))

/**
* Extracts the day of the month as an integer from a given date/timestamp/string.
* @group datetime_funcs
* @since 1.5.0
*/
def day_of_month(e: Column): Column = Day(e.expr)

/**
* Extracts the day of the month as an integer from a given date/timestamp/string.
* @group datetime_funcs
* @since 1.5.0
*/
def day_of_month(columnName: String): Column = day_of_month(Column(columnName))

/**
* Extracts the day of the year as an integer from a given date/timestamp/string.
* @group datetime_funcs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ class DateExpressionsSuite extends QueryTest {
Row(8, 8, 8))
}

test("day of month") {
val df = Seq((d, sdfDate.format(d), ts)).toDF("a", "b", "c")

checkAnswer(
df.select(day_of_month("a"), day_of_month("b"), day_of_month("c")),
Row(8, 8, 8))

checkAnswer(
df.selectExpr("day_of_month(a)", "day_of_month(b)", "day_of_month(c)"),
Row(8, 8, 8))
}

test("day in year") {
val df = Seq((d, sdfDate.format(d), ts)).toDF("a", "b", "c")

Expand Down

0 comments on commit 2259299

Please sign in to comment.