-
Notifications
You must be signed in to change notification settings - Fork 28.7k
[SPARK-52617][SQL]Cast TIME to/from TIMESTAMP_NTZ #51381
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
Open
SubhamSinghal
wants to merge
40
commits into
apache:master
Choose a base branch
from
SubhamSinghal:SPARK-52617_cast_time_to/from_TIMESTAMP_NTZ
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+163
−8
Open
Changes from 19 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
8799de5
[SPARK-52617][SQL]Cast TIME to/from TIMESTAMP_NTZ
subham611 c37bbf0
Adds time to TimestampNTZType conversion
subham611 352aacb
Fix linting
subham611 7faed87
Remove unused import
subham611 11c3a26
Adds can cast
subham611 4be2758
Fix import
subham611 e020fae
Resolved ambiguous import
subham611 5b9a10e
Fix UT failure
subham611 d6f4424
Enable casting in ansi mode
subham611 f60c6cb
Calculate current day outside buildCast
subham611 828d214
Adds RewriteTimeCastToTimestampNTZ rule
subham611 90482d6
Fix additional change
subham611 a9d1bdd
Fix unused import
subham611 fdc9b92
Fix UT
subham611 b919e4e
Fix UT failure
subham611 e2ef8af
Fix UT
subham611 0bada28
Fix UT
subham611 2a5e9ad
Move to resolver
subham611 7d77c1f
Revert unwanted changes
subham611 cb6ad55
Resolve comment
subham611 cd503a5
Delete resolver
subham611 8bc58da
Fix import
subham611 8a30b2a
Add back RewriteTimeCastToTimestampNTZ rule
subham611 fd1aef3
Fix UT
subham611 5c6c97f
Resolve comments
subham611 6169ea4
Update sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/anal…
SubhamSinghal 29ca860
Update sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util…
SubhamSinghal 011b254
Merge master
subham611 ca02825
Resolve comments
subham611 72bb9b5
remove new line
subham611 68f0c98
Fix lint
subham611 6b5ed62
Lint fix
subham611 e53bfe3
Modify rule
subham611 b488aae
Rewrite rule
subham611 867967e
Fix import order
subham611 ebf16ed
Adds codegen
subham611 58c0bea
Fix UT
subham611 b82e21c
Resolve comment
subham611 4239bef
Lint fix
subham611 fd5e76e
Adds type coersion rule
subham611 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
36 changes: 36 additions & 0 deletions
36
...scala/org/apache/spark/sql/catalyst/analysis/resolver/RewriteTimeCastToTimestampNTZ.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,36 @@ | ||
/* | ||
* 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.analysis.resolver | ||
|
||
import org.apache.spark.sql.catalyst.expressions.{Cast, CurrentDate, Expression, MakeTimestampNTZ} | ||
import org.apache.spark.sql.types.{TimestampNTZType, TimeType} | ||
|
||
/** | ||
* Rewrites Cast from TIME -> TIMESTAMP_NTZ to MakeTimestampNTZ(CurrentDate(), TIME) | ||
* | ||
* Example: CAST(TIME '15:30:00' AS TIMESTAMP_NTZ) => MakeTimestampNTZ(CurrentDate(), | ||
* Literal(15:30:00)) | ||
*/ | ||
object RewriteTimeCastToTimestampNTZ { | ||
def rewrite(expr: Expression): Expression = expr match { | ||
case c @ Cast(child, TimestampNTZType, _, _) | ||
if child.resolved && child.dataType.isInstanceOf[TimeType] => | ||
MakeTimestampNTZ(CurrentDate(), child) | ||
case other => | ||
other | ||
} | ||
} |
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
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
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
53 changes: 53 additions & 0 deletions
53
.../org/apache/spark/sql/catalyst/analysis/resolver/RewriteTimeCastToTimestampNTZSuite.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,53 @@ | ||
/* | ||
* 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.analysis.resolver | ||
|
||
import org.apache.spark.SparkFunSuite | ||
import org.apache.spark.sql.catalyst.expressions.{Cast, CurrentDate, Literal, MakeTimestampNTZ} | ||
import org.apache.spark.sql.types.{TimestampNTZType, TimeType} | ||
|
||
class RewriteTimeCastToTimestampNTZSuite extends SparkFunSuite { | ||
|
||
test("SPARK-52617: rewrite TIME -> TIMESTAMP_NTZ cast to MakeTimestampNTZ") { | ||
// TIME: 15:30:00 -> seconds = 15*3600 + 30*60 = 55800 | ||
val nanos = 55800L * 1_000_000_000L | ||
val timeLiteral = Literal(nanos, TimeType(6)) | ||
|
||
val castExpr = Cast(timeLiteral, TimestampNTZType) | ||
val rewrittenExpr = RewriteTimeCastToTimestampNTZ.rewrite(castExpr) | ||
|
||
val expectedExpr = MakeTimestampNTZ(CurrentDate(), timeLiteral) | ||
|
||
assert( | ||
rewrittenExpr.semanticEquals(expectedExpr), | ||
s""" | ||
|Expected: | ||
| $expectedExpr | ||
|But got: | ||
| $rewrittenExpr | ||
|""".stripMargin) | ||
} | ||
|
||
test("should not rewrite non-time casts") { | ||
val literal = Literal(42) | ||
val castExpr = Cast(literal, TimestampNTZType) | ||
|
||
val rewrittenExpr = RewriteTimeCastToTimestampNTZ.rewrite(castExpr) | ||
assert(rewrittenExpr eq castExpr) | ||
} | ||
} |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.