-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-8192] [SPARK-8193] [SQL] udf current_date, current_timestamp #6985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0b69a1f
udf current
adrian-wang 427d9dc
add tests and codegen
adrian-wang 98e8550
fix sytle
adrian-wang 61ed3d5
add in functions
adrian-wang e11ae75
refine tests
adrian-wang 27c9f95
refine tests..
adrian-wang 6a20b64
remove codegen and add lazy in testsuite
adrian-wang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeFunctions.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.catalyst.expressions | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.codegen.{CodeGenContext, GeneratedExpressionCode} | ||
| import org.apache.spark.sql.catalyst.util.DateTimeUtils | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
| /** | ||
| * Returns the current date at the start of query evaluation. | ||
| * All calls of current_date within the same query return the same value. | ||
| */ | ||
| case class CurrentDate() extends LeafExpression { | ||
| override def foldable: Boolean = true | ||
| override def nullable: Boolean = false | ||
|
|
||
| override def dataType: DataType = DateType | ||
|
|
||
| override def eval(input: InternalRow): Any = { | ||
| DateTimeUtils.millisToDays(System.currentTimeMillis()) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns the current timestamp at the start of query evaluation. | ||
| * All calls of current_timestamp within the same query return the same value. | ||
| */ | ||
| case class CurrentTimestamp() extends LeafExpression { | ||
| override def foldable: Boolean = true | ||
| override def nullable: Boolean = false | ||
|
|
||
| override def dataType: DataType = TimestampType | ||
|
|
||
| override def eval(input: InternalRow): Any = { | ||
| System.currentTimeMillis() * 10000L | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
...yst/src/test/scala/org/apache/spark/sql/catalyst/expressions/DatetimeFunctionsSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.catalyst.expressions | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.sql.catalyst.util.DateTimeUtils | ||
|
|
||
| class DatetimeFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper { | ||
| test("datetime function current_date") { | ||
| val d0 = DateTimeUtils.millisToDays(System.currentTimeMillis()) | ||
| val cd = CurrentDate().eval(EmptyRow).asInstanceOf[Int] | ||
| val d1 = DateTimeUtils.millisToDays(System.currentTimeMillis()) | ||
| assert(d0 <= cd && cd <= d1 && d1 - d0 <= 1) | ||
| } | ||
|
|
||
| test("datetime function current_timestamp") { | ||
| val ct = DateTimeUtils.toJavaTimestamp(CurrentTimestamp().eval(EmptyRow).asInstanceOf[Long]) | ||
| val t1 = System.currentTimeMillis() | ||
| assert(math.abs(t1 - ct.getTime) < 5000) | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
sql/core/src/test/scala/org/apache/spark/sql/DatetimeExpressionsSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql | ||
|
|
||
| import org.apache.spark.sql.catalyst.util.DateTimeUtils | ||
| import org.apache.spark.sql.functions._ | ||
|
|
||
| class DatetimeExpressionsSuite extends QueryTest { | ||
| private lazy val ctx = org.apache.spark.sql.test.TestSQLContext | ||
|
|
||
| import ctx.implicits._ | ||
|
|
||
| lazy val df1 = Seq((1, 2), (3, 1)).toDF("a", "b") | ||
|
|
||
| test("function current_date") { | ||
| val d0 = DateTimeUtils.millisToDays(System.currentTimeMillis()) | ||
| val d1 = DateTimeUtils.fromJavaDate(df1.select(current_date()).collect().head.getDate(0)) | ||
| val d2 = DateTimeUtils.fromJavaDate( | ||
| ctx.sql("""SELECT CURRENT_DATE()""").collect().head.getDate(0)) | ||
| val d3 = DateTimeUtils.millisToDays(System.currentTimeMillis()) | ||
| assert(d0 <= d1 && d1 <= d2 && d2 <= d3 && d3 - d0 <= 1) | ||
| } | ||
|
|
||
| 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)) | ||
| assert(math.abs(ctx.sql("""SELECT CURRENT_TIMESTAMP()""").collect().head.getTimestamp( | ||
| 0).getTime - System.currentTimeMillis()) < 5000) | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems it is possible that
CURRENT_TIMESTAMP() = CURRENT_TIMESTAMP()will return false?I see it fails jenkins (https://amplab.cs.berkeley.edu/jenkins/job/Spark-Master-SBT/3095/AMPLAB_JENKINS_BUILD_PROFILE=hadoop2.3,label=centos/testReport/junit/org.apache.spark.sql/DatetimeExpressionsSuite/function_current_timestamp/).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, this is a known issue as SPARK-9196 . we have to use a different way to evaluate this function. I think we'd better disable this function test for now.