Skip to content

Commit

Permalink
add in functions
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-wang committed Jul 2, 2015
1 parent 98e8550 commit 61ed3d5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
17 changes: 17 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 @@ -34,6 +34,7 @@ import org.apache.spark.util.Utils
*
* @groupname udf_funcs UDF functions
* @groupname agg_funcs Aggregate functions
* @groupname datetime_funcs Date time functions
* @groupname sort_funcs Sorting functions
* @groupname normal_funcs Non-aggregate functions
* @groupname math_funcs Math functions
Expand Down Expand Up @@ -989,6 +990,22 @@ object functions {
*/
def cosh(columnName: String): Column = cosh(Column(columnName))

/**
* Returns the current date.
*
* @group datetime_funcs
* @since 1.5.0
*/
def current_date(): Column = CurrentDate()

/**
* Returns the current timestamp.
*
* @group datetime_funcs
* @since 1.5.0
*/
def current_timestamp(): Column = CurrentTimestamp()

/**
* Computes the exponential of the given value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,30 @@ package org.apache.spark.sql
import java.sql.Date

import org.apache.spark.sql.catalyst.util.DateTimeUtils
import org.scalatest.BeforeAndAfterAll
import org.apache.spark.sql.functions._

class DatetimeExpressionsSuite extends QueryTest with BeforeAndAfterAll {
class DatetimeExpressionsSuite extends QueryTest {
private lazy val ctx = org.apache.spark.sql.test.TestSQLContext

import ctx.implicits._

val df1 = Seq((1, 2), (3, 1)).toDF("a", "b")

test("function current_date") {
// Date constructor would keep the original millis, we need to align it with begin of day.
checkAnswer(df1.select(current_date()),
Seq(
Row(new Date(DateTimeUtils.daysToMillis(
DateTimeUtils.millisToDays(System.currentTimeMillis())))),
Row(new Date(DateTimeUtils.daysToMillis(
DateTimeUtils.millisToDays(System.currentTimeMillis()))))))
checkAnswer(ctx.sql("""SELECT CURRENT_DATE()"""),
Row(new Date(DateTimeUtils.daysToMillis(
DateTimeUtils.millisToDays(System.currentTimeMillis())))))
}

test("function current_timestamp") {
checkAnswer(df1.select(countDistinct(current_timestamp())), Row(1))
// Execution in one query should return the same value
checkAnswer(ctx.sql("""SELECT CURRENT_TIMESTAMP() = CURRENT_TIMESTAMP()"""),
Row(true))
Expand Down

0 comments on commit 61ed3d5

Please sign in to comment.